@blackcode_sa/metaestetics-api 1.5.27 → 1.5.29

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.
Files changed (35) hide show
  1. package/dist/admin/index.d.mts +1199 -1
  2. package/dist/admin/index.d.ts +1199 -1
  3. package/dist/admin/index.js +1337 -2
  4. package/dist/admin/index.mjs +1333 -2
  5. package/dist/backoffice/index.d.mts +99 -7
  6. package/dist/backoffice/index.d.ts +99 -7
  7. package/dist/index.d.mts +4184 -2426
  8. package/dist/index.d.ts +4184 -2426
  9. package/dist/index.js +2692 -1546
  10. package/dist/index.mjs +2663 -1502
  11. package/package.json +1 -1
  12. package/src/admin/aggregation/clinic/clinic.aggregation.service.ts +642 -0
  13. package/src/admin/aggregation/patient/patient.aggregation.service.ts +141 -0
  14. package/src/admin/aggregation/practitioner/practitioner.aggregation.service.ts +433 -0
  15. package/src/admin/aggregation/procedure/procedure.aggregation.service.ts +508 -0
  16. package/src/admin/index.ts +53 -4
  17. package/src/index.ts +28 -4
  18. package/src/services/calendar/calendar-refactored.service.ts +1 -1
  19. package/src/services/clinic/clinic.service.ts +344 -77
  20. package/src/services/clinic/utils/clinic.utils.ts +187 -8
  21. package/src/services/clinic/utils/filter.utils.d.ts +23 -0
  22. package/src/services/clinic/utils/filter.utils.ts +264 -0
  23. package/src/services/practitioner/practitioner.service.ts +616 -5
  24. package/src/services/procedure/procedure.service.ts +678 -52
  25. package/src/services/reviews/reviews.service.ts +842 -0
  26. package/src/types/clinic/index.ts +24 -56
  27. package/src/types/practitioner/index.ts +34 -33
  28. package/src/types/procedure/index.ts +39 -0
  29. package/src/types/profile/index.ts +1 -1
  30. package/src/types/reviews/index.ts +126 -0
  31. package/src/validations/clinic.schema.ts +37 -64
  32. package/src/validations/practitioner.schema.ts +42 -32
  33. package/src/validations/procedure.schema.ts +14 -3
  34. package/src/validations/reviews.schema.ts +189 -0
  35. package/src/services/clinic/utils/review.utils.ts +0 -93
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@blackcode_sa/metaestetics-api",
3
3
  "private": false,
