Coverage for apps/appointments/serializers.py: 79%
69 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-05-23 10:47 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-05-23 10:47 -0600
1from django.utils import timezone
2from djmoney.contrib.django_rest_framework import MoneyField
3from rest_framework import serializers
5from apps.patients.serializers import PatientSerializer
6from apps.practitioners.models import Practitioner
7from base.mixins import ToRepresentationMixin
9from .models import (
10 Appointment,
11 AppointmentCharge,
12 AppointmentChargeExport,
13 AppointmentExport,
14 AppointmentPractitioner,
15 AppointmentType,
16)
19class AppointmentTypeSerializer(serializers.ModelSerializer):
20 """
21 Serializer for Appointment Type
22 """
24 class Meta:
25 model = AppointmentType
26 read_only_fields = ["random_slug"]
27 fields = read_only_fields + ["practitioner", "products", "duration", "name"]
30class AppointmentPractitionerSerializer(serializers.ModelSerializer):
31 """
32 Serializer for M2M for Practitioner and Appointment
33 """
35 class Meta:
36 model = AppointmentPractitioner
37 read_only_fields = ["random_slug", "created_at"]
38 fields = read_only_fields + ["appointment", "practitioner", "is_owner", "is_accepted"]
41class AppointmentSerializer(ToRepresentationMixin):
42 """
43 Serializer for Appointment
44 """
46 REPRESENTATION_FIELDS = [
47 ["practitioners", AppointmentPractitionerSerializer, True],
48 ["patient", PatientSerializer, False],
49 ["type_of", AppointmentTypeSerializer, False],
50 ]
52 practitioner = serializers.PrimaryKeyRelatedField(queryset=Practitioner.objects.all(), write_only=True)
53 total_price = MoneyField(max_digits=14, decimal_places=2, read_only=True)
54 pending_amount = MoneyField(max_digits=14, decimal_places=2, read_only=True)
55 paid_amount = MoneyField(max_digits=14, decimal_places=2, read_only=True)
57 class Meta:
58 model = Appointment
59 read_only_fields = [
60 "random_slug",
61 "created_at",
62 "practitioners",
63 "arrival_timestamp",
64 "start_timestamp",
65 "end_timestamp",
66 "payments",
67 ]
68 fields = read_only_fields + [
69 "practitioner",
70 "organization",
71 "patient",
72 "type_of",
73 "date",
74 "start_time",
75 "end_time",
76 "is_cancellable",
77 "is_confirmed",
78 "is_past",
79 "status",
80 "total_price",
81 "pending_amount",
82 "paid_amount",
83 "comments",
84 ]
86 def create(self, validated_data):
87 practitioner = validated_data.pop("practitioner")
89 # Validate for free practitioners
90 if not practitioner.is_paid:
91 today_appointments = Appointment.objects.filter(
92 practitioners__practitioner=practitioner,
93 created_at__month=timezone.now().date().month,
94 created_at__year=timezone.now().date().year,
95 )
96 if today_appointments.count() > 80:
97 raise serializers.ValidationError(
98 "Free user usage limit exceded (more than 80 appointments made this month"
99 )
101 appointment = super().create(validated_data)
103 AppointmentPractitioner.objects.create(
104 appointment=appointment, practitioner=practitioner, is_owner=True, is_accepted=True
105 )
107 return appointment
110class AppointmentChargeSerializer(serializers.ModelSerializer):
111 """
112 Serializer for AppointmentCharge
113 """
115 user_can_edit = serializers.SerializerMethodField()
116 pending_amount = MoneyField(max_digits=14, decimal_places=2, read_only=True)
117 paid_amount = MoneyField(max_digits=14, decimal_places=2, read_only=True)
118 total_price = MoneyField(max_digits=14, decimal_places=2, read_only=True)
120 class Meta:
121 model = AppointmentCharge
122 read_only_fields = ["random_slug", "created_at"]
123 fields = read_only_fields + [
124 "appointment",
125 "product",
126 "hard_cost",
127 "shared_cost",
128 "price",
129 "quantity",
130 "total_price",
131 "pending_amount",
132 "paid_amount",
133 "user_can_edit",
134 ]
136 def get_user_can_edit(self, appointment_charge: AppointmentCharge):
137 user = self.context.get("request").user
138 if hasattr(user, "practitioner"):
139 return True
140 elif user == appointment_charge.created_by:
141 return True
142 return False
145class AppointmentExportSerializer(serializers.ModelSerializer):
146 """
147 Serializer for AppointmentExport
148 """
150 class Meta:
151 model = AppointmentExport
152 read_only_fields = ["random_slug", "created_at", "export_file"]
153 fields = read_only_fields + ["organization", "practitioner", "from_date", "to_date"]
156class AppointmentChargeExportSerializer(serializers.ModelSerializer):
157 """
158 Serializer for AppointmentChargeExport
159 """
161 class Meta:
162 model = AppointmentChargeExport
163 read_only_fields = ["random_slug", "created_at", "export_file"]
164 fields = read_only_fields + [
165 "organization",
166 "practitioner",
167 ]
170class AppointmentSummarySerializer(serializers.Serializer):
171 """
172 Serializer for Appointment Date Count
173 """
175 from_date = serializers.DateField(required=True)
176 to_date = serializers.DateField(required=True)
177 practitioner = serializers.PrimaryKeyRelatedField(queryset=Practitioner.objects.all(), required=True)
180class AppointmentDateCountSerializer(serializers.Serializer):
181 """
182 Serializer for Appointment Date Count
183 """
185 date = serializers.DateField(read_only=True)
186 count = serializers.IntegerField(read_only=True)