@blackcode_sa/metaestetics-api 1.13.4 → 1.13.6

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.
@@ -1001,6 +1001,14 @@ interface ClinicLocation {
1001
1001
  geohash?: string | null;
1002
1002
  tz?: string | null;
1003
1003
  }
1004
+ /**
1005
+ * Interface for a break period during working hours
1006
+ */
1007
+ interface Break {
1008
+ title?: string;
1009
+ start: string;
1010
+ end: string;
1011
+ }
1004
1012
  /**
1005
1013
  * Interface for working hours
1006
1014
  */
@@ -1008,58 +1016,37 @@ interface WorkingHours {
1008
1016
  monday: {
1009
1017
  open: string;
1010
1018
  close: string;
1011
- breaks?: {
1012
- start: string;
1013
- end: string;
1014
- }[];
1019
+ breaks?: Break[];
1015
1020
  } | null;
1016
1021
  tuesday: {
1017
1022
  open: string;
1018
1023
  close: string;
1019
- breaks?: {
1020
- start: string;
1021
- end: string;
1022
- }[];
1024
+ breaks?: Break[];
1023
1025
  } | null;
1024
1026
  wednesday: {
1025
1027
  open: string;
1026
1028
  close: string;
1027
- breaks?: {
1028
- start: string;
1029
- end: string;
1030
- }[];
1029
+ breaks?: Break[];
1031
1030
  } | null;
1032
1031
  thursday: {
1033
1032
  open: string;
1034
1033
  close: string;
1035
- breaks?: {
1036
- start: string;
1037
- end: string;
1038
- }[];
1034
+ breaks?: Break[];
1039
1035
  } | null;
1040
1036
  friday: {
1041
1037
  open: string;
1042
1038
  close: string;
1043
- breaks?: {
1044
- start: string;
1045
- end: string;
1046
- }[];
1039
+ breaks?: Break[];
1047
1040
  } | null;
1048
1041
  saturday: {
1049
1042
  open: string;
1050
1043
  close: string;
1051
- breaks?: {
1052
- start: string;
1053
- end: string;
1054
- }[];
1044
+ breaks?: Break[];
1055
1045
  } | null;
1056
1046
  sunday: {
1057
1047
  open: string;
1058
1048
  close: string;
1059
- breaks?: {
1060
- start: string;
1061
- end: string;
1062
- }[];
1049
+ breaks?: Break[];
1063
1050
  } | null;
1064
1051
  }
1065
1052
  /**
@@ -3616,13 +3603,32 @@ declare class ClinicAggregationService {
3616
3603
  * Removes clinic from practitioners when a clinic is deleted.
3617
3604
  * @param practitionerIds IDs of practitioners associated with the clinic.
3618
3605
  * @param clinicId The ID of the deleted clinic.
3606
+ * @param checkAndDeactivateDoctors If true, checks if doctors have remaining active clinics and deactivates them if not.
3619
3607
  */
3620
- removeClinicFromPractitioners(practitionerIds: string[], clinicId: string): Promise<void>;
3608
+ removeClinicFromPractitioners(practitionerIds: string[], clinicId: string, checkAndDeactivateDoctors?: boolean): Promise<void>;
3609
+ /**
3610
+ * Checks if practitioners have any remaining active clinics and deactivates them if not.
3611
+ * @param practitionerIds IDs of practitioners to check
3612
+ */
3613
+ checkAndDeactivateDoctorsWithNoActiveClinics(practitionerIds: string[]): Promise<void>;
3614
+ /**
3615
+ * Checks if practitioners were deactivated because they had no active clinics,
3616
+ * and reactivates them if this clinic was their only clinic.
3617
+ * @param practitionerIds IDs of practitioners associated with the reactivated clinic
3618
+ * @param clinicId ID of the reactivated clinic
3619
+ */
3620
+ checkAndReactivateDoctorsForClinic(practitionerIds: string[], clinicId: string): Promise<void>;
3621
3621
  /**
3622
3622
  * Inactivates all procedures associated with a deleted clinic
3623
3623
  * @param procedureIds IDs of procedures associated with the clinic
3624
3624
  */
3625
3625
  inactivateProceduresForClinic(procedureIds: string[]): Promise<void>;
