oncall-engine/engine/apps/api/serializers/shift_swap.py
Joey Orlando 74b919ee3e
shift swap requests model + CRUD endpoints (#2597)
# What this PR does

This PR should allow us to start working on _most_ of the remaining
tasks for this feature set.
- Adds a basic `ShiftSwapRequest` model + CRUD endpoints. 
- Adds a `POST /api/internal/v1/shift_swaps/<id>/take` endpoint which
allows a benefactor to take a request (only when certain conditions
about the ssr are met)

Closes #2587 

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required) will be done in #2589
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required) (will update once we ship the finalized feature set)
2023-07-21 19:35:19 +00:00

80 lines
3 KiB
Python

import datetime
from django.utils import timezone
from rest_framework import serializers
from apps.schedules.models import OnCallSchedule, ShiftSwapRequest
from common.api_helpers.custom_fields import OrganizationFilteredPrimaryKeyRelatedField, TimeZoneAwareDatetimeField
from common.api_helpers.mixins import EagerLoadingMixin
class ShiftSwapRequestSerializer(EagerLoadingMixin, serializers.ModelSerializer):
id = serializers.CharField(read_only=True, source="public_primary_key")
schedule = OrganizationFilteredPrimaryKeyRelatedField(queryset=OnCallSchedule.objects)
created_at = TimeZoneAwareDatetimeField(read_only=True)
updated_at = TimeZoneAwareDatetimeField(read_only=True)
swap_start = TimeZoneAwareDatetimeField()
swap_end = TimeZoneAwareDatetimeField()
beneficiary = serializers.CharField(read_only=True, source="beneficiary.public_primary_key")
benefactor = serializers.SerializerMethodField(read_only=True)
SELECT_RELATED = [
"schedule",
"beneficiary",
"benefactor",
]
class Meta:
model = ShiftSwapRequest
fields = [
"id",
"created_at",
"updated_at",
"status",
"schedule",
"swap_start",
"swap_end",
"description",
"beneficiary",
"benefactor",
]
read_only_fields = [
"status",
]
def get_benefactor(self, obj) -> str | None:
return obj.benefactor.public_primary_key if obj.benefactor else None
@staticmethod
def validate_start_and_end_times(swap_start: datetime.datetime, swap_end: datetime.datetime) -> None:
if timezone.now() > swap_start:
raise serializers.ValidationError("swap_start must be a datetime in the future")
if swap_start > swap_end:
raise serializers.ValidationError("swap_end must occur after swap_start")
def validate(self, data):
swap_start = data.get("swap_start", None)
swap_end = data.get("swap_end", None)
if self.partial: # self.partial is true when it's a "partial update" aka PATCH
# if any time related field is specified then we will enforce that they must all be specified
time_fields = [swap_start, swap_end]
any_time_fields_specified = any(time_fields)
all_time_fields_specified = all(time_fields)
if any_time_fields_specified and not all_time_fields_specified:
raise serializers.ValidationError(
"when doing a partial update on time related fields, both start and end times must be specified"
)
elif all_time_fields_specified:
self.validate_start_and_end_times(swap_start, swap_end)
else:
self.validate_start_and_end_times(swap_start, swap_end)
# TODO: we should validate that the beneficiary actually has shifts for the specified schedule
# between swap_start and swap_end
return data