Skip to content

Commit

Permalink
5oappy/feature/create v2 improved (#177)
Browse files Browse the repository at this point in the history
## Describe your changes
repeat of
#172 (comment)

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
  • Loading branch information
5oappy authored Mar 12, 2024
1 parent 481d5b1 commit 29efaf5
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion controllers/v2/unavailability/api.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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,
Expand Down

0 comments on commit 29efaf5

Please sign in to comment.