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:
|
||||
|
||||
Reference in New Issue
Block a user