@blackcode_sa/metaestetics-api 1.6.19 → 1.6.20

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.
@@ -3345,6 +3345,7 @@ var AppointmentAggregationService = class {
3345
3345
  try {
3346
3346
  const batch = this.db.batch();
3347
3347
  let instancesCreatedCount = 0;
3348
+ let createdInstances = [];
3348
3349
  Logger.info(
3349
3350
  `[AggService] Found ${appointment.preProcedureRequirements.length} pre-requirements to process: ${JSON.stringify(
3350
3351
  appointment.preProcedureRequirements.map((r) => {
@@ -3441,6 +3442,10 @@ var AppointmentAggregationService = class {
3441
3442
  })}`
3442
3443
  );
3443
3444
  batch.set(newInstanceRef, newInstanceData);
3445
+ createdInstances.push({
3446
+ ref: newInstanceRef,
3447
+ data: newInstanceData
3448
+ });
3444
3449
  instancesCreatedCount++;
3445
3450
  Logger.debug(
3446
3451
  `[AggService] Added PatientRequirementInstance ${newInstanceRef.id} to batch for template ${template.id}.`
@@ -3452,12 +3457,97 @@ var AppointmentAggregationService = class {
3452
3457
  Logger.info(
3453
3458
  `[AggService] Successfully created ${instancesCreatedCount} PRE_APPOINTMENT requirement instances for appointment ${appointment.id}.`
3454
3459
  );
3460
+ try {
3461
+ const verifySnapshot = await this.db.collection(PATIENTS_COLLECTION).doc(appointment.patientId).collection(PATIENT_REQUIREMENTS_SUBCOLLECTION_NAME).where("appointmentId", "==", appointment.id).get();
3462
+ if (verifySnapshot.empty) {
3463
+ Logger.warn(
3464
+ `[AggService] Batch commit reported success but documents not found! Attempting direct creation as fallback...`
3465
+ );
3466
+ const fallbackPromises = createdInstances.map(
3467
+ async ({ ref, data }) => {
3468
+ try {
3469
+ await ref.set(data);
3470
+ Logger.info(
3471
+ `[AggService] Fallback direct creation success for ${ref.id}`
3472
+ );
3473
+ return true;
3474
+ } catch (fallbackError) {
3475
+ Logger.error(
3476
+ `[AggService] Fallback direct creation failed for ${ref.id}:`,
3477
+ fallbackError
3478
+ );
3479
+ return false;
3480
+ }
3481
+ }
3482
+ );
3483
+ const fallbackResults = await Promise.allSettled(
3484
+ fallbackPromises
3485
+ );
3486
+ const successCount = fallbackResults.filter(
3487
+ (r) => r.status === "fulfilled" && r.value === true
3488
+ ).length;
3489
+ if (successCount > 0) {
3490
+ Logger.info(
3491
+ `[AggService] Fallback mechanism created ${successCount} out of ${createdInstances.length} requirements`
3492
+ );
3493
+ } else {
3494
+ Logger.error(
3495
+ `[AggService] Both batch and fallback mechanisms failed to create requirements`
3496
+ );
3497
+ throw new Error(
3498
+ "Failed to create patient requirements through both batch and direct methods"
3499
+ );
3500
+ }
3501
+ } else {
3502
+ Logger.info(
3503
+ `[AggService] Verification confirmed ${verifySnapshot.size} requirement documents created`
3504
+ );
3505
+ }
3506
+ } catch (verifyError) {
3507
+ Logger.error(
3508
+ `[AggService] Error during verification of created requirements:`,
3509
+ verifyError
3510
+ );
3511
+ }
3455
3512
  } catch (commitError) {
3456
3513
  Logger.error(
3457
3514
  `[AggService] Error committing batch for PRE_APPOINTMENT requirement instances for appointment ${appointment.id}:`,
3458
3515
  commitError
3459
3516
  );
3460
- throw commitError;
3517
+ Logger.info(`[AggService] Attempting direct creation as fallback...`);
3518
+ const fallbackPromises = createdInstances.map(
3519
+ async ({ ref, data }) => {
3520
+ try {
3521
+ await ref.set(data);
3522
+ Logger.info(
3523
+ `[AggService] Fallback direct creation success for ${ref.id}`
3524
+ );
3525
+ return true;
3526
+ } catch (fallbackError) {
3527
+ Logger.error(
3528
+ `[AggService] Fallback direct creation failed for ${ref.id}:`,
3529
+ fallbackError
3530
+ );
3531
+ return false;
3532
+ }
3533
+ }
3534
+ );
3535
+ const fallbackResults = await Promise.allSettled(fallbackPromises);
3536
+ const successCount = fallbackResults.filter(
3537
+ (r) => r.status === "fulfilled" && r.value === true
3538
+ ).length;
3539
+ if (successCount > 0) {
3540
+ Logger.info(
3541
+ `[AggService] Fallback mechanism created ${successCount} out of ${createdInstances.length} requirements`
3542
+ );
3543
+ } else {
3544
+ Logger.error(
3545
+ `[AggService] Both batch and fallback mechanisms failed to create requirements`
3546
+ );
3547
+ throw new Error(
3548
+ "Failed to create patient requirements through both batch and direct methods"
3549
+ );
3550
+ }
3461
3551
  }
3462
3552
  } else {
3463
3553
  Logger.info(
@@ -3290,6 +3290,7 @@ var AppointmentAggregationService = class {
3290
3290
  try {
3291
3291
  const batch = this.db.batch();
3292
3292
  let instancesCreatedCount = 0;
3293
+ let createdInstances = [];
3293
3294
  Logger.info(
3294
3295
  `[AggService] Found ${appointment.preProcedureRequirements.length} pre-requirements to process: ${JSON.stringify(
3295
3296
  appointment.preProcedureRequirements.map((r) => {
@@ -3386,6 +3387,10 @@ var AppointmentAggregationService = class {
3386
3387
  })}`
3387
3388
  );
3388
3389
  batch.set(newInstanceRef, newInstanceData);
3390
+ createdInstances.push({
3391
+ ref: newInstanceRef,
3392
+ data: newInstanceData
3393
+ });
3389
3394
  instancesCreatedCount++;
3390
3395
  Logger.debug(
3391
3396
  `[AggService] Added PatientRequirementInstance ${newInstanceRef.id} to batch for template ${template.id}.`
@@ -3397,12 +3402,97 @@ var AppointmentAggregationService = class {
3397
3402
  Logger.info(
3398
3403
  `[AggService] Successfully created ${instancesCreatedCount} PRE_APPOINTMENT requirement instances for appointment ${appointment.id}.`
3399
3404
  );
3405
+ try {
3406
+ const verifySnapshot = await this.db.collection(PATIENTS_COLLECTION).doc(appointment.patientId).collection(PATIENT_REQUIREMENTS_SUBCOLLECTION_NAME).where("appointmentId", "==", appointment.id).get();
3407
+ if (verifySnapshot.empty) {
3408
+ Logger.warn(
3409
+ `[AggService] Batch commit reported success but documents not found! Attempting direct creation as fallback...`
3410
+ );
3411
+ const fallbackPromises = createdInstances.map(
3412
+ async ({ ref, data }) => {
3413
+ try {
3414
+ await ref.set(data);
3415
+ Logger.info(
3416
+ `[AggService] Fallback direct creation success for ${ref.id}`
3417
+ );
3418
+ return true;
3419
+ } catch (fallbackError) {
3420
+ Logger.error(
3421
+ `[AggService] Fallback direct creation failed for ${ref.id}:`,
3422
+ fallbackError
3423
+ );
3424
+ return false;
3425
+ }
3426
+ }
3427
+ );
3428
+ const fallbackResults = await Promise.allSettled(
3429
+ fallbackPromises
3430
+ );
3431
+ const successCount = fallbackResults.filter(
3432
+ (r) => r.status === "fulfilled" && r.value === true
3433
+ ).length;
3434
+ if (successCount > 0) {
3435
+ Logger.info(
3436
+ `[AggService] Fallback mechanism created ${successCount} out of ${createdInstances.length} requirements`
3437
+ );
3438
+ } else {
3439
+ Logger.error(
3440
+ `[AggService] Both batch and fallback mechanisms failed to create requirements`
3441
+ );
3442
+ throw new Error(
3443
+ "Failed to create patient requirements through both batch and direct methods"
3444
+ );
3445
+ }
3446
+ } else {
3447
+ Logger.info(
3448
+ `[AggService] Verification confirmed ${verifySnapshot.size} requirement documents created`
3449
+ );
3450
+ }
3451
+ } catch (verifyError) {
3452
+ Logger.error(
3453
+ `[AggService] Error during verification of created requirements:`,
3454
+ verifyError
3455
+ );
3456
+ }
3400
3457
  } catch (commitError) {
3401
3458
  Logger.error(
3402
3459
  `[AggService] Error committing batch for PRE_APPOINTMENT requirement instances for appointment ${appointment.id}:`,
3403
3460
  commitError
3404
3461
  );
3405
- throw commitError;
3462
+ Logger.info(`[AggService] Attempting direct creation as fallback...`);
3463
+ const fallbackPromises = createdInstances.map(
3464
+ async ({ ref, data }) => {
3465
+ try {
3466
+ await ref.set(data);
3467
+ Logger.info(
3468
+ `[AggService] Fallback direct creation success for ${ref.id}`
3469
+ );
3470
+ return true;
3471
+ } catch (fallbackError) {
3472
+ Logger.error(
3473
+ `[AggService] Fallback direct creation failed for ${ref.id}:`,
3474
+ fallbackError
3475
+ );
3476
+ return false;
3477
+ }
3478
+ }
3479
+ );
3480
+ const fallbackResults = await Promise.allSettled(fallbackPromises);
3481
+ const successCount = fallbackResults.filter(
3482
+ (r) => r.status === "fulfilled" && r.value === true
3483
+ ).length;
3484
+ if (successCount > 0) {
3485
+ Logger.info(
3486
+ `[AggService] Fallback mechanism created ${successCount} out of ${createdInstances.length} requirements`
3487
+ );
3488
+ } else {
3489
+ Logger.error(
3490
+ `[AggService] Both batch and fallback mechanisms failed to create requirements`
3491
+ );
3492
+ throw new Error(
3493
+ "Failed to create patient requirements through both batch and direct methods"
3494
+ );
3495
+ }
3406
3496
  }
3407
3497
  } else {
3408
3498
  Logger.info(
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@blackcode_sa/metaestetics-api",
3
3
  "private": false,
4
- "version": "1.6.19",
4
+ "version": "1.6.20",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.mjs",
@@ -507,6 +507,8 @@ export class AppointmentAggregationService {
507
507
  try {
508
508
  const batch = this.db.batch();
509
509
  let instancesCreatedCount = 0;
510
+ // Store created instances for fallback direct creation if needed
511
+ let createdInstances = [];
510
512
 
511
513
  // Log more details about the pre-requirements
512
514
  Logger.info(
@@ -633,6 +635,12 @@ export class AppointmentAggregationService {
633
635
  );
634
636
 
635
637
  batch.set(newInstanceRef, newInstanceData);
638
+ // Store for potential fallback
639
+ createdInstances.push({
640
+ ref: newInstanceRef,
641
+ data: newInstanceData,
642
+ });
643
+
636
644
  instancesCreatedCount++;
637
645
  Logger.debug(
638
646
  `[AggService] Added PatientRequirementInstance ${newInstanceRef.id} to batch for template ${template.id}.`
@@ -645,12 +653,113 @@ export class AppointmentAggregationService {
645
653
  Logger.info(
646
654
  `[AggService] Successfully created ${instancesCreatedCount} PRE_APPOINTMENT requirement instances for appointment ${appointment.id}.`
647
655
  );
656
+
657
+ // Verify creation success
658
+ try {
659
+ const verifySnapshot = await this.db
660
+ .collection(PATIENTS_COLLECTION)
661
+ .doc(appointment.patientId)
662
+ .collection(PATIENT_REQUIREMENTS_SUBCOLLECTION_NAME)
663
+ .where("appointmentId", "==", appointment.id)
664
+ .get();
665
+
666
+ if (verifySnapshot.empty) {
667
+ Logger.warn(
668
+ `[AggService] Batch commit reported success but documents not found! Attempting direct creation as fallback...`
669
+ );
670
+
671
+ // Fallback to direct creation if batch worked but docs aren't there
672
+ const fallbackPromises = createdInstances.map(
673
+ async ({ ref, data }) => {
674
+ try {
675
+ await ref.set(data);
676
+ Logger.info(
677
+ `[AggService] Fallback direct creation success for ${ref.id}`
678
+ );
679
+ return true;
680
+ } catch (fallbackError) {
681
+ Logger.error(
682
+ `[AggService] Fallback direct creation failed for ${ref.id}:`,
683
+ fallbackError
684
+ );
685
+ return false;
686
+ }
687
+ }
688
+ );
689
+
690
+ const fallbackResults = await Promise.allSettled(
691
+ fallbackPromises
692
+ );
693
+ const successCount = fallbackResults.filter(
694
+ (r) => r.status === "fulfilled" && r.value === true
695
+ ).length;
696
+
697
+ if (successCount > 0) {
698
+ Logger.info(
699
+ `[AggService] Fallback mechanism created ${successCount} out of ${createdInstances.length} requirements`
700
+ );
701
+ } else {
702
+ Logger.error(
703
+ `[AggService] Both batch and fallback mechanisms failed to create requirements`
704
+ );
705
+ throw new Error(
706
+ "Failed to create patient requirements through both batch and direct methods"
707
+ );
708
+ }
709
+ } else {
710
+ Logger.info(
711
+ `[AggService] Verification confirmed ${verifySnapshot.size} requirement documents created`
712
+ );
713
+ }
714
+ } catch (verifyError) {
715
+ Logger.error(
716
+ `[AggService] Error during verification of created requirements:`,
717
+ verifyError
718
+ );
719
+ }
648
720
  } catch (commitError) {
649
721
  Logger.error(
650
722
  `[AggService] Error committing batch for PRE_APPOINTMENT requirement instances for appointment ${appointment.id}:`,
651
723
  commitError
652
724
  );
653
- throw commitError; // Re-throw to ensure the caller knows there was a problem
725
+
726
+ // Try direct creation as fallback
727
+ Logger.info(`[AggService] Attempting direct creation as fallback...`);
728
+ const fallbackPromises = createdInstances.map(
729
+ async ({ ref, data }) => {
730
+ try {
731
+ await ref.set(data);
732
+ Logger.info(
733
+ `[AggService] Fallback direct creation success for ${ref.id}`
734
+ );
735
+ return true;
736
+ } catch (fallbackError) {
737
+ Logger.error(
738
+ `[AggService] Fallback direct creation failed for ${ref.id}:`,
739
+ fallbackError
740
+ );
741
+ return false;
742
+ }
743
+ }
744
+ );
745
+
746
+ const fallbackResults = await Promise.allSettled(fallbackPromises);
747
+ const successCount = fallbackResults.filter(
748
+ (r) => r.status === "fulfilled" && r.value === true
749
+ ).length;
750
+
751
+ if (successCount > 0) {
752
+ Logger.info(
753
+ `[AggService] Fallback mechanism created ${successCount} out of ${createdInstances.length} requirements`
754
+ );
755
+ } else {
756
+ Logger.error(
757
+ `[AggService] Both batch and fallback mechanisms failed to create requirements`
758
+ );
759
+ throw new Error(
760
+ "Failed to create patient requirements through both batch and direct methods"
761
+ );
762
+ }
654
763
  }
655
764
  } else {
656
765
  Logger.info(