Skip to content

Commit

Permalink
Merge pull request #1057 from ChildMindInstitute/M2-4894
Browse files Browse the repository at this point in the history
M2-4894: M2-5082: Push notification recipient fix, deleting all should keep individual fix
  • Loading branch information
Damirkhon authored Feb 5, 2024
2 parents 5fcf16c + 527f19a commit 81d9ac8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 40 deletions.
81 changes: 50 additions & 31 deletions src/apps/schedule/api/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ async def schedule_create(
schedule = await service.create_schedule(schema, applet_id)

try:
await applet_service.send_notification_to_applet_respondents(
applet_id,
"Your schedule has been changed, click to update.",
"Your schedule has been changed, click to update.",
FirebaseNotificationType.SCHEDULE_UPDATED,
respondent_ids=[schedule.respondent_id]
if schedule.respondent_id
else None,
)
respondent_ids = None
if schedule.respondent_id:
respondent_ids = [schedule.respondent_id]
else:
respondent_ids = await service.get_default_respondents(applet_id)
if respondent_ids:
await applet_service.send_notification_to_applet_respondents(
applet_id,
"Your schedule has been changed, click to update.",
"Your schedule has been changed, click to update.",
FirebaseNotificationType.SCHEDULE_UPDATED,
respondent_ids=respondent_ids,
)
except FirebaseError as e:
# mute error
logger.exception(e)
Expand Down Expand Up @@ -145,12 +149,15 @@ async def schedule_delete_all(
await service.delete_all_schedules(applet_id)

try:
await applet_service.send_notification_to_applet_respondents(
applet_id,
"Your schedule has been changed, click to update.",
"Your schedule has been changed, click to update.",
FirebaseNotificationType.SCHEDULE_UPDATED,
)
respondent_ids = await service.get_default_respondents(applet_id)
if respondent_ids:
await applet_service.send_notification_to_applet_respondents(
applet_id,
"Your schedule has been changed, click to update.",
"Your schedule has been changed, click to update.",
FirebaseNotificationType.SCHEDULE_UPDATED,
respondent_ids=respondent_ids,
)
except FirebaseError as e:
# mute error
logger.exception(e)
Expand All @@ -175,13 +182,20 @@ async def schedule_delete_by_id(
)

try:
await applet_service.send_notification_to_applet_respondents(
applet_id,
"Your schedule has been changed, click to update.",
"Your schedule has been changed, click to update.",
FirebaseNotificationType.SCHEDULE_UPDATED,
respondent_ids=[respondent_id] if respondent_id else None,
)
respondent_ids = None
if respondent_id:
respondent_ids = [respondent_id]
else:
respondent_ids = await service.get_default_respondents(applet_id)

if respondent_ids:
await applet_service.send_notification_to_applet_respondents(
applet_id,
"Your schedule has been changed, click to update.",
"Your schedule has been changed, click to update.",
FirebaseNotificationType.SCHEDULE_UPDATED,
respondent_ids=respondent_ids,
)
except FirebaseError as e:
# mute error
logger.exception(e)
Expand All @@ -207,15 +221,20 @@ async def schedule_update(
)

try:
await applet_service.send_notification_to_applet_respondents(
applet_id,
"Your schedule has been changed, click to update.",
"Your schedule has been changed, click to update.",
FirebaseNotificationType.SCHEDULE_UPDATED,
respondent_ids=[schedule.respondent_id]
if schedule.respondent_id
else None,
)
respondent_ids = None
if schedule.respondent_id:
respondent_ids = [schedule.respondent_id]
else:
respondent_ids = await service.get_default_respondents(applet_id)

if respondent_ids:
await applet_service.send_notification_to_applet_respondents(
applet_id,
"Your schedule has been changed, click to update.",
"Your schedule has been changed, click to update.",
FirebaseNotificationType.SCHEDULE_UPDATED,
respondent_ids=respondent_ids,
)
except FirebaseError as e:
# mute error
logger.exception(e)
Expand Down
30 changes: 23 additions & 7 deletions src/apps/schedule/crud/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
FlowEventAlreadyExists,
UserEventAlreadyExists,
)
from apps.workspaces.db.schemas import UserAppletAccessSchema
from apps.workspaces.domain.constants import Role
from infrastructure.database import BaseCRUD

__all__ = [
Expand Down Expand Up @@ -742,13 +744,6 @@ async def count_individual_events_by_user(
db_result = await self._execute(query)
return db_result.scalar()

async def get_all(self, applet_id: uuid.UUID) -> list[EventSchema]:
query: Query = select(EventSchema)
query = query.where(EventSchema.applet_id == applet_id)
query = query.where(EventSchema.is_deleted.is_(False))
result = await self._execute(query)
return result.scalars().all()

async def get_all_by_activity_flow_ids(
self,
applet_id: uuid.UUID,
Expand Down Expand Up @@ -778,6 +773,27 @@ async def get_all_by_activity_flow_ids(
events = result.scalars().all()
return events

async def get_default_schedule_user_ids_by_applet_id(
self, applet_id: uuid.UUID
) -> list[uuid.UUID]:
"""Return user ids for default schedule."""
individual_schedule_users = (
select(UserEventsSchema.user_id)
.join(EventSchema, UserEventsSchema.event_id == EventSchema.id)
.where(EventSchema.applet_id == applet_id)
.where(EventSchema.is_deleted == False) # noqa: E712
)
query: Query = select(UserAppletAccessSchema.user_id.label("user_id"))
query = query.where(UserAppletAccessSchema.applet_id == applet_id)
query = query.where(UserAppletAccessSchema.role == Role.RESPONDENT)
query = query.where(UserAppletAccessSchema.is_deleted == False) # noqa: E712
query = query.where(
UserAppletAccessSchema.user_id.not_in(individual_schedule_users)
)
result = await self._execute(query)
result = result.scalars().all()
return result


class UserEventsCRUD(BaseCRUD[UserEventsSchema]):
schema_class = UserEventsSchema
Expand Down
12 changes: 10 additions & 2 deletions src/apps/schedule/service/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ async def delete_all_schedules(self, applet_id: uuid.UUID):

event_schemas: list[EventSchema] = await EventCRUD(
self.session
).get_all(applet_id)
).get_all_by_applet_id_with_filter(applet_id)
event_ids = [event_schema.id for event_schema in event_schemas]
periodicity_ids = [
event_schema.periodicity_id for event_schema in event_schemas
Expand Down Expand Up @@ -1045,7 +1045,7 @@ async def count_events_by_user(self, user_id: uuid.UUID) -> int:

async def _get_notifications_and_reminder(
self, event_id: uuid.UUID
) -> (PublicNotification | None):
) -> PublicNotification | None:
"""Get notifications and reminder for event."""
notifications = await NotificationCRUD(
self.session
Expand Down Expand Up @@ -1227,3 +1227,11 @@ async def create_default_schedules_if_not_exist(
activity_ids=activities_without_events,
is_activity=True,
)

async def get_default_respondents(
self, applet_id: uuid.UUID
) -> list[uuid.UUID]:
default_respondents = await EventCRUD(
self.session
).get_default_schedule_user_ids_by_applet_id(applet_id)
return default_respondents

0 comments on commit 81d9ac8

Please sign in to comment.