Skip to content

Commit

Permalink
Chim/feature/integrate noti optimiser (#307)
Browse files Browse the repository at this point in the history
## Describe your changes
1. I add notification functionality inside `save` method in optimiser
class.

## Issue ticket number and link

[Jira](https://fireapp-emergiq-2024.atlassian.net/jira/software/projects/FIR/boards/2?selectedIssue=FIR-122)
  • Loading branch information
anannnchim authored Oct 8, 2024
1 parent 55f07b1 commit da18be1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
21 changes: 18 additions & 3 deletions controllers/v2/optimiser/api.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import logging
from flask_restful import Resource, marshal_with, reqparse
from repository.fcm_token_repository import FCMTokenRepository
from .response_models import optimiser_response_model
from repository.shift_repository import ShiftRepository
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
from services.optimiser.optimiser import Optimiser
import logging


# Initialise parser for potential arguments in future extensions (if needed)
parser = reqparse.RequestParser()
parser.add_argument('debug', type=bool, required=False, help="Optional debug mode flag.")


class OptimiserResource(Resource):

optimiser_repository: ShiftRepository
fcm_token_repository: FCMTokenRepository

def __init__(self, optimiser_repository: ShiftRepository = ShiftRepository()):
def __init__(
self,
optimiser_repository: ShiftRepository = ShiftRepository(),
fcm_token_repository: FCMTokenRepository = FCMTokenRepository()
):
self.optimiser_repository = optimiser_repository
self.fcm_token_repository = fcm_token_repository

@requires_auth
@is_user_or_has_role(None, UserType.ROOT_ADMIN)
Expand All @@ -33,7 +42,13 @@ def post(self):
try:
with session_scope() as session:
# Initialise and run the optimiser
optimiser = Optimiser(session=session, repository=self.optimiser_repository, debug=debug)
optimiser = Optimiser(
session=session,
repository=self.optimiser_repository,
debug=debug,
fcm_token_repository=self.fcm_token_repository
)

result = optimiser.solve()
optimiser.save_result(result)

Expand Down
24 changes: 22 additions & 2 deletions services/optimiser/optimiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import minizinc
from sqlalchemy import orm
from datetime import datetime

from domain import ShiftRequestVolunteer, UnavailabilityTime
from repository.fcm_token_repository import FCMTokenRepository
from services.optimiser.calculator import Calculator
from repository.shift_repository import ShiftRepository


# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
Expand All @@ -16,7 +17,13 @@ class Optimiser:
# The calculator generates data structures for the optimiser to solve.
calculator = None

def __init__(self, session: orm.session, repository: ShiftRepository, debug: bool):
def __init__(
self,
session: orm.session,
repository: ShiftRepository,
debug: bool,
fcm_token_repository: FCMTokenRepository = None,
):
"""
@param session: The SQLAlchemy session to use
@param repository: The repository class for database operations.
Expand All @@ -25,6 +32,7 @@ def __init__(self, session: orm.session, repository: ShiftRepository, debug: boo
self.calculator = Calculator(session)
self.repository = repository
self.debug = debug
self.fcm_token_repository = fcm_token_repository

@staticmethod
def generate_model_string():
Expand Down Expand Up @@ -204,6 +212,18 @@ def save_result(self, result) -> None:
'shift_end': shift.endTime
})

# Send notification
try:
title = "New shift assignment"
body = "You have been assigned to a new shift"
self.fcm_token_repository.notify_user(
user_id=user.id,
title=title,
body=body
)
except Exception as e:
logger.error(f"Error sending notification to user {user.id}: {e}")

# Update shifts to PENDING status
for shift_id in shifts_to_update:
self.repository.update_shift_pending(shift_id)
Expand Down

0 comments on commit da18be1

Please sign in to comment.