3626
+ /**
3627
+ * Reactivates all procedures associated with a reactivated clinic
3628
+ * Only reactivates procedures that still exist (haven't been deleted)
3629
+ * @param procedureIds IDs of procedures associated with the clinic
3630
+ */
3631
+ reactivateProceduresForClinic(procedureIds: string[]): Promise<void>;
3626
3632
  /**
3627
3633
  * Removes clinic from clinic group when a clinic is deleted
3628
3634
  * @param clinicGroupId ID of the parent clinic group
@@ -1001,6 +1001,14 @@ interface ClinicLocation {
1001
1001
  geohash?: string | null;
1002
1002
  tz?: string | null;
1003
1003
  }
1004
+ /**
1005
+ * Interface for a break period during working hours
1006
+ */
1007
+ interface Break {
1008
+ title?: string;
1009
+ start: string;
1010
+ end: string;
1011
+ }
1004
1012
  /**
1005
1013
  * Interface for working hours
1006
1014
  */
@@ -1008,58 +1016,37 @@ interface WorkingHours {
1008
1016
  monday: {
1009
1017
  open: string;
1010
1018
  close: string;
1011
- breaks?: {
1012
- start: string;
1013
- end: string;
1014
- }[];
1019
+ breaks?: Break[];
1015
1020
  } | null;
1016
1021
  tuesday: {
1017
1022
  open: string;
1018
1023
  close: string;
1019
- breaks?: {
1020
- start: string;
1021
- end: string;
1022
- }[];
1024
+ breaks?: Break[];
1023
1025
  } | null;
1024
1026
  wednesday: {
1025
1027
  open: string;
1026
1028
  close: string;
1027
- breaks?: {
1028
- start: string;
1029
- end: string;
1030
- }[];
1029
+ breaks?: Break[];
1031
1030
  } | null;
1032
1031
  thursday: {
1033
1032
  open: string;
1034
1033
  close: string;
1035
- breaks?: {
1036
- start: string;
1037
- end: string;
1038
- }[];
1034
+ breaks?: Break[];
1039
1035
  } | null;
1040
1036
  friday: {
1041
1037
  open: string;
1042
1038
  close: string;
1043
- breaks?: {
1044
- start: string;
1045
- end: string;
1046
- }[];
1039
+ breaks?: Break[];
1047
1040
  } | null;
1048
1041
  saturday: {
1049
1042
  open: string;
1050
1043
  close: string;
1051
- breaks?: {
1052
- start: string;
1053
- end: string;
1054
- }[];
1044
+ breaks?: Break[];
1055
1045
  } | null;
1056
1046
  sunday: {
1057
1047
  open: string;
1058
1048
  close: string;
1059
- breaks?: {
1060
- start: string;
1061
- end: string;
1062
- }[];
1049
+ breaks?: Break[];
1063
1050
  } | null;
1064
1051
  }
1065
1052
  /**
@@ -3616,13 +3603,32 @@ declare class ClinicAggregationService {
3616
3603
  * Removes clinic from practitioners when a clinic is deleted.
3617
3604
  * @param practitionerIds IDs of practitioners associated with the clinic.
3618
3605
  * @param clinicId The ID of the deleted clinic.
3606
+ * @param checkAndDeactivateDoctors If true, checks if doctors have remaining active clinics and deactivates them if not.
3619
3607
  */
3620
- removeClinicFromPractitioners(practitionerIds: string[], clinicId: string): Promise<void>;
3608
+ removeClinicFromPractitioners(practitionerIds: string[], clinicId: string, checkAndDeactivateDoctors?: boolean): Promise<void>;
3609
+ /**
3610
+ * Checks if practitioners have any remaining active clinics and deactivates them if not.
3611
+ * @param practitionerIds IDs of practitioners to check
3612
+ */
3613
+ checkAndDeactivateDoctorsWithNoActiveClinics(practitionerIds: string[]): Promise<void>;
3614
+ /**
3615
+ * Checks if practitioners were deactivated because they had no active clinics,
3616
+ * and reactivates them if this clinic was their only clinic.
3617
+ * @param practitionerIds IDs of practitioners associated with the reactivated clinic
3618
+ * @param clinicId ID of the reactivated clinic
3619
+ */
3620
+ checkAndReactivateDoctorsForClinic(practitionerIds: string[], clinicId: string): Promise<void>;
3621
3621
  /**
3622
3622
  * Inactivates all procedures associated with a deleted clinic
3623
3623
  * @param procedureIds IDs of procedures associated with the clinic
3624
3624
  */
