tango-app-api-store-builder 1.0.0-beta-177 → 1.0.0-beta-178

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.
@@ -7,6 +7,7 @@ import * as storeService from '../service/store.service.js';
7
7
  import * as processedTaskService from '../service/processedTaskservice.js';
8
8
  import * as fixtureShelfService from '../service/fixtureShelf.service.js';
9
9
  import * as vmService from '../service/planoVm.service.js';
10
+ import * as templateLogService from '../service/templateLog.service.js';
10
11
  import { createTask } from './task.controller.js';
11
12
  import mongoose from 'mongoose';
12
13
  import dayjs from 'dayjs';
@@ -59,7 +60,7 @@ async function getTemplateId( name, width ) {
59
60
  let templateId = 'Template-1';
60
61
  let fixtureTemplateData = await fixtureConfigService.sortAndFindOne( { 'fixtureCategory': name, 'fixtureWidth.unit': width.unit, 'fixtureWidth.value': width.value }, { templateIndex: 1 }, { _id: -1 } );
61
62
  if ( fixtureTemplateData.length ) {
62
- templateId= `Template-${fixtureTemplateData[0].templateIndex + 1}`;
63
+ templateId = `Template-${fixtureTemplateData[0].templateIndex + 1}`;
63
64
  }
64
65
  return templateId;
65
66
  }
