tango-app-api-store-builder 1.0.0-beta-196 → 1.0.0-beta-198

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-store-builder",
3
- "version": "1.0.0-beta-196",
3
+ "version": "1.0.0-beta-198",
4
4
  "description": "storeBuilder",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -999,11 +999,35 @@ export async function getMasterList( req, res ) {
999
999
  let page = req.body?.offset || 1;
1000
1000
  let skip = limit * ( page - 1 );
1001
1001
 
1002
+ let searchMasterList = [];
1003
+
1004
+ if ( req.body?.searchValue ) {
1005
+ let searchQuery = [
1006
+ {
1007
+ $match: {
1008
+ clientId: req.body.clientId,
1009
+ templateType: 'sub',
1010
+ fixtureName: { $regex: req.body.searchValue, $options: 'i' },
1011
+ },
1012
+ },
1013
+ {
1014
+ $group: {
1015
+ _id: '$masterTemplateId',
1016
+ },
1017
+ },
1018
+ ];
1019
+
1020
+ let subTemplateDetails = await fixtureConfigService.aggregate( searchQuery );
1021
+ if ( subTemplateDetails.length ) {
1022
+ searchMasterList = subTemplateDetails.map( ( ele ) => ele._id );
1023
+ }
1024
+ }
1025
+
1002
1026
  const matchStage = {
1003
1027
  clientId: req.body.clientId,
1004
1028
  templateType: 'master',
1005
1029
  ...( req.body?.searchValue ?
1006
- { fixtureName: { $regex: req.body.searchValue, $options: 'i' } } :
1030
+ { $or: [ { fixtureName: { $regex: req.body.searchValue, $options: 'i' } }, { _id: { $in: searchMasterList } } ] } :
1007
1031
  {} ),
1008
1032
  ...( req.body?.filter?.brand?.length ?
1009
1033
  {
@@ -1181,6 +1205,9 @@ export async function getChildList( req, res ) {
1181
1205
  clientId: req.body.clientId,
1182
1206
  templateType: 'sub',
1183
1207
  masterTemplateId: new ObjectId( req.body.masterTemplateId ),
1208
+ ...( req.body?.searchValue ?
1209
+ { fixtureName: { $regex: req.body.searchValue, $options: 'i' } } :
1210
+ {} ),
1184
1211
  };
1185
1212
 
1186
1213
  const query = [
@@ -933,19 +933,34 @@ export async function updateGlobalComment( req, res ) {
933
933
  return res.sendError( e, 500 );
934
934
  }
935
935
  }
936
- export async function getGlobalComment( req, res ) {
936
+ export async function getGlobalComment(req, res) {
937
937
  try {
938
- let layoutComment = await planoGlobalCommentService.find( {
939
- planoId: new mongoose.Types.ObjectId( req.body.planoId ),
940
- floorId: new mongoose.Types.ObjectId( req.body.floorId ),
941
- taskType: req.body.taskType,
942
- } );
938
+ // let layoutComment = await planoGlobalCommentService.find( {
939
+ // planoId: new mongoose.Types.ObjectId( req.body.planoId ),
940
+ // floorId: new mongoose.Types.ObjectId( req.body.floorId ),
941
+ // taskType: req.body.taskType,
942
+ // } );
943
+
944
+ let taskId = [];
945
+ if (req.body?.taskId) {
946
+ taskId.push(new mongoose.Types.ObjectId(req.body.taskId));
947
+ }
943
948
 
949
+ if (req.body?.refTaskId) {
950
+ taskId.push(new mongoose.Types.ObjectId(req.body.refTaskId));
951
+ }
952
+
953
+ let layoutComment = await planoGlobalCommentService.find({
954
+ planoId: new mongoose.Types.ObjectId(req.body.planoId),
955
+ floorId: new mongoose.Types.ObjectId(req.body.floorId),
956
+ ...(taskId.length && { taskId: { $in: taskId } }),
957
+ taskType: req.body.taskType,
958
+ });
944
959
 
945
- res.sendSuccess( layoutComment );
946
- } catch ( e ) {
947
- logger.error( { functionName: 'getGlobalComment', error: e } );
948
- return res.sendError( e, 500 );
960
+ res.sendSuccess(layoutComment);
961
+ } catch (e) {
962
+ logger.error({ functionName: 'getGlobalComment', error: e });
963
+ return res.sendError(e, 500);
949
964
  }
950
965
  }
951
966
 
@@ -9548,3 +9548,149 @@ async function updateHeaders() {
9548
9548
  console.log( key );
9549
9549
  } );
9550
9550
  }
9551
+
9552
+ export async function productMappings(req, res) {
9553
+ try {
9554
+ const CLEAR_COL_BEFORE_INSERT = false;
9555
+ if (req?.headers?.authorization?.split(" ")[1] !== "hwjXfCD6TgMvc82cuSGZ9bNv9MuXsaiQ6uvx") {
9556
+ return res.sendError("Unauthorized", 401);
9557
+ }
9558
+
9559
+ if (!req.files?.file) {
9560
+ return res.sendError("Excel file is required", 400);
9561
+ }
9562
+
9563
+ const payload = JSON.parse(req.body.payload);
9564
+
9565
+ const { fixtureId, Top, Mid, Bottom } = payload;
9566
+
9567
+ const storeName = "LKST98";
9568
+ const storeId = "11-4";
9569
+
9570
+ if (!fixtureId) {
9571
+ return res.sendError("fixtureId is required", 400);
9572
+ }
9573
+
9574
+ const fixtureData = { Top, Mid, Bottom };
9575
+
9576
+ const workbook = xlsx.read(req.files.file.data, { type: "buffer" });
9577
+ const sheetName = "Unique Mapping";
9578
+ const raw = xlsx.utils.sheet_to_json(workbook.Sheets[sheetName]);
9579
+
9580
+ const pidsList = Object.values(fixtureData).flat();
9581
+
9582
+ let pDetailsList = [];
9583
+ for (const pid of pidsList) {
9584
+ const pDetails = raw.find((pd) => pd["product_id"] == pid);
9585
+ if (pDetails) {
9586
+ pDetailsList.push({
9587
+ clientId: "11",
9588
+ productId: pDetails.product_id,
9589
+ productName: pDetails.parent_brand,
9590
+ pid: pDetails.product_id,
9591
+ rfId: pDetails.barcode,
9592
+ productImageUrl: pDetails.image_front,
9593
+ productType: pDetails.classification_name ?? "",
9594
+ brandName: pDetails.collection ?? "",
9595
+ category: pDetails.collection_group ?? "",
9596
+ subCategory: pDetails.sub_collection ?? "",
9597
+ type: "product",
9598
+ });
9599
+ }
9600
+ }
9601
+
9602
+ if(CLEAR_COL_BEFORE_INSERT) await planoProductService.deleteMany({});
9603
+
9604
+ // const uniqueProducts = await fixtureShelfService.find();
9605
+
9606
+ const fixtureTemplateData = await fixtureShelfService.find({ fixtureId });
9607
+
9608
+ if (!fixtureTemplateData?.length) {
9609
+ return res.sendError("No shelves found for fixtureId", 404);
9610
+ }
9611
+
9612
+ const planoId = fixtureTemplateData[0].planoId;
9613
+ const floorId = fixtureTemplateData[0].floorId;
9614
+
9615
+ const fixtureShelve = fixtureTemplateData.map((f) => ({
9616
+ id: f._id,
9617
+ zone: f.zone,
9618
+ }));
9619
+
9620
+ const result = await planoProductService.insertMany(pDetailsList);
9621
+
9622
+ let planoMappings = result.map((pd) => ({
9623
+ clientId: "11",
9624
+ storeName: storeName,
9625
+ storeId: storeId,
9626
+ type: "product",
9627
+ productId: pd._id,
9628
+ pid: pd.pid,
9629
+ planoId,
9630
+ floorId,
9631
+ fixtureId,
9632
+ shelfId: "temp", // will be reassigned in transformProducts
9633
+ }));
9634
+
9635
+ if(CLEAR_COL_BEFORE_INSERT) await planoMappingService.deleteMany({});
9636
+
9637
+ const shelfGroups = {
9638
+ Top: fixtureShelve.filter((f) => f.zone === "Top").map((f) => f.id),
9639
+ Mid: fixtureShelve.filter((f) => f.zone === "Mid").map((f) => f.id),
9640
+ Bottom: fixtureShelve.filter((f) => f.zone === "Bottom").map((f) => f.id),
9641
+ };
9642
+
9643
+ const transformed = transformProducts(planoMappings, shelfGroups, fixtureData);
9644
+
9645
+ const result2 = await planoMappingService.insertMany(transformed);
9646
+
9647
+ res.sendSuccess({
9648
+ fixtureData,
9649
+ shelfGroups,
9650
+ planoMappings: result2,
9651
+ });
9652
+ } catch (error) {
9653
+ console.log("@@ ~ error:", error);
9654
+ res.sendError("Internal Server Error", 500);
9655
+ }
9656
+ }
9657
+
9658
+ function transformProducts(products, shelvesByZone, fixtureData) {
9659
+ const result = [];
9660
+
9661
+ // Build lookup: pid → zone
9662
+ const pidToZone = {};
9663
+ for (const [zone, pids] of Object.entries(fixtureData)) {
9664
+ pids.forEach(pid => {
9665
+ pidToZone[pid] = zone;
9666
+ });
9667
+ }
9668
+
9669
+ // Group products by zone
9670
+ const productsByZone = {};
9671
+ for (const doc of products) {
9672
+ const zone = pidToZone[doc.pid];
9673
+ if (!zone) continue;
9674
+ if (!productsByZone[zone]) productsByZone[zone] = [];
9675
+ productsByZone[zone].push(doc);
9676
+ }
9677
+
9678
+ // Round-robin distribute products across shelves
9679
+ for (const [zone, docs] of Object.entries(productsByZone)) {
9680
+ const shelves = shelvesByZone[zone] || [];
9681
+ if (shelves.length === 0) {
9682
+ result.push(...docs);
9683
+ continue;
9684
+ }
9685
+
9686
+ docs.forEach((doc, idx) => {
9687
+ const shelfId = shelves[idx % shelves.length]; // cycle through shelves
9688
+ result.push({
9689
+ ...doc,
9690
+ shelfId
9691
+ });
9692
+ });
9693
+ }
9694
+
9695
+ return result;
9696
+ }
@@ -32,4 +32,5 @@ scriptRouter
32
32
  .post( '/recorrectTaskData', scriptController.recorrectTaskData )
33
33
  .post( '/migrateCrest', scriptController.migrateCrestv1 )
34
34
  .post( '/updatePlanoMappings', scriptController.updatePlanoMappings )
35
+ .post( '/productMappings', scriptController.productMappings )
35
36
  ;