4
- "version": "1.5.27",
4
+ "version": "1.5.29",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.mjs",
@@ -0,0 +1,642 @@
1
+ import * as admin from "firebase-admin";
2
+ import { ClinicInfo } from "../../../types/profile"; // Corrected path
3
+ import { PRACTITIONERS_COLLECTION } from "../../../types/practitioner"; // Corrected path
4
+ import { PROCEDURES_COLLECTION } from "../../../types/procedure"; // Added procedure collection
5
+ import { CLINIC_GROUPS_COLLECTION } from "../../../types/clinic"; // Added clinic group collection
6
+ import { ClinicLocation } from "../../../types/clinic"; // Added ClinicLocation type
7
+ import { CALENDAR_COLLECTION } from "../../../types/calendar"; // Added calendar collection
8
+ import { PATIENTS_COLLECTION } from "../../../types/patient"; // Added patient collection
9
+
10
+ const CALENDAR_SUBCOLLECTION_ID = "calendar"; // Define the subcollection ID explicitly
11
+
12
+ /**
13
+ * @class ClinicAggregationService
14
+ * @description Handles aggregation tasks related to clinic data updates.
15
+ * This service is intended to be used primarily by background functions (e.g., Cloud Functions)
16
+ * triggered by changes in the clinics collection.
17
+ */
18
+ export class ClinicAggregationService {
19
+ private db: admin.firestore.Firestore;
20
+
21
+ /**
22
+ * Constructor for ClinicAggregationService.
23
+ * @param firestore Optional Firestore instance. If not provided, it uses the default admin SDK instance.
24
+ */
25
+ constructor(firestore?: admin.firestore.Firestore) {
26
+ this.db = firestore || admin.firestore();
27
+ }
28
+
29
+ /**
30
+ * Adds clinic information to a clinic group when a new clinic is created
31
+ * @param clinicGroupId - ID of the parent clinic group
32
+ * @param clinicInfo - The clinic information to add to the group
33
+ * @returns {Promise<void>}
34
+ * @throws Will throw an error if the batch write fails
35
+ */
36
+ async addClinicToClinicGroup(
37
+ clinicGroupId: string,
38
+ clinicInfo: ClinicInfo
39
+ ): Promise<void> {
40
+ if (!clinicGroupId) {
41
+ console.log(
42
+ "[ClinicAggregationService] No Clinic Group ID provided for clinic addition. Skipping."
43
+ );
44
+ return;
45
+ }
46
+
47
+ const clinicId = clinicInfo.id;
48
+ const groupRef = this.db
49
+ .collection(CLINIC_GROUPS_COLLECTION)
50
+ .doc(clinicGroupId);
51
+
52
+ console.log(
53
+ `[ClinicAggregationService] Adding ClinicInfo (ID: ${clinicId}) to Clinic Group ${clinicGroupId}.`
54
+ );
55
+
56
+ try {
57
+ await groupRef.update({
58
+ clinicsInfo: admin.firestore.FieldValue.arrayUnion(clinicInfo),
59
+ clinicIds: admin.firestore.FieldValue.arrayUnion(clinicId),
60
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
61
+ });
62
+
63
+ console.log(
64
+ `[ClinicAggregationService] Successfully added ClinicInfo (ID: ${clinicId}) to Clinic Group ${clinicGroupId}.`
65
+ );
66
+ } catch (error) {
67
+ console.error(
68
+ `[ClinicAggregationService] Error adding ClinicInfo (ID: ${clinicId}) to Clinic Group ${clinicGroupId}:`,
69
+ error
70
+ );
71
+ throw error;
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Updates the ClinicInfo within the clinicsInfo array for multiple practitioners.
77
+ * This method is designed to be called when a clinic's core information changes.
78
+ * @param practitionerIds - IDs of practitioners associated with the clinic.
79
+ * @param clinicInfo - The updated ClinicInfo object to aggregate.
80
+ * @returns {Promise<void>}
81
+ * @throws Will throw an error if the batch write fails.
82
+ */
83
+ async updateClinicInfoInPractitioners(
84
+ practitionerIds: string[],
85
+ clinicInfo: ClinicInfo
86
+ ): Promise<void> {
87
+ if (!practitionerIds || practitionerIds.length === 0) {
88
+ console.log(
89
+ "[ClinicAggregationService] No practitioner IDs provided for clinic info update. Skipping."
90
+ );
91
+ return;
92
+ }
93
+
94
+ const batch = this.db.batch();
95
+ const clinicId = clinicInfo.id;
96
+
97
+ console.log(
98
+ `[ClinicAggregationService] Starting batch update of ClinicInfo (ID: ${clinicId}) in ${practitionerIds.length} practitioners.`
99
+ );
100
+
101
+ for (const practitionerId of practitionerIds) {
102
+ const practitionerRef = this.db
103
+ .collection(PRACTITIONERS_COLLECTION)
104
+ .doc(practitionerId);
105
+
106
+ // Remove old clinic info based on ID matcher
107
+ batch.update(practitionerRef, {
108
+ clinicsInfo: admin.firestore.FieldValue.arrayRemove({ id: clinicId }),
109
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
110
+ });
111
+ // Add updated clinic info
112
+ batch.update(practitionerRef, {
113
+ clinicsInfo: admin.firestore.FieldValue.arrayUnion(clinicInfo),
114
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
115
+ });
116
+ }
117
+
118
+ try {
119
+ await batch.commit();
120
+ console.log(
121
+ `[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in ${practitionerIds.length} practitioners.`
122
+ );
123
+ } catch (error) {
124
+ console.error(
125
+ `[ClinicAggregationService] Error committing batch update for ClinicInfo (ID: ${clinicId}) in practitioners:`,
126
+ error
127
+ );
128
+ throw error;
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Updates the aggregated clinicInfo field within relevant Procedure documents.
134
+ * @param procedureIds IDs of procedures performed at the clinic.
135
+ * @param clinicInfo The updated ClinicInfo object.
136
+ */
137
+ async updateClinicInfoInProcedures(
138
+ procedureIds: string[],
139
+ clinicInfo: ClinicInfo
140
+ ): Promise<void> {
141
+ if (!procedureIds || procedureIds.length === 0) {
142
+ console.log(
143
+ "[ClinicAggregationService] No procedure IDs provided for clinic info update. Skipping."
144
+ );
145
+ return;
146
+ }
147
+
148
+ const batch = this.db.batch();
149
+ const clinicId = clinicInfo.id;
150
+
151
+ console.log(
152
+ `[ClinicAggregationService] Starting batch update of clinicInfo field (for Clinic ID: ${clinicId}) in ${procedureIds.length} procedures.`
153
+ );
154
+
155
+ for (const procedureId of procedureIds) {
156
+ const procedureRef = this.db
157
+ .collection(PROCEDURES_COLLECTION)
158
+ .doc(procedureId);
159
+
160
+ // Update the embedded clinicInfo object directly
161
+ batch.update(procedureRef, {
162
+ clinicInfo: clinicInfo,
163
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
164
+ });
165
+ }
166
+
167
+ try {
168
+ await batch.commit();
169
+ console.log(
170
+ `[ClinicAggregationService] Successfully updated clinicInfo field in ${procedureIds.length} procedures for clinic ${clinicId}.`
171
+ );
172
+ } catch (error) {
173
+ console.error(
174
+ `[ClinicAggregationService] Error committing batch update for clinicInfo field in procedures (Clinic ID: ${clinicId}):`,
175
+ error
176
+ );
177
+ throw error;
178
+ }
179
+ }
180
+
181
+ /**
182
+ * Updates the aggregated clinicsInfo array within the parent ClinicGroup document.
183
+ * @param clinicGroupId The ID of the parent clinic group.
184
+ * @param clinicInfo The updated ClinicInfo object.
185
+ */
186
+ async updateClinicInfoInClinicGroup(
187
+ clinicGroupId: string,
188
+ clinicInfo: ClinicInfo
189
+ ): Promise<void> {
190
+ if (!clinicGroupId) {
191
+ console.log(
192
+ "[ClinicAggregationService] No Clinic Group ID provided for clinic info update. Skipping."
193
+ );
194
+ return;
195
+ }
196
+
197
+ const batch = this.db.batch();
198
+ const clinicId = clinicInfo.id;
199
+ const groupRef = this.db
200
+ .collection(CLINIC_GROUPS_COLLECTION)
201
+ .doc(clinicGroupId);
202
+
203
+ console.log(
204
+ `[ClinicAggregationService] Starting update of ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
205
+ );
206
+
207
+ // Remove old clinic info based on ID matcher
208
+ batch.update(groupRef, {
209
+ clinicsInfo: admin.firestore.FieldValue.arrayRemove({ id: clinicId }),
210
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
211
+ });
212
+ // Add updated clinic info
213
+ batch.update(groupRef, {
214
+ clinicsInfo: admin.firestore.FieldValue.arrayUnion(clinicInfo),
215
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
216
+ });
217
+
218
+ try {
219
+ await batch.commit();
220
+ console.log(
221
+ `[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
222
+ );
223
+ } catch (error) {
224
+ console.error(
225
+ `[ClinicAggregationService] Error committing update for ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}:`,
226
+ error
227
+ );
228
+ throw error;
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Updates relevant clinic information within Patient documents.
234
+ * NOTE: PatientProfile stores an array of PatientClinic objects, which only contain
235
+ * clinicId, assignedAt, assignedBy, isActive, notes. It does *not* store aggregated
236
+ * ClinicInfo (like name, photo). Therefore, this method currently has limited use
237
+ * for general clinic info updates. It might be relevant if Clinic.isActive status changes.
238
+ *
239
+ * @param patientIds IDs of patients associated with the clinic (e.g., from PatientProfile.clinicIds).
240
+ * @param clinicInfo The updated ClinicInfo object (primarily for logging/context).
241
+ * @param clinicIsActive The current active status of the updated clinic.
242
+ */
243
+ async updateClinicInfoInPatients(
244
+ patientIds: string[],
245
+ clinicInfo: ClinicInfo,
246
+ clinicIsActive: boolean
247
+ ): Promise<void> {
248
+ if (!patientIds || patientIds.length === 0) {
249
+ console.log(
250
+ "[ClinicAggregationService] No patient IDs provided for clinic info update. Skipping."
251
+ );
252
+ return;
253
+ }
254
+ const clinicId = clinicInfo.id;
255
+ console.warn(
256
+ `[ClinicAggregationService] updateClinicInfoInPatients called for clinic ${clinicId}. ` +
257
+ `Current PatientProfile structure doesn't store full ClinicInfo per patient clinic entry. ` +
258
+ `Consider specific logic if Clinic active status changes or if structure evolves. ` +
259
+ `No direct patient updates performed by this method for info changes. Clinic isActive: ${clinicIsActive}`,
260
+ { patientIds, clinicInfo }
261
+ );
262
+
263
+ // Potential Future Logic (Example: Update isActive in PatientClinic array):
264
+ /*
265
+ const batch = this.db.batch();
266
+ for (const patientId of patientIds) {
267
+ const patientRef = this.db.collection(PATIENTS_COLLECTION).doc(patientId);
268
+ // This is complex: Firestore cannot directly update a specific element in an array based on a condition.
269
+ // You would need to read the patient doc, find the PatientClinic object with the matching clinicId,
270
+ // update its isActive status in memory, and then write the entire modified 'clinics' array back.
271
+ // This is inefficient and prone to race conditions without transactions.
272
+ // A better approach might be needed if this functionality is critical.
273
+ // For now, we just log the warning.
274
+ }
275
+ // await batch.commit();
276
+ */
277
+ }
278
+
279
+ /**
280
+ * Updates the eventLocation for upcoming calendar events associated with a specific clinic.
281
+ * Uses a collection group query to find relevant events across all potential parent collections.
282
+ *
283
+ * @param clinicId The ID of the clinic whose location might have changed.
284
+ * @param newLocation The new ClinicLocation object.
285
+ */
286
+ async updateClinicLocationInCalendarEvents(
287
+ clinicId: string,
288
+ newLocation: ClinicLocation
289
+ ): Promise<void> {
290
+ if (!clinicId || !newLocation) {
291
+ console.log(
292
+ "[ClinicAggregationService] Missing clinicId or newLocation for calendar update. Skipping."
293
+ );
294
+ return;
295
+ }
296
+
297
+ console.log(
298
+ `[ClinicAggregationService] Querying upcoming calendar events via collection group '${CALENDAR_SUBCOLLECTION_ID}' for clinic ${clinicId} to update location.`
299
+ );
300
+
301
+ const now = admin.firestore.Timestamp.now();
302
+ // Use a Collection Group query
303
+ const calendarEventsQuery = this.db
304
+ .collectionGroup(CALENDAR_SUBCOLLECTION_ID)
305
+ .where("clinicBranchId", "==", clinicId)
306
+ .where("eventTime.start", ">", now);
307
+
308
+ try {
309
+ const snapshot = await calendarEventsQuery.get();
310
+ if (snapshot.empty) {
311
+ console.log(
312
+ `[ClinicAggregationService] No upcoming calendar events found via collection group for clinic ${clinicId}. No location updates needed.`
313
+ );
314
+ return;
315
+ }
316
+
317
+ const batch = this.db.batch();
318
+ snapshot.docs.forEach((doc) => {
319
+ // doc.ref points to the document within its specific subcollection
320
+ // (e.g., /practitioners/{id}/calendar/{eventId})
321
+ console.log(
322
+ `[ClinicAggregationService] Updating location for calendar event ${doc.ref.path}`
323
+ );
324
+ batch.update(doc.ref, {
325
+ eventLocation: newLocation,
326
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
327
+ });
328
+ });
329
+
330
+ await batch.commit();
331
+ console.log(
332
+ `[ClinicAggregationService] Successfully updated location in ${snapshot.size} upcoming calendar events via collection group for clinic ${clinicId}.`
333
+ );
334
+ } catch (error) {
335
+ console.error(
336
+ `[ClinicAggregationService] Error updating location in calendar events via collection group for clinic ${clinicId}:`,
337
+ error
338
+ );
339
+ throw error;
340
+ }
341
+ }
342
+
343
+ /**
344
+ * Updates clinic info in all upcoming calendar events associated with a specific clinic.
345
+ * @param clinicId The ID of the clinic whose info has been updated.
346
+ * @param clinicInfo The updated ClinicInfo object.
347
+ */
348
+ async updateClinicInfoInCalendarEvents(
349
+ clinicId: string,
350
+ clinicInfo: ClinicInfo
351
+ ): Promise<void> {
352
+ if (!clinicId || !clinicInfo) {
353
+ console.log(
354
+ "[ClinicAggregationService] Missing clinicId or clinicInfo for calendar update. Skipping."
355
+ );
356
+ return;
357
+ }
358
+
359
+ console.log(
360
+ `[ClinicAggregationService] Querying upcoming calendar events for clinic ${clinicId} to update clinic info.`
361
+ );
362
+
363
+ const now = admin.firestore.Timestamp.now();
364
+ // Use a Collection Group query
365
+ const calendarEventsQuery = this.db
366
+ .collectionGroup(CALENDAR_SUBCOLLECTION_ID)
367
+ .where("clinicBranchId", "==", clinicId)
368
+ .where("eventTime.start", ">", now);
369
+
370
+ try {
371
+ const snapshot = await calendarEventsQuery.get();
372
+ if (snapshot.empty) {
373
+ console.log(
374
+ `[ClinicAggregationService] No upcoming calendar events found for clinic ${clinicId}. No clinic info updates needed.`
375
+ );
376
+ return;
377
+ }
378
+
379
+ const batch = this.db.batch();
380
+ snapshot.docs.forEach((doc) => {
381
+ console.log(
382
+ `[ClinicAggregationService] Updating clinic info for calendar event ${doc.ref.path}`
383
+ );
384
+ batch.update(doc.ref, {
385
+ clinicInfo: clinicInfo,
386
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
387
+ });
388
+ });
389
+
390
+ await batch.commit();
391
+ console.log(
392
+ `[ClinicAggregationService] Successfully updated clinic info in ${snapshot.size} upcoming calendar events for clinic ${clinicId}.`
393
+ );
394
+ } catch (error) {
395
+ console.error(
396
+ `[ClinicAggregationService] Error updating clinic info in calendar events for clinic ${clinicId}:`,
397
+ error
398
+ );
399
+ throw error;
400
+ }
401
+ }
402
+
403
+ /**
404
+ * Removes clinic from practitioners when a clinic is deleted.
405
+ * @param practitionerIds IDs of practitioners associated with the clinic.
406
+ * @param clinicId The ID of the deleted clinic.
407
+ */
408
+ async removeClinicFromPractitioners(
409
+ practitionerIds: string[],
410
+ clinicId: string
411
+ ): Promise<void> {
412
+ if (!practitionerIds || practitionerIds.length === 0) {
413
+ console.log(
414
+ "[ClinicAggregationService] No practitioner IDs provided for clinic removal. Skipping."
415
+ );
416
+ return;
417
+ }
418
+
419
+ const batch = this.db.batch();
420
+
421
+ console.log(
422
+ `[ClinicAggregationService] Starting batch removal of Clinic (ID: ${clinicId}) from ${practitionerIds.length} practitioners.`
423
+ );
424
+
425
+ for (const practitionerId of practitionerIds) {
426
+ const practitionerRef = this.db
427
+ .collection(PRACTITIONERS_COLLECTION)
428
+ .doc(practitionerId);
429
+
430
+ // Remove clinic ID from clinicIds array
431
+ batch.update(practitionerRef, {
432
+ clinicIds: admin.firestore.FieldValue.arrayRemove(clinicId),
433
+ // Remove all clinic info objects where id matches the clinic ID
434
+ clinicsInfo: admin.firestore.FieldValue.arrayRemove({ id: clinicId }),
435
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
436
+ });
437
+ }
438
+
439
+ try {
440
+ await batch.commit();
441
+ console.log(
442
+ `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from ${practitionerIds.length} practitioners.`
443
+ );
444
+ } catch (error) {
445
+ console.error(
446
+ `[ClinicAggregationService] Error committing batch removal for Clinic (ID: ${clinicId}) from practitioners:`,
447
+ error
448
+ );
449
+ throw error;
450
+ }
451
+ }
452
+
453
+ /**
454
+ * Inactivates all procedures associated with a deleted clinic
455
+ * @param procedureIds IDs of procedures associated with the clinic
456
+ */
457
+ async inactivateProceduresForClinic(procedureIds: string[]): Promise<void> {
458
+ if (!procedureIds || procedureIds.length === 0) {
459
+ console.log(
460
+ "[ClinicAggregationService] No procedure IDs provided for inactivation. Skipping."
461
+ );
462
+ return;
463
+ }
464
+
465
+ const batch = this.db.batch();
466
+
467
+ console.log(
468
+ `[ClinicAggregationService] Starting inactivation of ${procedureIds.length} procedures.`
469
+ );
470
+
471
+ for (const procedureId of procedureIds) {
472
+ const procedureRef = this.db
473
+ .collection(PROCEDURES_COLLECTION)
474
+ .doc(procedureId);
475
+
476
+ batch.update(procedureRef, {
477
+ isActive: false,
478
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
479
+ });
480
+ }
481
+
482
+ try {
483
+ await batch.commit();
484
+ console.log(
485
+ `[ClinicAggregationService] Successfully inactivated ${procedureIds.length} procedures.`
486
+ );
487
+ } catch (error) {
488
+ console.error(
489
+ `[ClinicAggregationService] Error committing batch inactivation of procedures:`,
490
+ error
491
+ );
492
+ throw error;
493
+ }
494
+ }
495
+
496
+ /**
497
+ * Removes clinic from clinic group when a clinic is deleted
498
+ * @param clinicGroupId ID of the parent clinic group
499
+ * @param clinicId ID of the deleted clinic
500
+ */
501
+ async removeClinicFromClinicGroup(
502
+ clinicGroupId: string,
503
+ clinicId: string
504
+ ): Promise<void> {
505
+ if (!clinicGroupId) {
506
+ console.log(
507
+ "[ClinicAggregationService] No Clinic Group ID provided for clinic removal. Skipping."
508
+ );
509
+ return;
510
+ }
511
+
512
+ const groupRef = this.db
513
+ .collection(CLINIC_GROUPS_COLLECTION)
514
+ .doc(clinicGroupId);
515
+
516
+ console.log(
517
+ `[ClinicAggregationService] Removing Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
518
+ );
519
+
520
+ try {
521
+ await groupRef.update({
522
+ clinicIds: admin.firestore.FieldValue.arrayRemove(clinicId),
523
+ // Remove all clinic info objects where id matches the clinic ID
524
+ clinicsInfo: admin.firestore.FieldValue.arrayRemove({ id: clinicId }),
525
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
526
+ });
527
+
528
+ console.log(
529
+ `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
530
+ );
531
+ } catch (error) {
532
+ console.error(
533
+ `[ClinicAggregationService] Error removing Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}:`,
534
+ error
535
+ );
536
+ throw error;
537
+ }
538
+ }
539
+
540
+ /**
541
+ * Removes clinic from patients when a clinic is deleted
542
+ * @param patientIds IDs of patients associated with the clinic
543
+ * @param clinicId ID of the deleted clinic
544
+ */
545
+ async removeClinicFromPatients(
546
+ patientIds: string[],
547
+ clinicId: string
548
+ ): Promise<void> {
549
+ if (!patientIds || patientIds.length === 0) {
550
+ console.log(
551
+ "[ClinicAggregationService] No patient IDs provided for clinic removal. Skipping."
552
+ );
553
+ return;
554
+ }
555
+
556
+ const batch = this.db.batch();
557
+
558
+ console.log(
559
+ `[ClinicAggregationService] Starting batch removal of Clinic (ID: ${clinicId}) from ${patientIds.length} patients.`
560
+ );
561
+
562
+ for (const patientId of patientIds) {
563
+ const patientRef = this.db.collection(PATIENTS_COLLECTION).doc(patientId);
564
+
565
+ // Remove clinic ID from clinicIds array
566
+ batch.update(patientRef, {
567
+ clinicIds: admin.firestore.FieldValue.arrayRemove(clinicId),
568
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
569
+ });
570
+ }
571
+
572
+ try {
573
+ await batch.commit();
574
+ console.log(
575
+ `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from ${patientIds.length} patients.`
576
+ );
577
+ } catch (error) {
578
+ console.error(
579
+ `[ClinicAggregationService] Error committing batch removal for Clinic (ID: ${clinicId}) from patients:`,
580
+ error
581
+ );
582
+ throw error;
583
+ }
584
+ }
585
+
586
+ /**
587
+ * Cancels all upcoming calendar events associated with a deleted clinic
588
+ * @param clinicId ID of the deleted clinic
589
+ */
590
+ async cancelUpcomingCalendarEventsForClinic(clinicId: string): Promise<void> {
591
+ if (!clinicId) {
592
+ console.log(
593
+ "[ClinicAggregationService] No clinic ID provided for canceling calendar events. Skipping."
594
+ );
595
+ return;
596
+ }
597
+
598
+ console.log(
599
+ `[ClinicAggregationService] Querying upcoming calendar events for clinic ${clinicId} to cancel.`
600
+ );
601
+
602
+ const now = admin.firestore.Timestamp.now();
603
+ // Use a Collection Group query
604
+ const calendarEventsQuery = this.db
605
+ .collectionGroup(CALENDAR_SUBCOLLECTION_ID)
606
+ .where("clinicBranchId", "==", clinicId)
607
+ .where("eventTime.start", ">", now);
608
+
609
+ try {
610
+ const snapshot = await calendarEventsQuery.get();
611
+ if (snapshot.empty) {
612
+ console.log(
613
+ `[ClinicAggregationService] No upcoming calendar events found for clinic ${clinicId}. No events to cancel.`
614
+ );
615
+ return;
616
+ }
617
+
618
+ const batch = this.db.batch();
619
+ snapshot.docs.forEach((doc) => {
620
+ console.log(
621
+ `[ClinicAggregationService] Canceling calendar event ${doc.ref.path}`
622
+ );
623
+ batch.update(doc.ref, {
624
+ status: "CANCELED",
625
+ cancelReason: "Clinic deleted",
626
+ updatedAt: admin.firestore.FieldValue.serverTimestamp(),
627
+ });
628
+ });
629
+
630
+ await batch.commit();
631
+ console.log(
632
+ `[ClinicAggregationService] Successfully canceled ${snapshot.size} upcoming calendar events for clinic ${clinicId}.`
633
+ );
634
+ } catch (error) {
635
+ console.error(
636
+ `[ClinicAggregationService] Error canceling calendar events for clinic ${clinicId}:`,
637
+ error
638
+ );
639
+ throw error;
640
+ }
641
+ }
642
+ }