Prepare to send E-mail notifications

This commit is contained in:
Daniel Schick 2024-12-12 16:06:32 +01:00
parent e5d9d051ea
commit 7548de7609

View File

@ -6,6 +6,7 @@ from BreCal.database.update_database import evaluate_shipcall_state
from BreCal.database.sql_queries import create_sql_query_shipcall_get
from BreCal.database.sql_queries import SQLQuery
from BreCal.database.sql_utils import get_notification_for_shipcall_and_type
from BreCal.services.email_handling import EmailHandler
import threading
import schedule
@ -66,6 +67,74 @@ def UpdateNotifications():
except Exception as ex:
logging.error(ex)
def SendNotifications():
# perhaps this will be moved somewhere else later
try:
# find all notifications in level 1
pooledConnection = getPoolConnection()
query = "SELECT * from notification WHERE level = 1"
commands = pydapper.using(pooledConnection)
data = commands.query(query, model=model.Notification)
if len(data) == 0:
return
# cache participants and users for performance beforehand
query = "SELECT * from participant";
participants = commands.query(query, model=model.Participant)
email_dict = dict()
users_dict = dict()
user_query = "SELECT * from user"
users = commands.query(user_query, model=model.User)
for participant in participants:
for user in users:
if user.participant_id == participant.id:
if not participant.id in users_dict:
users_dict[participant.id] = []
users_dict[participant.id].append(user)
break
for notification in data:
if notification.participant_id not in users_dict:
continue
users = users_dict[notification.participant_id]
for user in users:
# send notification to user
if user.notify_email:
match notification.type:
case 1: # assignment
email_dict[user] += "\n"
email_dict[user] += "You have been assigned to a new shipcall: " + notification.message
case 2: # next 24 hours
email_dict[user] += "\n"
email_dict[user] += "A shipcall is scheduled for the next 24 hours: " + notification.message
case 3: # Time conflict
email_dict[user] += "\n"
email_dict[user] += "There is a time conflict in a shipcall: " + notification.message
case 4: # Time conflict resolved
email_dict[user] += "\n"
email_dict[user] += "A time conflict in a shipcall has been resolved: " + notification.message
case 5: # unassigned
email_dict[user] += "\n"
email_dict[user] += "You have been unassigned from a shipcall: " + notification.message
if user.notify_whatsapp:
# TBD
pass
if user.notify_signal:
# TBD
pass
# mark as sent
commands.execute("UPDATE notification SET level = 2 WHERE id = ?id?", param={"id":notification.id})
# send emails
if len(email_dict) > 0:
email_handler = EmailHandler(mail_server="smtp.gmail.com", mail_port=465, mail_address="")
for user, message in email_dict.items():
email_handler.send_email(user.user_email, "BreCal Notification", message)
except Exception as ex:
logging.error(ex)
def add_function_to_schedule__update_shipcalls(interval_in_minutes:int, options:dict={'past_days':2}):
kwargs_ = {"options":options}
schedule.every(interval_in_minutes).minutes.do(UpdateShipcalls, **kwargs_)
@ -75,8 +144,8 @@ def add_function_to_evaluate_notifications(interval_in_minutes:int=1):
schedule.every(interval_in_minutes).minutes.do(UpdateNotifications)
return
def add_function_to_schedule__send_notifications(vr, interval_in_minutes:int=10):
schedule.every(interval_in_minutes).minutes.do(vr.notifier.send_notifications)
def add_function_to_schedule_send_notifications(interval_in_minutes:int=10):
schedule.every(interval_in_minutes).minutes.do(SendNotifications)
return
def eval_next_24_hrs():
@ -93,8 +162,8 @@ def eval_next_24_hrs():
for participant in participants:
if participant["type"] == 1: # BSMD
continue
if participant["type"] == 32: # PORT AUTHORITY
continue
# if participant["type"] == 32: # PORT AUTHORITY # Christin: Brake möchte sie vielleicht doch haben
# continue
# check if "open" notification already exists
found_notification = False
for existing_notification in existing_notifications:
@ -122,8 +191,7 @@ def setup_schedule(update_shipcalls_interval_in_minutes:int=60):
schedule.every().day.at("09:00").do(eval_next_24_hrs)
# placeholder: create/send notifications
# add_function_to_schedule__send_notifications(...)
add_function_to_schedule_send_notifications(1)
# TODO: Add schedule function to evaluate all notifications in level 1 and create actions