@blackcode_sa/metaestetics-api 1.4.18 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,237 @@
1
+ import {
2
+ Firestore,
3
+ collection,
4
+ doc,
5
+ getDoc,
6
+ getDocs,
7
+ setDoc,
8
+ updateDoc,
9
+ deleteDoc,
10
+ query,
11
+ where,
12
+ orderBy,
13
+ Timestamp,
14
+ serverTimestamp,
15
+ QueryConstraint,
16
+ } from "firebase/firestore";
17
+ import {
18
+ CalendarEvent,
19
+ CalendarEventStatus,
20
+ CalendarEventType,
21
+ CreateCalendarEventData,
22
+ UpdateCalendarEventData,
23
+ CALENDAR_COLLECTION,
24
+ } from "../../../types/calendar";
25
+ import { getClinicCalendarEventDocRef } from "./docs.utils";
26
+ import { CLINIC_GROUPS_COLLECTION } from "../../../types/clinic";
27
+
28
+ /**
29
+ * Creates a calendar event for a clinic
30
+ * @param db - Firestore instance
31
+ * @param clinicId - ID of the clinic
32
+ * @param eventData - Calendar event data
33
+ * @param generateId - Function to generate a unique ID
34
+ * @returns Created calendar event
35
+ */
36
+ export async function createClinicCalendarEventUtil(
37
+ db: Firestore,
38
+ clinicId: string,
39
+ eventData: Omit<CreateCalendarEventData, "id" | "createdAt" | "updatedAt">,
40
+ generateId: () => string
41
+ ): Promise<CalendarEvent> {
42
+ // TODO: Add validation for event data
43
+ // - Check if clinic exists
44
+ // - Validate event time (start < end)
45
+ // - Check for overlapping events
46
+ // - Validate required fields
47
+
48
+ const eventId = generateId();
49
+ const eventRef = getClinicCalendarEventDocRef(db, clinicId, eventId);
50
+
51
+ const newEvent: CreateCalendarEventData = {
52
+ id: eventId,
53
+ ...eventData,
54
+ createdAt: serverTimestamp(),
55
+ updatedAt: serverTimestamp(),
56
+ };
57
+
58
+ await setDoc(eventRef, newEvent);
59
+
60
+ // Convert server timestamp to Timestamp for return value
61
+ return {
62
+ ...newEvent,
63
+ createdAt: Timestamp.now(),
64
+ updatedAt: Timestamp.now(),
65
+ } as CalendarEvent;
66
+ }
67
+
68
+ /**
69
+ * Gets a calendar event for a clinic
70
+ * @param db - Firestore instance
71
+ * @param clinicId - ID of the clinic
72
+ * @param eventId - ID of the event
73
+ * @returns Calendar event or null if not found
74
+ */
75
+ export async function getClinicCalendarEventUtil(
76
+ db: Firestore,
77
+ clinicId: string,
78
+ eventId: string
79
+ ): Promise<CalendarEvent | null> {
80
+ const eventRef = getClinicCalendarEventDocRef(db, clinicId, eventId);
81
+ const eventDoc = await getDoc(eventRef);
82
+
83
+ if (!eventDoc.exists()) {
84
+ return null;
85
+ }
86
+
87
+ return eventDoc.data() as CalendarEvent;
88
+ }
89
+
90
+ /**
91
+ * Gets calendar events for a clinic within a date range
92
+ * @param db - Firestore instance
93
+ * @param clinicId - ID of the clinic
94
+ * @param startDate - Start date of the range
95
+ * @param endDate - End date of the range
96
+ * @param additionalConstraints - Additional query constraints
97
+ * @returns Array of calendar events
98
+ */
99
+ export async function getClinicCalendarEventsUtil(
100
+ db: Firestore,
101
+ clinicId: string,
102
+ startDate: Date,
103
+ endDate: Date,
104
+ additionalConstraints: QueryConstraint[] = []
105
+ ): Promise<CalendarEvent[]> {
106
+ const startTimestamp = Timestamp.fromDate(startDate);
107
+ const endTimestamp = Timestamp.fromDate(endDate);
108
+
109
+ const eventsRef = collection(
110
+ db,
111
+ `${CALENDAR_COLLECTION}`,
112
+ clinicId,
113
+ `${CALENDAR_COLLECTION}`
114
+ );
115
+
116
+ const constraints: QueryConstraint[] = [
117
+ where("eventTime.start", ">=", startTimestamp),
118
+ where("eventTime.end", "<=", endTimestamp),
119
+ orderBy("eventTime.start", "asc"),
120
+ ...additionalConstraints,
121
+ ];
122
+
123
+ const q = query(eventsRef, ...constraints);
124
+
125
+ const querySnapshot = await getDocs(q);
126
+ return querySnapshot.docs.map((doc) => doc.data() as CalendarEvent);
127
+ }
128
+
129
+ /**
130
+ * Gets pending appointments for a clinic
131
+ * @param db - Firestore instance
132
+ * @param clinicId - ID of the clinic
133
+ * @returns Array of pending calendar events
134
+ */
135
+ export async function getPendingClinicAppointmentsUtil(
136
+ db: Firestore,
137
+ clinicId: string
138
+ ): Promise<CalendarEvent[]> {
139
+ const now = new Date();
140
+ const oneYearFromNow = new Date();
141
+ oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);
142
+
143
+ return getClinicCalendarEventsUtil(db, clinicId, now, oneYearFromNow, [
144
+ where("status", "==", CalendarEventStatus.PENDING),
145
+ where("eventType", "==", CalendarEventType.APPOINTMENT),
146
+ ]);
147
+ }
148
+
149
+ /**
150
+ * Updates a calendar event for a clinic
151
+ * @param db - Firestore instance
152
+ * @param clinicId - ID of the clinic
153
+ * @param eventId - ID of the event
154
+ * @param updateData - Data to update
155
+ * @returns Updated calendar event
156
+ */
157
+ export async function updateClinicCalendarEventUtil(
158
+ db: Firestore,
159
+ clinicId: string,
160
+ eventId: string,
161
+ updateData: Omit<UpdateCalendarEventData, "updatedAt">
162
+ ): Promise<CalendarEvent> {
163
+ // TODO: Add validation for update data
164
+ // - Check if event exists
165
+ // - Validate event time (start < end)
166
+ // - Check for overlapping events
167
+
168
+ const eventRef = getClinicCalendarEventDocRef(db, clinicId, eventId);
169
+
170
+ const updates: UpdateCalendarEventData = {
171
+ ...updateData,
172
+ updatedAt: serverTimestamp(),
173
+ };
174
+
175
+ await updateDoc(eventRef, updates as any);
176
+
177
+ // Get the updated document
178
+ const updatedDoc = await getDoc(eventRef);
179
+
180
+ if (!updatedDoc.exists()) {
181
+ throw new Error("Event not found after update");
182
+ }
183
+
184
+ return updatedDoc.data() as CalendarEvent;
185
+ }
186
+
187
+ /**
188
+ * Deletes a calendar event for a clinic
189
+ * @param db - Firestore instance
190
+ * @param clinicId - ID of the clinic
191
+ * @param eventId - ID of the event
192
+ */
193
+ export async function deleteClinicCalendarEventUtil(
194
+ db: Firestore,
195
+ clinicId: string,
196
+ eventId: string
197
+ ): Promise<void> {
198
+ const eventRef = getClinicCalendarEventDocRef(db, clinicId, eventId);
199
+ await deleteDoc(eventRef);
200
+ }
201
+
202
+ /**
203
+ * Checks if a clinic group has automatic appointment confirmation enabled
204
+ * @param db - Firestore instance
205
+ * @param clinicId - ID of the clinic
206
+ * @returns Boolean indicating if automatic confirmation is enabled
207
+ */
208
+ export async function checkAutoConfirmAppointmentsUtil(
209
+ db: Firestore,
210
+ clinicId: string
211
+ ): Promise<boolean> {
212
+ // First, get the clinic to find its clinic group
213
+ const clinicDoc = await getDoc(doc(db, `clinics/${clinicId}`));
214
+
215
+ if (!clinicDoc.exists()) {
216
+ throw new Error(`Clinic with ID ${clinicId} not found`);
217
+ }
218
+
219
+ const clinicData = clinicDoc.data();
220
+ const clinicGroupId = clinicData.clinicGroupId;
221
+
222
+ if (!clinicGroupId) {
223
+ return false; // Default to false if no clinic group is associated
224
+ }
225
+
226
+ // Get the clinic group settings
227
+ const clinicGroupDoc = await getDoc(
228
+ doc(db, `${CLINIC_GROUPS_COLLECTION}/${clinicGroupId}`)
229
+ );
230
+
231
+ if (!clinicGroupDoc.exists()) {
232
+ return false; // Default to false if clinic group doesn't exist
233
+ }
234
+
235
+ const clinicGroupData = clinicGroupDoc.data();
236
+ return !!clinicGroupData.autoConfirmAppointments; // Convert to boolean
237
+ }
@@ -0,0 +1,157 @@
1
+ import { Firestore, doc, DocumentReference } from "firebase/firestore";
2
+ import { CALENDAR_COLLECTION } from "../../../types/calendar";
3
+ import { PRACTITIONERS_COLLECTION } from "../../../types/practitioner";
4
+ import { PATIENTS_COLLECTION } from "../../../types/patient";
5
+ import { CLINICS_COLLECTION } from "../../../types/clinic";
6
+
7
+ /**
8
+ * Gets a reference to a practitioner's calendar event document
9
+ * @param db - Firestore instance
10
+ * @param practitionerId - ID of the practitioner
11
+ * @param eventId - ID of the event
12
+ * @returns DocumentReference to the calendar event
13
+ */
14
+ export function getPractitionerCalendarEventDocRef(
15
+ db: Firestore,
16
+ practitionerId: string,
17
+ eventId: string
18
+ ): DocumentReference {
19
+ return doc(
20
+ db,
21
+ `${PRACTITIONERS_COLLECTION}/${practitionerId}/${CALENDAR_COLLECTION}/${eventId}`
22
+ );
23
+ }
24
+
25
+ /**
26
+ * Gets a reference to a practitioner's calendar collection
27
+ * @param db - Firestore instance
28
+ * @param practitionerId - ID of the practitioner
29
+ * @returns DocumentReference to the calendar collection
30
+ */
31
+ export function getPractitionerCalendarCollectionRef(
32
+ db: Firestore,
33
+ practitionerId: string
34
+ ): DocumentReference {
35
+ return doc(
36
+ db,
37
+ `${PRACTITIONERS_COLLECTION}/${practitionerId}/${CALENDAR_COLLECTION}`
38
+ );
39
+ }
40
+
41
+ /**
42
+ * Gets a reference to a patient's calendar event document
43
+ * @param db - Firestore instance
44
+ * @param patientId - ID of the patient
45
+ * @param eventId - ID of the event
46
+ * @returns DocumentReference to the calendar event
47
+ */
48
+ export function getPatientCalendarEventDocRef(
49
+ db: Firestore,
50
+ patientId: string,
51
+ eventId: string
52
+ ): DocumentReference {
53
+ return doc(
54
+ db,
55
+ `${PATIENTS_COLLECTION}/${patientId}/${CALENDAR_COLLECTION}/${eventId}`
56
+ );
57
+ }
58
+
59
+ /**
60
+ * Gets a reference to a patient's calendar collection
61
+ * @param db - Firestore instance
62
+ * @param patientId - ID of the patient
63
+ * @returns DocumentReference to the calendar collection
64
+ */
65
+ export function getPatientCalendarCollectionRef(
66
+ db: Firestore,
67
+ patientId: string
68
+ ): DocumentReference {
69
+ return doc(db, `${PATIENTS_COLLECTION}/${patientId}/${CALENDAR_COLLECTION}`);
70
+ }
71
+
72
+ /**
73
+ * Gets a reference to a clinic's calendar event document
74
+ * @param db - Firestore instance
75
+ * @param clinicId - ID of the clinic
76
+ * @param eventId - ID of the event
77
+ * @returns DocumentReference to the calendar event
78
+ */
79
+ export function getClinicCalendarEventDocRef(
80
+ db: Firestore,
81
+ clinicId: string,
82
+ eventId: string
83
+ ): DocumentReference {
84
+ return doc(
85
+ db,
86
+ `${CLINICS_COLLECTION}/${clinicId}/${CALENDAR_COLLECTION}/${eventId}`
87
+ );
88
+ }
89
+
90
+ /**
91
+ * Gets a reference to a clinic's calendar collection
92
+ * @param db - Firestore instance
93
+ * @param clinicId - ID of the clinic
94
+ * @returns DocumentReference to the calendar collection
95
+ */
96
+ export function getClinicCalendarCollectionRef(
97
+ db: Firestore,
98
+ clinicId: string
99
+ ): DocumentReference {
100
+ return doc(db, `${CLINICS_COLLECTION}/${clinicId}/${CALENDAR_COLLECTION}`);
101
+ }
102
+
103
+ // Synced Calendar References
104
+
105
+ /**
106
+ * Gets a reference to a practitioner's synced calendar event document
107
+ * @param db - Firestore instance
108
+ * @param practitionerId - ID of the practitioner
109
+ * @param syncedCalendarId - ID of the synced calendar
110
+ * @returns DocumentReference to the synced calendar event
111
+ */
112
+ export function getPractitionerSyncedCalendarDocRef(
113
+ db: Firestore,
114
+ practitionerId: string,
115
+ syncedCalendarId: string
116
+ ): DocumentReference {
117
+ return doc(
118
+ db,
119
+ `${PRACTITIONERS_COLLECTION}/${practitionerId}/syncedCalendars/${syncedCalendarId}`
120
+ );
121
+ }
122
+
123
+ /**
124
+ * Gets a reference to a patient's synced calendar event document
125
+ * @param db - Firestore instance
126
+ * @param patientId - ID of the patient
127
+ * @param syncedCalendarId - ID of the synced calendar
128
+ * @returns DocumentReference to the synced calendar event
129
+ */
130
+ export function getPatientSyncedCalendarDocRef(
131
+ db: Firestore,
132
+ patientId: string,
133
+ syncedCalendarId: string
134
+ ): DocumentReference {
135
+ return doc(
136
+ db,
137
+ `${PATIENTS_COLLECTION}/${patientId}/syncedCalendars/${syncedCalendarId}`
138
+ );
139
+ }
140
+
141
+ /**
142
+ * Gets a reference to a clinic's synced calendar event document
143
+ * @param db - Firestore instance
144
+ * @param clinicId - ID of the clinic
145
+ * @param syncedCalendarId - ID of the synced calendar
146
+ * @returns DocumentReference to the synced calendar event
147
+ */
148
+ export function getClinicSyncedCalendarDocRef(
149
+ db: Firestore,
150
+ clinicId: string,
151
+ syncedCalendarId: string
152
+ ): DocumentReference {
153
+ return doc(
154
+ db,
155
+ `${CLINICS_COLLECTION}/${clinicId}/syncedCalendars/${syncedCalendarId}`
156
+ );
157
+ }