@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,198 @@
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 { getPatientCalendarEventDocRef } from "./docs.utils";
26
+
27
+ /**
28
+ * Creates a calendar event for a patient
29
+ * @param db - Firestore instance
30
+ * @param patientId - ID of the patient
31
+ * @param eventData - Calendar event data
32
+ * @param generateId - Function to generate a unique ID
33
+ * @returns Created calendar event
34
+ */
35
+ export async function createPatientCalendarEventUtil(
36
+ db: Firestore,
37
+ patientId: string,
38
+ eventData: Omit<CreateCalendarEventData, "id" | "createdAt" | "updatedAt">,
39
+ generateId: () => string
40
+ ): Promise<CalendarEvent> {
41
+ // TODO: Add validation for event data
42
+ // - Check if patient exists
43
+ // - Validate event time (start < end)
44
+ // - Check for overlapping events
45
+ // - Validate required fields
46
+
47
+ const eventId = generateId();
48
+ const eventRef = getPatientCalendarEventDocRef(db, patientId, eventId);
49
+
50
+ const newEvent: CreateCalendarEventData = {
51
+ id: eventId,
52
+ ...eventData,
53
+ createdAt: serverTimestamp(),
54
+ updatedAt: serverTimestamp(),
55
+ };
56
+
57
+ await setDoc(eventRef, newEvent);
58
+
59
+ // Convert server timestamp to Timestamp for return value
60
+ return {
61
+ ...newEvent,
62
+ createdAt: Timestamp.now(),
63
+ updatedAt: Timestamp.now(),
64
+ } as CalendarEvent;
65
+ }
66
+
67
+ /**
68
+ * Gets a calendar event for a patient
69
+ * @param db - Firestore instance
70
+ * @param patientId - ID of the patient
71
+ * @param eventId - ID of the event
72
+ * @returns Calendar event or null if not found
73
+ */
74
+ export async function getPatientCalendarEventUtil(
75
+ db: Firestore,
76
+ patientId: string,
77
+ eventId: string
78
+ ): Promise<CalendarEvent | null> {
79
+ const eventRef = getPatientCalendarEventDocRef(db, patientId, eventId);
80
+ const eventDoc = await getDoc(eventRef);
81
+
82
+ if (!eventDoc.exists()) {
83
+ return null;
84
+ }
85
+
86
+ return eventDoc.data() as CalendarEvent;
87
+ }
88
+
89
+ /**
90
+ * Gets calendar events for a patient within a date range
91
+ * @param db - Firestore instance
92
+ * @param patientId - ID of the patient
93
+ * @param startDate - Start date of the range
94
+ * @param endDate - End date of the range
95
+ * @param additionalConstraints - Additional query constraints
96
+ * @returns Array of calendar events
97
+ */
98
+ export async function getPatientCalendarEventsUtil(
99
+ db: Firestore,
100
+ patientId: string,
101
+ startDate: Date,
102
+ endDate: Date,
103
+ additionalConstraints: QueryConstraint[] = []
104
+ ): Promise<CalendarEvent[]> {
105
+ const startTimestamp = Timestamp.fromDate(startDate);
106
+ const endTimestamp = Timestamp.fromDate(endDate);
107
+
108
+ const eventsRef = collection(
109
+ db,
110
+ `${CALENDAR_COLLECTION}`,
111
+ patientId,
112
+ `${CALENDAR_COLLECTION}`
113
+ );
114
+
115
+ const constraints: QueryConstraint[] = [
116
+ where("eventTime.start", ">=", startTimestamp),
117
+ where("eventTime.end", "<=", endTimestamp),
118
+ orderBy("eventTime.start", "asc"),
119
+ ...additionalConstraints,
120
+ ];
121
+
122
+ const q = query(eventsRef, ...constraints);
123
+
124
+ const querySnapshot = await getDocs(q);
125
+ return querySnapshot.docs.map((doc) => doc.data() as CalendarEvent);
126
+ }
127
+
128
+ /**
129
+ * Gets upcoming appointments for a patient
130
+ * @param db - Firestore instance
131
+ * @param patientId - ID of the patient
132
+ * @returns Array of upcoming calendar events
133
+ */
134
+ export async function getUpcomingPatientAppointmentsUtil(
135
+ db: Firestore,
136
+ patientId: string
137
+ ): Promise<CalendarEvent[]> {
138
+ const now = new Date();
139
+ const oneYearFromNow = new Date();
140
+ oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);
141
+
142
+ return getPatientCalendarEventsUtil(db, patientId, now, oneYearFromNow, [
143
+ where("eventType", "==", CalendarEventType.APPOINTMENT),
144
+ ]);
145
+ }
146
+
147
+ /**
148
+ * Updates a calendar event for a patient
149
+ * @param db - Firestore instance
150
+ * @param patientId - ID of the patient
151
+ * @param eventId - ID of the event
152
+ * @param updateData - Data to update
153
+ * @returns Updated calendar event
154
+ */
155
+ export async function updatePatientCalendarEventUtil(
156
+ db: Firestore,
157
+ patientId: string,
158
+ eventId: string,
159
+ updateData: Omit<UpdateCalendarEventData, "updatedAt">
160
+ ): Promise<CalendarEvent> {
161
+ // TODO: Add validation for update data
162
+ // - Check if event exists
163
+ // - Validate event time (start < end)
164
+ // - Check for overlapping events
165
+
166
+ const eventRef = getPatientCalendarEventDocRef(db, patientId, eventId);
167
+
168
+ const updates: UpdateCalendarEventData = {
169
+ ...updateData,
170
+ updatedAt: serverTimestamp(),
171
+ };
172
+
173
+ await updateDoc(eventRef, updates as any);
174
+
175
+ // Get the updated document
176
+ const updatedDoc = await getDoc(eventRef);
177
+
178
+ if (!updatedDoc.exists()) {
179
+ throw new Error("Event not found after update");
180
+ }
181
+
182
+ return updatedDoc.data() as CalendarEvent;
183
+ }
184
+
185
+ /**
186
+ * Deletes a calendar event for a patient
187
+ * @param db - Firestore instance
188
+ * @param patientId - ID of the patient
189
+ * @param eventId - ID of the event
190
+ */
191
+ export async function deletePatientCalendarEventUtil(
192
+ db: Firestore,
193
+ patientId: string,
194
+ eventId: string
195
+ ): Promise<void> {
196
+ const eventRef = getPatientCalendarEventDocRef(db, patientId, eventId);
197
+ await deleteDoc(eventRef);
198
+ }
@@ -0,0 +1,221 @@
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 { getPractitionerCalendarEventDocRef } from "./docs.utils";
26
+
27
+ /**
28
+ * Creates a calendar event for a practitioner
29
+ * @param db - Firestore instance
30
+ * @param practitionerId - ID of the practitioner
31
+ * @param eventData - Calendar event data
32
+ * @param generateId - Function to generate a unique ID
33
+ * @returns Created calendar event
34
+ */
35
+ export async function createPractitionerCalendarEventUtil(
36
+ db: Firestore,
37
+ practitionerId: string,
38
+ eventData: Omit<CreateCalendarEventData, "id" | "createdAt" | "updatedAt">,
39
+ generateId: () => string
40
+ ): Promise<CalendarEvent> {
41
+ // TODO: Add validation for event data
42
+ // - Check if practitioner exists
43
+ // - Validate event time (start < end)
44
+ // - Check for overlapping events
45
+ // - Validate required fields
46
+
47
+ const eventId = generateId();
48
+ const eventRef = getPractitionerCalendarEventDocRef(
49
+ db,
50
+ practitionerId,
51
+ eventId
52
+ );
53
+
54
+ const newEvent: CreateCalendarEventData = {
55
+ id: eventId,
56
+ ...eventData,
57
+ createdAt: serverTimestamp(),
58
+ updatedAt: serverTimestamp(),
59
+ };
60
+
61
+ await setDoc(eventRef, newEvent);
62
+
63
+ // Convert server timestamp to Timestamp for return value
64
+ return {
65
+ ...newEvent,
66
+ createdAt: Timestamp.now(),
67
+ updatedAt: Timestamp.now(),
68
+ } as CalendarEvent;
69
+ }
70
+
71
+ /**
72
+ * Gets a calendar event for a practitioner
73
+ * @param db - Firestore instance
74
+ * @param practitionerId - ID of the practitioner
75
+ * @param eventId - ID of the event
76
+ * @returns Calendar event or null if not found
77
+ */
78
+ export async function getPractitionerCalendarEventUtil(
79
+ db: Firestore,
80
+ practitionerId: string,
81
+ eventId: string
82
+ ): Promise<CalendarEvent | null> {
83
+ const eventRef = getPractitionerCalendarEventDocRef(
84
+ db,
85
+ practitionerId,
86
+ eventId
87
+ );
88
+ const eventDoc = await getDoc(eventRef);
89
+
90
+ if (!eventDoc.exists()) {
91
+ return null;
92
+ }
93
+
94
+ return eventDoc.data() as CalendarEvent;
95
+ }
96
+
97
+ /**
98
+ * Gets calendar events for a practitioner within a date range
99
+ * @param db - Firestore instance
100
+ * @param practitionerId - ID of the practitioner
101
+ * @param startDate - Start date of the range
102
+ * @param endDate - End date of the range
103
+ * @param additionalConstraints - Additional query constraints
104
+ * @returns Array of calendar events
105
+ */
106
+ export async function getPractitionerCalendarEventsUtil(
107
+ db: Firestore,
108
+ practitionerId: string,
109
+ startDate: Date,
110
+ endDate: Date,
111
+ additionalConstraints: QueryConstraint[] = []
112
+ ): Promise<CalendarEvent[]> {
113
+ const startTimestamp = Timestamp.fromDate(startDate);
114
+ const endTimestamp = Timestamp.fromDate(endDate);
115
+
116
+ const eventsRef = collection(
117
+ db,
118
+ `${CALENDAR_COLLECTION}`,
119
+ practitionerId,
120
+ `${CALENDAR_COLLECTION}`
121
+ );
122
+
123
+ const constraints: QueryConstraint[] = [
124
+ where("eventTime.start", ">=", startTimestamp),
125
+ where("eventTime.end", "<=", endTimestamp),
126
+ orderBy("eventTime.start", "asc"),
127
+ ...additionalConstraints,
128
+ ];
129
+
130
+ const q = query(eventsRef, ...constraints);
131
+
132
+ const querySnapshot = await getDocs(q);
133
+ return querySnapshot.docs.map((doc) => doc.data() as CalendarEvent);
134
+ }
135
+
136
+ /**
137
+ * Gets confirmed appointments for a practitioner
138
+ * @param db - Firestore instance
139
+ * @param practitionerId - ID of the practitioner
140
+ * @returns Array of confirmed calendar events
141
+ */
142
+ export async function getConfirmedPractitionerAppointmentsUtil(
143
+ db: Firestore,
144
+ practitionerId: string
145
+ ): Promise<CalendarEvent[]> {
146
+ const now = new Date();
147
+ const oneYearFromNow = new Date();
148
+ oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);
149
+
150
+ return getPractitionerCalendarEventsUtil(
151
+ db,
152
+ practitionerId,
153
+ now,
154
+ oneYearFromNow,
155
+ [
156
+ where("status", "==", CalendarEventStatus.CONFIRMED),
157
+ where("eventType", "==", CalendarEventType.APPOINTMENT),
158
+ ]
159
+ );
160
+ }
161
+
162
+ /**
163
+ * Updates a calendar event for a practitioner
164
+ * @param db - Firestore instance
165
+ * @param practitionerId - ID of the practitioner
166
+ * @param eventId - ID of the event
167
+ * @param updateData - Data to update
168
+ * @returns Updated calendar event
169
+ */
170
+ export async function updatePractitionerCalendarEventUtil(
171
+ db: Firestore,
172
+ practitionerId: string,
173
+ eventId: string,
174
+ updateData: Omit<UpdateCalendarEventData, "updatedAt">
175
+ ): Promise<CalendarEvent> {
176
+ // TODO: Add validation for update data
177
+ // - Check if event exists
178
+ // - Validate event time (start < end)
179
+ // - Check for overlapping events
180
+
181
+ const eventRef = getPractitionerCalendarEventDocRef(
182
+ db,
183
+ practitionerId,
184
+ eventId
185
+ );
186
+
187
+ const updates: UpdateCalendarEventData = {
188
+ ...updateData,
189
+ updatedAt: serverTimestamp(),
190
+ };
191
+
192
+ await updateDoc(eventRef, updates as any);
193
+
194
+ // Get the updated document
195
+ const updatedDoc = await getDoc(eventRef);
196
+
197
+ if (!updatedDoc.exists()) {
198
+ throw new Error("Event not found after update");
199
+ }
200
+
201
+ return updatedDoc.data() as CalendarEvent;
202
+ }
203
+
204
+ /**
205
+ * Deletes a calendar event for a practitioner
206
+ * @param db - Firestore instance
207
+ * @param practitionerId - ID of the practitioner
208
+ * @param eventId - ID of the event
209
+ */
210
+ export async function deletePractitionerCalendarEventUtil(
211
+ db: Firestore,
212
+ practitionerId: string,
213
+ eventId: string
214
+ ): Promise<void> {
215
+ const eventRef = getPractitionerCalendarEventDocRef(
216
+ db,
217
+ practitionerId,
218
+ eventId
219
+ );
220
+ await deleteDoc(eventRef);
221
+ }