diff --git a/src/server/BreCal/validators/validation_rules.py b/src/server/BreCal/validators/validation_rules.py index a9d4a9c..454dd26 100644 --- a/src/server/BreCal/validators/validation_rules.py +++ b/src/server/BreCal/validators/validation_rules.py @@ -94,6 +94,7 @@ class ValidationRules(ValidationRuleFunctions): if evaluation_states_old is not None and evaluation_states_new is not None: pooledConnection = None + participants = None try: pooledConnection = getPoolConnection() commands = pydapper.using(pooledConnection) @@ -105,6 +106,10 @@ class ValidationRules(ValidationRuleFunctions): if state_old == state_new: continue + if participants is None: + participant_query = "SELECT participant_id, type FROM shipcall_participant_map WHERE shipcall_id = ?shipcall_id?" + participants = [participant for participant in commands.query(participant_query, model=dict, param={"shipcall_id" : int(shipcall_id)}) if participant.get("type") != 1] + notification_type = 3 # RED (mapped to time_conflict) send_notification = False @@ -125,22 +130,20 @@ class ValidationRules(ValidationRuleFunctions): send_notification = True if send_notification: - logging.info(f"Creating notification for shipcall {shipcall_id}, type={notification_type}") - query = f"INSERT INTO notification (shipcall_id, type, level, message) VALUES (?shipcall_id?, {notification_type}, 0, ?message?)" - commands.execute(query, param={"shipcall_id" : int(shipcall_id), "message" : violation}) + logging.info(f"Creating notification(s) for shipcall {shipcall_id}, type={notification_type}") + query = f"INSERT INTO notification (shipcall_id, participant_id, type, level, message) VALUES (?shipcall_id?, ?participant_id?, {notification_type}, 0, ?message?)" + for participant in participants: + commands.execute(query, param={"shipcall_id" : int(shipcall_id), "participant_id" : participant["participant_id"], "message" : violation}) if state_new == 1 and state_old != 0: # this resolves the time conflict logging.info(f"Resolving notifications for shipcall {shipcall_id}, type={notification_type}") - query = f"SELECT * from notification where shipcall_id = ?shipcall_id? and type = {notification_type} and level = 0" - existing_notification = commands.query(query, param={"shipcall_id" : int(shipcall_id)}) - logging.info(f"Found {len(existing_notification)} existing notifications (yet unsent)") - if len(existing_notification) > 0: - logging.info(f"Deleting notification id={existing_notification[0]['id']}") - query = "DELETE from notification where id = ?id?" - commands.execute(query, param={"id" : existing_notification[0]["id"]}) - else: - query = "INSERT INTO notification (shipcall_id, type, level) VALUES (?shipcall_id?, 4, 0)" - commands.execute(query, param={"shipcall_id" : int(shipcall_id)}) + query = f"DELETE from notification where shipcall_id = ?shipcall_id? and type = {notification_type} and level = 0" + deleted_count = commands.execute(query, param={"shipcall_id" : int(shipcall_id)}) + logging.info(f"Deleted {deleted_count} existing notifications (yet unsent)") + if deleted_count == 0: + query = "INSERT INTO notification (shipcall_id, participant_id, type, level) VALUES (?shipcall_id?, ?participant_id?, 4, 0)" + for participant in participants: + commands.execute(query, param={"shipcall_id" : int(shipcall_id), "participant_id" : participant["participant_id"]}) finally: if pooledConnection is not None: pooledConnection.close()