from django.conf import settings from django.db import models from apps.bookings.models import Booking class NotificationChannel(models.TextChoices): SMS = "sms", "SMS" WHATSAPP = "whatsapp", "WhatsApp" class NotificationEvent(models.TextChoices): BOOKING_CREATED = "booking_created", "Booking Created" BOOKING_CONFIRMED = "booking_confirmed", "Booking Confirmed" BOOKING_CANCELLED = "booking_cancelled", "Booking Cancelled" class NotificationStatus(models.TextChoices): PENDING = "pending", "Pending" SENT = "sent", "Sent" FAILED = "failed", "Failed" SKIPPED = "skipped", "Skipped" class Notification(models.Model): booking = models.ForeignKey( Booking, on_delete=models.CASCADE, related_name="notifications", null=True, blank=True, ) recipient = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, related_name="notifications", null=True, blank=True, ) phone_number = models.CharField(max_length=20, blank=True) channel = models.CharField(max_length=20, choices=NotificationChannel.choices) event = models.CharField(max_length=50, choices=NotificationEvent.choices) status = models.CharField( max_length=20, choices=NotificationStatus.choices, default=NotificationStatus.PENDING, ) provider = models.CharField(max_length=50, blank=True) message = models.TextField(blank=True) provider_payload = models.JSONField(default=dict, blank=True) error_message = models.TextField(blank=True) sent_at = models.DateTimeField(null=True, blank=True) created_at = models.DateTimeField(auto_now_add=True) class Meta: constraints = [ models.UniqueConstraint( fields=["booking", "recipient", "event", "channel"], name="uniq_notification_booking_event", ) ] def __str__(self) -> str: return f"{self.event} to {self.phone_number or self.recipient_id}"