@@ -71,54 +72,7 @@ export async function updateTemplate( req, res ) {
71
72
  if ( !templateDetails ) {
72
73
  return res.sendError( 'No data found', 204 );
73
74
  }
74
- if ( inputData.status == 'complete' ) {
75
- let newFixture;
76
- delete inputData.store;
77
- if ( req.body?.new ) {
78
- templateDetails = templateDetails.toObject();
79
- delete templateDetails._id;
80
- let templateData = { ...templateDetails, ...inputData };
81
- newFixture = await fixtureConfigService.create( templateData );
82
- }
83
-
84
- let storeFixtureDetails = await storeFixtureService.find( { fixtureConfigId: req.params.templateId } );
85
- if ( storeFixtureDetails.length ) {
86
- let fixtureList = storeFixtureDetails.map( ( ele ) => ele._id );
87
- let fixtureData = {
88
- ...inputData,
89
- fixtureConfigId: newFixture ? newFixture._id : req.params.templateId,
90
- };
91
- delete fixtureData._id;
92
- delete fixtureData.status;
93
- await storeFixtureService.updateMany( { _id: { $in: fixtureList } }, fixtureData );
94
- await Promise.all( storeFixtureDetails.map( async ( fixture ) => {
95
- await fixtureShelfService.deleteMany( { fixtureId: fixture } );
96
- let shelfData = [];
97
- inputData.shelfConfig.forEach( ( ele, index ) => {
98
- shelfData.push( {
99
- productCategory: ele.productCategory,
100
- productSubCategory: ele.productSubCategory,
101
- shelfType: ele.shelfType,
102
- trayRows: ele.trayRows,
103
- shelfNumber: index + 1,
104
- fixtureId: fixture,
105
- clientId: req.body.clientId,
106
- planoId: fixture.planoId,
107
- floorId: fixture.floorId,
108
- productBrandName: ele.productBrandName,
109
- shelfOrder: 'LTR',
110
- shelfSplitup: 0,
111
- storeId: fixture.storeId,
112
- storeName: fixture.storeName,
113
- productPerShelf: ele.productPerShelf,
114
- sectionZone: ele.zone,
115
- zone: ele.zone,
116
- } );
117
- } );
118
- await fixtureShelfService.insertMany( shelfData );
119
- } ) );
120
- }
121
- }
75
+ inputData.isEdited = true;
122
76
  await fixtureConfigService.updateOne( { _id: req.params.templateId }, inputData );
123
77
  return res.sendSuccess( 'Fixture template details updated successfully' );
124
78
  } catch ( e ) {
@@ -133,6 +87,9 @@ export async function deleteTemplate( req, res ) {
133
87
  if ( !templateDetails ) {
134
88
  return res.sendError( 'No data found', 204 );
135
89
  }
90
+ if ( templateDetails.templateType == 'master' ) {
91
+ return res.sendError( `Can't delete master template`, 400 );
92
+ }
136
93
  let getFixtureDetails = await storeFixtureService.find( { fixtureConfigId: req.body.templateId }, { _id: 1, planoId: 1 } );
137
94
  if ( getFixtureDetails.length ) {
138
95
  let planoDetails = await planoService.find( { _id: getFixtureDetails.map( ( ele ) => ele.planoId ) } );
@@ -146,7 +103,7 @@ export async function deleteTemplate( req, res ) {
146
103
  }
147
104
  }
148
105
 
149
- export async function duplicateTemplate( req, res ) {
106
+ export async function duplicateTemplateOld( req, res ) {
150
107
  try {
151
108
  let templateDetails = await fixtureConfigService.findOne( { _id: req.body.templateId }, { createdAt: 0, updatedAt: 0 } );
152
109
  if ( !templateDetails ) {
@@ -180,19 +137,6 @@ export async function getTemplateDetails( req, res ) {
180
137
  if ( !templateDetails ) {
181
138
  return res.sendError( 'No data found', 204 );
182
139
  }
183
- let query = [
184
- {
185
- $match: {
186
- fixtureConfigId: new ObjectId( req.query.templateId ),
187
- },
188
- },
189
- {
190
- $group: {
191
- _id: '',
192
- store: { $addToSet: '$storeName' },
193
- },
194
- },
195
- ];
196
140
  templateDetails = templateDetails.toObject();
197
141
  templateDetails.vmConfig = await Promise.all( templateDetails.vmConfig.map( async ( vm ) => {
198
142
  let vmDetails = await vmService.findOne( { _id: vm.vmId } );
@@ -201,9 +145,6 @@ export async function getTemplateDetails( req, res ) {
201
145
  return vm;
202
146
  }
203
147
  } ) );
204
- let mappedStoreList = await storeFixtureService.aggregate( query );
205
- let storeList = mappedStoreList?.[0]?.store || [];
206
- let storeDetails = await storeService.find( { clientId: templateDetails.clientId, storeName: { $in: storeList } }, { storeName: 1, storeId: 1, spocDetails: 1 } );
207
148
  let storeFixtureDetails = await storeFixtureService.find( { fixtureConfigId: req.query.templateId } );
208
149
  if ( storeFixtureDetails.length ) {
209
150
  let fixtureId = storeFixtureDetails.map( ( ele ) => ele.planoId );
@@ -213,10 +154,34 @@ export async function getTemplateDetails( req, res ) {
213
154
  }
214
155
  let data = {
215
156
  ...templateDetails,
216
- store: storeDetails.map( ( ele ) => {
217
- return { storeName: ele.storeName, storeId: ele.storeId, userEmail: ele?.spocDetails?.[0]?.email };
218
- } ),
219
157
  };
158
+ if ( templateDetails.templateType == 'master' ) {
159
+ let variantCount = await fixtureConfigService.count( { masterTemplateId: templateDetails._id } );
160
+ data = { ...data, variantCount };
161
+ } else {
162
+ let query = [
163
+ {
164
+ $match: {
165
+ fixtureConfigId: new ObjectId( req.query.templateId ),
166
+ },
167
+ },
168
+ {
169
+ $group: {
170
+ _id: '',
171
+ storeId: { $addToSet: '$storeId' },
172
+ },
173
+ },
174
+ {
175
+ $project: {
176
+ storeCount: { $size: '$storeId' },
177
+ },
178
+ },
179
+ ];
180
+
181
+ let templateStoreList = await storeFixtureService.aggregate( query );
182
+ data = { ...data, storeCount: templateStoreList?.[0]?.storeCount || 0 };
183
+ }
184
+
220
185
  return res.sendSuccess( data );
221
186
  } catch ( e ) {
222
187
  logger.error( { functionName: 'getTemplateDetails', error: e } );
@@ -233,20 +198,22 @@ export async function getTemplateList( req, res ) {
233
198
  const matchStage = {
234
199
  clientId: req.body.clientId,
235
200
  ...( req.body?.searchValue ?
236
- { fixtureName: { $regex: req.body.searchValue, $options: 'i' } } :
237
- {} ),
201
+ { fixtureName: { $regex: req.body.searchValue, $options: 'i' } } :
202
+ {} ),
238
203
  ...( req.body?.filter?.brand?.length ?
239
- { $or: [
240
- { 'productBrandName': { $in: req.body.filter.brand } },
241
- { 'shelfConfig.productBrandName': { $in: req.body.filter.brand } },
242
- ] } :
243
- {} ),
204
+ {
205
+ $or: [
206
+ { 'productBrandName': { $in: req.body.filter.brand } },
207
+ { 'shelfConfig.productBrandName': { $in: req.body.filter.brand } },
208
+ ],
209
+ } :
210
+ {} ),
244
211
  ...( req.body?.filter?.category?.length ?
245
- { fixtureCategory: { $in: req.body.filter.category } } :
246
- {} ),
212
+ { fixtureCategory: { $in: req.body.filter.category } } :
213
+ {} ),
247
214
  ...( req.body?.filter?.subCategory?.length ?
248
- { productSubCategory: { $in: req.body.subCategory.category } } :
249
- {} ),
215
+ { productSubCategory: { $in: req.body.subCategory.category } } :
216
+ {} ),
250
217
  };
251
218
 
252
219
  const query = [
@@ -546,6 +513,7 @@ export async function getAllTemplates( req, res ) {
546
513
  {
547
514
  $match: {
548
515
  clientId: req.body.clientId,
516
+ templateType: 'sub',
549
517
  },
550
518
  },
551
519
  {
@@ -613,3 +581,949 @@ export async function getFixtureTemplate( req, res ) {
613
581
  logger.error( { functionName: 'getFixtureTemplate', error: e } );
614
582
  }
615
583
  }
584
+
585
+
586
+ export async function assigedTemplate( req, res ) {
587
+ try {
588
+ let findFixtureList = await fixtureConfigService.aggregate( [
589
+ {
590
+ $match: {
591
+ templateType: 'sub',
592
+ },
593
+ },
594
+ {
595
+ $project: {
596
+ fixtureCategory: 1,
597
+ fixtureName: 1,
598
+ fixtureWidth: 1,
599
+ productBrandName: 1,
600
+ productCategory: 1,
601
+ },
602
+ },
603
+ {
604
+ $lookup: {
605
+ from: 'storefixtures',
606
+ let: { fixtureConfigId: '$_id' },
607
+ pipeline: [
608
+ { $match: { $expr: { $eq: [ '$fixtureConfigId', '$$fixtureConfigId' ] } } },
609
+ {
610
+ $project: {
611
+ storeId: 1,
612
+ },
613
+ },
614
+ ],
615
+ as: 'storeCount',
616
+ },
617
+ },
618
+ {
619
+ $unwind: {
620
+ path: '$storeCount',
621
+ preserveNullAndEmptyArrays: true,
622
+ },
623
+ },
624
+ {
625
+ $group: {
626
+ _id: '$_id',
627
+ fixtureCategory: { $first: '$fixtureCategory' },
628
+ fixtureName: { $first: '$fixtureName' },
629
+ fixtureWidth: { $first: '$fixtureWidth' },
630
+ productBrandName: { $first: '$productBrandName' },
631
+ productCategory: { $first: '$productCategory' },
632
+ storeCount: { $addToSet: '$storeCount.storeId' },
633
+ },
634
+ },
635
+ {
636
+ $project: {
637
+ fixtureCategory: 1,
638
+ fixtureName: 1,
639
+ fixtureWidth: 1,
640
+ productBrandName: 1,
641
+ productCategory: 1,
642
+ storeCount: { $size: '$storeCount' },
643
+ },
644
+ },
645
+
646
+ ] );
647
+ res.sendSuccess( findFixtureList );
648
+ } catch ( e ) {
649
+ logger.error( { functionName: 'assigedTemplate', error: e } );
650
+ }
651
+ }
652
+
653
+
654
+ export async function TemplateStoresList( req, res ) {
655
+ try {
656
+ let query = [];
657
+ let inputData = req.body;
658
+ let limit = inputData?.limit || 10;
659
+ let page = inputData?.offset || 1;
660
+ let skip = limit * ( page - 1 );
661
+ console.log( skip, limit );
662
+
663
+ query.push( {
664
+ $match: {
665
+ clientId: inputData.clientId,
666
+ fixtureConfigId: new mongoose.Types.ObjectId( inputData.fixtureConfigId ),
667
+ },
668
+ },
669
+ {
670
+ $sort: {
671
+ _id: -1,
672
+ },
673
+ },
674
+ {
675
+ $project: {
676
+ storeId: 1,
677
+ storeName: 1,
678
+ planoId: 1,
679
+ floorId: 1,
680
+ },
681
+ },
682
+ {
683
+ $group: {
684
+ _id: '$storeId',
685
+ storeName: { $first: '$storeName' },
686
+ planoId: { $first: '$planoId' },
687
+ storeId: { $first: '$storeId' },
688
+ floorId: { $first: '$floorId' },
689
+ },
690
+ },
691
+
692
+
693
+ );
694
+ if ( inputData.showlookup ) {
695
+ query.push(
696
+ {
697
+ $lookup: {
698
+ from: 'stores',
699
+ let: { storeId: '$storeId' },
700
+ pipeline: [
701
+ { $match: { $expr: { $eq: [ '$storeId', '$$storeId' ] } } },
702
+ {
703
+ $project: {
704
+ spocDetails: { $arrayElemAt: [ '$spocDetails', 0 ] },
705
+ },
706
+ },
707
+ ],
708
+ as: 'storeData',
709
+ },
710
+ },
711
+ { $unwind: { path: '$storeData', preserveNullAndEmptyArrays: true } },
712
+ {
713
+ $project: {
714
+ storeId: 1,
715
+ storeName: 1,
716
+ planoId: 1,
717
+ floorId: 1,
718
+ spocName: { $ifNull: [ '$storeData.spocDetails.name', '' ] },
719
+ spocEmail: { $ifNull: [ '$storeData.spocDetails.email', '' ] },
720
+ },
721
+ },
722
+ );
723
+ }
724
+
725
+ if ( inputData.sortColumnName && inputData.sortBy ) {
726
+ query.push( { $sort: { [inputData.sortColumnName]: inputData.sortBy } } );
727
+ }
728
+ if ( inputData.searchValue ) {
729
+ query.push( {
730
+ $match: {
731
+ storeName: { $regex: inputData.searchValue, $options: 'i' },
732
+ },
733
+ } );
734
+ }
735
+ let count = await storeFixtureService.aggregate( query );
736
+ if ( !inputData?.export ) {
737
+ query.push( { $skip: skip }, { $limit: limit } );
738
+ }
739
+ if ( inputData.showlookup ) {
740
+ query.push( {
741
+ $lookup: {
742
+ from: 'clusters',
743
+ let: { storeId: '$storeId' },
744
+ pipeline: [
745
+ {
746
+ $match: {
747
+ $expr: {
748
+ $anyElementTrue: {
749
+ $map: {
750
+ input: '$stores',
751
+ as: 'store',
752
+ in: { $eq: [ '$$store.storeId', '$$storeId' ] },
753
+ },
754
+ },
755
+ },
756
+ },
757
+ },
758
+ {
759
+ $project: {
760
+ clusterName: 1,
761
+ },
762
+ },
763
+ ],
764
+ as: 'clusterData',
765
+ },
766
+ }, {
767
+ $addFields: {
768
+ clusterData: {
769
+ $map: {
770
+ input: '$clusterData',
771
+ as: 'cd',
772
+ in: '$$cd.clusterName',
773
+ },
774
+ },
775
+ },
776
+ },
777
+ );
778
+ }
779
+
780
+
781
+ let response = await storeFixtureService.aggregate( query );
782
+
783
+ if ( !req.body.export && !response.length ) {
784
+ return res.sendError( 'No data found', 204 );
785
+ }
786
+ let result = {
787
+ count: count.length || 0,
788
+ data: response || [],
789
+ };
790
+ if ( !req.body.export ) {
791
+ return res.sendSuccess( result );
792
+ }
793
+ } catch ( e ) {
794
+ logger.error( { functionName: 'TemplateStoresList', error: e } );
795
+ res.sendError( e, 500 );
796
+ }
797
+ }
798
+
799
+
800
+ export async function replaceFixtureDetails( req, res ) {
801
+ try {
802
+ let inputData = req.body;
803
+ console.log( inputData );
804
+ let replaceFixtureData = await fixtureConfigService.findOne( { _id: inputData.replaceFixture } );
805
+
806
+ if ( !replaceFixtureData ) {
807
+ return res.sendError( 'No data found', 204 );
808
+ }
809
+ if ( inputData.storeList.length == 0 && inputData.publishAll ) {
810
+ await fixtureConfigService.updateOne( { _id: inputData.replaceFixture }, { isEdited: false } );
811
+ inputData.storeList = await storeFixtureService.find( { fixtureConfigId: inputData.findFixture }, { storeId: 1 } );
812
+ let data = {
813
+ templateId: inputData.replaceFixture,
814
+ data: replaceFixtureData,
815
+ };
816
+ await templateLogService.create( data );
817
+ }
818
+ if ( inputData.storeList.length ) {
819
+ const updateData = {
820
+ ...replaceFixtureData.toObject(),
821
+ fixtureConfigId: replaceFixtureData._id,
822
+ };
823
+ delete updateData._id;
824
+
825
+ await Promise.all(
826
+ inputData.storeList.map( async ( store ) => {
827
+ const replaceFixture = await storeFixtureService.findOne( {
828
+ fixtureConfigId: inputData.findFixture,
829
+ storeId: store.storeId,
830
+ } );
831
+
832
+ if ( !replaceFixture ) return;
833
+
834
+ console.log( replaceFixture._id );
835
+
836
+ const deleteFixShelves = await fixtureShelfService.deleteMany( {
837
+ fixtureId: replaceFixture._id,
838
+ } );
839
+ console.log( deleteFixShelves );
840
+
841
+ await storeFixtureService.updateMany(
842
+ { fixtureConfigId: inputData.findFixture, storeId: store.storeId },
843
+ updateData,
844
+ );
845
+
846
+ if ( replaceFixtureData.shelfConfig?.length ) {
847
+ const shelfList = replaceFixtureData.shelfConfig.map( ( shelf ) => {
848
+ const shelfData = {
849
+ ...shelf.toObject(),
850
+ floorId: replaceFixture.floorId,
851
+ planoId: replaceFixture.planoId,
852
+ clientId: replaceFixture.clientId,
853
+ fixtureId: replaceFixture._id,
854
+ storeId: replaceFixture.storeId,
855
+ storeName: replaceFixture.storeName,
856
+ };
857
+ delete shelfData._id;
858
+ return shelfData;
859
+ } );
860
+
861
+ try {
862
+ await fixtureShelfService.insertMany( shelfList );
863
+ } catch ( err ) {
864
+ console.error( 'Insert failed:', err );
865
+ }
866
+ }
867
+ } ),
868
+ );
869
+ } else {
870
+ return res.sendError( 'Store Not found', 500 );
871
+ }
872
+ res.sendSuccess( 'updatedSucessfully' );
873
+ } catch ( e ) {
874
+ logger.error( { functionName: 'replaceFixtureDetails', error: e } );
875
+ res.sendError( e, 500 );
876
+ }
877
+ }
878
+
879
+ export async function getMasterList( req, res ) {
880
+ try {
881
+ let limit = req.body?.limit || 10;
882
+ let page = req.body?.offset || 1;
883
+ let skip = limit * ( page - 1 );
884
+
885
+ const matchStage = {
886
+ clientId: req.body.clientId,
887
+ templateType: 'master',
888
+ ...( req.body?.searchValue ?
889
+ { fixtureName: { $regex: req.body.searchValue, $options: 'i' } } :
890
+ {} ),
891
+ ...( req.body?.filter?.brand?.length ?
892
+ {
893
+ $or: [
894
+ { 'productBrandName': { $in: req.body.filter.brand } },
895
+ { 'shelfConfig.productBrandName': { $in: req.body.filter.brand } },
896
+ ],
897
+ } :
898
+ {} ),
899
+ ...( req.body?.filter?.category?.length ?
900
+ { fixtureCategory: { $in: req.body.filter.category } } :
901
+ {} ),
902
+ ...( req.body?.filter?.subCategory?.length ?
903
+ { productSubCategory: { $in: req.body.subCategory.category } } :
904
+ {} ),
905
+ };
906
+
907
+ const query = [
908
+ { $match: matchStage },
909
+ {
910
+ $lookup: {
911
+ from: 'fixtureconfigs',
912
+ let: { mainId: '$_id' },
913
+ pipeline: [
914
+ {
915
+ $match: {
916
+ $expr: {
917
+ $eq: [ '$masterTemplateId', '$$mainId' ],
918
+ },
919
+ },
920
+ },
921
+ {
922
+ $group: {
923
+ _id: '$masterTemplateId',
924
+ count: { $sum: 1 },
925
+ },
926
+ },
927
+ ],
928
+ as: 'childtemplate',
929
+ },
930
+ },
931
+ { $unwind: { path: '$childtemplate', preserveNullAndEmptyArrays: true } },
932
+ {
933
+ $project: {
934
+ fixtureCategory: 1,
935
+ fixtureName: 1,
936
+ fixtureWidth: 1,
937
+ clientId: 1,
938
+ fixtureType: 1,
939
+ childCount: { $ifNull: [ '$childtemplate.count', 0 ] },
940
+ productBrandName: {
941
+ $setUnion: [
942
+ { $ifNull: [ '$productBrandName', [] ] },
943
+ {
944
+ $reduce: {
945
+ input: '$shelfConfig',
946
+ initialValue: [],
947
+ in: {
948
+ $concatArrays: [
949
+ '$$value',
950
+ { $ifNull: [ '$$this.productBrandName', [] ] },
951
+ ],
952
+ },
953
+ },
954
+ },
955
+ ],
956
+ },
957
+ productCategory: {
958
+ $setUnion: [
959
+ { $ifNull: [ '$productCategory', [] ] },
960
+ {
961
+ $reduce: {
962
+ input: '$shelfConfig',
963
+ initialValue: [],
964
+ in: {
965
+ $concatArrays: [
966
+ '$$value',
967
+ { $ifNull: [ '$$this.productCategory', [] ] },
968
+ ],
969
+ },
970
+ },
971
+ },
972
+ ],
973
+ },
974
+ productSubCategory: {
975
+ $setUnion: [
976
+ { $ifNull: [ '$productSubCategory', [] ] },
977
+ {
978
+ $reduce: {
979
+ input: '$shelfConfig',
980
+ initialValue: [],
981
+ in: {
982
+ $concatArrays: [
983
+ '$$value',
984
+ { $ifNull: [ '$$this.productSubCategory', [] ] },
985
+ ],
986
+ },
987
+ },
988
+ },
989
+ ],
990
+ },
991
+ },
992
+ },
993
+
994
+ ];
995
+
996
+ if ( req.body.filter.status.length ) {
997
+ query.push( {
998
+ $match: {
999
+ status: { $in: req.body.filter.status },
1000
+ },
1001
+ } );
1002
+ }
1003
+
1004
+ if ( req.body?.sortColumnName && req.body?.sortBy ) {
1005
+ query.push( {
1006
+ $sort: {
1007
+ [req.body.sortColumnName]: req.body.sortBy,
1008
+ },
1009
+ },
1010
+ );
1011
+ } else {
1012
+ query.push( {
1013
+ $sort: { _id: -1 },
1014
+ } );
1015
+ }
1016
+ query.push(
1017
+ {
1018
+ $facet: {
1019
+ ...( !req.body?.export ) ? {
1020
+ templateData: [ { $skip: skip }, { $limit: limit } ],
1021
+ } : { templateData: [ { $skip: skip } ] },
1022
+ count: [ { $count: 'total' } ],
1023
+ },
1024
+ },
1025
+ );
1026
+ let fixtureDetails = await fixtureConfigService.aggregate( query );
1027
+
1028
+
1029
+ if ( !fixtureDetails[0]?.templateData.length ) {
1030
+ return res.sendError( 'No data found', 204 );
1031
+ }
1032
+ let result = {
1033
+ count: fixtureDetails[0].count[0].total,
1034
+ data: fixtureDetails[0].templateData,
1035
+ };
1036
+
1037
+ return res.sendSuccess( result );
1038
+ } catch ( e ) {
1039
+ logger.error( { functionName: 'getMasterList', error: e } );
1040
+ return res.sendError( e, 500 );
1041
+ }
1042
+ }
1043
+
1044
+ export async function getChildList( req, res ) {
1045
+ try {
1046
+ let limit = req.body?.limit || 10;
1047
+ let page = req.body?.offset || 1;
1048
+ let skip = limit * ( page - 1 );
1049
+
1050
+ const matchStage = {
1051
+ clientId: req.body.clientId,
1052
+ templateType: 'sub',
1053
+ masterTemplateId: new ObjectId( req.body.masterTemplateId ),
1054
+ };
1055
+
1056
+ const query = [
1057
+ { $match: matchStage },
1058
+ {
1059
+ $lookup: {
1060
+ from: 'storefixtures',
1061
+ let: { templateId: '$_id' },
1062
+ pipeline: [
1063
+ {
1064
+ $match: {
1065
+ $expr: {
1066
+ $eq: [ '$$templateId', '$fixtureConfigId' ],
1067
+ },
1068
+ },
1069
+ },
1070
+ {
1071
+ $group: {
1072
+ _id: null,
1073
+ planoId: { $addToSet: '$planoId' },
1074
+ storeList: { $addToSet: '$storeName' },
1075
+ },
1076
+ },
1077
+ {
1078
+ $project: {
1079
+ _id: 0,
1080
+ planoId: 1,
1081
+ storeList: 1,
1082
+ },
1083
+ },
1084
+ ],
1085
+ as: 'storeFixtureDetails',
1086
+ },
1087
+ },
1088
+ {
1089
+ $project: {
1090
+ fixtureCategory: 1,
1091
+ fixtureName: 1,
1092
+ fixtureWidth: 1,
1093
+ productBrandName: {
1094
+ $setUnion: [
1095
+ { $ifNull: [ '$productBrandName', [] ] },
1096
+ {
1097
+ $reduce: {
1098
+ input: '$shelfConfig',
1099
+ initialValue: [],
1100
+ in: {
1101
+ $concatArrays: [
1102
+ '$$value',
1103
+ { $ifNull: [ '$$this.productBrandName', [] ] },
1104
+ ],
1105
+ },
1106
+ },
1107
+ },
1108
+ ],
1109
+ },
1110
+ productCategory: {
1111
+ $setUnion: [
1112
+ { $ifNull: [ '$productCategory', [] ] },
1113
+ {
1114
+ $reduce: {
1115
+ input: '$shelfConfig',
1116
+ initialValue: [],
1117
+ in: {
1118
+ $concatArrays: [
1119
+ '$$value',
1120
+ { $ifNull: [ '$$this.productCategory', [] ] },
1121
+ ],
1122
+ },
1123
+ },
1124
+ },
1125
+ ],
1126
+ },
1127
+ productSubCategory: {
1128
+ $setUnion: [
1129
+ { $ifNull: [ '$productSubCategory', [] ] },
1130
+ {
1131
+ $reduce: {
1132
+ input: '$shelfConfig',
1133
+ initialValue: [],
1134
+ in: {
1135
+ $concatArrays: [
1136
+ '$$value',
1137
+ { $ifNull: [ '$$this.productSubCategory', [] ] },
1138
+ ],
1139
+ },
1140
+ },
1141
+ },
1142
+ ],
1143
+ },
1144
+ clientId: 1,
1145
+ status: 1,
1146
+ fixtureType: 1,
1147
+ planoId: { $ifNull: [ { $arrayElemAt: [ '$storeFixtureDetails.planoId', 0 ] }, [] ] },
1148
+ storeList: { $ifNull: [ { $arrayElemAt: [ '$storeFixtureDetails.storeList', 0 ] }, [] ] },
1149
+ updatedAt: 1,
1150
+ },
1151
+ },
1152
+ {
1153
+ $lookup: {
1154
+ from: 'planograms',
1155
+ let: { planoIds: '$planoId' },
1156
+ pipeline: [
1157
+ {
1158
+ $match: {
1159
+ $expr: {
1160
+ $in: [ '$_id', { $ifNull: [ '$$planoIds', [] ] } ],
1161
+ },
1162
+ },
1163
+ },
1164
+ {
1165
+ $group: {
1166
+ _id: null,
1167
+ statusList: { $addToSet: '$status' },
1168
+ },
1169
+ },
1170
+ ],
1171
+ as: 'planoStatus',
1172
+ },
1173
+ },
1174
+ {
1175
+ $project: {
1176
+ fixtureCategory: 1,
1177
+ fixtureName: 1,
1178
+ fixtureWidth: 1,
1179
+ productBrandName: 1,
1180
+ productCategory: 1,
1181
+ clientId: 1,
1182
+ fixtureType: 1,
1183
+ productSubCategory: 1,
1184
+ status: 1,
1185
+ templateId: 1,
1186
+ planoId: 1,
1187
+ vmCapacity: 1,
1188
+ productCapacity: 1,
1189
+ storeList: 1,
1190
+ storeCount: { $size: '$storeList' },
1191
+ planoStatus: { $ifNull: [ { $arrayElemAt: [ '$planoStatus.statusList', 0 ] }, [] ] },
1192
+ status: {
1193
+ $cond: {
1194
+ if: { $in: [ 'completed', { $ifNull: [ { $arrayElemAt: [ '$planoStatus.statusList', 0 ] }, [] ] } ] },
1195
+ then: 'active',
1196
+ else: {
1197
+ $cond: {
1198
+ if: {
1199
+ $eq: [
1200
+ '$status', 'draft',
1201
+ ],
1202
+ },
1203
+ then: 'draft',
1204
+ else: 'inactive',
1205
+ },
1206
+ },
1207
+ },
1208
+ },
1209
+ updatedAt: 1,
1210
+ },
1211
+ },
1212
+ ];
1213
+
1214
+ if ( req.body.searchValue ) {
1215
+ query.push( {
1216
+ $match: {
1217
+ fixtureName: { $regex: req.body.searchValue, $options: 'i' },
1218
+ },
1219
+ } );
1220
+ }
1221
+
1222
+ if ( req.body?.sortColumnName && req.body?.sortBy ) {
1223
+ query.push( {
1224
+ $sort: {
1225
+ [req.body.sortColumnName]: req.body.sortBy,
1226
+ },
1227
+ },
1228
+ );
1229
+ } else {
1230
+ query.push( {
1231
+ $sort: { _id: -1 },
1232
+ } );
1233
+ }
1234
+ query.push(
1235
+ {
1236
+ $facet: {
1237
+ templateData: [ { $skip: skip }, { $limit: limit } ],
1238
+ count: [ { $count: 'total' } ],
1239
+ },
1240
+ },
1241
+ );
1242
+ let fixtureDetails = await fixtureConfigService.aggregate( query );
1243
+ if ( !fixtureDetails[0]?.templateData.length ) {
1244
+ return res.sendError( 'No data found', 204 );
1245
+ }
1246
+ let result = {
1247
+ count: fixtureDetails[0].count[0].total,
1248
+ data: fixtureDetails[0].templateData,
1249
+ };
1250
+ return res.sendSuccess( result );
1251
+ } catch ( e ) {
1252
+ logger.error( { functionName: 'getChildList', error: e } );
1253
+ return res.sendError( e, 500 );
1254
+ }
1255
+ }
1256
+
1257
+ export async function getTreeListOld( req, res ) {
1258
+ try {
1259
+ let query = [
1260
+ {
1261
+ $match: {
1262
+ clientId: req.query.clientId,
1263
+ templateType: 'master',
1264
+ ...( req.query.masterId && { masterTemplateId: req.query.masterId } ),
1265
+ },
1266
+ },
1267
+ {
1268
+ $group: {
1269
+ _id: '$templateGroupName',
1270
+ },
1271
+ },
1272
+ {
1273
+ $project: {
1274
+ _id: 0,
1275
+ group: '$_id',
1276
+ },
1277
+ },
1278
+ ];
1279
+ let getGroupDetails = await fixtureConfigService.aggregate( query );
1280
+ if ( !getGroupDetails.length ) {
1281
+ return res.sendError( 'No data found', 204 );
1282
+ }
1283
+ let treeList = await Promise.all( getGroupDetails.map( async ( ele ) => {
1284
+ let groupDetails = await fixtureConfigService.find( { templateGroupName: ele.group, templateType: 'master' } );
1285
+ let masterDetails = await Promise.all( groupDetails.map( async ( group ) => {
1286
+ let templateDetails = await fixtureConfigService.find( { masterTemplateId: group._id } );
1287
+ let templateList = await Promise.all( templateDetails.map( async ( temp ) => {
1288
+ let storeFixtureCount = await storeFixtureService.count( { fixtureConfigId: ele._id } );
1289
+ return { ...temp.toObject(), storeCount: storeFixtureCount };
1290
+ } ) );
1291
+ return { ...group.toObject(), subTemplateList: templateList };
1292
+ } ) );
1293
+ return { groupName: ele.group, masterTemplate: masterDetails };
1294
+ } ) );
1295
+ return res.sendSuccess( treeList );
1296
+ } catch ( e ) {
1297
+ logger.error( { functionName: 'getTreeList', error: e } );
1298
+ return res.sendError( e, 500 );
1299
+ }
1300
+ }
1301
+
1302
+ export async function getTreeList( req, res ) {
1303
+ try {
1304
+ let query = [
1305
+ {
1306
+ $match: {
1307
+ ...( req.query?.masterId ) ? { masterTemplateId: new ObjectId( req.query.masterId ) } :{ clientId: req.query.clientId, templateType: 'master' },
1308
+ },
1309
+ },
1310
+ ];
1311
+
1312
+ let fixtureDetails = await fixtureConfigService.aggregate( query );
1313
+ if ( req?.query?.masterId ) {
1314
+ fixtureDetails = await Promise.all( fixtureDetails.map( async ( fixt ) => {
1315
+ let storeFixtureCount = await storeFixtureService.count( { fixtureConfigId: fixt._id } );
1316
+ fixt.vmConfig = await Promise.all( fixt.vmConfig.map( async ( vm ) => {
1317
+ let vmDetails = await vmService.findOne( { _id: vm.vmId }, { _id: 0 } );
1318
+ if ( vmDetails ) {
1319
+ return { ...vm, ...vmDetails.toObject() };
1320
+ }
1321
+ } ) );
1322
+ return { ...fixt, storeCount: storeFixtureCount };
1323
+ } ) );
1324
+ }
1325
+ return res.sendSuccess( fixtureDetails );
1326
+ } catch ( e ) {
1327
+ logger.error( { functionName: 'getTreeList', error: e } );
1328
+ return res.sendError( e, 500 );
1329
+ }
1330
+ }
1331
+
1332
+ export async function duplicateTemplate( req, res ) {
1333
+ try {
1334
+ let templateDetails = await fixtureConfigService.findOne( { _id: req.body.templateId }, { createdAt: 0, updatedAt: 0 } );
1335
+ if ( !templateDetails ) {
1336
+ return res.sendError( 'No data found', 204 );
1337
+ }
1338
+ let getAllTemplate;
1339
+ let counter = 1;
1340
+ let newFixtureName;
1341
+ templateDetails = templateDetails.toObject();
1342
+ let type = templateDetails.templateType;
1343
+ if ( templateDetails.templateType == 'master' ) {
1344
+ templateDetails = { ...templateDetails, masterTemplateId: templateDetails._id, templateType: 'sub' };
1345
+ getAllTemplate = await fixtureConfigService.findAndSort( { masterTemplateId: templateDetails.masterTemplateId }, {}, { templateIndex: -1 } );
1346
+ newFixtureName = templateDetails.fixtureName + ` - Variant ${getAllTemplate.length + 1}`;
1347
+ if ( getAllTemplate?.length ) {
1348
+ let fixtureNameList = getAllTemplate.map( ( ele ) => ele.fixtureName );
1349
+ while ( fixtureNameList.includes( newFixtureName ) ) {
1350
+ newFixtureName = templateDetails.fixtureName + ` - Variant ${counter}`;
1351
+ counter++;
1352
+ }
1353
+ }
1354
+ } else {
1355
+ getAllTemplate = await fixtureConfigService.findAndSort( { fixtureName: { $regex: templateDetails.fixtureName.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ), $options: 'i' } }, { fixtureName: 1, templateIndex: 1 }, { templateIndex: -1 } );
1356
+ newFixtureName = templateDetails.fixtureName + ` (${counter})`;
1357
+ if ( getAllTemplate?.length ) {
1358
+ let fixtureNameList = getAllTemplate.map( ( ele ) => ele.fixtureName );
1359
+ while ( fixtureNameList.includes( newFixtureName ) ) {
1360
+ newFixtureName = templateDetails.fixtureName + ` (${counter})`;
1361
+ counter++;
1362
+ }
1363
+ }
1364
+ }
1365
+ templateDetails.templateIndex = ( getAllTemplate?.[0]?.templateIndex ?? 0 )+ 1;
1366
+ delete templateDetails._id;
1367
+ delete templateDetails.templateGroupName;
1368
+ templateDetails.fixtureName = newFixtureName;
1369
+ templateDetails.status = 'complete';
1370
+ let duplicateDetails = await fixtureConfigService.create( templateDetails );
1371
+ return res.sendSuccess( { message: type == 'sub' ? 'Fixture template duplicated successfully' : 'Variant created successfully', id: duplicateDetails?._id } );
1372
+ } catch ( e ) {
1373
+ logger.error( { functionName: 'duplicateFixture', error: e } );
1374
+ return res.sendError( e, 500 );
1375
+ }
1376
+ }
1377
+
1378
+ export async function getTemplateStoreList( req, res ) {
1379
+ try {
1380
+ let templateDetails = await fixtureConfigService.findOne( { _id: req.body.templateId } );
1381
+ if ( !templateDetails ) {
1382
+ return res.sendError( 'N0 data found', 204 );
1383
+ }
1384
+ let limit = req.body?.limit || 10;
1385
+ let page = req.body?.offset || 0;
1386
+ let skip = limit * ( page - 1 );
1387
+ let query = [
1388
+ {
1389
+ $match: {
1390
+ fixtureConfigId: new ObjectId( req.body.templateId ),
1391
+ },
1392
+ },
1393
+ {
1394
+ $group: {
1395
+ _id: '$storeName',
1396
+ storeId: { $first: '$storeId' },
1397
+ updatedAt: { $first: '$updatedAt' },
1398
+ planoId: { $push: '$planoId' },
1399
+ },
1400
+ },
1401
+ {
1402
+ $project: {
1403
+ storeName: '$_id',
1404
+ storeId: 1,
1405
+ updatedAt: 1,
1406
+ planoId: 1,
1407
+ },
1408
+ },
1409
+ {
1410
+ $lookup: {
1411
+ from: 'stores',
1412
+ let: { store: '$storeId' },
1413
+ pipeline: [
1414
+ {
1415
+ $match: {
1416
+ $expr: {
1417
+ $eq: [ '$storeId', '$$store' ],
1418
+ },
1419
+ },
1420
+ },
1421
+ {
1422
+ $project: {
1423
+ _id: 0,
1424
+ storeProfile: 1,
1425
+ },
1426
+ },
1427
+ ],
1428
+ as: 'storeDetails',
1429
+ },
1430
+ },
1431
+ {
1432
+ $lookup: {
1433
+ from: 'clusters',
1434
+ let: { store: '$storeId' },
1435
+ pipeline: [
1436
+ {
1437
+ $match: {
1438
+ $expr: {
1439
+ $in: [ '$$store', '$stores.storeId' ],
1440
+ },
1441
+ },
1442
+ },
1443
+ {
1444
+ $group: {
1445
+ _id: '',
1446
+ clusterName: { $addToSet: '$clusterName' },
1447
+ },
1448
+ },
1449
+ {
1450
+ $project: {
1451
+ _id: 0,
1452
+ clusterName: 1,
1453
+ },
1454
+ },
1455
+ ],
1456
+ as: 'clusterDetails',
1457
+ },
1458
+ },
1459
+ {
1460
+ $project: {
1461
+ storeName: 1,
1462
+ storeId: 1,
1463
+ updatedAt: 1,
1464
+ planoId: 1,
1465
+ storeDetails: { $ifNull: [ { $arrayElemAt: [ '$storeDetails.storeProfile', 0 ] }, {} ] },
1466
+ clusterDetails: { $ifNull: [ { $arrayElemAt: [ '$clusterDetails.clusterName', 0 ] }, {} ] },
1467
+ clusterDetails: { $ifNull: [ { $arrayElemAt: [ '$clusterDetails.clusterName', 0 ] }, {} ] },
1468
+ },
1469
+ },
1470
+ ];
1471
+ if ( req.body.searchValue ) {
1472
+ query.push( {
1473
+ $match: {
1474
+ storeName: { $regex: req.body.searchValue, $options: 'i' },
1475
+ },
1476
+ } );
1477
+ }
1478
+ if ( req.body.sortColumnName && req.body.sortBy ) {
1479
+ query.push( { $sort: { [req.body.sortColumnName]: req.body.sortBy } } );
1480
+ } else {
1481
+ query.push( { $sort: { storeName: 1 } } );
1482
+ }
1483
+ query.push(
1484
+ {
1485
+ $facet: {
1486
+ data: [
1487
+ { $skip: skip }, { $limit: limit },
1488
+ ],
1489
+ count: [
1490
+ { $count: 'total' },
1491
+ ],
1492
+ },
1493
+ },
1494
+ );
1495
+
1496
+ let getStoreDetails = await storeFixtureService.aggregate( query );
1497
+ if ( !getStoreDetails[0].data.length ) {
1498
+ return res.sendError( 'No data found', 204 );
1499
+ }
1500
+ let result = {
1501
+ count: getStoreDetails?.[0]?.count?.[0]?.total || 0,
1502
+ data: getStoreDetails?.[0]?.data || [],
1503
+ };
1504
+ let status = templateDetails.isEdited ? 'Yet to publish' : 'Published';
1505
+ result.data.forEach( ( ele ) => {
1506
+ ele.status = status;
1507
+ } );
1508
+ return res.sendSuccess( result );
1509
+ } catch ( e ) {
1510
+ logger.error( { functionName: 'getTemplateStoreList', error: e } );
1511
+ return res.sendError( e, 500 );
1512
+ }
1513
+ }
1514
+
1515
+ export async function templateVersionList( req, res ) {
1516
+ try {
1517
+ if ( !req.query?.templateId ) {
1518
+ return res.sendError( 'Template id is required', 400 );
1519
+ }
1520
+ let configLogDetails = await templateLogService.find( { templateId: req.query.templateId }, {}, { _id: -1 } );
1521
+ if ( !configLogDetails.length ) {
1522
+ return res.sendError( 'NO data found', 204 );
1523
+ }
1524
+ return res.sendSuccess( configLogDetails );
1525
+ } catch ( e ) {
1526
+ logger.error( { functionName: 'templateVersionList', error: e } );
1527
+ return res.sendError( e, 500 );
1528
+ }
1529
+ }