@blackcode_sa/metaestetics-api 1.6.19 → 1.6.21

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) => {
@@ -3417,6 +3418,8 @@ var AppointmentAggregationService = class {
3417
3418
  return instructionObject;
3418
3419
  });
3419
3420
  const newInstanceData = {
3421
+ id: newInstanceRef.id,
3422
+ // Add the ID to the document data
3420
3423
  patientId: appointment.patientId,
3421
3424
  appointmentId: appointment.id,
3422
3425
  originalRequirementId: template.id,
@@ -3441,6 +3444,10 @@ var AppointmentAggregationService = class {
3441
3444
  })}`
3442
3445
  );
3443
3446
  batch.set(newInstanceRef, newInstanceData);
3447
+ createdInstances.push({
3448
+ ref: newInstanceRef,
3449
+ data: newInstanceData
3450
+ });
3444
3451
  instancesCreatedCount++;
3445
3452
  Logger.debug(
3446
3453
  `[AggService] Added PatientRequirementInstance ${newInstanceRef.id} to batch for template ${template.id}.`
@@ -3452,12 +3459,97 @@ var AppointmentAggregationService = class {
3452
3459
  Logger.info(
3453
3460
  `[AggService] Successfully created ${instancesCreatedCount} PRE_APPOINTMENT requirement instances for appointment ${appointment.id}.`
3454
3461
  );
3462
+ try {
3463
+ const verifySnapshot = await this.db.collection(PATIENTS_COLLECTION).doc(appointment.patientId).collection(PATIENT_REQUIREMENTS_SUBCOLLECTION_NAME).where("appointmentId", "==", appointment.id).get();
3464
+ if (verifySnapshot.empty) {
3465
+ Logger.warn(
3466
+ `[AggService] Batch commit reported success but documents not found! Attempting direct creation as fallback...`
3467
+ );
3468
+ const fallbackPromises = createdInstances.map(
3469
+ async ({ ref, data }) => {
3470
+ try {
3471
+ await ref.set(data);
3472
+ Logger.info(
3473
+ `[AggService] Fallback direct creation success for ${ref.id}`
3474
+ );
3475
+ return true;
3476
+ } catch (fallbackError) {
3477
+ Logger.error(
3478
+ `[AggService] Fallback direct creation failed for ${ref.id}:`,
3479
+ fallbackError
3480
+ );
3481
+ return false;
3482
+ }
3483
+ }
3484
+ );
3485
+ const fallbackResults = await Promise.allSettled(
3486
+ fallbackPromises
3487
+ );
3488
+ const successCount = fallbackResults.filter(
3489
+ (r) => r.status === "fulfilled" && r.value === true
3490
+ ).length;
3491
+ if (successCount > 0) {
3492
+ Logger.info(
3493
+ `[AggService] Fallback mechanism created ${successCount} out of ${createdInstances.length} requirements`
3494
+ );
3495
+ } else {
3496
+ Logger.error(
3497
+ `[AggService] Both batch and fallback mechanisms failed to create requirements`
3498
+ );
3499
+ throw new Error(
3500
+ "Failed to create patient requirements through both batch and direct methods"
3501
+ );
3502
+ }
3503
+ } else {
3504
+ Logger.info(
3505
+ `[AggService] Verification confirmed ${verifySnapshot.size} requirement documents created`
3506
+ );
3507
+ }
3508
+ } catch (verifyError) {
3509
+ Logger.error(
3510
+ `[AggService] Error during verification of created requirements:`,
3511
+ verifyError
3512
+ );
3513
+ }
3455
3514
  } catch (commitError) {
3456
3515
  Logger.error(
3457
3516
  `[AggService] Error committing batch for PRE_APPOINTMENT requirement instances for appointment ${appointment.id}:`,
3458
3517
  commitError
3459
3518
  );
3460
- throw commitError;
3519
+ Logger.info(`[AggService] Attempting direct creation as fallback...`);
3520
+ const fallbackPromises = createdInstances.map(
3521
+ async ({ ref, data }) => {
3522
+ try {
3523
+ await ref.set(data);
3524
+ Logger.info(
3525
+ `[AggService] Fallback direct creation success for ${ref.id}`
3526
+ );
3527
+ return true;
3528
+ } catch (fallbackError) {
3529
+ Logger.error(
3530
+ `[AggService] Fallback direct creation failed for ${ref.id}:`,
3531
+ fallbackError
3532
+ );
3533
+ return false;
3534
+ }
3535
+ }
3536
+ );
3537
+ const fallbackResults = await Promise.allSettled(fallbackPromises);
3538
+ const successCount = fallbackResults.filter(
3539
+ (r) => r.status === "fulfilled" && r.value === true
3540
+ ).length;
3541
+ if (successCount > 0) {
3542
+ Logger.info(
3543
+ `[AggService] Fallback mechanism created ${successCount} out of ${createdInstances.length} requirements`
3544
+ );
3545
+ } else {
3546
+ Logger.error(
3547
+ `[AggService] Both batch and fallback mechanisms failed to create requirements`
3548
+ );
3549
+ throw new Error(
3550
+ "Failed to create patient requirements through both batch and direct methods"
3551
+ );
3552
+ }
3461
3553
  }
3462
3554
  } else {
3463
3555
  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) => {
@@ -3362,6 +3363,8 @@ var AppointmentAggregationService = class {
3362
3363
  return instructionObject;
3363
3364
  });
3364
3365
  const newInstanceData = {
3366
+ id: newInstanceRef.id,
3367
+ // Add the ID to the document data
3365
3368
  patientId: appointment.patientId,
3366
3369
  appointmentId: appointment.id,
3367
3370
  originalRequirementId: template.id,
@@ -3386,6 +3389,10 @@ var AppointmentAggregationService = class {
3386
3389
  })}`
