Skip to content

Commit

Permalink
integrate notification to optimizer output
Browse files Browse the repository at this point in the history
  • Loading branch information
anannnchim committed Oct 7, 2024
1 parent 55f07b1 commit 7f2b8e8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions controllers/v2/optimiser/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask_restful import Resource, marshal_with, reqparse
from .response_models import optimiser_response_model
from repository.shift_repository import ShiftRepository
from repository.fcm_token_repository import FCMTokenRepository
from services.jwk import requires_auth, is_user_or_has_role
from domain import UserType, session_scope
from controllers.v2.v2_blueprint import v2_api
Expand Down Expand Up @@ -37,6 +38,25 @@ def post(self):
result = optimiser.solve()
optimiser.save_result(result)

# Extract user id from result
user_id_list = optimiser.extract_user_ids_from_result(result)

# check if there is no user
if not user_id_list:
logging.info("No users found to notify.")
else:
# Initialize FCM token repository for retrieving token and notifying user
fcm_token_repo = FCMTokenRepository()

# Loop through user id, retrieve token and send message
for user_id in user_id_list:
title = "New Shift Assignment"
body = "You have been assigned to a new shift."
try:
fcm_token_repo.notify_user(user_id=user_id, title=title, body=body)
except Exception as e:
logging.error(f"Error sending notification to user {user_id}: {e}")

# Return raw data, the marshaller will format it
return {
"message": "Optimisation completed successfully",
Expand Down
28 changes: 28 additions & 0 deletions services/optimiser/optimiser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
from typing import List

import minizinc
from sqlalchemy import orm
from datetime import datetime
Expand Down Expand Up @@ -214,3 +216,29 @@ def save_result(self, result) -> None:
except Exception as e:
logging.error(f"Error processing result data: {e}")
raise # Rethrow the exception after logging

def extract_user_ids_from_result(self, result) -> List[int]:
"""
Extract the user IDs of volunteers assigned to shift based on the result of optimiser
:param result: The result of optimiser
:return: A list of user ids
"""
user_id_list = []

try:
# Loop through over shifts, volunteers and roles in result
for shift_index, shift_assignment in enumerate(result["possible_assignment"]):
for volunteer_index, volunteer_assignments in enumerate(shift_assignment):
for role_index, is_assigned in enumerate(volunteer_assignments):
if is_assigned:
user = self.calculator.get_volunteer_by_index(volunteer_index)

# avoid duplicate
if user.id not in user_id_list:
user_id_list.append(user.id)

except Exception as e:
logging.error(f"Error extracting user IDs from result: {e}")

return user_id_list

0 comments on commit 7f2b8e8

Please sign in to comment.