@blackcode_sa/metaestetics-api 1.12.40 → 1.12.42

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
@@ -2337,7 +2337,7 @@ var AppointmentService = class extends BaseService {
2337
2337
  * @returns The updated appointment
2338
2338
  */
2339
2339
  async updateAppointment(appointmentId, data) {
2340
- var _a, _b;
2340
+ var _a, _b, _c, _d;
2341
2341
  try {
2342
2342
  console.log(`[APPOINTMENT_SERVICE] Updating appointment with ID: ${appointmentId}`);
2343
2343
  if ((_a = data.metadata) == null ? void 0 : _a.zonePhotos) {
@@ -2360,18 +2360,34 @@ var AppointmentService = class extends BaseService {
2360
2360
  }
2361
2361
  data.metadata.zonePhotos = migratedZonePhotos;
2362
2362
  }
2363
- if (((_b = data.metadata) == null ? void 0 : _b.recommendedProcedures) && Array.isArray(data.metadata.recommendedProcedures)) {
2364
- const validRecommendations = data.metadata.recommendedProcedures.filter(
2365
- (rec) => rec.note && typeof rec.note === "string" && rec.note.trim().length > 0
2366
- );
2363
+ console.log(
2364
+ "[APPOINTMENT_SERVICE] \u{1F50D} BEFORE CLEANUP - recommendedProcedures:",
2365
+ JSON.stringify((_b = data.metadata) == null ? void 0 : _b.recommendedProcedures, null, 2)
2366
+ );
2367
+ if (((_c = data.metadata) == null ? void 0 : _c.recommendedProcedures) && Array.isArray(data.metadata.recommendedProcedures)) {
2368
+ const validRecommendations = data.metadata.recommendedProcedures.filter((rec) => {
2369
+ const isValid = rec.note && typeof rec.note === "string" && rec.note.trim().length > 0;
2370
+ if (!isValid) {
2371
+ console.log("[APPOINTMENT_SERVICE] \u274C INVALID recommendation found:", rec);
2372
+ }
2373
+ return isValid;
2374
+ });
2367
2375
  if (validRecommendations.length !== data.metadata.recommendedProcedures.length) {
2368
2376
  console.log(
2369
- `[APPOINTMENT_SERVICE] Removing ${data.metadata.recommendedProcedures.length - validRecommendations.length} invalid recommended procedures with empty notes`
2377
+ `[APPOINTMENT_SERVICE] \u{1F9F9} Removing ${data.metadata.recommendedProcedures.length - validRecommendations.length} invalid recommended procedures with empty notes`
2370
2378
  );
2371
2379
  data.metadata.recommendedProcedures = validRecommendations;
2380
+ } else {
2381
+ console.log("[APPOINTMENT_SERVICE] \u2705 All recommendedProcedures are valid");
2372
2382
  }
2373
2383
  }
2384
+ console.log(
2385
+ "[APPOINTMENT_SERVICE] \u{1F50D} AFTER CLEANUP - recommendedProcedures:",
2386
+ JSON.stringify((_d = data.metadata) == null ? void 0 : _d.recommendedProcedures, null, 2)
2387
+ );
2388
+ console.log("[APPOINTMENT_SERVICE] \u{1F50D} Starting Zod validation...");
2374
2389
  const validatedData = await updateAppointmentSchema.parseAsync(data);
2390
+ console.log("[APPOINTMENT_SERVICE] \u2705 Zod validation passed!");
2375
2391
  const updatedAppointment = await updateAppointmentUtil(this.db, appointmentId, validatedData);
2376
2392
  console.log(`[APPOINTMENT_SERVICE] Appointment ${appointmentId} updated successfully`);
2377
2393
  return updatedAppointment;
@@ -18631,6 +18647,11 @@ var SubcategoryService = class extends BaseService {
18631
18647
 
18632
18648
  // src/backoffice/services/technology.service.ts
18633
18649
  var import_firestore61 = require("firebase/firestore");
18650
+
18651
+ // src/backoffice/types/product.types.ts
18652
+ var PRODUCTS_COLLECTION = "products";
18653
+
18654
+ // src/backoffice/services/technology.service.ts
18634
18655
  var DEFAULT_CERTIFICATION_REQUIREMENT = {
18635
18656
  minimumLevel: "aesthetician" /* AESTHETICIAN */,
18636
18657
  requiredSpecialties: []
@@ -18792,7 +18813,18 @@ var TechnologyService = class extends BaseService {
18792
18813
  });
18793
18814
  updateData.updatedAt = /* @__PURE__ */ new Date();
18794
18815
  const docRef = (0, import_firestore61.doc)(this.technologiesRef, id);
18816
+ const beforeTech = await this.getById(id);
18795
18817
  await (0, import_firestore61.updateDoc)(docRef, updateData);
18818
+ const categoryChanged = beforeTech && updateData.categoryId && beforeTech.categoryId !== updateData.categoryId;
18819
+ const subcategoryChanged = beforeTech && updateData.subcategoryId && beforeTech.subcategoryId !== updateData.subcategoryId;
18820
+ const nameChanged = beforeTech && updateData.name && beforeTech.name !== updateData.name;
18821
+ if (categoryChanged || subcategoryChanged || nameChanged) {
18822
+ await this.updateProductsInSubcollection(id, {
18823
+ categoryId: updateData.categoryId,
18824
+ subcategoryId: updateData.subcategoryId,
18825
+ technologyName: updateData.name
18826
+ });
18827
+ }
18796
18828
  return this.getById(id);
18797
18829
  }
18798
18830
  /**
@@ -19228,18 +19260,139 @@ var TechnologyService = class extends BaseService {
19228
19260
  })
19229
19261
  );
19230
19262
  }
19263
+ // ==========================================
19264
+ // NEW METHODS: Product assignment management
19265
+ // ==========================================
19266
+ /**
19267
+ * Assigns multiple products to a technology
19268
+ * Updates each product's assignedTechnologyIds array
19269
+ */
19270
+ async assignProducts(technologyId, productIds) {
19271
+ const batch = (0, import_firestore61.writeBatch)(this.db);
19272
+ for (const productId of productIds) {
19273
+ const productRef = (0, import_firestore61.doc)(this.db, PRODUCTS_COLLECTION, productId);
19274
+ batch.update(productRef, {
19275
+ assignedTechnologyIds: (0, import_firestore61.arrayUnion)(technologyId),
19276
+ updatedAt: /* @__PURE__ */ new Date()
19277
+ });
19278
+ }
19279
+ await batch.commit();
19280
+ }
19281
+ /**
19282
+ * Unassigns multiple products from a technology
19283
+ * Updates each product's assignedTechnologyIds array
19284
+ */
19285
+ async unassignProducts(technologyId, productIds) {
19286
+ const batch = (0, import_firestore61.writeBatch)(this.db);
19287
+ for (const productId of productIds) {
19288
+ const productRef = (0, import_firestore61.doc)(this.db, PRODUCTS_COLLECTION, productId);
19289
+ batch.update(productRef, {
19290
+ assignedTechnologyIds: (0, import_firestore61.arrayRemove)(technologyId),
19291
+ updatedAt: /* @__PURE__ */ new Date()
19292
+ });
19293
+ }
19294
+ await batch.commit();
19295
+ }
19296
+ /**
19297
+ * Gets products assigned to a specific technology
19298
+ * Reads from top-level collection for immediate consistency (Cloud Functions may lag)
19299
+ */
19300
+ async getAssignedProducts(technologyId) {
19301
+ const q = (0, import_firestore61.query)(
19302
+ (0, import_firestore61.collection)(this.db, PRODUCTS_COLLECTION),
19303
+ (0, import_firestore61.where)("assignedTechnologyIds", "array-contains", technologyId),
19304
+ (0, import_firestore61.where)("isActive", "==", true),
19305
+ (0, import_firestore61.orderBy)("name")
19306
+ );
19307
+ const snapshot = await (0, import_firestore61.getDocs)(q);
19308
+ return snapshot.docs.map(
19309
+ (doc44) => ({
19310
+ id: doc44.id,
19311
+ ...doc44.data()
19312
+ })
19313
+ );
19314
+ }
19315
+ /**
19316
+ * Gets products NOT assigned to a specific technology
19317
+ */
19318
+ async getUnassignedProducts(technologyId) {
19319
+ const q = (0, import_firestore61.query)(
19320
+ (0, import_firestore61.collection)(this.db, PRODUCTS_COLLECTION),
19321
+ (0, import_firestore61.where)("isActive", "==", true),
19322
+ (0, import_firestore61.orderBy)("name")
19323
+ );
19324
+ const snapshot = await (0, import_firestore61.getDocs)(q);
19325
+ const allProducts = snapshot.docs.map(
19326
+ (doc44) => ({
19327
+ id: doc44.id,
19328
+ ...doc44.data()
19329
+ })
19330
+ );
19331
+ return allProducts.filter(
19332
+ (product) => {
19333
+ var _a;
19334
+ return !((_a = product.assignedTechnologyIds) == null ? void 0 : _a.includes(technologyId));
19335
+ }
19336
+ );
19337
+ }
19338
+ /**
19339
+ * Gets product assignment statistics for a technology
19340
+ */
19341
+ async getProductStats(technologyId) {
19342
+ const products = await this.getAssignedProducts(technologyId);
19343
+ const byBrand = {};
19344
+ products.forEach((product) => {
19345
+ byBrand[product.brandName] = (byBrand[product.brandName] || 0) + 1;
19346
+ });
19347
+ return {
19348
+ totalAssigned: products.length,
19349
+ byBrand
19350
+ };
19351
+ }
19352
+ /**
19353
+ * Updates products in technology subcollection when technology metadata changes
19354
+ * @param technologyId - ID of the technology
19355
+ * @param updates - Fields to update (categoryId, subcategoryId, technologyName)
19356
+ */
19357
+ async updateProductsInSubcollection(technologyId, updates) {
19358
+ const productsRef = (0, import_firestore61.collection)(this.db, TECHNOLOGIES_COLLECTION, technologyId, PRODUCTS_COLLECTION);
19359
+ const productsSnapshot = await (0, import_firestore61.getDocs)(productsRef);
19360
+ if (productsSnapshot.empty) {
19361
+ return;
19362
+ }
19363
+ const batch = (0, import_firestore61.writeBatch)(this.db);
19364
+ for (const productDoc of productsSnapshot.docs) {
19365
+ const productRef = productDoc.ref;
19366
+ const updateFields = {};
19367
+ if (updates.categoryId !== void 0) {
19368
+ updateFields.categoryId = updates.categoryId;
19369
+ }
19370
+ if (updates.subcategoryId !== void 0) {
19371
+ updateFields.subcategoryId = updates.subcategoryId;
19372
+ }
19373
+ if (updates.technologyName !== void 0) {
19374
+ updateFields.technologyName = updates.technologyName;
19375
+ }
19376
+ if (Object.keys(updateFields).length > 0) {
19377
+ batch.update(productRef, updateFields);
19378
+ }
19379
+ }
19380
+ await batch.commit();
19381
+ }
19231
19382
  };
19232
19383
 
19233
19384
  // src/backoffice/services/product.service.ts
19234
19385
  var import_firestore62 = require("firebase/firestore");
19235
-
19236
- // src/backoffice/types/product.types.ts
19237
- var PRODUCTS_COLLECTION = "products";
19238
-
19239
- // src/backoffice/services/product.service.ts
19240
19386
  var ProductService = class extends BaseService {
19241
19387
  /**
19242
- * Gets reference to products collection under a technology
19388
+ * Gets reference to top-level products collection (source of truth)
19389
+ * @returns Firestore collection reference
19390
+ */
19391
+ getTopLevelProductsRef() {
19392
+ return (0, import_firestore62.collection)(this.db, PRODUCTS_COLLECTION);
19393
+ }
19394
+ /**
19395
+ * Gets reference to products collection under a technology (backward compatibility)
19243
19396
  * @param technologyId - ID of the technology
19244
19397
  * @returns Firestore collection reference
19245
19398
  */
@@ -19255,6 +19408,7 @@ var ProductService = class extends BaseService {
19255
19408
  ...product,
19256
19409
  brandId,
19257
19410
  technologyId,
19411
+ // Required for old subcollection structure
19258
19412
  createdAt: now,
19259
19413
  updatedAt: now,
19260
19414
  isActive: true
@@ -19314,30 +19468,26 @@ var ProductService = class extends BaseService {
19314
19468
  }
19315
19469
  /**
19316
19470
  * Gets counts of active products grouped by category, subcategory, and technology.
19317
- * This uses a single collectionGroup query for efficiency.
19471
+ * Queries technology subcollections which have the legacy fields synced by Cloud Functions.
19318
19472
  */
19319
19473
  async getProductCounts() {
19320
- const q = (0, import_firestore62.query)((0, import_firestore62.collectionGroup)(this.db, PRODUCTS_COLLECTION), (0, import_firestore62.where)("isActive", "==", true));
19321
- const snapshot = await (0, import_firestore62.getDocs)(q);
19322
19474
  const counts = {
19323
19475
  byCategory: {},
19324
19476
  bySubcategory: {},
19325
19477
  byTechnology: {}
19326
19478
  };
19327
- if (snapshot.empty) {
19328
- return counts;
19329
- }
19479
+ const q = (0, import_firestore62.query)((0, import_firestore62.collectionGroup)(this.db, PRODUCTS_COLLECTION), (0, import_firestore62.where)("isActive", "==", true));
19480
+ const snapshot = await (0, import_firestore62.getDocs)(q);
19330
19481
  snapshot.docs.forEach((doc44) => {
19331
19482
  const product = doc44.data();
19332
- const { categoryId, subcategoryId, technologyId } = product;
19333
- if (categoryId) {
19334
- counts.byCategory[categoryId] = (counts.byCategory[categoryId] || 0) + 1;
19483
+ if (product.categoryId) {
19484
+ counts.byCategory[product.categoryId] = (counts.byCategory[product.categoryId] || 0) + 1;
19335
19485
  }
19336
- if (subcategoryId) {
19337
- counts.bySubcategory[subcategoryId] = (counts.bySubcategory[subcategoryId] || 0) + 1;
19486
+ if (product.subcategoryId) {
19487
+ counts.bySubcategory[product.subcategoryId] = (counts.bySubcategory[product.subcategoryId] || 0) + 1;
19338
19488
  }
19339
- if (technologyId) {
19340
- counts.byTechnology[technologyId] = (counts.byTechnology[technologyId] || 0) + 1;
19489
+ if (product.technologyId) {
19490
+ counts.byTechnology[product.technologyId] = (counts.byTechnology[product.technologyId] || 0) + 1;
19341
19491
  }
19342
19492
  });
19343
19493
  return counts;
@@ -19416,6 +19566,160 @@ var ProductService = class extends BaseService {
19416
19566
  ...docSnap.data()
19417
19567
  };
19418
19568
  }
19569
+ // ==========================================
19570
+ // NEW METHODS: Top-level collection (preferred)
19571
+ // ==========================================
19572
+ /**
19573
+ * Creates a new product in the top-level collection
19574
+ */
19575
+ async createTopLevel(brandId, product, technologyIds = []) {
19576
+ const now = /* @__PURE__ */ new Date();
19577
+ const newProduct = {
19578
+ ...product,
19579
+ brandId,
19580
+ assignedTechnologyIds: technologyIds,
19581
+ createdAt: now,
19582
+ updatedAt: now,
19583
+ isActive: true
19584
+ };
19585
+ const productRef = await (0, import_firestore62.addDoc)(this.getTopLevelProductsRef(), newProduct);
19586
+ return { id: productRef.id, ...newProduct };
19587
+ }
19588
+ /**
19589
+ * Gets all products from the top-level collection
19590
+ */
19591
+ async getAllTopLevel(options) {
19592
+ const { rowsPerPage, lastVisible, brandId } = options;
19593
+ const constraints = [(0, import_firestore62.where)("isActive", "==", true), (0, import_firestore62.orderBy)("name")];
19594
+ if (brandId) {
19595
+ constraints.push((0, import_firestore62.where)("brandId", "==", brandId));
19596
+ }
19597
+ if (lastVisible) {
19598
+ constraints.push((0, import_firestore62.startAfter)(lastVisible));
19599
+ }
19600
+ constraints.push((0, import_firestore62.limit)(rowsPerPage));
19601
+ const q = (0, import_firestore62.query)(this.getTopLevelProductsRef(), ...constraints);
19602
+ const snapshot = await (0, import_firestore62.getDocs)(q);
19603
+ const products = snapshot.docs.map(
19604
+ (doc44) => ({
19605
+ id: doc44.id,
19606
+ ...doc44.data()
19607
+ })
19608
+ );
19609
+ const newLastVisible = snapshot.docs[snapshot.docs.length - 1];
19610
+ return { products, lastVisible: newLastVisible };
19611
+ }
19612
+ /**
19613
+ * Gets a product by ID from the top-level collection
19614
+ */
19615
+ async getByIdTopLevel(productId) {
19616
+ const docRef = (0, import_firestore62.doc)(this.getTopLevelProductsRef(), productId);
19617
+ const docSnap = await (0, import_firestore62.getDoc)(docRef);
19618
+ if (!docSnap.exists()) return null;
19619
+ return {
19620
+ id: docSnap.id,
19621
+ ...docSnap.data()
19622
+ };
19623
+ }
19624
+ /**
19625
+ * Updates a product in the top-level collection
19626
+ */
19627
+ async updateTopLevel(productId, product) {
19628
+ const updateData = {
19629
+ ...product,
19630
+ updatedAt: /* @__PURE__ */ new Date()
19631
+ };
19632
+ const docRef = (0, import_firestore62.doc)(this.getTopLevelProductsRef(), productId);
19633
+ await (0, import_firestore62.updateDoc)(docRef, updateData);
19634
+ return this.getByIdTopLevel(productId);
19635
+ }
19636
+ /**
19637
+ * Deletes a product from the top-level collection (soft delete)
19638
+ */
19639
+ async deleteTopLevel(productId) {
19640
+ await this.updateTopLevel(productId, {
19641
+ isActive: false
19642
+ });
19643
+ }
19644
+ /**
19645
+ * Assigns a product to a technology
19646
+ */
19647
+ async assignToTechnology(productId, technologyId) {
19648
+ const docRef = (0, import_firestore62.doc)(this.getTopLevelProductsRef(), productId);
19649
+ await (0, import_firestore62.updateDoc)(docRef, {
19650
+ assignedTechnologyIds: (0, import_firestore62.arrayUnion)(technologyId),
19651
+ updatedAt: /* @__PURE__ */ new Date()
19652
+ });
19653
+ }
19654
+ /**
19655
+ * Unassigns a product from a technology
19656
+ */
19657
+ async unassignFromTechnology(productId, technologyId) {
19658
+ const docRef = (0, import_firestore62.doc)(this.getTopLevelProductsRef(), productId);
19659
+ await (0, import_firestore62.updateDoc)(docRef, {
19660
+ assignedTechnologyIds: (0, import_firestore62.arrayRemove)(technologyId),
19661
+ updatedAt: /* @__PURE__ */ new Date()
19662
+ });
19663
+ }
19664
+ /**
19665
+ * Gets products assigned to a specific technology
19666
+ */
19667
+ async getAssignedProducts(technologyId) {
19668
+ const q = (0, import_firestore62.query)(
19669
+ this.getTopLevelProductsRef(),
19670
+ (0, import_firestore62.where)("assignedTechnologyIds", "array-contains", technologyId),
19671
+ (0, import_firestore62.where)("isActive", "==", true),
19672
+ (0, import_firestore62.orderBy)("name")
19673
+ );
19674
+ const snapshot = await (0, import_firestore62.getDocs)(q);
19675
+ return snapshot.docs.map(
19676
+ (doc44) => ({
19677
+ id: doc44.id,
19678
+ ...doc44.data()
19679
+ })
19680
+ );
19681
+ }
19682
+ /**
19683
+ * Gets products NOT assigned to a specific technology
19684
+ */
19685
+ async getUnassignedProducts(technologyId) {
19686
+ const q = (0, import_firestore62.query)(
19687
+ this.getTopLevelProductsRef(),
19688
+ (0, import_firestore62.where)("isActive", "==", true),
19689
+ (0, import_firestore62.orderBy)("name")
19690
+ );
19691
+ const snapshot = await (0, import_firestore62.getDocs)(q);
19692
+ const allProducts = snapshot.docs.map(
19693
+ (doc44) => ({
19694
+ id: doc44.id,
19695
+ ...doc44.data()
19696
+ })
19697
+ );
19698
+ return allProducts.filter(
19699
+ (product) => {
19700
+ var _a;
19701
+ return !((_a = product.assignedTechnologyIds) == null ? void 0 : _a.includes(technologyId));
19702
+ }
19703
+ );
19704
+ }
19705
+ /**
19706
+ * Gets all products for a brand (from top-level collection)
19707
+ */
19708
+ async getByBrand(brandId) {
19709
+ const q = (0, import_firestore62.query)(
19710
+ this.getTopLevelProductsRef(),
19711
+ (0, import_firestore62.where)("brandId", "==", brandId),
19712
+ (0, import_firestore62.where)("isActive", "==", true),
19713
+ (0, import_firestore62.orderBy)("name")
19714
+ );
19715
+ const snapshot = await (0, import_firestore62.getDocs)(q);
19716
+ return snapshot.docs.map(
19717
+ (doc44) => ({
19718
+ id: doc44.id,
19719
+ ...doc44.data()
19720
+ })
19721
+ );
19722
+ }
19419
19723
  };
19420
19724
 
19421
19725
  // src/backoffice/services/constants.service.ts