From d6becc43ead216b81d0c668c6611e95627363293 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Fri, 12 Dec 2025 09:16:52 +0100 Subject: [PATCH] Create notifications for each shipcall in the list --- .../BreCal/validators/validation_rules.py | 83 ++++++++++--------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/src/server/BreCal/validators/validation_rules.py b/src/server/BreCal/validators/validation_rules.py index 03ce506..85f507d 100644 --- a/src/server/BreCal/validators/validation_rules.py +++ b/src/server/BreCal/validators/validation_rules.py @@ -87,47 +87,54 @@ class ValidationRules(ValidationRuleFunctions): # build the list of evaluation times ('now', as isoformat) #evaluation_time = self.get_notification_times(evaluation_states_new) - send_notification = False if evaluation_states_old is not None and evaluation_states_new is not None: - if len(evaluation_states_old) == 1 and len(evaluation_states_new) == 1: - if evaluation_states_old[0] != evaluation_states_new[0]: - pooledConnection = None - try: - pooledConnection = getPoolConnection() - commands = pydapper.using(pooledConnection) - notification_type = 3 # RED (mapped to time_conflict) - if evaluation_states_new[0] == 2: - match evaluation_states_old[0]: - case 0: - send_notification = True - case 1: - send_notification = True - notification_type = 6 # YELLOW (mapped to missing_data) - if evaluation_states_new[0] == 3: - match evaluation_states_old[0]: - case 0: - send_notification = True - case 1: - send_notification = True - case 2: - send_notification = True + pooledConnection = None + try: + pooledConnection = getPoolConnection() + commands = pydapper.using(pooledConnection) - if send_notification: - 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_df.index[0]), "message" : violations[0]}) + for shipcall_id, state_old_raw, state_new_raw, violation in zip(shipcall_df.index, evaluation_states_old, evaluation_states_new, violations): + state_old = int(state_old_raw) if state_old_raw is not None else 0 + state_new = int(state_new_raw) if state_new_raw is not None else 0 - if evaluation_states_new[0] == 1 and evaluation_states_old[0] != 0: # this resolves the conflict - 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_df.index[0])}) - if len(existing_notification) > 0: - 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_df.index[0])}) - finally: - if pooledConnection is not None: - pooledConnection.close() + if state_old == state_new: + continue + + notification_type = 3 # RED (mapped to time_conflict) + send_notification = False + + if state_new == 2: + match state_old: + case 0: + send_notification = True + case 1: + send_notification = True + notification_type = 6 # YELLOW (mapped to missing_data) + elif state_new == 3: + match state_old: + case 0: + send_notification = True + case 1: + send_notification = True + case 2: + send_notification = True + + if send_notification: + 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}) + + if state_new == 1 and state_old != 0: # this resolves the conflict + 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)}) + if len(existing_notification) > 0: + 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)}) + finally: + if pooledConnection is not None: + pooledConnection.close() # build the list of 'evaluation_notifications_sent'. The value is 'False', when a notification should be created