tango-app-api-store-builder 1.0.0-beta-95 → 1.0.0-beta-97

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.
@@ -15,6 +15,7 @@ import * as planoTaskComplianceService from '../service/planoTask.service.js';
15
15
  import * as planoQrConversionRequestService from '../service/planoQrConversionRequest.service.js';
16
16
  import * as fixtureConfigService from '../service/fixtureConfig.service.js';
17
17
  import * as planoStaticData from '../service/planoStaticData.service.js';
18
+ import * as planoVmService from '../service/planoVm.service.js';
18
19
 
19
20
 
20
21
  dayjs.extend( utc );
@@ -639,9 +640,15 @@ export async function storeFixturesv1( req, res ) {
639
640
 
640
641
  const vmDetails = await Promise.all( vms.map( async ( vm ) => {
641
642
  const vmTemplate = await planoProductService.findOne( { _id: vm.toObject().productId } );
643
+ const params = {
644
+ Bucket: JSON.parse( process.env.BUCKET ).storeBuilder,
645
+ file_path: vmTemplate?.productImageUrl,
646
+ };
647
+ const vmImage = await signedUrl( params );
642
648
  return {
643
649
  ...vm.toObject(),
644
650
  ...vmTemplate?.toObject(),
651
+ ...( typeof vmImage === 'string' && { productImageUrl: vmImage } ),
645
652
  };
646
653
  } ) );
647
654
 
@@ -753,7 +760,7 @@ export async function storeFixturesv1( req, res ) {
753
760
  } ),
754
761
  );
755
762
 
756
- const productCount = await planoMappingService.count( { floorId: floor._id } );
763
+ // const productCount = await planoMappingService.count( { floorId: floor._id } );
757
764
 
