Skip to content

Commit db01d57

Browse files
committed
unregistering from events
1 parent 51eed65 commit db01d57

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

app/api/endpoints/registration.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ async def get_volunteers_by_event(event_id: str) -> list[Registration]:
2121
@router.get("/events/{volunteer_id}", response_model=list[Event])
2222
async def get_events_by_volunteer(
2323
volunteer_id: str,
24-
status: Annotated[RegistrationStatus | None, Query(description="Filter by status")] = None,
24+
registration_status: Annotated[
25+
RegistrationStatus | None, Query(description="Filter by status")
26+
] = None,
2527
current_user: Annotated[User, Depends(get_current_user)] = None,
2628
) -> list[Event]:
2729
volunteer = await volunteer_model.get_volunteer_by_id(volunteer_id)
@@ -37,7 +39,7 @@ async def get_events_by_volunteer(
3739
elif current_user.user_type != UserType.ADMIN:
3840
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Access denied")
3941

40-
return await registration_model.get_events_by_volunteer(volunteer_id, status)
42+
return await registration_model.get_events_by_volunteer(volunteer_id, registration_status)
4143

4244

4345
@router.post("/new", response_model=Registration)
@@ -57,3 +59,23 @@ async def create_registrion(
5759
)
5860

5961
return await registration_model.create_registration(registration, current_user.entity_id)
62+
63+
64+
@router.put("/unregister/{registration_id}", response_model=Registration)
65+
async def unregister_registration(
66+
registration_id: str,
67+
current_user: Annotated[User, Depends(get_current_user)],
68+
) -> Registration:
69+
if current_user.user_type != UserType.VOLUNTEER:
70+
raise HTTPException(
71+
status_code=status.HTTP_403_FORBIDDEN,
72+
detail="Only volunteers can unregister from events",
73+
)
74+
75+
if current_user.entity_id is None:
76+
raise HTTPException(
77+
status_code=status.HTTP_400_BAD_REQUEST,
78+
detail="You must be associated with a volunteer profile to unregister from an event",
79+
)
80+
81+
return await registration_model.unregister_registration(registration_id, current_user.entity_id)

app/models/registration.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,29 @@ async def create_registration(
8989

9090
return self._to_registration(inserted_doc)
9191

92+
async def unregister_registration(
93+
self, registration_id: str, volunteer_id: str
94+
) -> Registration:
95+
registration = await self.registrations.find_one({"_id": ObjectId(registration_id)})
96+
97+
if not registration:
98+
raise HTTPException(
99+
status_code=status.HTTP_404_NOT_FOUND, detail="Registration not found"
100+
)
101+
102+
if str(registration["volunteer_id"]) != volunteer_id:
103+
raise HTTPException(
104+
status_code=status.HTTP_403_FORBIDDEN,
105+
detail="Not authorized to unregister from this event",
106+
)
107+
108+
await self.registrations.update_one(
109+
{"_id": ObjectId(registration_id)},
110+
{"$set": {"registration_status": RegistrationStatus.UNREGISTERED}},
111+
)
112+
updated_doc = await self.registrations.find_one({"_id": ObjectId(registration_id)})
113+
return self._to_registration(updated_doc)
114+
92115
def _to_registration(self, doc) -> Registration:
93116
registration_data = doc.copy()
94117
registration_data["id"] = str(registration_data["_id"])

0 commit comments

Comments
 (0)