From 29efaf534715f62e156396f2986af87c7209c165 Mon Sep 17 00:00:00 2001 From: 5oappy <111340376+5oappy@users.noreply.github.com> Date: Tue, 12 Mar 2024 22:49:26 +1100 Subject: [PATCH] 5oappy/feature/create v2 improved (#177) ## Describe your changes repeat of https://github.com/TechlauncherFireApp/backend/pull/172#issue-2177547567 with changed branch name, also fixed issues with code style within v2/unavailability/api.py ## Issue ticket number and link https://fireapp-emergiq-2024.atlassian.net/browse/FE-68 --- controllers/v2/unavailability/api.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/controllers/v2/unavailability/api.py b/controllers/v2/unavailability/api.py index 23a223a4..87870c79 100644 --- a/controllers/v2/unavailability/api.py +++ b/controllers/v2/unavailability/api.py @@ -1,9 +1,14 @@ +import json +import uuid + from flask import jsonify from flask_restful import reqparse, Resource, marshal_with, inputs +from domain.entity import unavailability_time from .response_models import volunteer_unavailability_time from domain import session_scope, UserType from repository.volunteer_unavailability_v2 import * +from repository.unavailability_repository import * from services.jwk import requires_auth, is_user_or_has_role from controllers.v2.v2_blueprint import v2_api @@ -43,7 +48,7 @@ def delete(self, user_id, event_id): return {"message": "Unavailability event not found."}, 404 except Exception as e: # HTTP 500 Internal Server Error - return {"description": "Internal server error", "error": str(e)}, 500 + return {"message": "Internal server error", "error": str(e)}, 500 class VolunteerUnavailabilityV2(Resource): @@ -64,7 +69,26 @@ def get(self, user_id): def post(self, user_id): try: args = edit_parser.parse_args() + # Check if start time is earlier than end time. + if args['start'] >= args['end']: + return {"message": "Start time must be earlier than end time"}, 400 # HTTP 400 Bad Request + 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 == user_id, + UnavailabilityTime.start < args['end'], + UnavailabilityTime.end > args['start'], + UnavailabilityTime.periodicity == args['periodicity'] + ).all() + if overlapping_events: + overlapping_details = [] + for event in overlapping_events: + overlapping_details.append({ + "eventId": event.eventId}) + return {"message": "Time frames overlap with existing events", + "overlapping_events": overlapping_details}, 400 # HTTP 400 Bad Request + eventId = create_event( session, user_id,