Skip to content

Commit

Permalink
fetching scheduled messages
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullai-t committed Oct 25, 2023
1 parent 425ee35 commit ff28aa5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/api/store/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ def delete_message(self, message_id, context) -> Tuple[dict, MassEnergizeAPIErro
messages = Message.objects.filter(id=message_id)
if not is_admin_of_community(context, messages.first().community.id):
return None, NotAuthorizedError()

schedule_info = messages.first().scheduled_info or {}
if schedule_info.get("schedule_id"):
result = AsyncResult(schedule_info.get("schedule_id"))
result.revoke()

messages.update(is_deleted=True)
message = messages.first()
# TODO: also remove it from all places that it was ever set in many to many or foreign key
Expand Down Expand Up @@ -289,7 +295,6 @@ def list_community_admin_messages(self, context: Context, args):
with_ids = Q()
if message_ids:
with_ids = Q(id__in=message_ids)

if context.user_is_super_admin:
messages = Message.objects.filter(
Q(
Expand All @@ -299,6 +304,7 @@ def list_community_admin_messages(self, context: Context, args):
with_ids,
*filter_params
).distinct()

elif context.user_is_community_admin:
messages = Message.objects.filter(
Q(
Expand Down Expand Up @@ -382,15 +388,15 @@ def send_message(self, context, args) -> Tuple[dict, MassEnergizeAPIError]:
result.revoke()
# schedule new task and update message
schedule_id = send_scheduled_email.apply_async(args=[ subject,message,email_list],eta=schedule).id
scheduled_info ={} if not args.get("schedule", None) else {"schedule_id": schedule_id, "schedule": str(schedule),"recipients":email_list}
scheduled_info ={} if not args.get("schedule", None) else {"schedule_id": schedule_id, "schedule": str(schedule),"recipients":{"audience_type":audience_type, "audience":audience, "sub_audience_type":sub_audience_type}}
messages.update(**{"scheduled_info": scheduled_info, "body": message, "title": subject})
else:
schedule_id = send_scheduled_email.apply_async(args=[ subject,message,email_list],eta=schedule).id
new_message = Message(
title=subject,
body=message,
user=user,
scheduled_info = {} if not args.get("schedule", None) else {"schedule_id": schedule_id, "schedule": str(schedule),"recipients":email_list}
scheduled_info = {} if not args.get("schedule", None) else {"schedule_id": schedule_id, "schedule": str(schedule),"recipients":{"audience_type":audience_type, "audience":audience, "sub_audience_type":sub_audience_type}}
)
new_message.save()
return new_message, None
Expand Down
6 changes: 6 additions & 0 deletions src/api/utils/filter_functions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import json
import operator
from functools import reduce
Expand Down Expand Up @@ -144,6 +145,7 @@ def get_messages_filter_params(params):
teams = params.get("team", None)
status = None
forwarded =None
is_scheduled = params.get("is_scheduled", None)

search_text = params.get("search_text", None)
if search_text:
Expand All @@ -167,6 +169,10 @@ def get_messages_filter_params(params):
if "No" in params.get("forwarded to team admin?", []):
forwarded= False


if is_scheduled:
query.append(Q(scheduled_info__schedule__gt=json.dumps(datetime.datetime.now().isoformat())))

if communities:
query.append(Q(community__name__in=communities))
if teams:
Expand Down

0 comments on commit ff28aa5

Please sign in to comment.