3625
3625
  inactivateProceduresForClinic(procedureIds: string[]): Promise<void>;
3626
+ /**
3627
+ * Reactivates all procedures associated with a reactivated clinic
3628
+ * Only reactivates procedures that still exist (haven't been deleted)
3629
+ * @param procedureIds IDs of procedures associated with the clinic
3630
+ */
3631
+ reactivateProceduresForClinic(procedureIds: string[]): Promise<void>;
3626
3632
  /**
3627
3633
  * Removes clinic from clinic group when a clinic is deleted
3628
3634
  * @param clinicGroupId ID of the parent clinic group
@@ -4399,8 +4399,9 @@ var ClinicAggregationService = class {
4399
4399
  * Removes clinic from practitioners when a clinic is deleted.
4400
4400
  * @param practitionerIds IDs of practitioners associated with the clinic.
4401
4401
  * @param clinicId The ID of the deleted clinic.
4402
+ * @param checkAndDeactivateDoctors If true, checks if doctors have remaining active clinics and deactivates them if not.
4402
4403
  */
4403
- async removeClinicFromPractitioners(practitionerIds, clinicId) {
4404
+ async removeClinicFromPractitioners(practitionerIds, clinicId, checkAndDeactivateDoctors = false) {
4404
4405
  if (!practitionerIds || practitionerIds.length === 0) {
4405
4406
  console.log(
4406
4407
  "[ClinicAggregationService] No practitioner IDs provided for clinic removal. Skipping."
@@ -4408,6 +4409,7 @@ var ClinicAggregationService = class {
4408
4409
  return;
4409
4410
  }
4410
4411
  const batch = this.db.batch();
4412
+ const practitionersToCheck = [];
4411
4413
  console.log(
4412
4414
  `[ClinicAggregationService] Starting batch removal of Clinic (ID: ${clinicId}) from ${practitionerIds.length} practitioners.`
4413
4415
  );
@@ -4429,6 +4431,9 @@ var ClinicAggregationService = class {
4429
4431
  clinicsInfo: filteredClinicsInfo,
4430
4432
  updatedAt: admin7.firestore.FieldValue.serverTimestamp()
4431
4433
  });
4434
+ if (checkAndDeactivateDoctors) {
4435
+ practitionersToCheck.push(practitionerId);
4436
+ }
4432
4437
  }
4433
4438
  }
4434
4439
  try {
@@ -4436,6 +4441,11 @@ var ClinicAggregationService = class {
4436
4441
  console.log(
4437
4442
  `[ClinicAggregationService] Successfully removed Clinic (ID: ${clinicId}) from ${practitionerIds.length} practitioners.`
4438
4443
  );
4444
+ if (checkAndDeactivateDoctors && practitionersToCheck.length > 0) {
4445
+ await this.checkAndDeactivateDoctorsWithNoActiveClinics(
4446
+ practitionersToCheck
4447
+ );
4448
+ }
4439
4449
  } catch (error) {
4440
4450
  console.error(
4441
4451
  `[ClinicAggregationService] Error committing batch removal for Clinic (ID: ${clinicId}) from practitioners:`,
@@ -4444,6 +4454,161 @@ var ClinicAggregationService = class {
4444
4454
  throw error;
4445
4455
  }
4446
4456
  }
4457
+ /**
4458
+ * Checks if practitioners have any remaining active clinics and deactivates them if not.
4459
+ * @param practitionerIds IDs of practitioners to check
4460
+ */
4461
+ async checkAndDeactivateDoctorsWithNoActiveClinics(practitionerIds) {
4462
+ if (!practitionerIds || practitionerIds.length === 0) {
4463
+ return;
4464
+ }
4465
+ console.log(
4466
+ `[ClinicAggregationService] Checking ${practitionerIds.length} practitioners for remaining active clinics.`
4467
+ );
4468
+ const batch = this.db.batch();
4469
+ let deactivationCount = 0;
4470
+ for (const practitionerId of practitionerIds) {
4471
+ const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
4472
+ const practitionerDoc = await practitionerRef.get();
4473
+ if (!practitionerDoc.exists) {
4474
+ continue;
4475
+ }
4476
+ const practitionerData = practitionerDoc.data();
4477
+ const clinicIds = (practitionerData == null ? void 0 : practitionerData.clinics) || [];
4478
+ if (clinicIds.length === 0) {
4479
+ console.log(
4480
+ `[ClinicAggregationService] Practitioner ${practitionerId} has no remaining clinics. Deactivating.`
4481
+ );
4482
+ batch.update(practitionerRef, {
4483
+ isActive: false,
4484
+ updatedAt: admin7.firestore.FieldValue.serverTimestamp()
4485
+ });
4486
+ deactivationCount++;
4487
+ } else {
4488
+ const clinicRefs = clinicIds.map(
4489
+ (id) => this.db.collection(CLINICS_COLLECTION).doc(id)
4490
+ );
4491
+ const clinicDocs = await Promise.all(
4492
+ clinicRefs.map((ref) => ref.get())
4493
+ );
4494
+ const hasActiveClinic = clinicDocs.some(
4495
+ (doc3) => {
4496
+ var _a;
4497
+ return doc3.exists && ((_a = doc3.data()) == null ? void 0 : _a.isActive) === true;
4498
+ }
4499
+ );
4500
+ if (!hasActiveClinic) {
4501
+ console.log(
4502
+ `[ClinicAggregationService] Practitioner ${practitionerId} has no remaining active clinics. Deactivating.`
4503
+ );
4504
+ batch.update(practitionerRef, {
4505
+ isActive: false,
4506
+ updatedAt: admin7.firestore.FieldValue.serverTimestamp()
4507
+ });
4508
+ deactivationCount++;
4509
+ }
4510
+ }
4511
+ }
4512
+ if (deactivationCount > 0) {
4513
+ try {
4514
+ await batch.commit();
4515
+ console.log(
4516
+ `[ClinicAggregationService] Successfully deactivated ${deactivationCount} practitioners with no remaining active clinics.`
4517
+ );
4518
+ } catch (error) {
4519
+ console.error(
4520
+ `[ClinicAggregationService] Error deactivating practitioners:`,
4521
+ error
4522
+ );
4523
+ throw error;
4524
+ }
4525
+ } else {
4526
+ console.log(
4527
+ `[ClinicAggregationService] All practitioners have remaining active clinics. No deactivations needed.`
4528
+ );
4529
+ }
4530
+ }
4531
+ /**
4532
+ * Checks if practitioners were deactivated because they had no active clinics,
4533
+ * and reactivates them if this clinic was their only clinic.
4534
+ * @param practitionerIds IDs of practitioners associated with the reactivated clinic
4535
+ * @param clinicId ID of the reactivated clinic
4536
+ */
4537
+ async checkAndReactivateDoctorsForClinic(practitionerIds, clinicId) {
4538
+ if (!practitionerIds || practitionerIds.length === 0) {
4539
+ return;
4540
+ }
4541
+ console.log(
4542
+ `[ClinicAggregationService] Checking ${practitionerIds.length} practitioners for reactivation after clinic ${clinicId} was reactivated.`
4543
+ );
4544
+ const batch = this.db.batch();
4545
+ let reactivationCount = 0;
4546
+ for (const practitionerId of practitionerIds) {
4547
+ const practitionerRef = this.db.collection(PRACTITIONERS_COLLECTION).doc(practitionerId);
4548
+ const practitionerDoc = await practitionerRef.get();
4549
+ if (!practitionerDoc.exists) {
4550
+ continue;
4551
+ }
4552
+ const practitionerData = practitionerDoc.data();
4553
+ if ((practitionerData == null ? void 0 : practitionerData.isActive) === false) {
4554
+ const clinicIds = (practitionerData == null ? void 0 : practitionerData.clinics) || [];
4555
+ if (clinicIds.includes(clinicId)) {
4556
+ const otherClinicIds = clinicIds.filter((id) => id !== clinicId);
4557
+ if (otherClinicIds.length === 0) {
4558
+ console.log(
4559
+ `[ClinicAggregationService] Practitioner ${practitionerId} was deactivated because clinic ${clinicId} was their only clinic. Reactivating.`
4560
+ );
4561
+ batch.update(practitionerRef, {
4562
+ isActive: true,
4563
+ updatedAt: admin7.firestore.FieldValue.serverTimestamp()
4564
+ });
4565
+ reactivationCount++;
4566
+ } else {
4567
+ const otherClinicRefs = otherClinicIds.map(
4568
+ (id) => this.db.collection(CLINICS_COLLECTION).doc(id)
4569
+ );
4570
+ const otherClinicDocs = await Promise.all(
4571
+ otherClinicRefs.map((ref) => ref.get())
4572
+ );
4573
+ const hasOtherActiveClinic = otherClinicDocs.some(
4574
+ (doc3) => {
4575
+ var _a;
4576
+ return doc3.exists && ((_a = doc3.data()) == null ? void 0 : _a.isActive) === true;
4577
+ }
4578
+ );
4579
+ if (!hasOtherActiveClinic) {
4580
+ console.log(
4581
+ `[ClinicAggregationService] Practitioner ${practitionerId} has no other active clinics. Reactivating because clinic ${clinicId} was reactivated.`
4582
+ );
4583
+ batch.update(practitionerRef, {
4584
+ isActive: true,
4585
+ updatedAt: admin7.firestore.FieldValue.serverTimestamp()
4586
+ });
4587
+ reactivationCount++;
4588
+ }
4589
+ }
4590
+ }
4591
+ }
4592
+ }
4593
+ if (reactivationCount > 0) {
4594
+ try {
4595
+ await batch.commit();
4596
+ console.log(
4597
+ `[ClinicAggregationService] Successfully reactivated ${reactivationCount} practitioners.`
4598
+ );
4599
+ } catch (error) {
4600
+ console.error(
4601
+ `[ClinicAggregationService] Error reactivating practitioners:`,
4602
+ error
4603
+ );
4604
+ throw error;
4605
+ }
4606
+ } else {
4607
+ console.log(
4608
+ `[ClinicAggregationService] No practitioners needed reactivation.`
4609
+ );
4610
+ }
4611
+ }
4447
4612
  /**
4448
4613
  * Inactivates all procedures associated with a deleted clinic
4449
4614
  * @param procedureIds IDs of procedures associated with the clinic
@@ -4479,6 +4644,57 @@ var ClinicAggregationService = class {
4479
4644
  throw error;
4480
4645
  }
4481
4646
  }
4647
+ /**
4648
+ * Reactivates all procedures associated with a reactivated clinic
4649
+ * Only reactivates procedures that still exist (haven't been deleted)
4650
+ * @param procedureIds IDs of procedures associated with the clinic
4651
+ */
4652
+ async reactivateProceduresForClinic(procedureIds) {
4653
+ if (!procedureIds || procedureIds.length === 0) {
4654
+ console.log(
4655
+ "[ClinicAggregationService] No procedure IDs provided for reactivation. Skipping."
4656
+ );
4657
+ return;
4658
+ }
4659
+ const batch = this.db.batch();
4660
+ let reactivationCount = 0;
4661
+ console.log(
4662
+ `[ClinicAggregationService] Starting reactivation of ${procedureIds.length} procedures.`
4663
+ );
4664
+ for (const procedureId of procedureIds) {
4665
+ const procedureRef = this.db.collection(PROCEDURES_COLLECTION).doc(procedureId);
4666
+ const procedureDoc = await procedureRef.get();
4667
+ if (procedureDoc.exists) {
4668
+ batch.update(procedureRef, {
4669
+ isActive: true,
4670
+ updatedAt: admin7.firestore.FieldValue.serverTimestamp()
4671
+ });
4672
+ reactivationCount++;
4673
+ } else {
4674
+ console.log(
4675
+ `[ClinicAggregationService] Procedure ${procedureId} no longer exists. Skipping reactivation.`
4676
+ );
4677
+ }
4678
+ }
4679
+ if (reactivationCount > 0) {
4680
+ try {
4681
+ await batch.commit();
4682
+ console.log(
4683
+ `[ClinicAggregationService] Successfully reactivated ${reactivationCount} procedures.`
4684
+ );
4685
+ } catch (error) {
4686
+ console.error(
4687
+ `[ClinicAggregationService] Error committing batch reactivation of procedures:`,
4688
+ error
4689
+ );
4690
+ throw error;
4691
+ }
4692
+ } else {
4693
+ console.log(
4694
+ `[ClinicAggregationService] No procedures to reactivate (all may have been deleted).`
4695
+ );
4696
+ }
4697
+ }
4482
4698
  /**
4483
4699
  * Removes clinic from clinic group when a clinic is deleted
4484
4700
  * @param clinicGroupId ID of the parent clinic group