-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Yiran li/feature/get unavailability (#178)
## Describe your changes Modified the read feature to filter and return only the records that represent unavailability times in the future. I have conducted basic testing on Postman to verify the functionality works as expected without any apparent issues. ![PFRZ30RTMLX05} FR(2B5D4](https://github.com/TechlauncherFireApp/backend/assets/51045255/0656d799-0363-4941-b729-797ddbce7b29) ![@GZ9WWX9Z 6S`FM`` K$BK1](https://github.com/TechlauncherFireApp/backend/assets/51045255/2b728267-f164-483c-8baa-d05a77c379a2) For the security considerations, the userId is extracted from the URL, and the get function is protected by the @requires_auth decorator, so I think this setup ensures that users can only access their own unavailability records. I'm not sure whether I have misunderstanding about it. ## Issue ticket number and link https://fireapp-emergiq-2024.atlassian.net/jira/software/projects/FE/boards/1?selectedIssue=FE-145
- Loading branch information
Showing
2 changed files
with
165 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,115 @@ | ||
import logging | ||
|
||
from repository.unavailability_repository import * | ||
from flask import jsonify | ||
|
||
from datetime import datetime | ||
|
||
def edit_event(session, userId, eventId, title=None, start=None, end=None, periodicity=None): | ||
try: | ||
event = session.query(UnavailabilityTime).filter(UnavailabilityTime.eventId == eventId, | ||
UnavailabilityTime.userId == userId).first() | ||
if event is None: | ||
from domain import UnavailabilityTime, session_scope | ||
|
||
|
||
class EventRepository: | ||
def __init__(self): | ||
pass | ||
def edit_event(self, userId, eventId, title=None, start=None, end=None, periodicity=None): | ||
with session_scope() as session: | ||
try: | ||
event = session.query(UnavailabilityTime).filter(UnavailabilityTime.eventId == eventId, | ||
UnavailabilityTime.userId == userId).first() | ||
if event is None: | ||
return False | ||
if title is not None: | ||
event.title = title | ||
if start is not None: | ||
event.start = start | ||
if end is not None: | ||
event.end = end | ||
if end is not None: | ||
event.periodicity = periodicity | ||
session.commit() | ||
return True | ||
except Exception as e: | ||
session.rollback() | ||
logging.error(e) | ||
return None | ||
|
||
def get_event(self, userId): | ||
""" | ||
get all the non-availability events of the given user | ||
:param session: session | ||
:param userId: Integer, user id, who want to query the events | ||
""" | ||
now = datetime.now() | ||
with session_scope() as session: | ||
try: | ||
# only show the unavailability time that is end in the future | ||
events = session.query(UnavailabilityTime).filter( | ||
UnavailabilityTime.userId == userId, UnavailabilityTime.status == 1, UnavailabilityTime.end > now).all() | ||
if events: | ||
event_records = [] | ||
for event in events: | ||
# if the start time is earlier than now, then show from now to the end time | ||
start_time = max(event.start, now) | ||
event_record = { | ||
"eventId": event.eventId, | ||
"userId": event.userId, | ||
"title": event.title, | ||
"startTime": start_time.isoformat(), | ||
"endTime": event.end.isoformat(), | ||
"periodicity": event.periodicity | ||
} | ||
event_records.append(event_record) | ||
return jsonify(event_records) | ||
else: | ||
return None | ||
except Exception as e: | ||
logging.error(e) | ||
return None | ||
|
||
# copy from repository.unavailability_repository.py | ||
def create_event(self, userId, title, startTime, endTime, periodicity): | ||
""" | ||
Function to create an event | ||
:param session: session | ||
:param userId: Integer, user id | ||
:param title: String, reason why unavailable | ||
:param startTime: DateTime, from what time is unavailable | ||
:param endTime: DateTime, to what time is unavailable | ||
:param periodicity: Integer, Daily = 1, Weekly = 2, One-Off = 3 | ||
""" | ||
event = UnavailabilityTime(userId=userId, title=title, start=startTime, end=endTime, | ||
periodicity=periodicity) | ||
with session_scope() as session: | ||
session.add(event) | ||
# session.expunge(question) | ||
session.flush() | ||
return event.eventId | ||
|
||
# copy from repository.unavailability_repository.py | ||
def remove_event(self, userId, eventId): | ||
""" | ||
Function to remove an event | ||
:param session: session | ||
:param userId: Integer, user id, who want to remove an event | ||
:param eventId: Integer, event id want to remove | ||
:return: True: remove successful | ||
False: remove failed | ||
""" | ||
with session_scope() as session: | ||
existing = session.query(UnavailabilityTime).filter(UnavailabilityTime.userId == userId, | ||
UnavailabilityTime.eventId == eventId).first() | ||
if existing is not None and existing.status is True: | ||
existing.status = False | ||
return True | ||
return False | ||
if title is not None: | ||
event.title = title | ||
if start is not None: | ||
event.start = start | ||
if end is not None: | ||
event.end = end | ||
if end is not None: | ||
event.periodicity = periodicity | ||
session.commit() | ||
return True | ||
except Exception as e: | ||
session.rollback() | ||
logging.error(e) | ||
return None | ||
|
||
# copy from post function in api.py written by Steven | ||
def check_overlapping_events(self, userId, startTime, endTime, periodicity): | ||
with session_scope() as session: | ||
# checks if new time frame overlaps with any existing in the database for specific userId | ||
overlapping_events = session.query(UnavailabilityTime).filter( | ||
UnavailabilityTime.userId == userId, | ||
UnavailabilityTime.start < endTime, | ||
UnavailabilityTime.end > startTime, | ||
UnavailabilityTime.periodicity == periodicity | ||
).all() | ||
return overlapping_events |