Booking lifecycle notifications and status updates
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
from apps.bookings.models import Booking
|
||||
from apps.bookings.models import Booking, BookingStatus
|
||||
from apps.bookings.services import validate_booking_request
|
||||
from apps.salons.models import Service, StaffProfile
|
||||
|
||||
@@ -27,7 +28,7 @@ class BookingSerializer(serializers.ModelSerializer):
|
||||
"notes",
|
||||
"created_at",
|
||||
]
|
||||
read_only_fields = ["id", "salon", "status", "price_amount", "currency", "created_at"]
|
||||
read_only_fields = ["id", "salon", "price_amount", "currency", "created_at"]
|
||||
|
||||
def get_staff_name(self, obj):
|
||||
if not obj.staff:
|
||||
@@ -36,6 +37,27 @@ class BookingSerializer(serializers.ModelSerializer):
|
||||
last = obj.staff.user.last_name or ""
|
||||
return (first + " " + last).strip() or obj.staff.user.email
|
||||
|
||||
def validate(self, attrs):
|
||||
if not self.instance or "status" not in attrs:
|
||||
return attrs
|
||||
|
||||
new_status = attrs["status"]
|
||||
old_status = self.instance.status
|
||||
if new_status == old_status:
|
||||
return attrs
|
||||
|
||||
user = self.context["request"].user
|
||||
role = getattr(user, "role", None)
|
||||
|
||||
if new_status == BookingStatus.CONFIRMED and role not in {"manager", "staff", "admin"}:
|
||||
raise serializers.ValidationError({"status": _("Only staff or managers can confirm bookings.")})
|
||||
if new_status == BookingStatus.COMPLETED and role not in {"manager", "staff", "admin"}:
|
||||
raise serializers.ValidationError({"status": _("Only staff or managers can complete bookings.")})
|
||||
if new_status == BookingStatus.CANCELLED and role not in {"manager", "staff", "admin", "customer"}:
|
||||
raise serializers.ValidationError({"status": _("You are not allowed to cancel this booking.")})
|
||||
|
||||
return attrs
|
||||
|
||||
|
||||
class BookingCreateSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from rest_framework import permissions, viewsets
|
||||
|
||||
from apps.bookings.models import Booking
|
||||
from apps.bookings.models import Booking, BookingStatus
|
||||
from apps.bookings.serializers import BookingCreateSerializer, BookingSerializer
|
||||
from apps.notifications.models import NotificationEvent
|
||||
from apps.notifications.services import notify_booking_lifecycle, notify_on_status_change
|
||||
|
||||
|
||||
class BookingViewSet(viewsets.ModelViewSet):
|
||||
@@ -21,3 +23,12 @@ class BookingViewSet(viewsets.ModelViewSet):
|
||||
if self.action == "create":
|
||||
return BookingCreateSerializer
|
||||
return BookingSerializer
|
||||
|
||||
def perform_create(self, serializer):
|
||||
booking = serializer.save()
|
||||
notify_booking_lifecycle(booking, NotificationEvent.BOOKING_CREATED)
|
||||
|
||||
def perform_update(self, serializer):
|
||||
previous_status = self.get_object().status
|
||||
booking = serializer.save()
|
||||
notify_on_status_change(booking, previous_status)
|
||||
|
||||
Reference in New Issue
Block a user