added notifyer

This commit is contained in:
Kfir Dayan 2024-01-09 11:01:39 +02:00
parent 53a63877a1
commit 212b18a47a
4 changed files with 71 additions and 4 deletions

View file

@ -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 ###

View file

@ -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)

View file

@ -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()
# 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

View file

@ -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]