@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
@@ -30,10 +30,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/admin/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
+ ClinicAggregationService: () => ClinicAggregationService,
33
34
  NOTIFICATIONS_COLLECTION: () => NOTIFICATIONS_COLLECTION,
34
35
  NotificationStatus: () => NotificationStatus,
35
36
  NotificationType: () => NotificationType,
36
37
  NotificationsAdmin: () => NotificationsAdmin,
38
+ PatientAggregationService: () => PatientAggregationService,
39
+ PractitionerAggregationService: () => PractitionerAggregationService,
40
+ ProcedureAggregationService: () => ProcedureAggregationService,
37
41
  UserRole: () => UserRole
38
42
  });
39
43
  module.exports = __toCommonJS(index_exports);
@@ -60,9 +64,9 @@ var NotificationStatus = /* @__PURE__ */ ((NotificationStatus2) => {
60
64
  var admin = __toESM(require("firebase-admin"));
61
65
  var import_expo_server_sdk = require("expo-server-sdk");
62
66
  var NotificationsAdmin = class {
63
- constructor(firestore2) {
67
+ constructor(firestore6) {
64
68
  this.expo = new import_expo_server_sdk.Expo();
65
- this.db = firestore2 || admin.firestore();
69
+ this.db = firestore6 || admin.firestore();
66
70
  }
67
71
  /**
68
72
  * Dohvata notifikaciju po ID-u
@@ -226,6 +230,1330 @@ var NotificationsAdmin = class {
226
230
  }
227
231
  };
228
232
 
233
+ // src/admin/aggregation/clinic/clinic.aggregation.service.ts
234
+ var admin2 = __toESM(require("firebase-admin"));
235
+
236
+ // src/types/practitioner/index.ts
237
+ var PRACTITIONERS_COLLECTION = "practitioners";
238
+
239
+ // src/types/procedure/index.ts
240
+ var PROCEDURES_COLLECTION = "procedures";
241
+
242
+ // src/types/clinic/index.ts
243
+ var CLINIC_GROUPS_COLLECTION = "clinic_groups";
244
+ var CLINICS_COLLECTION = "clinics";
245
+
246
+ // src/types/patient/index.ts
247
+ var PATIENTS_COLLECTION = "patients";
248
+
249
+ // src/admin/aggregation/clinic/clinic.aggregation.service.ts
250
+ var CALENDAR_SUBCOLLECTION_ID = "calendar";
251
+ var ClinicAggregationService = class {
252
+ /**
253
+ * Constructor for ClinicAggregationService.
254
+ * @param firestore Optional Firestore instance. If not provided, it uses the default admin SDK instance.
255
+ */
256
+ constructor(firestore6) {
257
+ this.db = firestore6 || admin2.firestore();
258
+ }
259
+ /**
260
+ * Adds clinic information to a clinic group when a new clinic is created
261
+ * @param clinicGroupId - ID of the parent clinic group
262
+ * @param clinicInfo - The clinic information to add to the group
263
+ * @returns {Promise<void>}
264
+ * @throws Will throw an error if the batch write fails
265
+ */
266
+ async addClinicToClinicGroup(clinicGroupId, clinicInfo) {
267
+ if (!clinicGroupId) {
268
+ console.log(
269
+ "[ClinicAggregationService] No Clinic Group ID provided for clinic addition. Skipping."
270
+ );
271
+ return;
272
+ }
273
+ const clinicId = clinicInfo.id;
274
+ const groupRef = this.db.collection(CLINIC_GROUPS_COLLECTION).doc(clinicGroupId);
275
+ console.log(
276
+ `[ClinicAggregationService] Adding ClinicInfo (ID: ${clinicId}) to Clinic Group ${clinicGroupId}.`
277
+ );
278
+ try {
279
+ await groupRef.update({
280
+ clinicsInfo: admin2.firestore.FieldValue.arrayUnion(clinicInfo),
281
+ clinicIds: admin2.firestore.FieldValue.arrayUnion(clinicId),
282
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
283
+ });
284
+ console.log(
285
+ `[ClinicAggregationService] Successfully added ClinicInfo (ID: ${clinicId}) to Clinic Group ${clinicGroupId}.`
286
+ );
287
+ } catch (error) {
288
+ console.error(
289
+ `[ClinicAggregationService] Error adding ClinicInfo (ID: ${clinicId}) to Clinic Group ${clinicGroupId}:`,
290
+ error
291
+ );
292
+ throw error;
293
+ }
294
+ }
295
+ /**
296
+ * Updates the ClinicInfo within the clinicsInfo array for multiple practitioners.
297
+ * This method is designed to be called when a clinic's core information changes.
298
+ * @param practitionerIds - IDs of practitioners associated with the clinic.
299
+ * @param clinicInfo - The updated ClinicInfo object to aggregate.
300
+ * @returns {Promise<void>}
301
+ * @throws Will throw an error if the batch write fails.
302
+ */
303
+ async updateClinicInfoInPractitioners(practitionerIds, clinicInfo) {
304
+ if (!practitionerIds || practitionerIds.length === 0) {
305
+ console.log(
306
+ "[ClinicAggregationService] No practitioner IDs provided for clinic info update. Skipping."
307
+ );
308
+ return;
309
+ }
310
+ const batch = this.db.batch();
311
+ const clinicId = clinicInfo.id;
312
+ console.log(
313
+ `[ClinicAggregationService] Starting batch update of ClinicInfo (ID: ${clinicId}) in ${practitionerIds.length} practitioners.`
314
+ );
315
+ for (const practitionerId of practitionerIds) {
316
+ const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
317
+ batch.update(practitionerRef, {
318
+ clinicsInfo: admin2.firestore.FieldValue.arrayRemove({ id: clinicId }),
319
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
320
+ });
321
+ batch.update(practitionerRef, {
322
+ clinicsInfo: admin2.firestore.FieldValue.arrayUnion(clinicInfo),
323
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
324
+ });
325
+ }
326
+ try {
327
+ await batch.commit();
328
+ console.log(
329
+ `[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in ${practitionerIds.length} practitioners.`
330
+ );
331
+ } catch (error) {
332
+ console.error(
333
+ `[ClinicAggregationService] Error committing batch update for ClinicInfo (ID: ${clinicId}) in practitioners:`,
334
+ error
335
+ );
336
+ throw error;
337
+ }
338
+ }
339
+ /**
340
+ * Updates the aggregated clinicInfo field within relevant Procedure documents.
341
+ * @param procedureIds IDs of procedures performed at the clinic.
342
+ * @param clinicInfo The updated ClinicInfo object.
343
+ */
344
+ async updateClinicInfoInProcedures(procedureIds, clinicInfo) {
345
+ if (!procedureIds || procedureIds.length === 0) {
346
+ console.log(
347
+ "[ClinicAggregationService] No procedure IDs provided for clinic info update. Skipping."
348
+ );
349
+ return;
350
+ }
351
+ const batch = this.db.batch();
352
+ const clinicId = clinicInfo.id;
353
+ console.log(
354
+ `[ClinicAggregationService] Starting batch update of clinicInfo field (for Clinic ID: ${clinicId}) in ${procedureIds.length} procedures.`
355
+ );
356
+ for (const procedureId of procedureIds) {
357
+ const procedureRef = this.db.collection(PROCEDURES_COLLECTION).doc(procedureId);
358
+ batch.update(procedureRef, {
359
+ clinicInfo,
360
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
361
+ });
362
+ }
363
+ try {
364
+ await batch.commit();
365
+ console.log(
366
+ `[ClinicAggregationService] Successfully updated clinicInfo field in ${procedureIds.length} procedures for clinic ${clinicId}.`
367
+ );
368
+ } catch (error) {
369
+ console.error(
370
+ `[ClinicAggregationService] Error committing batch update for clinicInfo field in procedures (Clinic ID: ${clinicId}):`,
371
+ error
372
+ );
373
+ throw error;
374
+ }
375
+ }
376
+ /**
377
+ * Updates the aggregated clinicsInfo array within the parent ClinicGroup document.
378
+ * @param clinicGroupId The ID of the parent clinic group.
379
+ * @param clinicInfo The updated ClinicInfo object.
380
+ */
381
+ async updateClinicInfoInClinicGroup(clinicGroupId, clinicInfo) {
382
+ if (!clinicGroupId) {
383
+ console.log(
384
+ "[ClinicAggregationService] No Clinic Group ID provided for clinic info update. Skipping."
385
+ );
386
+ return;
387
+ }
388
+ const batch = this.db.batch();
389
+ const clinicId = clinicInfo.id;
390
+ const groupRef = this.db.collection(CLINIC_GROUPS_COLLECTION).doc(clinicGroupId);
391
+ console.log(
392
+ `[ClinicAggregationService] Starting update of ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
393
+ );
394
+ batch.update(groupRef, {
395
+ clinicsInfo: admin2.firestore.FieldValue.arrayRemove({ id: clinicId }),
396
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
397
+ });
398
+ batch.update(groupRef, {
399
+ clinicsInfo: admin2.firestore.FieldValue.arrayUnion(clinicInfo),
400
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
401
+ });
402
+ try {
403
+ await batch.commit();
404
+ console.log(
405
+ `[ClinicAggregationService] Successfully updated ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}.`
406
+ );
407
+ } catch (error) {
408
+ console.error(
409
+ `[ClinicAggregationService] Error committing update for ClinicInfo (ID: ${clinicId}) in Clinic Group ${clinicGroupId}:`,
410
+ error
411
+ );
412
+ throw error;
413
+ }
414
+ }
415
+ /**
416
+ * Updates relevant clinic information within Patient documents.
417
+ * NOTE: PatientProfile stores an array of PatientClinic objects, which only contain
418
+ * clinicId, assignedAt, assignedBy, isActive, notes. It does *not* store aggregated
419
+ * ClinicInfo (like name, photo). Therefore, this method currently has limited use
420
+ * for general clinic info updates. It might be relevant if Clinic.isActive status changes.
421
+ *
422
+ * @param patientIds IDs of patients associated with the clinic (e.g., from PatientProfile.clinicIds).
423
+ * @param clinicInfo The updated ClinicInfo object (primarily for logging/context).
424
+ * @param clinicIsActive The current active status of the updated clinic.
425
+ */
426
+ async updateClinicInfoInPatients(patientIds, clinicInfo, clinicIsActive) {
427
+ if (!patientIds || patientIds.length === 0) {
428
+ console.log(
429
+ "[ClinicAggregationService] No patient IDs provided for clinic info update. Skipping."
430
+ );
431
+ return;
432
+ }
433
+ const clinicId = clinicInfo.id;
434
+ console.warn(
435
+ `[ClinicAggregationService] updateClinicInfoInPatients called for clinic ${clinicId}. Current PatientProfile structure doesn't store full ClinicInfo per patient clinic entry. Consider specific logic if Clinic active status changes or if structure evolves. No direct patient updates performed by this method for info changes. Clinic isActive: ${clinicIsActive}`,
436
+ { patientIds, clinicInfo }
437
+ );
438
+ }
439
+ /**
440
+ * Updates the eventLocation for upcoming calendar events associated with a specific clinic.
441
+ * Uses a collection group query to find relevant events across all potential parent collections.
442
+ *
443
+ * @param clinicId The ID of the clinic whose location might have changed.
444
+ * @param newLocation The new ClinicLocation object.
445
+ */
446
+ async updateClinicLocationInCalendarEvents(clinicId, newLocation) {
447
+ if (!clinicId || !newLocation) {
448
+ console.log(
449
+ "[ClinicAggregationService] Missing clinicId or newLocation for calendar update. Skipping."
450
+ );
451
+ return;
452
+ }
453
+ console.log(
454
+ `[ClinicAggregationService] Querying upcoming calendar events via collection group '${CALENDAR_SUBCOLLECTION_ID}' for clinic ${clinicId} to update location.`
455
+ );
456
+ const now = admin2.firestore.Timestamp.now();
457
+ const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID).where("clinicBranchId", "==", clinicId).where("eventTime.start", ">", now);
458
+ try {
459
+ const snapshot = await calendarEventsQuery.get();
460
+ if (snapshot.empty) {
461
+ console.log(
462
+ `[ClinicAggregationService] No upcoming calendar events found via collection group for clinic ${clinicId}. No location updates needed.`
463
+ );
464
+ return;
465
+ }
466
+ const batch = this.db.batch();
467
+ snapshot.docs.forEach((doc) => {
468
+ console.log(
469
+ `[ClinicAggregationService] Updating location for calendar event ${doc.ref.path}`
470
+ );
471
+ batch.update(doc.ref, {
472
+ eventLocation: newLocation,
473
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
474
+ });
475
+ });
476
+ await batch.commit();
477
+ console.log(
478
+ `[ClinicAggregationService] Successfully updated location in ${snapshot.size} upcoming calendar events via collection group for clinic ${clinicId}.`
479
+ );
480
+ } catch (error) {
481
+ console.error(
482
+ `[ClinicAggregationService] Error updating location in calendar events via collection group for clinic ${clinicId}:`,
483
+ error
484
+ );
485
+ throw error;
486
+ }
487
+ }
488
+ /**
489
+ * Updates clinic info in all upcoming calendar events associated with a specific clinic.
490
+ * @param clinicId The ID of the clinic whose info has been updated.
491
+ * @param clinicInfo The updated ClinicInfo object.
492
+ */
493
+ async updateClinicInfoInCalendarEvents(clinicId, clinicInfo) {
494
+ if (!clinicId || !clinicInfo) {
495
+ console.log(
496
+ "[ClinicAggregationService] Missing clinicId or clinicInfo for calendar update. Skipping."
497
+ );
498
+ return;
499
+ }
500
+ console.log(
501
+ `[ClinicAggregationService] Querying upcoming calendar events for clinic ${clinicId} to update clinic info.`
502
+ );
503
+ const now = admin2.firestore.Timestamp.now();
504
+ const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID).where("clinicBranchId", "==", clinicId).where("eventTime.start", ">", now);
505
+ try {
506
+ const snapshot = await calendarEventsQuery.get();
507
+ if (snapshot.empty) {
508
+ console.log(
509
+ `[ClinicAggregationService] No upcoming calendar events found for clinic ${clinicId}. No clinic info updates needed.`
510
+ );
511
+ return;
512
+ }
513
+ const batch = this.db.batch();
514
+ snapshot.docs.forEach((doc) => {
515
+ console.log(
516
+ `[ClinicAggregationService] Updating clinic info for calendar event ${doc.ref.path}`
517
+ );
518
+ batch.update(doc.ref, {
519
+ clinicInfo,
520
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
521
+ });
522
+ });
523
+ await batch.commit();
524
+ console.log(
525
+ `[ClinicAggregationService] Successfully updated clinic info in ${snapshot.size} upcoming calendar events for clinic ${clinicId}.`
526
+ );
527
+ } catch (error) {
528
+ console.error(
529
+ `[ClinicAggregationService] Error updating clinic info in calendar events for clinic ${clinicId}:`,
530
+ error
531
+ );
532
+ throw error;
533
+ }
534
+ }
535
+ /**
536
+ * Removes clinic from practitioners when a clinic is deleted.
537
+ * @param practitionerIds IDs of practitioners associated with the clinic.
538
+ * @param clinicId The ID of the deleted clinic.
539
+ */
540
+ async removeClinicFromPractitioners(practitionerIds, clinicId) {
541
+ if (!practitionerIds || practitionerIds.length === 0) {
542
+ console.log(
543
+ "[ClinicAggregationService] No practitioner IDs provided for clinic removal. Skipping."
544
+ );
545
+ return;
546
+ }
547
+ const batch = this.db.batch();
548
+ console.log(
549
+ `[ClinicAggregationService] Starting batch removal of Clinic (ID: ${clinicId}) from ${practitionerIds.length} practitioners.`
550
+ );
551
+ for (const practitionerId of practitionerIds) {
552
+ const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
553
+ batch.update(practitionerRef, {
554
+ clinicIds: admin2.firestore.FieldValue.arrayRemove(clinicId),
555
+ // Remove all clinic info objects where id matches the clinic ID
556
+ clinicsInfo: admin2.firestore.FieldValue.arrayRemove({ id: clinicId }),
557
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
558
+ });
559
+ }
560
+ try {
561
+ await batch.commit();
562
+ console.log(
563
+ `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from ${practitionerIds.length} practitioners.`
564
+ );
565
+ } catch (error) {
566
+ console.error(
567
+ `[ClinicAggregationService] Error committing batch removal for Clinic (ID: ${clinicId}) from practitioners:`,
568
+ error
569
+ );
570
+ throw error;
571
+ }
572
+ }
573
+ /**
574
+ * Inactivates all procedures associated with a deleted clinic
575
+ * @param procedureIds IDs of procedures associated with the clinic
576
+ */
577
+ async inactivateProceduresForClinic(procedureIds) {
578
+ if (!procedureIds || procedureIds.length === 0) {
579
+ console.log(
580
+ "[ClinicAggregationService] No procedure IDs provided for inactivation. Skipping."
581
+ );
582
+ return;
583
+ }
584
+ const batch = this.db.batch();
585
+ console.log(
586
+ `[ClinicAggregationService] Starting inactivation of ${procedureIds.length} procedures.`
587
+ );
588
+ for (const procedureId of procedureIds) {
589
+ const procedureRef = this.db.collection(PROCEDURES_COLLECTION).doc(procedureId);
590
+ batch.update(procedureRef, {
591
+ isActive: false,
592
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
593
+ });
594
+ }
595
+ try {
596
+ await batch.commit();
597
+ console.log(
598
+ `[ClinicAggregationService] Successfully inactivated ${procedureIds.length} procedures.`
599
+ );
600
+ } catch (error) {
601
+ console.error(
602
+ `[ClinicAggregationService] Error committing batch inactivation of procedures:`,
603
+ error
604
+ );
605
+ throw error;
606
+ }
607
+ }
608
+ /**
609
+ * Removes clinic from clinic group when a clinic is deleted
610
+ * @param clinicGroupId ID of the parent clinic group
611
+ * @param clinicId ID of the deleted clinic
612
+ */
613
+ async removeClinicFromClinicGroup(clinicGroupId, clinicId) {
614
+ if (!clinicGroupId) {
615
+ console.log(
616
+ "[ClinicAggregationService] No Clinic Group ID provided for clinic removal. Skipping."
617
+ );
618
+ return;
619
+ }
620
+ const groupRef = this.db.collection(CLINIC_GROUPS_COLLECTION).doc(clinicGroupId);
621
+ console.log(
622
+ `[ClinicAggregationService] Removing Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
623
+ );
624
+ try {
625
+ await groupRef.update({
626
+ clinicIds: admin2.firestore.FieldValue.arrayRemove(clinicId),
627
+ // Remove all clinic info objects where id matches the clinic ID
628
+ clinicsInfo: admin2.firestore.FieldValue.arrayRemove({ id: clinicId }),
629
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
630
+ });
631
+ console.log(
632
+ `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}.`
633
+ );
634
+ } catch (error) {
635
+ console.error(
636
+ `[ClinicAggregationService] Error removing Clinic (ID: ${clinicId}) from Clinic Group ${clinicGroupId}:`,
637
+ error
638
+ );
639
+ throw error;
640
+ }
641
+ }
642
+ /**
643
+ * Removes clinic from patients when a clinic is deleted
644
+ * @param patientIds IDs of patients associated with the clinic
645
+ * @param clinicId ID of the deleted clinic
646
+ */
647
+ async removeClinicFromPatients(patientIds, clinicId) {
648
+ if (!patientIds || patientIds.length === 0) {
649
+ console.log(
650
+ "[ClinicAggregationService] No patient IDs provided for clinic removal. Skipping."
651
+ );
652
+ return;
653
+ }
654
+ const batch = this.db.batch();
655
+ console.log(
656
+ `[ClinicAggregationService] Starting batch removal of Clinic (ID: ${clinicId}) from ${patientIds.length} patients.`
657
+ );
658
+ for (const patientId of patientIds) {
659
+ const patientRef = this.db.collection(PATIENTS_COLLECTION).doc(patientId);
660
+ batch.update(patientRef, {
661
+ clinicIds: admin2.firestore.FieldValue.arrayRemove(clinicId),
662
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
663
+ });
664
+ }
665
+ try {
666
+ await batch.commit();
667
+ console.log(
668
+ `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from ${patientIds.length} patients.`
669
+ );
670
+ } catch (error) {
671
+ console.error(
672
+ `[ClinicAggregationService] Error committing batch removal for Clinic (ID: ${clinicId}) from patients:`,
673
+ error
674
+ );
675
+ throw error;
676
+ }
677
+ }
678
+ /**
679
+ * Cancels all upcoming calendar events associated with a deleted clinic
680
+ * @param clinicId ID of the deleted clinic
681
+ */
682
+ async cancelUpcomingCalendarEventsForClinic(clinicId) {
683
+ if (!clinicId) {
684
+ console.log(
685
+ "[ClinicAggregationService] No clinic ID provided for canceling calendar events. Skipping."
686
+ );
687
+ return;
688
+ }
689
+ console.log(
690
+ `[ClinicAggregationService] Querying upcoming calendar events for clinic ${clinicId} to cancel.`
691
+ );
692
+ const now = admin2.firestore.Timestamp.now();
693
+ const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID).where("clinicBranchId", "==", clinicId).where("eventTime.start", ">", now);
694
+ try {
695
+ const snapshot = await calendarEventsQuery.get();
696
+ if (snapshot.empty) {
697
+ console.log(
698
+ `[ClinicAggregationService] No upcoming calendar events found for clinic ${clinicId}. No events to cancel.`
699
+ );
700
+ return;
701
+ }
702
+ const batch = this.db.batch();
703
+ snapshot.docs.forEach((doc) => {
704
+ console.log(
705
+ `[ClinicAggregationService] Canceling calendar event ${doc.ref.path}`
706
+ );
707
+ batch.update(doc.ref, {
708
+ status: "CANCELED",
709
+ cancelReason: "Clinic deleted",
710
+ updatedAt: admin2.firestore.FieldValue.serverTimestamp()
711
+ });
712
+ });
713
+ await batch.commit();
714
+ console.log(
715
+ `[ClinicAggregationService] Successfully canceled ${snapshot.size} upcoming calendar events for clinic ${clinicId}.`
716
+ );
717
+ } catch (error) {
718
+ console.error(
719
+ `[ClinicAggregationService] Error canceling calendar events for clinic ${clinicId}:`,
720
+ error
721
+ );
722
+ throw error;
723
+ }
724
+ }
725
+ };
726
+
727
+ // src/admin/aggregation/practitioner/practitioner.aggregation.service.ts
728
+ var admin3 = __toESM(require("firebase-admin"));
729
+ var CALENDAR_SUBCOLLECTION_ID2 = "calendar";
730
+ var PractitionerAggregationService = class {
731
+ constructor(firestore6) {
732
+ this.db = firestore6 || admin3.firestore();
733
+ }
734
+ /**
735
+ * Adds practitioner information to a clinic when a new practitioner is created
736
+ * @param clinicId - ID of the clinic to update
737
+ * @param doctorInfo - Doctor information to add to the clinic
738
+ * @returns {Promise<void>}
739
+ */
740
+ async addPractitionerToClinic(clinicId, doctorInfo) {
741
+ if (!clinicId || !doctorInfo) {
742
+ console.log(
743
+ "[PractitionerAggregationService] Missing clinicId or doctorInfo for adding practitioner to clinic. Skipping."
744
+ );
745
+ return;
746
+ }
747
+ const practitionerId = doctorInfo.id;
748
+ console.log(
749
+ `[PractitionerAggregationService] Adding practitioner ${practitionerId} to clinic ${clinicId}.`
750
+ );
751
+ const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
752
+ try {
753
+ await clinicRef.update({
754
+ doctors: admin3.firestore.FieldValue.arrayUnion(practitionerId),
755
+ doctorsInfo: admin3.firestore.FieldValue.arrayUnion(doctorInfo),
756
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
757
+ });
758
+ console.log(
759
+ `[PractitionerAggregationService] Successfully added practitioner ${practitionerId} to clinic ${clinicId}.`
760
+ );
761
+ } catch (error) {
762
+ console.error(
763
+ `[PractitionerAggregationService] Error adding practitioner ${practitionerId} to clinic ${clinicId}:`,
764
+ error
765
+ );
766
+ throw error;
767
+ }
768
+ }
769
+ /**
770
+ * Updates practitioner information in associated clinics
771
+ * @param clinicIds - IDs of clinics associated with the practitioner
772
+ * @param doctorInfo - Updated doctor information
773
+ * @returns {Promise<void>}
774
+ */
775
+ async updatePractitionerInfoInClinics(clinicIds, doctorInfo) {
776
+ if (!clinicIds || clinicIds.length === 0 || !doctorInfo) {
777
+ console.log(
778
+ "[PractitionerAggregationService] Missing clinicIds or doctorInfo for updating practitioner in clinics. Skipping."
779
+ );
780
+ return;
781
+ }
782
+ const batch = this.db.batch();
783
+ const practitionerId = doctorInfo.id;
784
+ console.log(
785
+ `[PractitionerAggregationService] Starting batch update of practitioner ${practitionerId} in ${clinicIds.length} clinics.`
786
+ );
787
+ for (const clinicId of clinicIds) {
788
+ const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
789
+ batch.update(clinicRef, {
790
+ doctorsInfo: admin3.firestore.FieldValue.arrayRemove({
791
+ id: practitionerId
792
+ }),
793
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
794
+ });
795
+ batch.update(clinicRef, {
796
+ doctorsInfo: admin3.firestore.FieldValue.arrayUnion(doctorInfo),
797
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
798
+ });
799
+ }
800
+ try {
801
+ await batch.commit();
802
+ console.log(
803
+ `[PractitionerAggregationService] Successfully updated practitioner ${practitionerId} info in ${clinicIds.length} clinics.`
804
+ );
805
+ } catch (error) {
806
+ console.error(
807
+ `[PractitionerAggregationService] Error updating practitioner ${practitionerId} info in clinics:`,
808
+ error
809
+ );
810
+ throw error;
811
+ }
812
+ }
813
+ /**
814
+ * Updates practitioner information in associated procedures
815
+ * @param procedureIds - IDs of procedures associated with the practitioner
816
+ * @param doctorInfo - Updated doctor information
817
+ * @returns {Promise<void>}
818
+ */
819
+ async updatePractitionerInfoInProcedures(procedureIds, doctorInfo) {
820
+ if (!procedureIds || procedureIds.length === 0 || !doctorInfo) {
821
+ console.log(
822
+ "[PractitionerAggregationService] Missing procedureIds or doctorInfo for updating practitioner in procedures. Skipping."
823
+ );
824
+ return;
825
+ }
826
+ const batch = this.db.batch();
827
+ const practitionerId = doctorInfo.id;
828
+ console.log(
829
+ `[PractitionerAggregationService] Starting batch update of practitioner ${practitionerId} in ${procedureIds.length} procedures.`
830
+ );
831
+ for (const procedureId of procedureIds) {
832
+ const procedureRef = this.db.collection(PROCEDURES_COLLECTION).doc(procedureId);
833
+ batch.update(procedureRef, {
834
+ doctorInfo,
835
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
836
+ });
837
+ }
838
+ try {
839
+ await batch.commit();
840
+ console.log(
841
+ `[PractitionerAggregationService] Successfully updated practitioner ${practitionerId} info in ${procedureIds.length} procedures.`
842
+ );
843
+ } catch (error) {
844
+ console.error(
845
+ `[PractitionerAggregationService] Error updating practitioner ${practitionerId} info in procedures:`,
846
+ error
847
+ );
848
+ throw error;
849
+ }
850
+ }
851
+ /**
852
+ * Updates practitioner information in calendar events
853
+ * @param practitionerId - ID of the practitioner
854
+ * @param practitionerInfo - Updated practitioner information
855
+ * @returns {Promise<void>}
856
+ */
857
+ async updatePractitionerInfoInCalendarEvents(practitionerId, practitionerInfo) {
858
+ if (!practitionerId || !practitionerInfo) {
859
+ console.log(
860
+ "[PractitionerAggregationService] Missing practitionerId or practitionerInfo for calendar update. Skipping."
861
+ );
862
+ return;
863
+ }
864
+ console.log(
865
+ `[PractitionerAggregationService] Querying upcoming calendar events for practitioner ${practitionerId} to update practitioner info.`
866
+ );
867
+ const now = admin3.firestore.Timestamp.now();
868
+ const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID2).where("practitionerId", "==", practitionerId).where("eventTime.start", ">", now);
869
+ try {
870
+ const snapshot = await calendarEventsQuery.get();
871
+ if (snapshot.empty) {
872
+ console.log(
873
+ `[PractitionerAggregationService] No upcoming calendar events found for practitioner ${practitionerId}. No doctor info updates needed.`
874
+ );
875
+ return;
876
+ }
877
+ const batch = this.db.batch();
878
+ snapshot.docs.forEach((doc) => {
879
+ console.log(
880
+ `[PractitionerAggregationService] Updating practitioner info for calendar event ${doc.ref.path}`
881
+ );
882
+ batch.update(doc.ref, {
883
+ practitionerInfo,
884
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
885
+ });
886
+ });
887
+ await batch.commit();
888
+ console.log(
889
+ `[PractitionerAggregationService] Successfully updated practitioner info in ${snapshot.size} upcoming calendar events for practitioner ${practitionerId}.`
890
+ );
891
+ } catch (error) {
892
+ console.error(
893
+ `[PractitionerAggregationService] Error updating practitioner info in calendar events for practitioner ${practitionerId}:`,
894
+ error
895
+ );
896
+ throw error;
897
+ }
898
+ }
899
+ /**
900
+ * Removes practitioner from clinics when a practitioner is deleted
901
+ * @param clinicIds - IDs of clinics associated with the practitioner
902
+ * @param practitionerId - ID of the deleted practitioner
903
+ * @returns {Promise<void>}
904
+ */
905
+ async removePractitionerFromClinics(clinicIds, practitionerId) {
906
+ if (!clinicIds || clinicIds.length === 0 || !practitionerId) {
907
+ console.log(
908
+ "[PractitionerAggregationService] Missing clinicIds or practitionerId for removing practitioner from clinics. Skipping."
909
+ );
910
+ return;
911
+ }
912
+ const batch = this.db.batch();
913
+ console.log(
914
+ `[PractitionerAggregationService] Starting batch removal of practitioner ${practitionerId} from ${clinicIds.length} clinics.`
915
+ );
916
+ for (const clinicId of clinicIds) {
917
+ const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
918
+ batch.update(clinicRef, {
919
+ doctors: admin3.firestore.FieldValue.arrayRemove(practitionerId),
920
+ // Remove all doctor info objects where id matches the practitioner ID
921
+ doctorsInfo: admin3.firestore.FieldValue.arrayRemove({
922
+ id: practitionerId
923
+ }),
924
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
925
+ });
926
+ }
927
+ try {
928
+ await batch.commit();
929
+ console.log(
930
+ `[PractitionerAggregationService] Successfully removed practitioner ${practitionerId} from ${clinicIds.length} clinics.`
931
+ );
932
+ } catch (error) {
933
+ console.error(
934
+ `[PractitionerAggregationService] Error removing practitioner ${practitionerId} from clinics:`,
935
+ error
936
+ );
937
+ throw error;
938
+ }
939
+ }
940
+ /**
941
+ * Cancels all upcoming calendar events for a deleted practitioner
942
+ * @param practitionerId - ID of the deleted practitioner
943
+ * @returns {Promise<void>}
944
+ */
945
+ async cancelUpcomingCalendarEventsForPractitioner(practitionerId) {
946
+ if (!practitionerId) {
947
+ console.log(
948
+ "[PractitionerAggregationService] Missing practitionerId for canceling calendar events. Skipping."
949
+ );
950
+ return;
951
+ }
952
+ console.log(
953
+ `[PractitionerAggregationService] Querying upcoming calendar events for practitioner ${practitionerId} to cancel.`
954
+ );
955
+ const now = admin3.firestore.Timestamp.now();
956
+ const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID2).where("practitionerId", "==", practitionerId).where("eventTime.start", ">", now);
957
+ try {
958
+ const snapshot = await calendarEventsQuery.get();
959
+ if (snapshot.empty) {
960
+ console.log(
961
+ `[PractitionerAggregationService] No upcoming calendar events found for practitioner ${practitionerId}. No events to cancel.`
962
+ );
963
+ return;
964
+ }
965
+ const batch = this.db.batch();
966
+ snapshot.docs.forEach((doc) => {
967
+ console.log(
968
+ `[PractitionerAggregationService] Canceling calendar event ${doc.ref.path}`
969
+ );
970
+ batch.update(doc.ref, {
971
+ status: "CANCELED",
972
+ cancelReason: "Practitioner deleted",
973
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
974
+ });
975
+ });
976
+ await batch.commit();
977
+ console.log(
978
+ `[PractitionerAggregationService] Successfully canceled ${snapshot.size} upcoming calendar events for practitioner ${practitionerId}.`
979
+ );
980
+ } catch (error) {
981
+ console.error(
982
+ `[PractitionerAggregationService] Error canceling calendar events for practitioner ${practitionerId}:`,
983
+ error
984
+ );
985
+ throw error;
986
+ }
987
+ }
988
+ /**
989
+ * Removes practitioner from patients when a practitioner is deleted
990
+ * @param patientIds - IDs of patients associated with the practitioner
991
+ * @param practitionerId - ID of the deleted practitioner
992
+ * @returns {Promise<void>}
993
+ */
994
+ async removePractitionerFromPatients(patientIds, practitionerId) {
995
+ if (!patientIds || patientIds.length === 0 || !practitionerId) {
996
+ console.log(
997
+ "[PractitionerAggregationService] Missing patientIds or practitionerId for removing practitioner from patients. Skipping."
998
+ );
999
+ return;
1000
+ }
1001
+ const batch = this.db.batch();
1002
+ console.log(
1003
+ `[PractitionerAggregationService] Starting batch removal of practitioner ${practitionerId} from ${patientIds.length} patients.`
1004
+ );
1005
+ for (const patientId of patientIds) {
1006
+ const patientRef = this.db.collection(PATIENTS_COLLECTION).doc(patientId);
1007
+ batch.update(patientRef, {
1008
+ doctorIds: admin3.firestore.FieldValue.arrayRemove(practitionerId),
1009
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1010
+ });
1011
+ }
1012
+ try {
1013
+ await batch.commit();
1014
+ console.log(
1015
+ `[PractitionerAggregationService] Successfully removed practitioner ${practitionerId} from ${patientIds.length} patients.`
1016
+ );
1017
+ } catch (error) {
1018
+ console.error(
1019
+ `[PractitionerAggregationService] Error removing practitioner ${practitionerId} from patients:`,
1020
+ error
1021
+ );
1022
+ throw error;
1023
+ }
1024
+ }
1025
+ /**
1026
+ * Inactivates all procedures associated with a deleted practitioner
1027
+ * @param procedureIds - IDs of procedures provided by the practitioner
1028
+ * @returns {Promise<void>}
1029
+ */
1030
+ async inactivateProceduresForPractitioner(procedureIds) {
1031
+ if (!procedureIds || procedureIds.length === 0) {
1032
+ console.log(
1033
+ "[PractitionerAggregationService] No procedure IDs provided for inactivation. Skipping."
1034
+ );
1035
+ return;
1036
+ }
1037
+ const batch = this.db.batch();
1038
+ console.log(
1039
+ `[PractitionerAggregationService] Starting inactivation of ${procedureIds.length} procedures.`
1040
+ );
1041
+ for (const procedureId of procedureIds) {
1042
+ const procedureRef = this.db.collection(PROCEDURES_COLLECTION).doc(procedureId);
1043
+ batch.update(procedureRef, {
1044
+ isActive: false,
1045
+ updatedAt: admin3.firestore.FieldValue.serverTimestamp()
1046
+ });
1047
+ }
1048
+ try {
1049
+ await batch.commit();
1050
+ console.log(
1051
+ `[PractitionerAggregationService] Successfully inactivated ${procedureIds.length} procedures.`
1052
+ );
1053
+ } catch (error) {
1054
+ console.error(
1055
+ `[PractitionerAggregationService] Error committing batch inactivation of procedures:`,
1056
+ error
1057
+ );
1058
+ throw error;
1059
+ }
1060
+ }
1061
+ };
1062
+
1063
+ // src/admin/aggregation/procedure/procedure.aggregation.service.ts
1064
+ var admin4 = __toESM(require("firebase-admin"));
1065
+ var CALENDAR_SUBCOLLECTION_ID3 = "calendar";
1066
+ var ProcedureAggregationService = class {
1067
+ constructor(firestore6) {
1068
+ this.db = firestore6 || admin4.firestore();
1069
+ }
1070
+ /**
1071
+ * Adds procedure information to a practitioner when a new procedure is created
1072
+ * @param practitionerId - ID of the practitioner who performs the procedure
1073
+ * @param procedureSummary - Summary information about the procedure
1074
+ * @returns {Promise<void>}
1075
+ */
1076
+ async addProcedureToPractitioner(practitionerId, procedureSummary) {
1077
+ if (!practitionerId || !procedureSummary) {
1078
+ console.log(
1079
+ "[ProcedureAggregationService] Missing practitionerId or procedureSummary for adding procedure to practitioner. Skipping."
1080
+ );
1081
+ return;
1082
+ }
1083
+ const procedureId = procedureSummary.id;
1084
+ console.log(
1085
+ `[ProcedureAggregationService] Adding procedure ${procedureId} to practitioner ${practitionerId}.`
1086
+ );
1087
+ const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
1088
+ try {
1089
+ await practitionerRef.update({
1090
+ procedureIds: admin4.firestore.FieldValue.arrayUnion(procedureId),
1091
+ proceduresInfo: admin4.firestore.FieldValue.arrayUnion(procedureSummary),
1092
+ updatedAt: admin4.firestore.FieldValue.serverTimestamp()
1093
+ });
1094
+ console.log(
1095
+ `[ProcedureAggregationService] Successfully added procedure ${procedureId} to practitioner ${practitionerId}.`
1096
+ );
1097
+ } catch (error) {
1098
+ console.error(
1099
+ `[ProcedureAggregationService] Error adding procedure ${procedureId} to practitioner ${practitionerId}:`,
1100
+ error
1101
+ );
1102
+ throw error;
1103
+ }
1104
+ }
1105
+ /**
1106
+ * Adds procedure information to a clinic when a new procedure is created
1107
+ * @param clinicId - ID of the clinic where the procedure is performed
1108
+ * @param procedureSummary - Summary information about the procedure
1109
+ * @returns {Promise<void>}
1110
+ */
1111
+ async addProcedureToClinic(clinicId, procedureSummary) {
1112
+ if (!clinicId || !procedureSummary) {
1113
+ console.log(
1114
+ "[ProcedureAggregationService] Missing clinicId or procedureSummary for adding procedure to clinic. Skipping."
1115
+ );
1116
+ return;
1117
+ }
1118
+ const procedureId = procedureSummary.id;
1119
+ console.log(
1120
+ `[ProcedureAggregationService] Adding procedure ${procedureId} to clinic ${clinicId}.`
1121
+ );
1122
+ const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
1123
+ try {
1124
+ await clinicRef.update({
1125
+ procedures: admin4.firestore.FieldValue.arrayUnion(procedureId),
1126
+ proceduresInfo: admin4.firestore.FieldValue.arrayUnion(procedureSummary),
1127
+ updatedAt: admin4.firestore.FieldValue.serverTimestamp()
1128
+ });
1129
+ console.log(
1130
+ `[ProcedureAggregationService] Successfully added procedure ${procedureId} to clinic ${clinicId}.`
1131
+ );
1132
+ } catch (error) {
1133
+ console.error(
1134
+ `[ProcedureAggregationService] Error adding procedure ${procedureId} to clinic ${clinicId}:`,
1135
+ error
1136
+ );
1137
+ throw error;
1138
+ }
1139
+ }
1140
+ /**
1141
+ * Updates procedure information in a practitioner document
1142
+ * @param practitionerId - ID of the practitioner who performs the procedure
1143
+ * @param procedureSummary - Updated summary information about the procedure
1144
+ * @returns {Promise<void>}
1145
+ */
1146
+ async updateProcedureInfoInPractitioner(practitionerId, procedureSummary) {
1147
+ if (!practitionerId || !procedureSummary) {
1148
+ console.log(
1149
+ "[ProcedureAggregationService] Missing practitionerId or procedureSummary for updating procedure in practitioner. Skipping."
1150
+ );
1151
+ return;
1152
+ }
1153
+ const procedureId = procedureSummary.id;
1154
+ console.log(
1155
+ `[ProcedureAggregationService] Updating procedure ${procedureId} info in practitioner ${practitionerId}.`
1156
+ );
1157
+ const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
1158
+ try {
1159
+ await this.db.runTransaction(async (transaction) => {
1160
+ const practitionerDoc = await transaction.get(practitionerRef);
1161
+ if (!practitionerDoc.exists) {
1162
+ throw new Error(
1163
+ `Practitioner ${practitionerId} does not exist for procedure update`
1164
+ );
1165
+ }
1166
+ const practitionerData = practitionerDoc.data();
1167
+ if (!practitionerData) {
1168
+ throw new Error(
1169
+ `Practitioner ${practitionerId} data is empty for procedure update`
1170
+ );
1171
+ }
1172
+ const proceduresInfo = practitionerData.proceduresInfo || [];
1173
+ const updatedProceduresInfo = proceduresInfo.filter(
1174
+ (p) => p.id !== procedureId
1175
+ );
1176
+ updatedProceduresInfo.push(procedureSummary);
1177
+ transaction.update(practitionerRef, {
1178
+ proceduresInfo: updatedProceduresInfo,
1179
+ updatedAt: admin4.firestore.FieldValue.serverTimestamp()
1180
+ });
1181
+ });
1182
+ console.log(
1183
+ `[ProcedureAggregationService] Successfully updated procedure ${procedureId} info in practitioner ${practitionerId}.`
1184
+ );
1185
+ } catch (error) {
1186
+ console.error(
1187
+ `[ProcedureAggregationService] Error updating procedure ${procedureId} info in practitioner ${practitionerId}:`,
1188
+ error
1189
+ );
1190
+ throw error;
1191
+ }
1192
+ }
1193
+ /**
1194
+ * Updates procedure information in a clinic document
1195
+ * @param clinicId - ID of the clinic where the procedure is performed
1196
+ * @param procedureSummary - Updated summary information about the procedure
1197
+ * @returns {Promise<void>}
1198
+ */
1199
+ async updateProcedureInfoInClinic(clinicId, procedureSummary) {
1200
+ if (!clinicId || !procedureSummary) {
1201
+ console.log(
1202
+ "[ProcedureAggregationService] Missing clinicId or procedureSummary for updating procedure in clinic. Skipping."
1203
+ );
1204
+ return;
1205
+ }
1206
+ const procedureId = procedureSummary.id;
1207
+ console.log(
1208
+ `[ProcedureAggregationService] Updating procedure ${procedureId} info in clinic ${clinicId}.`
1209
+ );
1210
+ const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
1211
+ try {
1212
+ await this.db.runTransaction(async (transaction) => {
1213
+ const clinicDoc = await transaction.get(clinicRef);
1214
+ if (!clinicDoc.exists) {
1215
+ throw new Error(
1216
+ `Clinic ${clinicId} does not exist for procedure update`
1217
+ );
1218
+ }
1219
+ const clinicData = clinicDoc.data();
1220
+ if (!clinicData) {
1221
+ throw new Error(
1222
+ `Clinic ${clinicId} data is empty for procedure update`
1223
+ );
1224
+ }
1225
+ const proceduresInfo = clinicData.proceduresInfo || [];
1226
+ const updatedProceduresInfo = proceduresInfo.filter(
1227
+ (p) => p.id !== procedureId
1228
+ );
1229
+ updatedProceduresInfo.push(procedureSummary);
1230
+ transaction.update(clinicRef, {
1231
+ proceduresInfo: updatedProceduresInfo,
1232
+ updatedAt: admin4.firestore.FieldValue.serverTimestamp()
1233
+ });
1234
+ });
1235
+ console.log(
1236
+ `[ProcedureAggregationService] Successfully updated procedure ${procedureId} info in clinic ${clinicId}.`
1237
+ );
1238
+ } catch (error) {
1239
+ console.error(
1240
+ `[ProcedureAggregationService] Error updating procedure ${procedureId} info in clinic ${clinicId}:`,
1241
+ error
1242
+ );
1243
+ throw error;
1244
+ }
1245
+ }
1246
+ /**
1247
+ * Updates procedure information in calendar events
1248
+ * @param procedureId - ID of the procedure
1249
+ * @param procedureInfo - Updated procedure information
1250
+ * @returns {Promise<void>}
1251
+ */
1252
+ async updateProcedureInfoInCalendarEvents(procedureId, procedureInfo) {
1253
+ if (!procedureId || !procedureInfo) {
1254
+ console.log(
1255
+ "[ProcedureAggregationService] Missing procedureId or procedureInfo for calendar update. Skipping."
1256
+ );
1257
+ return;
1258
+ }
1259
+ console.log(
1260
+ `[ProcedureAggregationService] Querying upcoming calendar events for procedure ${procedureId} to update procedure info.`
1261
+ );
1262
+ const now = admin4.firestore.Timestamp.now();
1263
+ const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID3).where("procedureId", "==", procedureId).where("eventTime.start", ">", now);
1264
+ try {
1265
+ const snapshot = await calendarEventsQuery.get();
1266
+ if (snapshot.empty) {
1267
+ console.log(
1268
+ `[ProcedureAggregationService] No upcoming calendar events found for procedure ${procedureId}. No procedure info updates needed.`
1269
+ );
1270
+ return;
1271
+ }
1272
+ const batch = this.db.batch();
1273
+ snapshot.docs.forEach((doc) => {
1274
+ console.log(
1275
+ `[ProcedureAggregationService] Updating procedure info for calendar event ${doc.ref.path}`
1276
+ );
1277
+ batch.update(doc.ref, {
1278
+ procedureInfo,
1279
+ updatedAt: admin4.firestore.FieldValue.serverTimestamp()
1280
+ });
1281
+ });
1282
+ await batch.commit();
1283
+ console.log(
1284
+ `[ProcedureAggregationService] Successfully updated procedure info in ${snapshot.size} upcoming calendar events for procedure ${procedureId}.`
1285
+ );
1286
+ } catch (error) {
1287
+ console.error(
1288
+ `[ProcedureAggregationService] Error updating procedure info in calendar events for procedure ${procedureId}:`,
1289
+ error
1290
+ );
1291
+ throw error;
1292
+ }
1293
+ }
1294
+ /**
1295
+ * Cancels all upcoming calendar events for a procedure
1296
+ * @param procedureId - ID of the procedure
1297
+ * @returns {Promise<void>}
1298
+ */
1299
+ async cancelUpcomingCalendarEventsForProcedure(procedureId) {
1300
+ if (!procedureId) {
1301
+ console.log(
1302
+ "[ProcedureAggregationService] Missing procedureId for canceling calendar events. Skipping."
1303
+ );
1304
+ return;
1305
+ }
1306
+ console.log(
1307
+ `[ProcedureAggregationService] Querying upcoming calendar events for procedure ${procedureId} to cancel.`
1308
+ );
1309
+ const now = admin4.firestore.Timestamp.now();
1310
+ const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID3).where("procedureId", "==", procedureId).where("eventTime.start", ">", now);
1311
+ try {
1312
+ const snapshot = await calendarEventsQuery.get();
1313
+ if (snapshot.empty) {
1314
+ console.log(
1315
+ `[ProcedureAggregationService] No upcoming calendar events found for procedure ${procedureId}. No events to cancel.`
1316
+ );
1317
+ return;
1318
+ }
1319
+ const batch = this.db.batch();
1320
+ snapshot.docs.forEach((doc) => {
1321
+ console.log(
1322
+ `[ProcedureAggregationService] Canceling calendar event ${doc.ref.path}`
1323
+ );
1324
+ batch.update(doc.ref, {
1325
+ status: "CANCELED",
1326
+ cancelReason: "Procedure deleted or inactivated",
1327
+ updatedAt: admin4.firestore.FieldValue.serverTimestamp()
1328
+ });
1329
+ });
1330
+ await batch.commit();
1331
+ console.log(
1332
+ `[ProcedureAggregationService] Successfully canceled ${snapshot.size} upcoming calendar events for procedure ${procedureId}.`
1333
+ );
1334
+ } catch (error) {
1335
+ console.error(
1336
+ `[ProcedureAggregationService] Error canceling calendar events for procedure ${procedureId}:`,
1337
+ error
1338
+ );
1339
+ throw error;
1340
+ }
1341
+ }
1342
+ /**
1343
+ * Removes procedure from a practitioner when a procedure is deleted or inactivated
1344
+ * @param practitionerId - ID of the practitioner who performs the procedure
1345
+ * @param procedureId - ID of the procedure
1346
+ * @returns {Promise<void>}
1347
+ */
1348
+ async removeProcedureFromPractitioner(practitionerId, procedureId) {
1349
+ if (!practitionerId || !procedureId) {
1350
+ console.log(
1351
+ "[ProcedureAggregationService] Missing practitionerId or procedureId for removing procedure from practitioner. Skipping."
1352
+ );
1353
+ return;
1354
+ }
1355
+ console.log(
1356
+ `[ProcedureAggregationService] Removing procedure ${procedureId} from practitioner ${practitionerId}.`
1357
+ );
1358
+ const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
1359
+ try {
1360
+ await this.db.runTransaction(async (transaction) => {
1361
+ const practitionerDoc = await transaction.get(practitionerRef);
1362
+ if (!practitionerDoc.exists) {
1363
+ throw new Error(
1364
+ `Practitioner ${practitionerId} does not exist for procedure removal`
1365
+ );
1366
+ }
1367
+ const practitionerData = practitionerDoc.data();
1368
+ if (!practitionerData) {
1369
+ throw new Error(
1370
+ `Practitioner ${practitionerId} data is empty for procedure removal`
1371
+ );
1372
+ }
1373
+ const proceduresInfo = practitionerData.proceduresInfo || [];
1374
+ const updatedProceduresInfo = proceduresInfo.filter(
1375
+ (p) => p.id !== procedureId
1376
+ );
1377
+ transaction.update(practitionerRef, {
1378
+ procedureIds: admin4.firestore.FieldValue.arrayRemove(procedureId),
1379
+ proceduresInfo: updatedProceduresInfo,
1380
+ updatedAt: admin4.firestore.FieldValue.serverTimestamp()
1381
+ });
1382
+ });
1383
+ console.log(
1384
+ `[ProcedureAggregationService] Successfully removed procedure ${procedureId} from practitioner ${practitionerId}.`
1385
+ );
1386
+ } catch (error) {
1387
+ console.error(
1388
+ `[ProcedureAggregationService] Error removing procedure ${procedureId} from practitioner ${practitionerId}:`,
1389
+ error
1390
+ );
1391
+ throw error;
1392
+ }
1393
+ }
1394
+ /**
1395
+ * Removes procedure from a clinic when a procedure is deleted or inactivated
1396
+ * @param clinicId - ID of the clinic where the procedure is performed
1397
+ * @param procedureId - ID of the procedure
1398
+ * @returns {Promise<void>}
1399
+ */
1400
+ async removeProcedureFromClinic(clinicId, procedureId) {
1401
+ if (!clinicId || !procedureId) {
1402
+ console.log(
1403
+ "[ProcedureAggregationService] Missing clinicId or procedureId for removing procedure from clinic. Skipping."
1404
+ );
1405
+ return;
1406
+ }
1407
+ console.log(
1408
+ `[ProcedureAggregationService] Removing procedure ${procedureId} from clinic ${clinicId}.`
1409
+ );
1410
+ const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
1411
+ try {
1412
+ await this.db.runTransaction(async (transaction) => {
1413
+ const clinicDoc = await transaction.get(clinicRef);
1414
+ if (!clinicDoc.exists) {
1415
+ throw new Error(
1416
+ `Clinic ${clinicId} does not exist for procedure removal`
1417
+ );
1418
+ }
1419
+ const clinicData = clinicDoc.data();
1420
+ if (!clinicData) {
1421
+ throw new Error(
1422
+ `Clinic ${clinicId} data is empty for procedure removal`
1423
+ );
1424
+ }
1425
+ const proceduresInfo = clinicData.proceduresInfo || [];
1426
+ const updatedProceduresInfo = proceduresInfo.filter(
1427
+ (p) => p.id !== procedureId
1428
+ );
1429
+ transaction.update(clinicRef, {
1430
+ procedures: admin4.firestore.FieldValue.arrayRemove(procedureId),
1431
+ proceduresInfo: updatedProceduresInfo,
1432
+ updatedAt: admin4.firestore.FieldValue.serverTimestamp()
1433
+ });
1434
+ });
1435
+ console.log(
1436
+ `[ProcedureAggregationService] Successfully removed procedure ${procedureId} from clinic ${clinicId}.`
1437
+ );
1438
+ } catch (error) {
1439
+ console.error(
1440
+ `[ProcedureAggregationService] Error removing procedure ${procedureId} from clinic ${clinicId}:`,
1441
+ error
1442
+ );
1443
+ throw error;
1444
+ }
1445
+ }
1446
+ };
1447
+
1448
+ // src/admin/aggregation/patient/patient.aggregation.service.ts
1449
+ var admin5 = __toESM(require("firebase-admin"));
1450
+ var CALENDAR_SUBCOLLECTION_ID4 = "calendar";
1451
+ var PatientAggregationService = class {
1452
+ constructor(firestore6) {
1453
+ this.db = firestore6 || admin5.firestore();
1454
+ }
1455
+ // --- Methods for Patient Creation --- >
1456
+ // No specific aggregations defined for patient creation in the plan.
1457
+ // --- Methods for Patient Update --- >
1458
+ /**
1459
+ * Updates patient information in calendar events
1460
+ * @param patientId - ID of the patient
1461
+ * @param patientInfo - Updated patient information
1462
+ * @returns {Promise<void>}
1463
+ */
1464
+ async updatePatientInfoInCalendarEvents(patientId, patientInfo) {
1465
+ if (!patientId || !patientInfo) {
1466
+ console.log(
1467
+ "[PatientAggregationService] Missing patientId or patientInfo for calendar update. Skipping."
1468
+ );
1469
+ return;
1470
+ }
1471
+ console.log(
1472
+ `[PatientAggregationService] Querying upcoming calendar events for patient ${patientId} to update patient info.`
1473
+ );
1474
+ const now = admin5.firestore.Timestamp.now();
1475
+ const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID4).where("patientId", "==", patientId).where("eventTime.start", ">", now);
1476
+ try {
1477
+ const snapshot = await calendarEventsQuery.get();
1478
+ if (snapshot.empty) {
1479
+ console.log(
1480
+ `[PatientAggregationService] No upcoming calendar events found for patient ${patientId}. No patient info updates needed.`
1481
+ );
1482
+ return;
1483
+ }
1484
+ const batch = this.db.batch();
1485
+ snapshot.docs.forEach((doc) => {
1486
+ console.log(
1487
+ `[PatientAggregationService] Updating patient info for calendar event ${doc.ref.path}`
1488
+ );
1489
+ batch.update(doc.ref, {
1490
+ patientInfo,
1491
+ updatedAt: admin5.firestore.FieldValue.serverTimestamp()
1492
+ });
1493
+ });
1494
+ await batch.commit();
1495
+ console.log(
1496
+ `[PatientAggregationService] Successfully updated patient info in ${snapshot.size} upcoming calendar events for patient ${patientId}.`
1497
+ );
1498
+ } catch (error) {
1499
+ console.error(
1500
+ `[PatientAggregationService] Error updating patient info in calendar events for patient ${patientId}:`,
1501
+ error
1502
+ );
1503
+ throw error;
1504
+ }
1505
+ }
1506
+ // --- Methods for Patient Deletion --- >
1507
+ /**
1508
+ * Cancels all upcoming calendar events associated with a deleted patient
1509
+ * @param patientId - ID of the deleted patient
1510
+ * @returns {Promise<void>}
1511
+ */
1512
+ async cancelUpcomingCalendarEventsForPatient(patientId) {
1513
+ if (!patientId) {
1514
+ console.log(
1515
+ "[PatientAggregationService] Missing patientId for canceling calendar events. Skipping."
1516
+ );
1517
+ return;
1518
+ }
1519
+ console.log(
1520
+ `[PatientAggregationService] Querying upcoming calendar events for patient ${patientId} to cancel.`
1521
+ );
1522
+ const now = admin5.firestore.Timestamp.now();
1523
+ const calendarEventsQuery = this.db.collectionGroup(CALENDAR_SUBCOLLECTION_ID4).where("patientId", "==", patientId).where("eventTime.start", ">", now);
1524
+ try {
1525
+ const snapshot = await calendarEventsQuery.get();
1526
+ if (snapshot.empty) {
1527
+ console.log(
1528
+ `[PatientAggregationService] No upcoming calendar events found for patient ${patientId}. No events to cancel.`
1529
+ );
1530
+ return;
1531
+ }
1532
+ const batch = this.db.batch();
1533
+ snapshot.docs.forEach((doc) => {
1534
+ console.log(
1535
+ `[PatientAggregationService] Canceling calendar event ${doc.ref.path}`
1536
+ );
1537
+ batch.update(doc.ref, {
1538
+ status: "CANCELED",
1539
+ cancelReason: "Patient deleted",
1540
+ updatedAt: admin5.firestore.FieldValue.serverTimestamp()
1541
+ });
1542
+ });
1543
+ await batch.commit();
1544
+ console.log(
1545
+ `[PatientAggregationService] Successfully canceled ${snapshot.size} upcoming calendar events for patient ${patientId}.`
1546
+ );
1547
+ } catch (error) {
1548
+ console.error(
1549
+ `[PatientAggregationService] Error canceling calendar events for patient ${patientId}:`,
1550
+ error
1551
+ );
1552
+ throw error;
1553
+ }
1554
+ }
1555
+ };
1556
+
229
1557
  // src/types/index.ts
230
1558
  var UserRole = /* @__PURE__ */ ((UserRole2) => {
231
1559
  UserRole2["PATIENT"] = "patient";
@@ -234,11 +1562,18 @@ var UserRole = /* @__PURE__ */ ((UserRole2) => {
234
1562
  UserRole2["CLINIC_ADMIN"] = "clinic_admin";
235
1563
  return UserRole2;
236
1564
  })(UserRole || {});
1565
+
1566
+ // src/admin/index.ts
1567
+ console.log("[Admin Module] Initialized and services exported.");
237
1568
  // Annotate the CommonJS export names for ESM import in node:
238
1569
  0 && (module.exports = {
1570
+ ClinicAggregationService,
239
1571
  NOTIFICATIONS_COLLECTION,
240
1572
  NotificationStatus,
241
1573
  NotificationType,
242
1574
  NotificationsAdmin,
1575
+ PatientAggregationService,
1576
+ PractitionerAggregationService,
1577
+ ProcedureAggregationService,
243
1578
  UserRole
244
1579
  });