Event-CRUD-Flask-python3-API/services/EventNotifyerService.py
2024-01-09 11:01:39 +02:00

37 lines
No EOL
1.5 KiB
Python

# this Class is for the scheduler
#this class will have a function that locates the upcomming events Using the Event service.
from services.EventService import EventService
from services.UserService import UserService
from models import UserEventAssociation, db
class EventNotifyerService:
def __init__(self):
pass
def notifyUpcommingEvents():
# this method will get the upcoming events from the event service
# foreach event, locate the users with the same location as the event but not the users that already been notifyed about the event
# foreach user, append to UserEventAssociation table that the user is been notified about the event
events = EventService.get_upcoming_events()
for event in events:
# locate users with the same location as the event but not the users that already been notifyed about the event (users with UserEventAssociation)
users = UserService.get_users_with_same_location_but_not_been_notifyed(event['location'])
if not users:
print("No user to notify")
continue
for user in users:
print("Found user to notify:", user)
if notify_user(user['email'], event):
user_event_association = UserEventAssociation(
user_id=user['id'],
event_id=event['id']
)
db.session.add(user_event_association)
db.session.commit()
def notify_user(userEmail, event):
# send email to user is the email been sent, return true; otherwise, return false
return True