diff --git a/migrations/versions/30ce6cd40d05_adda_col_notifyed_bool.py b/migrations/versions/30ce6cd40d05_adda_col_notifyed_bool.py new file mode 100644 index 0000000..ffc3603 --- /dev/null +++ b/migrations/versions/30ce6cd40d05_adda_col_notifyed_bool.py @@ -0,0 +1,32 @@ +"""AddA_col_Notifyed_Bool + +Revision ID: 30ce6cd40d05 +Revises: 04e41f293ec2 +Create Date: 2024-01-09 10:46:50.964332 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '30ce6cd40d05' +down_revision = '04e41f293ec2' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('user_event', schema=None) as batch_op: + batch_op.drop_column('notified') + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('user_event', schema=None) as batch_op: + batch_op.add_column(sa.Column('notified', sa.BOOLEAN(), nullable=True)) + + # ### end Alembic commands ### diff --git a/models.py b/models.py index 855a8bf..b748f1f 100644 --- a/models.py +++ b/models.py @@ -10,7 +10,6 @@ class UserEventAssociation(db.Model): __tablename__ = 'user_event' user_id = Column(String(36), ForeignKey('user.id'), primary_key=True) event_id = Column(Integer, ForeignKey('event.id'), primary_key=True) - notified = Column(Boolean, default=False) class Event(db.Model): id = Column(db.Integer, primary_key=True) diff --git a/services/EventNotifyerService.py b/services/EventNotifyerService.py index 4aa5d38..d24bf4a 100644 --- a/services/EventNotifyerService.py +++ b/services/EventNotifyerService.py @@ -1,10 +1,37 @@ # 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(): - return EventService.get_all_upcomming_events() \ No newline at end of file + # 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 + + \ No newline at end of file diff --git a/services/UserService.py b/services/UserService.py index 22f11cb..fdb455c 100644 --- a/services/UserService.py +++ b/services/UserService.py @@ -1,5 +1,6 @@ from flask_bcrypt import Bcrypt -from models import db, User +from models import db, User, UserEventAssociation + bcrypt = Bcrypt() @@ -35,3 +36,11 @@ class UserService: if user and bcrypt.check_password_hash(user.password_hash, data['password']): return user return None + + @staticmethod + def get_users_with_same_location_but_not_been_notifyed(location): + users = User.query.filter_by(location=location).all() + users = [user for user in users if not UserEventAssociation.query.filter_by(user_id=user.id).first()] + return [user.to_dict() for user in users] + +