758
765
  const otherElements = await storeFixtureService.find( {
759
766
  floorId: floor._id,
@@ -860,158 +867,6 @@ export async function updateStatus( req, res ) {
860
867
  }
861
868
  }
862
869
 
863
- export async function fixtureShelfProduct( req, res ) {
864
- try {
865
- if ( !req.body.fixtureId ) {
866
- return res.sendError( 'Fixture id is required', 400 );
867
- }
868
-
869
- let fixtureDetails = await storeFixtureService.findOne( { _id: req.body.fixtureId } );
870
- if ( !fixtureDetails ) {
871
- return res.sendError( 'Fixture not found', 204 );
872
- }
873
- // let planoDetails = await planoService.findOne( { _id: fixtureDetails.planoId } );
874
- let shelfDetails = await fixtureShelfService.find( { fixtureId: req.body.fixtureId } );
875
- // let query;
876
- // switch ( planoDetails.productResolutionLevel ) {
877
- // case 'L1':
878
- // query = { floorId: fixtureDetails.floorId };
879
- // break;
880
- // default:
881
- // query = { floorId: fixtureDetails.floorId, fixtureId: req.body.fixtureId };
882
- // break;
883
- // }
884
- // query = {
885
- // ...query,
886
- // ...( [ 'L3', 'L4' ].includes( planoDetails.productResolutionLevel ) ) ? { shelfId: shelf._id } : {},
887
- // productId: { $in: productIdList },
888
- // date: new Date( dayjs().format( 'YYYY-MM-DD' ) ),
889
- // };
890
- let shelfList = [];
891
- for ( let shelf of shelfDetails ) {
892
- let data = { ...shelf._doc, products: [] };
893
- let productMappingDetails = await planoMappingService.find( { shelfId: shelf._id } );
894
- let productIdList = productMappingDetails.map( ( item ) => item.productId );
895
- let productDetails = await planoProductService.find( { _id: productIdList } );
896
- let productComplianceDetails = await planoComplianceService.find( { date: new Date( dayjs().format( 'YYYY-MM-DD' ) ), shelfId: shelf._id } );
897
- let product = [];
898
- productDetails.forEach( ( item ) => {
899
- let data = { ...item._doc, status: 'missing', rfId: '' };
900
- let getPosition = productMappingDetails.find( ( ele ) => ele.productId.toString() == item._id.toString() );
901
- let findCompliance = productComplianceDetails.find( ( ele ) => ele.shelfPosition == getPosition.shelfPosition );
902
- if ( findCompliance ) {
903
- data.status = findCompliance.compliance;
904
- }
905
- data.rfId = getPosition.rfId;
906
- product.push( data );
907
- } );
908
- data.products = product;
909
- shelfList.push( data );
910
- }
911
- fixtureDetails = { ...fixtureDetails._doc, shelves: shelfList };
912
- return res.sendSuccess( fixtureDetails );
913
- } catch ( e ) {
914
- logger.error( { functionName: 'fixtureShelfProduct', error: e } );
915
- return res.sendError( e, 500 );
916
- }
917
- }
918
-
919
- // export async function fixtureShelfProductv1( req, res ) {
920
- // try {
921
- // const { planoId, fixtureId } = req.body;
922
-
923
- // const [ planogram, fixture ] = await Promise.all( [
924
- // planoService.findOne(
925
- // { _id: new mongoose.Types.ObjectId( planoId ) },
926
- // { storeId: 1, storeName: 1, planoId: '$_id', productResolutionLevel: 1 },
927
- // ),
928
- // storeFixtureService.findOne( {
929
- // _id: new mongoose.Types.ObjectId( fixtureId ),
930
- // } ),
931
- // ] );
932
-
933
- // if ( !planogram ) return res.sendError( 'Planogram not found', 204 );
934
- // if ( !fixture ) return res.sendError( 'Fixture not found', 204 );
935
-
936
- // const currentDate = new Date( dayjs().format( 'YYYY-MM-DD' ) );
937
-
938
- // const getProductsAndVms = async ( mappings ) => {
939
- // const productIds = mappings.map( ( mapping ) => mapping.productId );
940
- // const [ products, vms ] = await Promise.all( [
941
- // planoProductService.find( { _id: { $in: productIds }, type: 'product' } ),
942
- // planoProductService.find( { _id: { $in: productIds }, type: 'vm' } ),
943
- // ] );
944
-
945
- // const productMap = new Map( products.map( ( product ) => [ product._id.toString(), product.toObject() ] ) );
946
- // const vmMap = new Map( vms.map( ( vm ) => [ vm._id.toString(), vm.toObject() ] ) );
947
-
948
- // const productDetails = await Promise.all(
949
- // mappings.filter( ( item ) => item.type === 'product' ).map( async ( mapping ) => {
950
- // const productData = productMap.get( mapping.productId.toString() ) || {};
951
- // const mappingCompliance = await planoComplianceService.findOne( {
952
- // planoMappingId: mapping._id,
953
- // date: currentDate,
954
- // } );
955
- // const status = mappingCompliance ? mappingCompliance.compliance : '';
956
- // return { ...mapping.toObject(), ...productData, status };
957
- // } ),
958
- // );
959
-
960
- // const vmDetails = await Promise.all(
961
- // mappings.filter( ( item ) => item.type === 'vm' ).map( async ( mapping ) => {
962
- // const vmData = vmMap.get( mapping.productId.toString() ) || {};
963
- // return { ...mapping.toObject(), ...vmData };
964
- // } ),
965
- // );
966
-
967
- // return { productDetails, vmDetails };
968
- // };
969
-
970
- // if ( fixture.toObject().productResolutionLevel === 'L1' ) {
971
- // const productMappings = await planoMappingService.find( { fixtureId: new mongoose.Types.ObjectId( fixtureId ) } );
972
- // const { productDetails, vmDetails } = await getProductsAndVms( productMappings );
973
- // return res.sendSuccess( { ...fixture.toObject(), products: productDetails, vms: vmDetails } );
974
- // }
975
- // if ( fixture.toObject().productResolutionLevel === 'L2' || fixture.toObject().productResolutionLevel === 'L4' ) {
976
- // const fixtureShelves = await fixtureShelfService.find( { fixtureId: new mongoose.Types.ObjectId( fixtureId ) } );
977
- // if ( !fixtureShelves.length ) return res.sendError( 'No shelves found for the fixture', 204 );
978
- // const shelfProducts = await Promise.all(
979
- // fixtureShelves.map( async ( shelf ) => {
980
- // const productMappings = await planoMappingService.find( { shelfId: shelf._id } );
981
- // const { productDetails, vmDetails } = await getProductsAndVms( productMappings );
982
- // return { ...shelf.toObject(), products: productDetails, vms: vmDetails };
983
- // } ),
984
- // );
985
- // return res.sendSuccess( { ...fixture.toObject(), shelves: shelfProducts } );
986
- // }
987
- // if ( fixture.toObject().productResolutionLevel === 'L3' ) {
988
- // const fixtureShelves = await fixtureShelfService.find( { fixtureId: new mongoose.Types.ObjectId( fixtureId ) } );
989
- // if ( !fixtureShelves.length ) return res.sendError( 'No shelves found for the fixture', 204 );
990
- // const groupedShelves = await ( async () => {
991
- // const shelfProducts = await Promise.all(
992
- // fixtureShelves.map( async ( shelf ) => {
993
- // const productMappings = await planoMappingService.find( { shelfId: shelf._id } );
994
- // const { productDetails, vmDetails } = await getProductsAndVms( productMappings );
995
- // return { ...shelf.toObject(), products: productDetails, vms: vmDetails };
996
- // } ),
997
- // );
998
- // return shelfProducts.reduce( ( acc, shelf ) => {
999
- // const sectionName = shelf.sectionName || 'Unknown';
1000
- // if ( !acc[sectionName] ) {
1001
- // acc[sectionName] = [];
1002
- // }
1003
- // acc[sectionName].push( shelf );
1004
- // return acc;
1005
- // }, {} );
1006
- // } )();
1007
- // return res.sendSuccess( { ...fixture.toObject(), categories: groupedShelves } );
1008
- // }
1009
- // return res.sendError( 'Incorrect resolution level', 400 );
1010
- // } catch ( e ) {
1011
- // logger.error( { functionName: 'fixtureShelfProductv1', error: e, message: req.body } );
1012
- // return res.sendError( e, 500 );
1013
- // }
1014
- // }
1015
870
 
1016
871
  export async function fixtureShelfProductv1( req, res ) {
1017
872
  try {
@@ -1119,140 +974,6 @@ export async function fixtureShelfProductv1( req, res ) {
1119
974
  }
1120
975
 
1121
976
 
1122
- export async function scan( req, res ) {
1123
- try {
1124
- let shelfId;
1125
- if ( !req.body.planoId ) {
1126
- return res.sendError( 'Plano id is required', 400 );
1127
- }
1128
- if ( !req.body.rfId ) {
1129
- return res.sendError( 'RFID is required', 400 );
1130
- }
1131
- let planoDetails = await planoService.findOne( { _id: req.body.planoId } );
1132
- if ( !planoDetails ) {
1133
- return res.sendError( 'No data found', 204 );
1134
- }
1135
- if ( ![ 'L1', 'L2' ].includes( planoDetails.productResolutionLevel ) ) {
1136
- if ( !req.body.shelfId ) {
1137
- let shelfDetails = await fixtureShelfService.findOne( { rfId: req.body.rfId } );
1138
- if ( !shelfDetails ) {
1139
- return res.sendError( 'Please scan shelf first', 400 );
1140
- }
1141
- return res.sendSuccess( shelfDetails._id );
1142
- }
1143
- }
1144
- if ( planoDetails.productResolutionLevel == 'L5' ) {
1145
- let shelfDetails = await fixtureShelfService.findOne( { _id: req.body.shelfId } );
1146
- if ( !shelfDetails ) {
1147
- return res.sendError( 'No data found', 204 );
1148
- }
1149
- if ( shelfDetails.shelfCapacity < req.body.shelfPosition ) {
1150
- return res.sendError( 'Shelf capacity exceeded', 400 );
1151
- }
1152
- shelfDetails = await fixtureShelfService.find( { sectionName: shelfDetails?.sectionName } );
1153
- shelfId = req.body.shelfId;
1154
- req.body.shelfId = shelfDetails.map( ( ele ) => ele._id );
1155
- }
1156
- let productCheck = await planoMappingService.findOne( { rfId: req.body.rfId } );
1157
- if ( !productCheck ) {
1158
- return res.sendError( 'Product not found', 400 );
1159
- }
1160
- let query;
1161
- switch ( planoDetails.productResolutionLevel ) {
1162
- case 'L1':
1163
- if ( !req.body.floorId ) {
1164
- return res.sendError( 'Floor id is required', 400 );
1165
- }
1166
- query = { floorId: req.body.floorId };
1167
- break;
1168
- case 'L2':
1169
- if ( !req.body.floorId ) {
1170
- return res.sendError( 'Floor id is required', 400 );
1171
- }
1172
- if ( !req.body.fixtureId ) {
1173
- return res.sendError( 'Fixture id is required', 400 );
1174
- }
1175
- query = { floorId: req.body.floorId, fixtureId: req.body.fixtureId };
1176
- break;
1177
- case 'L3':
1178
- if ( !req.body.floorId ) {
1179
- return res.sendError( 'Floor id is required', 400 );
1180
- }
1181
- if ( !req.body.fixtureId ) {
1182
- return res.sendError( 'Fixture id is required', 400 );
1183
- }
1184
- if ( !req.body.shelfId ) {
1185
- return res.sendError( 'Shelf id is required', 400 );
1186
- }
1187
- query = { floorId: req.body.floorId, fixtureId: req.body.fixtureId, shelfId: req.body.shelfId };
1188
- break;
1189
- case 'L4':
1190
- if ( !req.body.floorId ) {
1191
- return res.sendError( 'Floor id is required', 400 );
1192
- }
1193
- if ( !req.body.fixtureId ) {
1194
- return res.sendError( 'Fixture id is required', 400 );
1195
- }
1196
- if ( !req.body.shelfId ) {
1197
- return res.sendError( 'Shelf id is required', 400 );
1198
- }
1199
- if ( !req.body.shelfPosition ) {
1200
- return res.sendError( 'Shelf position is required', 400 );
1201
- }
1202
- query = { floorId: req.body.floorId, fixtureId: req.body.fixtureId, shelfId: req.body.shelfId, shelfPosition: req.body.shelfPosition };
1203
- break;
1204
- case 'L5':
1205
- if ( !req.body.floorId ) {
1206
- return res.sendError( 'Floor id is required', 400 );
1207
- }
1208
- if ( !req.body.fixtureId ) {
1209
- return res.sendError( 'Fixture id is required', 400 );
1210
- }
1211
- if ( !req.body.shelfId ) {
1212
- return res.sendError( 'Shelf id is required', 400 );
1213
- }
1214
- query = { floorId: req.body.floorId, fixtureId: req.body.fixtureId, shelfId: { $in: req.body.shelfId } };
1215
- break;
1216
- default:
1217
- return res.sendError( 'Product not found', 400 );
1218
- break;
1219
- }
1220
- query = { ...query, rfId: req.body.rfId };
1221
-
1222
- let planoProductDetails = await planoMappingService.findOne( query );
1223
- // let data = {
1224
- // ...( planoProductDetails ) ? { ...planoProductDetails._doc } : { planoId: req.body?.planoId, floorId: req.body?.floorId, fixtureId: req.body?.fixtureId, shelfId: shelfId, clientId: planoDetails.clientId, storeName: planoDetails.storeName, storeId: planoDetails.storeId, shelfPosition: req.body?.shelfPosition },
1225
- // rfId: req.body.rfId,
1226
- // compliance: !planoProductDetails ? 'misplaced' : 'proper',
1227
- // date: new Date( dayjs().format( 'YYYY-MM-DD' ) ),
1228
- // };
1229
- let data = {
1230
- planoId: req.body?.planoId,
1231
- floorId: req.body?.floorId,
1232
- fixtureId: req.body?.fixtureId,
1233
- shelfId: shelfId,
1234
- clientId: planoDetails.clientId,
1235
- storeName: planoDetails.storeName,
1236
- storeId: planoDetails.storeId,
1237
- shelfPosition: req.body?.shelfPosition,
1238
- rfId: req.body.rfId,
1239
- compliance: !planoProductDetails ? 'misplaced' : 'proper',
1240
- date: new Date( dayjs().format( 'YYYY-MM-DD' ) ),
1241
- };
1242
- delete data._id;
1243
- delete query.rfId;
1244
- query = { ...query, date: new Date( dayjs().format( 'YYYY-MM-DD' ) ), shelfPosition: req.body.shelfPosition };
1245
- await planoComplianceService.updateOne( query, data );
1246
- if ( !planoProductDetails ) {
1247
- return res.sendSuccess( false );
1248
- }
1249
- return res.sendSuccess( true );
1250
- } catch ( e ) {
1251
- logger.error( { functonName: 'scan', error: e } );
1252
- return res.sendError( e, 500 );
1253
- }
1254
- }
1255
-
1256
977
  export async function scanv1( req, res ) {
1257
978
  try {
1258
979
  if ( !req.body.floorId ) return res.sendError( 'Floor id is required', 400 );
@@ -2055,7 +1776,7 @@ export async function storeFixturesTask( req, res ) {
2055
1776
  } ),
2056
1777
  );
2057
1778
 
2058
- const productCount = await planoMappingService.count( { floorId: floor._id } );
1779
+ // const productCount = await planoMappingService.count( { floorId: floor._id } );
2059
1780
 
2060
1781
  const otherElements = await storeFixtureService.find( {
2061
1782
  floorId: floor._id,
@@ -2878,3 +2599,525 @@ export async function qrScan( req, res ) {
2878
2599
  return res.sendError( e, 500 );
2879
2600
  }
2880
2601
  }
2602
+
2603
+ export async function storeFixturesv2( req, res ) {
2604
+ try {
2605
+ const planoIds = req.body.id
2606
+ .filter( ( id ) => mongoose.Types.ObjectId.isValid( id ) )
2607
+ .map( ( id ) => new mongoose.Types.ObjectId( id ) );
2608
+
2609
+ const planograms = await planoService.find(
2610
+ {
2611
+ $or: [
2612
+ { _id: { $in: planoIds } },
2613
+ { storeId: { $in: req.body.id } },
2614
+ ],
2615
+ },
2616
+ { storeId: 1, storeName: 1, planoId: '$_id', productResolutionLevel: 1, scanType: 1, clientId: 1, validateShelfSections: 1 },
2617
+ );
2618
+
2619
+ if ( !planograms?.length ) return res.sendError( 'No data found', 204 );
2620
+
2621
+ const currentDate = new Date( dayjs().format( 'YYYY-MM-DD' ) );
2622
+
2623
+ const storeLayout = await Promise.all(
2624
+ planograms.map( async ( planogram ) => {
2625
+ const floors = await storeBuilderService.find(
2626
+ { planoId: planogram._id },
2627
+ { floorName: 1, layoutPolygon: 1, planoId: 1 },
2628
+ );
2629
+
2630
+ const floorsWithFixtures = await Promise.all(
2631
+ floors.map( async ( floor ) => {
2632
+ let productCapacity = 0;
2633
+ const layoutPolygonWithFixtures = await Promise.all(
2634
+ floor.layoutPolygon.map( async ( element ) => {
2635
+ const fixtures = await storeFixtureService.findAndSort( {
2636
+ floorId: floor._id,
2637
+ associatedElementType: element.elementType,
2638
+ associatedElementNumber: element.elementNumber,
2639
+ fixtureType: 'wall',
2640
+ }, { shelfcount: 0 }, { fixtureNumber: 1 } );
2641
+
2642
+ const fixturesWithStatus = await Promise.all(
2643
+ fixtures.map( async ( fixture ) => {
2644
+ if ( fixture?.imageUrl ) {
2645
+ let params = {
2646
+ Bucket: JSON.parse( process.env.BUCKET ).storeBuilder,
2647
+ file_path: fixture.imageUrl,
2648
+ };
2649
+ fixture.imageUrl = await signedUrl( params );
2650
+ } else {
2651
+ fixture.imageUrl = '';
2652
+ }
2653
+ productCapacity += fixture.toObject().fixtureCapacity;
2654
+ const productCount = await planoMappingService.count( { fixtureId: fixture._id, type: 'product' } );
2655
+
2656
+ const vmCount = await planoMappingService.count( { fixtureId: fixture._id, type: 'vm' } );
2657
+
2658
+ const complianceCount = await planoComplianceService.count( {
2659
+ fixtureId: fixture._id,
2660
+ compliance: 'proper',
2661
+ date: currentDate,
2662
+ } );
2663
+
2664
+ const shelves = await fixtureShelfService.findAndSort( { fixtureId: fixture._id }, { }, { shelfNumber: 1 } );
2665
+
2666
+ const shelfDetails = await Promise.all(
2667
+ shelves.map( async ( shelf ) => {
2668
+ const productCount = await planoMappingService.count( { fixtureId: fixture._id, shelfId: shelf.toObject(), type: 'product' } );
2669
+
2670
+ const vmCount = await planoMappingService.count( { fixtureId: fixture._id, shelfId: shelf.toObject(), type: 'vm' } );
2671
+
2672
+ return {
2673
+ ...shelf.toObject(),
2674
+ productCount: productCount,
2675
+ vmCount: vmCount,
2676
+ };
2677
+ } ),
2678
+ );
2679
+
2680
+ let fixtureStatus;
2681
+
2682
+ const cvProcessStatus = await planoQrConversionRequestService.count( { fixtureId: fixture._id, date: currentDate, status: 'initiated' } );
2683
+
2684
+ if ( cvProcessStatus ) {
2685
+ fixtureStatus = 'inprogress';
2686
+ } else {
2687
+ const missingCount = await planoComplianceService.count( {
2688
+ fixtureId: fixture._id,
2689
+ compliance: 'missing',
2690
+ date: currentDate,
2691
+ } );
2692
+ fixtureStatus = complianceCount === 0 && !missingCount ? '' : complianceCount === productCount ? 'complete' : 'incomplete';
2693
+ }
2694
+
2695
+
2696
+ const vmDetails = await Promise.all( fixture.toObject()?.vmConfig?.map( async ( vm ) => {
2697
+ const vmInfo = await planoVmService.findOne( { _id: vm.vmId } );
2698
+ return {
2699
+ ...vm,
2700
+ ...vmInfo?.toObject(),
2701
+ };
2702
+ } ) );
2703
+
2704
+ return {
2705
+ ...fixture.toObject(),
2706
+ status: fixtureStatus,
2707
+ shelfCount: shelves.length,
2708
+ productCount: productCount,
2709
+ vmCount: vmCount,
2710
+ shelfConfig: shelfDetails,
2711
+ vmConfig: vmDetails,
2712
+ };
2713
+ } ),
2714
+ );
2715
+
2716
+ const otherElements = await storeFixtureService.find( {
2717
+ floorId: floor._id,
2718
+ associatedElementType: element.elementType,
2719
+ associatedElementNumber: element.elementNumber,
2720
+ fixtureType: 'other',
2721
+ } );
2722
+
2723
+ return {
2724
+ ...element,
2725
+ fixtures: fixturesWithStatus,
2726
+ otherElements: otherElements,
2727
+ };
2728
+ } ),
2729
+ );
2730
+
2731
+ const centerFixtures = await storeFixtureService.find( {
2732
+ floorId: floor._id,
2733
+ fixtureType: 'floor',
2734
+ } );
2735
+
2736
+ const centerFixturesWithStatus = await Promise.all(
2737
+ centerFixtures.map( async ( fixture ) => {
2738
+ if ( fixture?.imageUrl ) {
2739
+ let params = {
2740
+ Bucket: JSON.parse( process.env.BUCKET ).storeBuilder,
2741
+ file_path: fixture.imageUrl,
2742
+ };
2743
+ fixture.imageUrl = await signedUrl( params );
2744
+ } else {
2745
+ fixture.imageUrl = '';
2746
+ }
2747
+ productCapacity += fixture.toObject().fixtureCapacity;
2748
+ const productCount = await planoMappingService.count( { fixtureId: fixture._id, type: 'product' } );
2749
+
2750
+ const vmCount = await planoMappingService.count( { fixtureId: fixture._id, type: 'vm' } );
2751
+
2752
+ const complianceCount = await planoComplianceService.count( {
2753
+ fixtureId: fixture._id,
2754
+ compliance: 'proper',
2755
+ date: currentDate,
2756
+ } );
2757
+
2758
+ const shelves = await fixtureShelfService.findAndSort( { fixtureId: fixture._id }, { }, { shelfNumber: 1 } );
2759
+
2760
+ const shelfDetails = await Promise.all(
2761
+ shelves.map( async ( shelf ) => {
2762
+ const productCount = await planoMappingService.count( { fixtureId: fixture._id, shelfId: shelf.toObject(), type: 'product' } );
2763
+
2764
+ const vmCount = await planoMappingService.count( { fixtureId: fixture._id, shelfId: shelf.toObject(), type: 'vm' } );
2765
+
2766
+ return {
2767
+ ...shelf.toObject(),
2768
+ productCount: productCount,
2769
+ vmCount: vmCount,
2770
+ };
2771
+ } ),
2772
+ );
2773
+
2774
+ let fixtureStatus;
2775
+
2776
+ const cvProcessStatus = await planoQrConversionRequestService.count( { fixtureId: fixture._id, date: currentDate, status: 'initiated' } );
2777
+
2778
+ if ( cvProcessStatus ) {
2779
+ fixtureStatus = 'inprogress';
2780
+ } else {
2781
+ const missingCount = await planoComplianceService.count( {
2782
+ fixtureId: fixture._id,
2783
+ compliance: 'missing',
2784
+ date: currentDate,
2785
+ } );
2786
+ fixtureStatus = complianceCount === 0 && !missingCount ? '' : complianceCount === productCount ? 'complete' : 'incomplete';
2787
+ }
2788
+
2789
+
2790
+ const vmDetails = await Promise.all( fixture.toObject().vmConfig.map( async ( vm ) => {
2791
+ const vmInfo = await planoVmService.findOne( { _id: vm.vmId } );
2792
+
2793
+ return {
2794
+ ...vm,
2795
+ ...vmInfo?.toObject(),
2796
+ };
2797
+ } ) );
2798
+
2799
+ return {
2800
+ ...fixture.toObject(),
2801
+ status: fixtureStatus,
2802
+ shelfCount: shelves.shelves,
2803
+ productCount: productCount,
2804
+ vmCount: vmCount,
2805
+ shelfConfig: shelfDetails,
2806
+ vmConfig: vmDetails,
2807
+
2808
+ };
2809
+ } ),
2810
+ );
2811
+
2812
+
2813
+ const otherElements = await storeFixtureService.find( {
2814
+ floorId: floor._id,
2815
+ associatedElementType: { $exists: false },
2816
+ associatedElementNumber: { $exists: false },
2817
+ fixtureType: 'other',
2818
+ } );
2819
+
2820
+ return {
2821
+ ...floor.toObject(),
2822
+ layoutPolygon: layoutPolygonWithFixtures,
2823
+ centerFixture: centerFixturesWithStatus,
2824
+ productCount: productCapacity,
2825
+ // productCapacity: productCapacity,
2826
+ otherElements: otherElements,
2827
+ };
2828
+ } ),
2829
+ );
2830
+
2831
+ return {
2832
+ ...planogram.toObject(),
2833
+ floors: floorsWithFixtures,
2834
+ };
2835
+ } ),
2836
+ );
2837
+
2838
+ return res.sendSuccess( storeLayout );
2839
+ } catch ( e ) {
2840
+ logger.error( { functionName: 'storeFixturesv1', error: e, message: req.body } );
2841
+ return res.sendError( e, 500 );
2842
+ }
2843
+ }
2844
+
2845
+ export async function fixtureShelfProductv2( req, res ) {
2846
+ try {
2847
+ const { planoId, fixtureId } = req.body;
2848
+
2849
+ const [ planogram, fixture ] = await Promise.all( [
2850
+ planoService.findOne(
2851
+ { _id: new mongoose.Types.ObjectId( planoId ) },
2852
+ { storeId: 1, storeName: 1, planoId: '$_id', productResolutionLevel: 1 },
2853
+ ),
2854
+ storeFixtureService.findOne( { _id: new mongoose.Types.ObjectId( fixtureId ) } ),
2855
+ ] );
2856
+
2857
+ if ( !planogram ) return res.sendError( 'Planogram not found', 204 );
2858
+ if ( !fixture ) return res.sendError( 'Fixture not found', 204 );
2859
+
2860
+
2861
+ const currentDate = new Date( dayjs().format( 'YYYY-MM-DD' ) );
2862
+
2863
+ const getProducts = async ( mappings ) => {
2864
+ const productIds = mappings.map( ( mapping ) => mapping.productId );
2865
+ const products = await planoProductService.find( { _id: { $in: productIds }, type: 'product' } );
2866
+ const productMap = new Map( products.map( ( product ) => [ product._id.toString(), product.toObject() ] ) );
2867
+
2868
+ return await Promise.all(
2869
+ mappings.map( async ( mapping ) => {
2870
+ const productData = productMap.get( mapping?.productId?.toString() ) || {};
2871
+ delete productData._id;
2872
+ const mappingCompliance = await planoComplianceService.findOne( {
2873
+ planoMappingId: mapping._id,
2874
+ date: currentDate,
2875
+ } );
2876
+ const status = mappingCompliance ? mappingCompliance.compliance : '';
2877
+ return { ...mapping.toObject(), ...productData, status };
2878
+ } ),
2879
+ );
2880
+ };
2881
+
2882
+ const vmDetails = await Promise.all( fixture.toObject()?.vmConfig?.map( async ( vm ) => {
2883
+ const vmInfo = await planoVmService.findOne( { _id: vm.vmId } );
2884
+ return {
2885
+ ...vm,
2886
+ ...vmInfo?.toObject(),
2887
+ };
2888
+ } ) );
2889
+
2890
+ if ( fixture.toObject().productResolutionLevel === 'L1' ) {
2891
+ const productMappings = await planoMappingService.find( { fixtureId: new mongoose.Types.ObjectId( fixtureId ), type: 'product' } );
2892
+ const productDetails = await getProducts( productMappings );
2893
+ return res.sendSuccess( { ...fixture.toObject(), products: productDetails, vmConfig: vmDetails, productCount: productMappings.length } );
2894
+ }
2895
+
2896
+ if ( [ 'L2', 'L3', 'L4' ].includes( fixture.toObject().productResolutionLevel ) ) {
2897
+ const fixtureShelves = await fixtureShelfService.findAndSort( { fixtureId: new mongoose.Types.ObjectId( fixtureId ) }, {}, { shelfNumber: 1 } );
2898
+ const productCount = await planoMappingService.count( { fixtureId: new mongoose.Types.ObjectId( fixtureId ), type: 'product' } );
2899
+ const shelfProducts = await Promise.all(
2900
+ fixtureShelves.map( async ( shelf ) => {
2901
+ const productMappings = await planoMappingService.find( { shelfId: shelf._id, type: 'product' } );
2902
+ const productDetails = await getProducts( productMappings );
2903
+ return { ...shelf.toObject(), products: productDetails };
2904
+ } ),
2905
+ );
2906
+ return res.sendSuccess( { ...fixture.toObject(), shelves: shelfProducts, vmConfig: vmDetails, productCount: productCount } );
2907
+ }
2908
+
2909
+ return res.sendError( 'Incorrect resolution level', 400 );
2910
+ } catch ( e ) {
2911
+ logger.error( { functionName: 'fixtureShelfProductv1', error: e, message: req.body } );
2912
+ return res.sendError( e, 500 );
2913
+ }
2914
+ }
2915
+
2916
+ export async function storeFixturesTaskv2( req, res ) {
2917
+ try {
2918
+ const planoIds = req.body.id
2919
+ .filter( ( id ) => mongoose.Types.ObjectId.isValid( id ) )
2920
+ .map( ( id ) => new mongoose.Types.ObjectId( id ) );
2921
+
2922
+ const planograms = await planoService.find(
2923
+ {
2924
+ $or: [
2925
+ { _id: { $in: planoIds } },
2926
+ { storeId: { $in: req.body.id } },
2927
+ ],
2928
+ },
2929
+ { storeId: 1, storeName: 1, planoId: '$_id', productResolutionLevel: 1, scanType: 1, validateShelfSections: 1 },
2930
+ );
2931
+
2932
+ if ( !planograms?.length ) return res.sendError( 'No data found', 204 );
2933
+
2934
+
2935
+ const storeLayout = await Promise.all(
2936
+ planograms.map( async ( planogram ) => {
2937
+ const floors = await storeBuilderService.find(
2938
+ { planoId: planogram._id },
2939
+ { floorName: 1, layoutPolygon: 1, planoId: 1 },
2940
+ );
2941
+
2942
+ const floorsWithFixtures = await Promise.all(
2943
+ floors.map( async ( floor ) => {
2944
+ let productCapacity = 0;
2945
+ const layoutPolygonWithFixtures = await Promise.all(
2946
+ floor.layoutPolygon.map( async ( element ) => {
2947
+ const fixtures = await storeFixtureService.findAndSort( {
2948
+ floorId: floor._id,
2949
+ associatedElementType: element.elementType,
2950
+ associatedElementNumber: element.elementNumber,
2951
+ fixtureType: 'wall',
2952
+ }, { shelfcount: 0 }, { fixtureNumber: 1 } );
2953
+
2954
+ const fixturesWithStatus = await Promise.all(
2955
+ fixtures.map( async ( fixture ) => {
2956
+ if ( fixture?.imageUrl || fixture.vmImageUrl ) {
2957
+ let params = {
2958
+ Bucket: JSON.parse( process.env.BUCKET ).storeBuilder,
2959
+ file_path: req.body.type == 'vm' ? fixture.vmImageUrl : fixture.imageUrl,
2960
+ };
2961
+ fixture.imageUrl = await signedUrl( params );
2962
+ } else {
2963
+ fixture.imageUrl = '';
2964
+ }
2965
+ productCapacity += fixture.toObject().fixtureCapacity;
2966
+ const productCount = await planoMappingService.count( { fixtureId: fixture._id, type: 'product' } );
2967
+
2968
+ const vmCount = await planoMappingService.count( { fixtureId: fixture._id, type: 'vm' } );
2969
+
2970
+ const compliance = await planoTaskComplianceService.findOne( {
2971
+ fixtureId: fixture._id,
2972
+ type: req.body?.type ? req.body.type : 'fixture',
2973
+ date_string: req.body?.date,
2974
+ }, { status: 1 } );
2975
+
2976
+ const shelves = await fixtureShelfService.findAndSort( { fixtureId: fixture._id }, { shelfNumber: 1, sectionName: 1, sectionZone: 1, shelfCapacity: 1, shelfSplitup: 1 }, { shelfNumber: 1 } );
2977
+
2978
+ const shelfDetails = await Promise.all(
2979
+ shelves.map( async ( shelf ) => {
2980
+ const productCount = await planoMappingService.count( { fixtureId: fixture._id, shelfId: shelf.toObject(), type: 'product' } );
2981
+
2982
+ const vmCount = await planoMappingService.count( { fixtureId: fixture._id, shelfId: shelf.toObject(), type: 'vm' } );
2983
+
2984
+ return {
2985
+ ...shelf.toObject(),
2986
+ productCount: productCount,
2987
+ vmCount: vmCount,
2988
+ };
2989
+ } ),
2990
+ );
2991
+
2992
+
2993
+ const vmDetails = await Promise.all( fixture.toObject()?.vmConfig?.map( async ( vm ) => {
2994
+ const vmInfo = await planoVmService.findOne( { _id: vm.vmId } );
2995
+ return {
2996
+ ...vm,
2997
+ ...vmInfo?.toObject(),
2998
+ };
2999
+ } ) );
3000
+
3001
+ return {
3002
+ ...fixture.toObject(),
3003
+ status: compliance?.status ? compliance.status : '',
3004
+ shelfCount: shelves.length,
3005
+ productCount: productCount,
3006
+ vmCount: vmCount,
3007
+ shelfConfig: shelfDetails,
3008
+ vmConfig: vmDetails,
3009
+ };
3010
+ } ),
3011
+ );
3012
+
3013
+ const otherElements = await storeFixtureService.find( {
3014
+ floorId: floor._id,
3015
+ associatedElementType: element.elementType,
3016
+ associatedElementNumber: element.elementNumber,
3017
+ fixtureType: 'other',
3018
+ } );
3019
+
3020
+ return {
3021
+ ...element,
3022
+ fixtures: fixturesWithStatus,
3023
+ otherElements: otherElements,
3024
+ };
3025
+ } ),
3026
+ );
3027
+
3028
+ const centerFixtures = await storeFixtureService.find( {
3029
+ floorId: floor._id,
3030
+ fixtureType: 'floor',
3031
+ } );
3032
+
3033
+ const centerFixturesWithStatus = await Promise.all(
3034
+ centerFixtures.map( async ( fixture ) => {
3035
+ if ( fixture?.imageUrl || fixture.vmImageUrl ) {
3036
+ let params = {
3037
+ Bucket: JSON.parse( process.env.BUCKET ).storeBuilder,
3038
+ file_path: req.body.type == 'vm' ? fixture.vmImageUrl : fixture.imageUrl,
3039
+ };
3040
+ fixture.imageUrl = await signedUrl( params );
3041
+ } else {
3042
+ fixture.imageUrl = '';
3043
+ }
3044
+ productCapacity += fixture.toObject().fixtureCapacity;
3045
+ const productCount = await planoMappingService.count( { fixtureId: fixture._id, type: 'product' } );
3046
+
3047
+ const vmCount = await planoMappingService.count( { fixtureId: fixture._id, type: 'vm' } );
3048
+
3049
+ const compliance = await planoTaskComplianceService.findOne( {
3050
+ fixtureId: fixture._id,
3051
+ type: req.body?.type ? req.body.type : 'fixture',
3052
+ date_string: req.body?.date,
3053
+ }, { status: 1 } );
3054
+
3055
+ const shelves = await fixtureShelfService.findAndSort( { fixtureId: fixture._id }, { shelfNumber: 1, sectionName: 1, sectionZone: 1, shelfCapacity: 1, shelfSplitup: 1 }, { shelfNumber: 1 } );
3056
+
3057
+ const shelfDetails = await Promise.all(
3058
+ shelves.map( async ( shelf ) => {
3059
+ const productCount = await planoMappingService.count( { fixtureId: fixture._id, shelfId: shelf.toObject(), type: 'product' } );
3060
+
3061
+ const vmCount = await planoMappingService.count( { fixtureId: fixture._id, shelfId: shelf.toObject(), type: 'vm' } );
3062
+
3063
+ return {
3064
+ ...shelf.toObject(),
3065
+ productCount: productCount,
3066
+ vmCount: vmCount,
3067
+ };
3068
+ } ),
3069
+ );
3070
+
3071
+ const vmDetails = await Promise.all( fixture.toObject()?.vmConfig?.map( async ( vm ) => {
3072
+ const vmInfo = await planoVmService.findOne( { _id: vm.vmId } );
3073
+ return {
3074
+ ...vm,
3075
+ ...vmInfo?.toObject(),
3076
+ };
3077
+ } ) );
3078
+
3079
+ return {
3080
+ ...fixture.toObject(),
3081
+ status: compliance?.status ? compliance.status : '',
3082
+ shelfCount: shelves.shelves,
3083
+ productCount: productCount,
3084
+ vmCount: vmCount,
3085
+ shelfConfig: shelfDetails,
3086
+ vms: vmDetails,
3087
+ };
3088
+ } ),
3089
+ );
3090
+
3091
+ // const productCount = await planoMappingService.count( { floorId: floor._id } );
3092
+
3093
+ const otherElements = await storeFixtureService.find( {
3094
+ floorId: floor._id,
3095
+ associatedElementType: { $exists: false },
3096
+ associatedElementNumber: { $exists: false },
3097
+ fixtureType: 'other',
3098
+ } );
3099
+
3100
+ return {
3101
+ ...floor.toObject(),
3102
+ layoutPolygon: layoutPolygonWithFixtures,
3103
+ centerFixture: centerFixturesWithStatus,
3104
+ productCount: productCapacity,
3105
+ // productCapacity: productCapacity,
3106
+ otherElements: otherElements,
3107
+ };
3108
+ } ),
3109
+ );
3110
+
3111
+ return {
3112
+ ...planogram.toObject(),
3113
+ floors: floorsWithFixtures,
3114
+ };
3115
+ } ),
3116
+ );
3117
+
3118
+ return res.sendSuccess( storeLayout );
3119
+ } catch ( e ) {
3120
+ logger.error( { functionName: 'storeFixturesTask', error: e, message: req.body } );
3121
+ return res.sendError( e, 500 );
3122
+ }
3123
+ }