tango-app-api-store-builder 1.0.0-beta-96 → 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 );
@@ -759,7 +760,7 @@ export async function storeFixturesv1( req, res ) {
759
760
  } ),
760
761
  );
761
762
 
762
- const productCount = await planoMappingService.count( { floorId: floor._id } );
763
+ // const productCount = await planoMappingService.count( { floorId: floor._id } );
763
764
 
764
765
  const otherElements = await storeFixtureService.find( {
765
766
  floorId: floor._id,
@@ -866,158 +867,6 @@ export async function updateStatus( req, res ) {
866
867
  }
867
868
  }
868
869
 
869
- export async function fixtureShelfProduct( req, res ) {
870
- try {
871
- if ( !req.body.fixtureId ) {
872
- return res.sendError( 'Fixture id is required', 400 );
873
- }
874
-
875
- let fixtureDetails = await storeFixtureService.findOne( { _id: req.body.fixtureId } );
876
- if ( !fixtureDetails ) {
877
- return res.sendError( 'Fixture not found', 204 );
878
- }
879
- // let planoDetails = await planoService.findOne( { _id: fixtureDetails.planoId } );
880
- let shelfDetails = await fixtureShelfService.find( { fixtureId: req.body.fixtureId } );
881
- // let query;
882
- // switch ( planoDetails.productResolutionLevel ) {
883
- // case 'L1':
884
- // query = { floorId: fixtureDetails.floorId };
885
- // break;
886
- // default:
887
- // query = { floorId: fixtureDetails.floorId, fixtureId: req.body.fixtureId };
888
- // break;
889
- // }
890
- // query = {
891
- // ...query,
892
- // ...( [ 'L3', 'L4' ].includes( planoDetails.productResolutionLevel ) ) ? { shelfId: shelf._id } : {},
893
- // productId: { $in: productIdList },
894
- // date: new Date( dayjs().format( 'YYYY-MM-DD' ) ),
895
- // };
896
- let shelfList = [];
897
- for ( let shelf of shelfDetails ) {
898
- let data = { ...shelf._doc, products: [] };
899
- let productMappingDetails = await planoMappingService.find( { shelfId: shelf._id } );
900
- let productIdList = productMappingDetails.map( ( item ) => item.productId );
901
- let productDetails = await planoProductService.find( { _id: productIdList } );
902
- let productComplianceDetails = await planoComplianceService.find( { date: new Date( dayjs().format( 'YYYY-MM-DD' ) ), shelfId: shelf._id } );
903
- let product = [];
904
- productDetails.forEach( ( item ) => {
905
- let data = { ...item._doc, status: 'missing', rfId: '' };
906
- let getPosition = productMappingDetails.find( ( ele ) => ele.productId.toString() == item._id.toString() );
907
- let findCompliance = productComplianceDetails.find( ( ele ) => ele.shelfPosition == getPosition.shelfPosition );
908
- if ( findCompliance ) {
909
- data.status = findCompliance.compliance;
910
- }
911
- data.rfId = getPosition.rfId;
912
- product.push( data );
913
- } );
914
- data.products = product;
915
- shelfList.push( data );
916
- }
917
- fixtureDetails = { ...fixtureDetails._doc, shelves: shelfList };
918
- return res.sendSuccess( fixtureDetails );
919
- } catch ( e ) {
920
- logger.error( { functionName: 'fixtureShelfProduct', error: e } );
921
- return res.sendError( e, 500 );
922
- }
923
- }
924
-
925
- // export async function fixtureShelfProductv1( req, res ) {
926
- // try {
927
- // const { planoId, fixtureId } = req.body;
928
-
929
- // const [ planogram, fixture ] = await Promise.all( [
930
- // planoService.findOne(
931
- // { _id: new mongoose.Types.ObjectId( planoId ) },
932
- // { storeId: 1, storeName: 1, planoId: '$_id', productResolutionLevel: 1 },
933
- // ),
934
- // storeFixtureService.findOne( {
935
- // _id: new mongoose.Types.ObjectId( fixtureId ),
936
- // } ),
937
- // ] );
938
-
939
- // if ( !planogram ) return res.sendError( 'Planogram not found', 204 );
940
- // if ( !fixture ) return res.sendError( 'Fixture not found', 204 );
941
-
942
- // const currentDate = new Date( dayjs().format( 'YYYY-MM-DD' ) );
943
-
944
- // const getProductsAndVms = async ( mappings ) => {
945
- // const productIds = mappings.map( ( mapping ) => mapping.productId );
946
- // const [ products, vms ] = await Promise.all( [
947
- // planoProductService.find( { _id: { $in: productIds }, type: 'product' } ),
948
- // planoProductService.find( { _id: { $in: productIds }, type: 'vm' } ),
949
- // ] );
950
-
951
- // const productMap = new Map( products.map( ( product ) => [ product._id.toString(), product.toObject() ] ) );
952
- // const vmMap = new Map( vms.map( ( vm ) => [ vm._id.toString(), vm.toObject() ] ) );
953
-
954
- // const productDetails = await Promise.all(
955
- // mappings.filter( ( item ) => item.type === 'product' ).map( async ( mapping ) => {
956
- // const productData = productMap.get( mapping.productId.toString() ) || {};
957
- // const mappingCompliance = await planoComplianceService.findOne( {
958
- // planoMappingId: mapping._id,
959
- // date: currentDate,
960
- // } );
961
- // const status = mappingCompliance ? mappingCompliance.compliance : '';
962
- // return { ...mapping.toObject(), ...productData, status };
963
- // } ),
964
- // );
965
-
966
- // const vmDetails = await Promise.all(
967
- // mappings.filter( ( item ) => item.type === 'vm' ).map( async ( mapping ) => {
968
- // const vmData = vmMap.get( mapping.productId.toString() ) || {};
969
- // return { ...mapping.toObject(), ...vmData };
970
- // } ),
971
- // );
972
-
973
- // return { productDetails, vmDetails };
974
- // };
975
-
976
- // if ( fixture.toObject().productResolutionLevel === 'L1' ) {
977
- // const productMappings = await planoMappingService.find( { fixtureId: new mongoose.Types.ObjectId( fixtureId ) } );
978
- // const { productDetails, vmDetails } = await getProductsAndVms( productMappings );
979
- // return res.sendSuccess( { ...fixture.toObject(), products: productDetails, vms: vmDetails } );
980
- // }
981
- // if ( fixture.toObject().productResolutionLevel === 'L2' || fixture.toObject().productResolutionLevel === 'L4' ) {
982
- // const fixtureShelves = await fixtureShelfService.find( { fixtureId: new mongoose.Types.ObjectId( fixtureId ) } );
983
- // if ( !fixtureShelves.length ) return res.sendError( 'No shelves found for the fixture', 204 );
984
- // const shelfProducts = await Promise.all(
985
- // fixtureShelves.map( async ( shelf ) => {
986
- // const productMappings = await planoMappingService.find( { shelfId: shelf._id } );
987
- // const { productDetails, vmDetails } = await getProductsAndVms( productMappings );
988
- // return { ...shelf.toObject(), products: productDetails, vms: vmDetails };
989
- // } ),
990
- // );
991
- // return res.sendSuccess( { ...fixture.toObject(), shelves: shelfProducts } );
992
- // }
993
- // if ( fixture.toObject().productResolutionLevel === 'L3' ) {
994
- // const fixtureShelves = await fixtureShelfService.find( { fixtureId: new mongoose.Types.ObjectId( fixtureId ) } );
995
- // if ( !fixtureShelves.length ) return res.sendError( 'No shelves found for the fixture', 204 );
996
- // const groupedShelves = await ( async () => {
997
- // const shelfProducts = await Promise.all(
998
- // fixtureShelves.map( async ( shelf ) => {
999
- // const productMappings = await planoMappingService.find( { shelfId: shelf._id } );
1000
- // const { productDetails, vmDetails } = await getProductsAndVms( productMappings );
1001
- // return { ...shelf.toObject(), products: productDetails, vms: vmDetails };
1002
- // } ),
1003
- // );
1004
- // return shelfProducts.reduce( ( acc, shelf ) => {
1005
- // const sectionName = shelf.sectionName || 'Unknown';
1006
- // if ( !acc[sectionName] ) {
1007
- // acc[sectionName] = [];
1008
- // }
1009
- // acc[sectionName].push( shelf );
1010
- // return acc;
1011
- // }, {} );
1012
- // } )();
1013
- // return res.sendSuccess( { ...fixture.toObject(), categories: groupedShelves } );
1014
- // }
1015
- // return res.sendError( 'Incorrect resolution level', 400 );
1016
- // } catch ( e ) {
1017
- // logger.error( { functionName: 'fixtureShelfProductv1', error: e, message: req.body } );
1018
- // return res.sendError( e, 500 );
1019
- // }
1020
- // }
1021
870
 
1022
871
  export async function fixtureShelfProductv1( req, res ) {
1023
872
  try {
@@ -1125,140 +974,6 @@ export async function fixtureShelfProductv1( req, res ) {
1125
974
  }
1126
975
 
1127
976
 
1128
- export async function scan( req, res ) {
1129
- try {
1130
- let shelfId;
1131
- if ( !req.body.planoId ) {
1132
- return res.sendError( 'Plano id is required', 400 );
1133
- }
1134
- if ( !req.body.rfId ) {
1135
- return res.sendError( 'RFID is required', 400 );
1136
- }
1137
- let planoDetails = await planoService.findOne( { _id: req.body.planoId } );
1138
- if ( !planoDetails ) {
1139
- return res.sendError( 'No data found', 204 );
1140
- }
1141
- if ( ![ 'L1', 'L2' ].includes( planoDetails.productResolutionLevel ) ) {
1142
- if ( !req.body.shelfId ) {
1143
- let shelfDetails = await fixtureShelfService.findOne( { rfId: req.body.rfId } );
1144
- if ( !shelfDetails ) {
1145
- return res.sendError( 'Please scan shelf first', 400 );
1146
- }
1147
- return res.sendSuccess( shelfDetails._id );
1148
- }
1149
- }
1150
- if ( planoDetails.productResolutionLevel == 'L5' ) {
1151
- let shelfDetails = await fixtureShelfService.findOne( { _id: req.body.shelfId } );
1152
- if ( !shelfDetails ) {
1153
- return res.sendError( 'No data found', 204 );
1154
- }
1155
- if ( shelfDetails.shelfCapacity < req.body.shelfPosition ) {
1156
- return res.sendError( 'Shelf capacity exceeded', 400 );
1157
- }
1158
- shelfDetails = await fixtureShelfService.find( { sectionName: shelfDetails?.sectionName } );
1159
- shelfId = req.body.shelfId;
1160
- req.body.shelfId = shelfDetails.map( ( ele ) => ele._id );
1161
- }
1162
- let productCheck = await planoMappingService.findOne( { rfId: req.body.rfId } );
1163
- if ( !productCheck ) {
1164
- return res.sendError( 'Product not found', 400 );
1165
- }
1166
- let query;
1167
- switch ( planoDetails.productResolutionLevel ) {
1168
- case 'L1':
1169
- if ( !req.body.floorId ) {
1170
- return res.sendError( 'Floor id is required', 400 );
1171
- }
1172
- query = { floorId: req.body.floorId };
1173
- break;
1174
- case 'L2':
1175
- if ( !req.body.floorId ) {
1176
- return res.sendError( 'Floor id is required', 400 );
1177
- }
1178
- if ( !req.body.fixtureId ) {
1179
- return res.sendError( 'Fixture id is required', 400 );
1180
- }
1181
- query = { floorId: req.body.floorId, fixtureId: req.body.fixtureId };
1182
- break;
1183
- case 'L3':
1184
- if ( !req.body.floorId ) {
1185
- return res.sendError( 'Floor id is required', 400 );
1186
- }
1187
- if ( !req.body.fixtureId ) {
1188
- return res.sendError( 'Fixture id is required', 400 );
1189
- }
1190
- if ( !req.body.shelfId ) {
1191
- return res.sendError( 'Shelf id is required', 400 );
1192
- }
1193
- query = { floorId: req.body.floorId, fixtureId: req.body.fixtureId, shelfId: req.body.shelfId };
1194
- break;
1195
- case 'L4':
1196
- if ( !req.body.floorId ) {
1197
- return res.sendError( 'Floor id is required', 400 );
1198
- }
1199
- if ( !req.body.fixtureId ) {
1200
- return res.sendError( 'Fixture id is required', 400 );
1201
- }
1202
- if ( !req.body.shelfId ) {
1203
- return res.sendError( 'Shelf id is required', 400 );
1204
- }
1205
- if ( !req.body.shelfPosition ) {
1206
- return res.sendError( 'Shelf position is required', 400 );
1207
- }
1208
- query = { floorId: req.body.floorId, fixtureId: req.body.fixtureId, shelfId: req.body.shelfId, shelfPosition: req.body.shelfPosition };
1209
- break;
1210
- case 'L5':
1211
- if ( !req.body.floorId ) {
1212
- return res.sendError( 'Floor id is required', 400 );
1213
- }
1214
- if ( !req.body.fixtureId ) {
1215
- return res.sendError( 'Fixture id is required', 400 );
1216
- }
1217
- if ( !req.body.shelfId ) {
1218
- return res.sendError( 'Shelf id is required', 400 );
1219
- }
1220
- query = { floorId: req.body.floorId, fixtureId: req.body.fixtureId, shelfId: { $in: req.body.shelfId } };
1221
- break;
1222
- default:
1223
- return res.sendError( 'Product not found', 400 );
1224
- break;
1225
- }
1226
- query = { ...query, rfId: req.body.rfId };
1227
-
1228
- let planoProductDetails = await planoMappingService.findOne( query );
1229
- // let data = {
1230
- // ...( 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 },
1231
- // rfId: req.body.rfId,
1232
- // compliance: !planoProductDetails ? 'misplaced' : 'proper',
1233
- // date: new Date( dayjs().format( 'YYYY-MM-DD' ) ),
1234
- // };
1235
- let data = {
1236
- planoId: req.body?.planoId,
1237
- floorId: req.body?.floorId,
1238
- fixtureId: req.body?.fixtureId,
1239
- shelfId: shelfId,
1240
- clientId: planoDetails.clientId,
1241
- storeName: planoDetails.storeName,
1242
- storeId: planoDetails.storeId,
1243
- shelfPosition: req.body?.shelfPosition,
1244
- rfId: req.body.rfId,
1245
- compliance: !planoProductDetails ? 'misplaced' : 'proper',
1246
- date: new Date( dayjs().format( 'YYYY-MM-DD' ) ),
1247
- };
1248
- delete data._id;
1249
- delete query.rfId;
1250
- query = { ...query, date: new Date( dayjs().format( 'YYYY-MM-DD' ) ), shelfPosition: req.body.shelfPosition };
1251
- await planoComplianceService.updateOne( query, data );
1252
- if ( !planoProductDetails ) {
1253
- return res.sendSuccess( false );
1254
- }
1255
- return res.sendSuccess( true );
1256
- } catch ( e ) {
1257
- logger.error( { functonName: 'scan', error: e } );
1258
- return res.sendError( e, 500 );
1259
- }
1260
- }
1261
-
1262
977
  export async function scanv1( req, res ) {
1263
978
  try {
1264
979
  if ( !req.body.floorId ) return res.sendError( 'Floor id is required', 400 );
@@ -2061,7 +1776,7 @@ export async function storeFixturesTask( req, res ) {
2061
1776
  } ),
2062
1777
  );
2063
1778
 
2064
- const productCount = await planoMappingService.count( { floorId: floor._id } );
1779
+ // const productCount = await planoMappingService.count( { floorId: floor._id } );
2065
1780
 
2066
1781
  const otherElements = await storeFixtureService.find( {
2067
1782
  floorId: floor._id,
@@ -2884,3 +2599,525 @@ export async function qrScan( req, res ) {
2884
2599
  return res.sendError( e, 500 );
2885
2600
  }
2886
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
+ }