@blackcode_sa/metaestetics-api 1.12.47 → 1.12.48

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.
package/dist/index.js CHANGED
@@ -16493,20 +16493,29 @@ var ProcedureService = class extends BaseService {
16493
16493
  async createProcedure(data) {
16494
16494
  var _a, _b, _c;
16495
16495
  const validatedData = createProcedureSchema.parse(data);
16496
- if (!validatedData.productId) {
16497
- throw new Error("productId is required for regular procedures. Use createConsultationProcedure for product-free procedures.");
16498
- }
16496
+ const isProductFree = !validatedData.productId;
16499
16497
  const procedureId = this.generateId();
16500
- const [category, subcategory, technology, product] = await Promise.all([
16498
+ const baseEntitiesPromises = [
16501
16499
  this.categoryService.getById(validatedData.categoryId),
16502
16500
  this.subcategoryService.getById(validatedData.categoryId, validatedData.subcategoryId),
16503
- this.technologyService.getById(validatedData.technologyId),
16504
- this.productService.getById(validatedData.technologyId, validatedData.productId)
16505
- // Safe: validated above
16506
- ]);
16507
- if (!category || !subcategory || !technology || !product) {
16501
+ this.technologyService.getById(validatedData.technologyId)
16502
+ ];
16503
+ if (!isProductFree) {
16504
+ baseEntitiesPromises.push(
16505
+ this.productService.getById(validatedData.technologyId, validatedData.productId)
16506
+ );
16507
+ }
16508
+ const results = await Promise.all(baseEntitiesPromises);
16509
+ const category = results[0];
16510
+ const subcategory = results[1];
16511
+ const technology = results[2];
16512
+ const product = isProductFree ? void 0 : results[3] || void 0;
16513
+ if (!category || !subcategory || !technology) {
16508
16514
  throw new Error("One or more required base entities not found");
16509
16515
  }
16516
+ if (!isProductFree && !product) {
16517
+ throw new Error("Product not found for regular procedure");
16518
+ }
16510
16519
  const clinicRef = (0, import_firestore55.doc)(this.db, CLINICS_COLLECTION, validatedData.clinicBranchId);
16511
16520
  const clinicSnapshot = await (0, import_firestore55.getDoc)(clinicRef);
16512
16521
  if (!clinicSnapshot.exists()) {
@@ -16611,23 +16620,35 @@ var ProcedureService = class extends BaseService {
16611
16620
  if (!practitionerIds || practitionerIds.length === 0) {
16612
16621
  throw new Error("Practitioner IDs array cannot be empty.");
16613
16622
  }
16614
- if (!baseData.productId) {
16615
- throw new Error("productId is required for regular procedures. Use createConsultationProcedure for product-free procedures.");
16616
- }
16623
+ const isProductFree = !baseData.productId;
16617
16624
  const validationData = { ...baseData, practitionerId: practitionerIds[0] };
16618
16625
  const validatedData = createProcedureSchema.parse(validationData);
16619
- const [category, subcategory, technology, product, clinicSnapshot] = await Promise.all([
16626
+ const baseEntitiesPromises = [
16620
16627
  this.categoryService.getById(validatedData.categoryId),
16621
16628
  this.subcategoryService.getById(validatedData.categoryId, validatedData.subcategoryId),
16622
- this.technologyService.getById(validatedData.technologyId),
16623
- this.productService.getById(validatedData.technologyId, validatedData.productId),
16624
- // Safe: validated above
16625
- (0, import_firestore55.getDoc)((0, import_firestore55.doc)(this.db, CLINICS_COLLECTION, validatedData.clinicBranchId))
16629
+ this.technologyService.getById(validatedData.technologyId)
16630
+ ];
16631
+ if (!isProductFree) {
16632
+ baseEntitiesPromises.push(
16633
+ this.productService.getById(validatedData.technologyId, validatedData.productId)
16634
+ );
16635
+ }
16636
+ const clinicSnapshotPromise = (0, import_firestore55.getDoc)((0, import_firestore55.doc)(this.db, CLINICS_COLLECTION, validatedData.clinicBranchId));
16637
+ const [baseResults, clinicSnapshot] = await Promise.all([
16638
+ Promise.all(baseEntitiesPromises),
16639
+ clinicSnapshotPromise
16626
16640
  ]);
16627
- if (!category || !subcategory || !technology || !product) {
16641
+ const category = baseResults[0];
16642
+ const subcategory = baseResults[1];
16643
+ const technology = baseResults[2];
16644
+ const product = isProductFree ? void 0 : baseResults[3] || void 0;
16645
+ if (!category || !subcategory || !technology) {
16628
16646
  throw new Error("One or more required base entities not found");
16629
16647
  }
16630
- if (!clinicSnapshot.exists()) {
16648
+ if (!isProductFree && !product) {
16649
+ throw new Error("Product not found for regular procedure");
16650
+ }
16651
+ if (!clinicSnapshot || !clinicSnapshot.exists()) {
16631
16652
  throw new Error(`Clinic with ID ${validatedData.clinicBranchId} not found`);
16632
16653
  }
16633
16654
  const clinic = clinicSnapshot.data();
package/dist/index.mjs CHANGED
@@ -16741,20 +16741,29 @@ var ProcedureService = class extends BaseService {
16741
16741
  async createProcedure(data) {
16742
16742
  var _a, _b, _c;
16743
16743
  const validatedData = createProcedureSchema.parse(data);
16744
- if (!validatedData.productId) {
16745
- throw new Error("productId is required for regular procedures. Use createConsultationProcedure for product-free procedures.");
16746
- }
16744
+ const isProductFree = !validatedData.productId;
16747
16745
  const procedureId = this.generateId();
16748
- const [category, subcategory, technology, product] = await Promise.all([
16746
+ const baseEntitiesPromises = [
16749
16747
  this.categoryService.getById(validatedData.categoryId),
16750
16748
  this.subcategoryService.getById(validatedData.categoryId, validatedData.subcategoryId),
16751
- this.technologyService.getById(validatedData.technologyId),
16752
- this.productService.getById(validatedData.technologyId, validatedData.productId)
16753
- // Safe: validated above
16754
- ]);
16755
- if (!category || !subcategory || !technology || !product) {
16749
+ this.technologyService.getById(validatedData.technologyId)
16750
+ ];
16751
+ if (!isProductFree) {
16752
+ baseEntitiesPromises.push(
16753
+ this.productService.getById(validatedData.technologyId, validatedData.productId)
16754
+ );
16755
+ }
16756
+ const results = await Promise.all(baseEntitiesPromises);
16757
+ const category = results[0];
16758
+ const subcategory = results[1];
16759
+ const technology = results[2];
16760
+ const product = isProductFree ? void 0 : results[3] || void 0;
16761
+ if (!category || !subcategory || !technology) {
16756
16762
  throw new Error("One or more required base entities not found");
16757
16763
  }
16764
+ if (!isProductFree && !product) {
16765
+ throw new Error("Product not found for regular procedure");
16766
+ }
16758
16767
  const clinicRef = doc36(this.db, CLINICS_COLLECTION, validatedData.clinicBranchId);
16759
16768
  const clinicSnapshot = await getDoc37(clinicRef);
16760
16769
  if (!clinicSnapshot.exists()) {
@@ -16859,23 +16868,35 @@ var ProcedureService = class extends BaseService {
16859
16868
  if (!practitionerIds || practitionerIds.length === 0) {
16860
16869
  throw new Error("Practitioner IDs array cannot be empty.");
16861
16870
  }
16862
- if (!baseData.productId) {
16863
- throw new Error("productId is required for regular procedures. Use createConsultationProcedure for product-free procedures.");
16864
- }
16871
+ const isProductFree = !baseData.productId;
16865
16872
  const validationData = { ...baseData, practitionerId: practitionerIds[0] };
16866
16873
  const validatedData = createProcedureSchema.parse(validationData);
16867
- const [category, subcategory, technology, product, clinicSnapshot] = await Promise.all([
16874
+ const baseEntitiesPromises = [
16868
16875
  this.categoryService.getById(validatedData.categoryId),
16869
16876
  this.subcategoryService.getById(validatedData.categoryId, validatedData.subcategoryId),
16870
- this.technologyService.getById(validatedData.technologyId),
16871
- this.productService.getById(validatedData.technologyId, validatedData.productId),
16872
- // Safe: validated above
16873
- getDoc37(doc36(this.db, CLINICS_COLLECTION, validatedData.clinicBranchId))
16877
+ this.technologyService.getById(validatedData.technologyId)
16878
+ ];
16879
+ if (!isProductFree) {
16880
+ baseEntitiesPromises.push(
16881
+ this.productService.getById(validatedData.technologyId, validatedData.productId)
16882
+ );
16883
+ }
16884
+ const clinicSnapshotPromise = getDoc37(doc36(this.db, CLINICS_COLLECTION, validatedData.clinicBranchId));
16885
+ const [baseResults, clinicSnapshot] = await Promise.all([
16886
+ Promise.all(baseEntitiesPromises),
16887
+ clinicSnapshotPromise
16874
16888
  ]);
16875
- if (!category || !subcategory || !technology || !product) {
16889
+ const category = baseResults[0];
16890
+ const subcategory = baseResults[1];
16891
+ const technology = baseResults[2];
16892
+ const product = isProductFree ? void 0 : baseResults[3] || void 0;
16893
+ if (!category || !subcategory || !technology) {
16876
16894
  throw new Error("One or more required base entities not found");
16877
16895
  }
16878
- if (!clinicSnapshot.exists()) {
16896
+ if (!isProductFree && !product) {
16897
+ throw new Error("Product not found for regular procedure");
16898
+ }
16899
+ if (!clinicSnapshot || !clinicSnapshot.exists()) {
16879
16900
  throw new Error(`Clinic with ID ${validatedData.clinicBranchId} not found`);
16880
16901
  }
16881
16902
  const clinic = clinicSnapshot.data();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@blackcode_sa/metaestetics-api",
3
3
  "private": false,
4
- "version": "1.12.47",
4
+ "version": "1.12.48",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -194,26 +194,41 @@ export class ProcedureService extends BaseService {
194
194
  async createProcedure(data: CreateProcedureData): Promise<Procedure> {
195
195
  const validatedData = createProcedureSchema.parse(data);
196
196
 
197
- // Validate that productId is provided (regular procedures require products)
198
- if (!validatedData.productId) {
199
- throw new Error('productId is required for regular procedures. Use createConsultationProcedure for product-free procedures.');
200
- }
197
+ // Check if this is a product-free procedure (e.g., free consultation)
198
+ const isProductFree = !validatedData.productId;
201
199
 
202
200
  // Generate procedure ID first so we can use it for media uploads
203
201
  const procedureId = this.generateId();
204
202
 
205
- // Get references to related entities (Category, Subcategory, Technology, Product)
206
- const [category, subcategory, technology, product] = await Promise.all([
203
+ // Get references to related entities (Category, Subcategory, Technology, and optionally Product)
204
+ const baseEntitiesPromises = [
207
205
  this.categoryService.getById(validatedData.categoryId),
208
206
  this.subcategoryService.getById(validatedData.categoryId, validatedData.subcategoryId),
209
207
  this.technologyService.getById(validatedData.technologyId),
210
- this.productService.getById(validatedData.technologyId, validatedData.productId!), // Safe: validated above
211
- ]);
208
+ ];
209
+
210
+ // Only fetch product if productId is provided
211
+ if (!isProductFree) {
212
+ baseEntitiesPromises.push(
213
+ this.productService.getById(validatedData.technologyId, validatedData.productId!)
214
+ );
215
+ }
212
216
 
213
- if (!category || !subcategory || !technology || !product) {
217
+ const results = await Promise.all(baseEntitiesPromises);
218
+ const category = results[0] as Category | null;
219
+ const subcategory = results[1] as Subcategory | null;
220
+ const technology = results[2] as Technology | null;
221
+ const product = isProductFree ? undefined : ((results[3] as Product | null) || undefined);
222
+
223
+ if (!category || !subcategory || !technology) {
214
224
  throw new Error('One or more required base entities not found');
215
225
  }
216
226
 
227
+ // For regular procedures, validate product exists
228
+ if (!isProductFree && !product) {
229
+ throw new Error('Product not found for regular procedure');
230
+ }
231
+
217
232
  // Get clinic and practitioner information for aggregation
218
233
  const clinicRef = doc(this.db, CLINICS_COLLECTION, validatedData.clinicBranchId);
219
234
  const clinicSnapshot = await getDoc(clinicRef);
@@ -343,28 +358,50 @@ export class ProcedureService extends BaseService {
343
358
  throw new Error('Practitioner IDs array cannot be empty.');
344
359
  }
345
360
 
346
- // Validate that productId is provided (regular procedures require products)
347
- if (!baseData.productId) {
348
- throw new Error('productId is required for regular procedures. Use createConsultationProcedure for product-free procedures.');
349
- }
361
+ // Check if this is a product-free procedure
362
+ const isProductFree = !baseData.productId;
350
363
 
351
364
  // Add a dummy practitionerId for the validation schema to pass
352
365
  const validationData = { ...baseData, practitionerId: practitionerIds[0] };
353
366
  const validatedData = createProcedureSchema.parse(validationData);
354
367
 
355
368
  // 2. Fetch common data once to avoid redundant reads
356
- const [category, subcategory, technology, product, clinicSnapshot] = await Promise.all([
369
+ const baseEntitiesPromises = [
357
370
  this.categoryService.getById(validatedData.categoryId),
358
371
  this.subcategoryService.getById(validatedData.categoryId, validatedData.subcategoryId),
359
372
  this.technologyService.getById(validatedData.technologyId),
360
- this.productService.getById(validatedData.technologyId, validatedData.productId!), // Safe: validated above
361
- getDoc(doc(this.db, CLINICS_COLLECTION, validatedData.clinicBranchId)),
373
+ ];
374
+
375
+ // Only fetch product if productId is provided
376
+ if (!isProductFree) {
377
+ baseEntitiesPromises.push(
378
+ this.productService.getById(validatedData.technologyId, validatedData.productId!)
379
+ );
380
+ }
381
+
382
+ // Fetch clinic separately to maintain type safety
383
+ const clinicSnapshotPromise = getDoc(doc(this.db, CLINICS_COLLECTION, validatedData.clinicBranchId));
384
+
385
+ const [baseResults, clinicSnapshot] = await Promise.all([
386
+ Promise.all(baseEntitiesPromises),
387
+ clinicSnapshotPromise
362
388
  ]);
389
+
390
+ const category = baseResults[0] as Category | null;
391
+ const subcategory = baseResults[1] as Subcategory | null;
392
+ const technology = baseResults[2] as Technology | null;
393
+ const product = isProductFree ? undefined : ((baseResults[3] as Product | null) || undefined);
363
394
 
364
- if (!category || !subcategory || !technology || !product) {
395
+ if (!category || !subcategory || !technology) {
365
396
  throw new Error('One or more required base entities not found');
366
397
  }
367
- if (!clinicSnapshot.exists()) {
398
+
399
+ // For regular procedures, validate product exists
400
+ if (!isProductFree && !product) {
401
+ throw new Error('Product not found for regular procedure');
402
+ }
403
+
404
+ if (!clinicSnapshot || !clinicSnapshot.exists()) {
368
405
  throw new Error(`Clinic with ID ${validatedData.clinicBranchId} not found`);
369
406
  }
370
407
  const clinic = clinicSnapshot.data() as Clinic;