3387
3390
  );
3388
3391
  batch.set(newInstanceRef, newInstanceData);
3392
+ createdInstances.push({
3393
+ ref: newInstanceRef,
3394
+ data: newInstanceData
3395
+ });
3389
3396
  instancesCreatedCount++;
3390
3397
  Logger.debug(
3391
3398
  `[AggService] Added PatientRequirementInstance ${newInstanceRef.id} to batch for template ${template.id}.`
@@ -3397,12 +3404,97 @@ var AppointmentAggregationService = class {
3397
3404
  Logger.info(
3398
3405
  `[AggService] Successfully created ${instancesCreatedCount} PRE_APPOINTMENT requirement instances for appointment ${appointment.id}.`
3399
3406
  );
3407
+ try {
3408
+ const verifySnapshot = await this.db.collection(PATIENTS_COLLECTION).doc(appointment.patientId).collection(PATIENT_REQUIREMENTS_SUBCOLLECTION_NAME).where("appointmentId", "==", appointment.id).get();
3409
+ if (verifySnapshot.empty) {
3410
+ Logger.warn(
3411
+ `[AggService] Batch commit reported success but documents not found! Attempting direct creation as fallback...`
3412
+ );
3413
+ const fallbackPromises = createdInstances.map(
3414
+ async ({ ref, data }) => {
3415
+ try {
3416
+ await ref.set(data);
3417
+ Logger.info(
3418
+ `[AggService] Fallback direct creation success for ${ref.id}`
3419
+ );
3420
+ return true;
3421
+ } catch (fallbackError) {
3422
+ Logger.error(
3423
+ `[AggService] Fallback direct creation failed for ${ref.id}:`,
3424
+ fallbackError
3425
+ );
3426
+ return false;
3427
+ }
3428
+ }
3429
+ );
3430
+ const fallbackResults = await Promise.allSettled(
3431
+ fallbackPromises
3432
+ );
3433
+ const successCount = fallbackResults.filter(
3434
+ (r) => r.status === "fulfilled" && r.value === true
3435
+ ).length;
3436
+ if (successCount > 0) {
3437
+ Logger.info(
3438
+ `[AggService] Fallback mechanism created ${successCount} out of ${createdInstances.length} requirements`
3439
+ );
3440
+ } else {
3441
+ Logger.error(
3442
+ `[AggService] Both batch and fallback mechanisms failed to create requirements`
3443
+ );
3444
+ throw new Error(
3445
+ "Failed to create patient requirements through both batch and direct methods"
3446
+ );
3447
+ }
3448
+ } else {
3449
+ Logger.info(
3450
+ `[AggService] Verification confirmed ${verifySnapshot.size} requirement documents created`
3451
+ );
3452
+ }
3453
+ } catch (verifyError) {
3454
+ Logger.error(
3455
+ `[AggService] Error during verification of created requirements:`,
3456
+ verifyError
3457
+ );
3458
+ }
3400
3459
  } catch (commitError) {
3401
3460
  Logger.error(
3402
3461
  `[AggService] Error committing batch for PRE_APPOINTMENT requirement instances for appointment ${appointment.id}:`,
3403
3462
  commitError
3404
3463
  );
3405
- throw commitError;
3464
+ Logger.info(`[AggService] Attempting direct creation as fallback...`);
3465
+ const fallbackPromises = createdInstances.map(
3466
+ async ({ ref, data }) => {
3467
+ try {
3468
+ await ref.set(data);
3469
+ Logger.info(
3470
+ `[AggService] Fallback direct creation success for ${ref.id}`
3471
+ );
3472
+ return true;
3473
+ } catch (fallbackError) {
3474
+ Logger.error(
3475
+ `[AggService] Fallback direct creation failed for ${ref.id}:`,
3476
+ fallbackError
3477
+ );
3478
+ return false;
3479
+ }
3480
+ }
3481
+ );
3482
+ const fallbackResults = await Promise.allSettled(fallbackPromises);
3483
+ const successCount = fallbackResults.filter(
3484
+ (r) => r.status === "fulfilled" && r.value === true
3485
+ ).length;
3486
+ if (successCount > 0) {
3487
+ Logger.info(
3488
+ `[AggService] Fallback mechanism created ${successCount} out of ${createdInstances.length} requirements`
3489
+ );
3490
+ } else {
3491
+ Logger.error(
3492
+ `[AggService] Both batch and fallback mechanisms failed to create requirements`
3493
+ );
3494
+ throw new Error(
3495
+ "Failed to create patient requirements through both batch and direct methods"
3496
+ );
3497
+ }
3406
3498
  }
3407
3499
  } else {
3408
3500
  Logger.info(