tango-app-api-audit 3.4.2 → 3.4.3-alpha.0

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 {
15
15
  createUserAudit,
16
16
  findOneUserAudit,
17
17
  updateOneUserAudit,
18
+ aggregateUserAuditCount,
18
19
  } from '../service/userAudit.service.js';
19
20
  import {
20
21
  aggregateAssignAudit,
@@ -61,6 +62,7 @@ import {
61
62
  import { aggregateStoreEmpDetection } from '../service/storeEmpDetection.service.js';
62
63
  import { aggregateBinaryAudit } from '../service/binaryAudit.service.js';
63
64
  import { aggregateTraxAuditData, findOneTraxAuditData } from '../service/traxAuditData.service.js';
65
+ import { createAuditUserWallet } from '../service/auditUserWallet.service.js';
64
66
 
65
67
  /* < -- *** Configuration *** --> */
66
68
 
@@ -341,7 +343,7 @@ export async function getAuditFile( req, res ) {
341
343
  ) {
342
344
  const logData = {
343
345
  userId: log.userId,
344
- userName: userdata.name,
346
+ userName: userdata.userName,
345
347
  logType: 'audit',
346
348
  logSubType: 'auditStart',
347
349
  logData: {
@@ -421,7 +423,7 @@ export async function getAuditFile( req, res ) {
421
423
  zoneName: msg.zone_id,
422
424
  moduleType: inputData.moduleType,
423
425
  };
424
- const query ={
426
+ const query = {
425
427
  storeId: msg.store_id,
426
428
  zoneName: msg.zone_id,
427
429
  fileDate: msg.curr_date,
@@ -443,7 +445,7 @@ export async function getAuditFile( req, res ) {
443
445
  if ( inputData.nextId ) {
444
446
  const logData = {
445
447
  userId: req.user._id,
446
- userName: userdata.name,
448
+ userName: userdata.userName,
447
449
  logType: 'audit',
448
450
  logSubType: 'auditStart',
449
451
  logData: {
@@ -549,7 +551,7 @@ export async function getAuditFile( req, res ) {
549
551
  zoneName: msg.zone_id,
550
552
  moduleType: inputData.moduleType,
551
553
  };
552
- const query ={
554
+ const query = {
553
555
  storeId: msg.store_id,
554
556
  zoneName: msg.zone_id,
555
557
  fileDate: msg.curr_date,
@@ -595,7 +597,7 @@ export async function getAuditFile( req, res ) {
595
597
  if ( inputData.nextId ) {
596
598
  const logData = {
597
599
  userId: insertData.userId,
598
- userName: userdata.name,
600
+ userName: userdata.userName,
599
601
  logType: 'audit',
600
602
  logSubType: 'auditStart',
601
603
  logData: {
@@ -751,7 +753,7 @@ export async function workSpace( req, res ) {
751
753
  ( inputData.offset - 1 ) * limit :
752
754
  0;
753
755
  const dateRange = await getDate( new Date(), new Date() );
754
- const filter =[
756
+ const filter = [
755
757
  { status: { $in: [ 'active', 'hold' ] } },
756
758
  { 'auditConfigs.audit': { $eq: true } },
757
759
  { clientId: { $in: inputData.clientId } },
@@ -1063,26 +1065,26 @@ export async function workSpace( req, res ) {
1063
1065
  pendingByUser:
1064
1066
  merge[i].pendingCount >= 0 ?
1065
1067
  Number( merge[i].pendingCount ) +
1066
- ( merge[i].clientAsignedCount ?
1067
- Number( merge[i].clientAsignedCount ) :
1068
- 0 ) :
1068
+ ( merge[i].clientAsignedCount ?
1069
+ Number( merge[i].clientAsignedCount ) :
1070
+ 0 ) :
1069
1071
  0,
1070
1072
  totalCount: merge[i].totalCount,
1071
1073
  queueName: merge[i].queueName,
1072
1074
  isDraft: merge[i]?.isDrafted || false,
1073
1075
  isEnable:
1074
1076
  Number( pending ) > 0 ||
1075
- merge[i].isDrafted ||
1076
- ( merge[i].userAsignedCount && merge[i].userAsignedCount > 0 ) ||
1077
- ( merge[i].userInprogressCount && merge[i].userInprogressCount > 0 ) ?
1077
+ merge[i].isDrafted ||
1078
+ ( merge[i].userAsignedCount && merge[i].userAsignedCount > 0 ) ||
1079
+ ( merge[i].userInprogressCount && merge[i].userInprogressCount > 0 ) ?
1078
1080
  1 :
1079
1081
  0,
1080
1082
  completedRatio: merge[i].totalCount ?
1081
1083
  merge[i].completedStoresCount ?
1082
1084
  Math.round(
1083
1085
  ( Number( merge[i].completedStoresCount ) /
1084
- Number( merge[i].totalCount ) ) *
1085
- 100,
1086
+ Number( merge[i].totalCount ) ) *
1087
+ 100,
1086
1088
  ) :
1087
1089
  0 :
1088
1090
  0,
@@ -1406,7 +1408,7 @@ export async function save( req, res ) {
1406
1408
  fileDate: inputData.fileDate,
1407
1409
  moduleType: inputData.moduleType,
1408
1410
  zoneName: inputData.zoneName,
1409
- // auditType: inputData.auditType,
1411
+ // auditType: inputData.auditType,
1410
1412
  },
1411
1413
  {
1412
1414
  status: 'completed',
@@ -1447,7 +1449,7 @@ export async function save( req, res ) {
1447
1449
  export async function userAuditHistory( req, res ) {
1448
1450
  try {
1449
1451
  const inputData = req.body;
1450
- const userId = inputData.userId? new mongoose.Types.ObjectId( inputData.userId ) : req.user._id;
1452
+ const userId = inputData.userId ? new mongoose.Types.ObjectId( inputData.userId ) : req.user._id;
1451
1453
  const limit = inputData.limit || 10;
1452
1454
  const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
1453
1455
  const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
@@ -1559,12 +1561,15 @@ export async function userAuditHistory( req, res ) {
1559
1561
  moduleType: 1,
1560
1562
  beforeCount: 1,
1561
1563
  afterCount: 1,
1562
- accuracy: { $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, { $round: [
1563
- {
1564
- $multiply: [ { $divide: [ '$afterCount', '$beforeCount' ] }, 100 ],
1565
- }, 1,
1566
- ],
1567
- }, null ] },
1564
+ accuracy: {
1565
+ $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, {
1566
+ $round: [
1567
+ {
1568
+ $multiply: [ { $divide: [ '$afterCount', '$beforeCount' ] }, 100 ],
1569
+ }, 1,
1570
+ ],
1571
+ }, null ],
1572
+ },
1568
1573
 
1569
1574
  startTime: {
1570
1575
  $dateToString: {
@@ -1614,7 +1619,7 @@ export async function userAuditHistory( req, res ) {
1614
1619
  },
1615
1620
  ];
1616
1621
 
1617
- if ( inputData.searchValue && inputData.searchValue!== '' ) {
1622
+ if ( inputData.searchValue && inputData.searchValue !== '' ) {
1618
1623
  query.push( {
1619
1624
  $match: {
1620
1625
  $or: [
@@ -1656,7 +1661,7 @@ export async function userAuditHistory( req, res ) {
1656
1661
  'Brand Name': element.clientName,
1657
1662
  'Store ID': element.storeId,
1658
1663
  'Store Name': element.storeName,
1659
- 'Zone Name': element.moduleType == 'zone'? element.zoneName : '',
1664
+ 'Zone Name': element.moduleType == 'zone' ? element.zoneName : '',
1660
1665
  'Audit Type': element.auditType,
1661
1666
  'Product Type': element.moduleType,
1662
1667
  'Before Count': element.beforeCount,
@@ -1716,7 +1721,7 @@ export async function clientMetrics( req, res ) {
1716
1721
 
1717
1722
  ];
1718
1723
 
1719
- if ( inputData?.filterByClient?.length> 0 ) {
1724
+ if ( inputData?.filterByClient?.length > 0 ) {
1720
1725
  filter.push(
1721
1726
  { clientId: { $in: inputData.filterByClient } },
1722
1727
  );
@@ -1788,21 +1793,22 @@ export async function clientMetrics( req, res ) {
1788
1793
  inprogressStores: 1,
1789
1794
  notAssignedStores: 1,
1790
1795
  fileCount: { $ifNull: [ '$fileCount', 0 ] },
1791
- completionPercentage: { $ifNull: [
1792
- {
1793
- $round: [ {
1794
- $multiply: [
1795
- 100, {
1796
- $divide: [
1797
- '$completedStores', '$$totalAuditFiles',
1798
- ],
1799
- },
1800
- ],
1801
- }, 1 ],
1802
-
1803
- },
1804
- 0,
1805
- ],
1796
+ completionPercentage: {
1797
+ $ifNull: [
1798
+ {
1799
+ $round: [ {
1800
+ $multiply: [
1801
+ 100, {
1802
+ $divide: [
1803
+ '$completedStores', '$$totalAuditFiles',
1804
+ ],
1805
+ },
1806
+ ],
1807
+ }, 1 ],
1808
+
1809
+ },
1810
+ 0,
1811
+ ],
1806
1812
  },
1807
1813
  },
1808
1814
  },
@@ -1836,7 +1842,7 @@ export async function clientMetrics( req, res ) {
1836
1842
  },
1837
1843
  ];
1838
1844
 
1839
- if ( inputData?.filterByStatus?.length> 0 ) {
1845
+ if ( inputData?.filterByStatus?.length > 0 ) {
1840
1846
  query.push(
1841
1847
  {
1842
1848
  $match: { clientStatus: { $in: inputData.filterByStatus } },
@@ -1845,7 +1851,7 @@ export async function clientMetrics( req, res ) {
1845
1851
  );
1846
1852
  }
1847
1853
 
1848
- if ( inputData.searchValue && inputData.searchValue!== '' ) {
1854
+ if ( inputData.searchValue && inputData.searchValue !== '' ) {
1849
1855
  query.push( {
1850
1856
  $match: {
1851
1857
  $or: [
@@ -1991,12 +1997,15 @@ export async function storeMetrics( req, res ) {
1991
1997
  auditType: 1,
1992
1998
  beforeCount: 1,
1993
1999
  afterCount: { $ifNull: [ '$afterCount', null ] },
1994
- accuracy: { $cond: [ { $eq: [ '$status', 'completed' ] }, { $round: [
1995
- {
1996
- $multiply: [ { $divide: [ '$afterCount', '$beforeCount' ] }, 100 ],
1997
- }, 1,
1998
- ],
1999
- }, null ] },
2000
+ accuracy: {
2001
+ $cond: [ { $eq: [ '$status', 'completed' ] }, {
2002
+ $round: [
2003
+ {
2004
+ $multiply: [ { $divide: [ '$afterCount', '$beforeCount' ] }, 100 ],
2005
+ }, 1,
2006
+ ],
2007
+ }, null ],
2008
+ },
2000
2009
  timeSpent: 1,
2001
2010
  status: 1,
2002
2011
  },
@@ -2071,12 +2080,15 @@ export async function storeMetrics( req, res ) {
2071
2080
  auditType: 1,
2072
2081
  beforeCount: 1,
2073
2082
  afterCount: { $ifNull: [ '$afterCount', null ] },
2074
- accuracy: { $cond: [ { $eq: [ '$status', 'completed' ] }, { $round: [
2075
- {
2076
- $multiply: [ { $divide: [ '$afterCount', '$beforeCount' ] }, 100 ],
2077
- }, 1,
2078
- ],
2079
- }, null ] },
2083
+ accuracy: {
2084
+ $cond: [ { $eq: [ '$status', 'completed' ] }, {
2085
+ $round: [
2086
+ {
2087
+ $multiply: [ { $divide: [ '$afterCount', '$beforeCount' ] }, 100 ],
2088
+ }, 1,
2089
+ ],
2090
+ }, null ],
2091
+ },
2080
2092
  timeSpent: {
2081
2093
 
2082
2094
  $cond: [
@@ -2222,12 +2234,12 @@ export async function userMetrics( req, res ) {
2222
2234
 
2223
2235
  );
2224
2236
  }
2225
- if ( inputData?.filterByStatus?.length> 0 ) {
2237
+ if ( inputData?.filterByStatus?.length > 0 ) {
2226
2238
  filter.push(
2227
2239
  { auditStatus: { $in: inputData.filterByStatus } },
2228
2240
  );
2229
2241
  }
2230
- if ( inputData?.filterByUser?.length> 0 ) {
2242
+ if ( inputData?.filterByUser?.length > 0 ) {
2231
2243
  const temp = inputData.filterByUser.map( ( item ) => new mongoose.Types.ObjectId( item ) );
2232
2244
  logger.info( { temp: temp } );
2233
2245
  filter.push(
@@ -2273,11 +2285,15 @@ export async function userMetrics( req, res ) {
2273
2285
  totalCompletedFiles: 1,
2274
2286
  beforeCount: 1,
2275
2287
  afterCount: 1,
2276
- mappingPerc: { $round: [
2277
- { $multiply: [
2278
- { $divide: [ '$afterCount', '$beforeCount' ] }, 100,
2279
- ] }, 2,
2280
- ] },
2288
+ mappingPerc: {
2289
+ $round: [
2290
+ {
2291
+ $multiply: [
2292
+ { $divide: [ '$afterCount', '$beforeCount' ] }, 100,
2293
+ ],
2294
+ }, 2,
2295
+ ],
2296
+ },
2281
2297
  checkIntime: 1,
2282
2298
  checkOutTime: 1,
2283
2299
  differenceInSeconds: {
@@ -2406,7 +2422,7 @@ export async function userMetrics( req, res ) {
2406
2422
  },
2407
2423
  },
2408
2424
  ];
2409
- if ( inputData.searchValue && inputData.searchValue!== '' ) {
2425
+ if ( inputData.searchValue && inputData.searchValue !== '' ) {
2410
2426
  query.push( {
2411
2427
  $match: {
2412
2428
  $or: [
@@ -2474,7 +2490,7 @@ export async function pendingSummaryTable( req, res ) {
2474
2490
  const limit = inputData.limit || 10;
2475
2491
  const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
2476
2492
  const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
2477
- let filters =[
2493
+ let filters = [
2478
2494
  { fileDateISO: { $gte: dateRange.start } },
2479
2495
  { fileDateISO: { $lte: dateRange.end } },
2480
2496
  { auditStatus: { $ne: 'completed' } },
@@ -2490,7 +2506,7 @@ export async function pendingSummaryTable( req, res ) {
2490
2506
  if ( inputData.filterByStatus.length > 0 ) {
2491
2507
  filters.push( { auditStatus: { $in: inputData.filterByStatus } } );
2492
2508
  }
2493
- const query =[
2509
+ const query = [
2494
2510
  {
2495
2511
  $match: {
2496
2512
  $and: filters,
@@ -2505,11 +2521,12 @@ export async function pendingSummaryTable( req, res ) {
2505
2521
  fileDate: 1,
2506
2522
  moduleType: 1,
2507
2523
  beforeCount: 1,
2508
- startTime: { $dateToString: {
2509
- format: '%Y-%m-%d %H:%M:%S',
2510
- date: '$startTime',
2511
- timezone: 'Asia/Kolkata',
2512
- },
2524
+ startTime: {
2525
+ $dateToString: {
2526
+ format: '%Y-%m-%d %H:%M:%S',
2527
+ date: '$startTime',
2528
+ timezone: 'Asia/Kolkata',
2529
+ },
2513
2530
 
2514
2531
  },
2515
2532
  auditType: 1,
@@ -2561,7 +2578,7 @@ export async function pendingSummaryTable( req, res ) {
2561
2578
  },
2562
2579
  },
2563
2580
  ];
2564
- if ( inputData.searchValue && inputData.searchValue!== '' ) {
2581
+ if ( inputData.searchValue && inputData.searchValue !== '' ) {
2565
2582
  query.push( {
2566
2583
  $match: {
2567
2584
  $or: [
@@ -2660,16 +2677,16 @@ export async function overAllAuditSummary( req, res ) {
2660
2677
 
2661
2678
  };
2662
2679
  const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
2663
- let filters =[
2680
+ let filters = [
2664
2681
  { fileDateISO: { $gte: dateRange.start } },
2665
2682
  { fileDateISO: { $lte: dateRange.end } },
2666
2683
  { moduleType: { $eq: inputData.moduleType } },
2667
2684
  { clientId: { $in: inputData.clientId } },
2668
2685
  ];
2669
2686
 
2670
- const storeQuery = inputData.clientId.length? { 'edge.firstFile': true, 'clientId': { $in: inputData. clientId } } : { 'edge.firstFile': true };
2687
+ const storeQuery = inputData.clientId.length ? { 'edge.firstFile': true, 'clientId': { $in: inputData.clientId } } : { 'edge.firstFile': true };
2671
2688
  temp.installedStores = await countDocumentsStore( storeQuery );
2672
- const auditClientDataQuery =[
2689
+ const auditClientDataQuery = [
2673
2690
  {
2674
2691
  $match: {
2675
2692
  $and: filters,
@@ -2690,10 +2707,10 @@ export async function overAllAuditSummary( req, res ) {
2690
2707
  },
2691
2708
  ];
2692
2709
  const auditClientData = await aggregateAuditStoreData( auditClientDataQuery );
2693
- if ( auditClientData.length >0 ) {
2710
+ if ( auditClientData.length > 0 ) {
2694
2711
  temp.auditStores = auditClientData[0].totalFilesCount;
2695
2712
  }
2696
- const storeAuditQuery =[
2713
+ const storeAuditQuery = [
2697
2714
  {
2698
2715
  $match: {
2699
2716
  $and: filters,
@@ -2743,7 +2760,7 @@ export async function overAllAuditSummary( req, res ) {
2743
2760
  ];
2744
2761
  const storeAudit = await aggregateStoreAudit( storeAuditQuery );
2745
2762
  if ( storeAudit.length > 0 ) {
2746
- temp.inprogressStores= storeAudit[0].inprogressStores;
2763
+ temp.inprogressStores = storeAudit[0].inprogressStores;
2747
2764
  temp.completedStores = storeAudit[0].completedStores;
2748
2765
  temp.auditInprogress = storeAudit[0].auditInprogress;
2749
2766
  temp.reAuditInprogress = storeAudit[0].reAuditInprogress;
@@ -2767,7 +2784,7 @@ export async function reTrigger( req, res ) {
2767
2784
  const openSearch = JSON.parse( process.env.OPENSEARCH );
2768
2785
  const inputData = req.body;
2769
2786
  const sqs = JSON.parse( process.env.SQS );
2770
- const query ={
2787
+ const query = {
2771
2788
  storeId: inputData.storeId,
2772
2789
  fileDate: inputData.fileDate,
2773
2790
  zoneName: inputData.zoneName,
@@ -2798,11 +2815,11 @@ export async function reTrigger( req, res ) {
2798
2815
  return res.sendError( error, code );
2799
2816
  } else {
2800
2817
  if ( req.audit.status !== 'completed' ) {
2801
- const userRecord ={
2818
+ const userRecord = {
2802
2819
  auditStatus: 'skipped',
2803
2820
  isDraft: false,
2804
2821
  };
2805
- const query ={
2822
+ const query = {
2806
2823
  storeId: inputData.storeId,
2807
2824
  zoneName: inputData.zoneName,
2808
2825
  fileDate: inputData.fileDate,
@@ -2812,7 +2829,7 @@ export async function reTrigger( req, res ) {
2812
2829
  await updateOneUserAudit( query, userRecord );
2813
2830
  }
2814
2831
 
2815
- const storeRecord ={
2832
+ const storeRecord = {
2816
2833
  status: 'not_assign',
2817
2834
  };
2818
2835
 
@@ -2859,11 +2876,11 @@ export async function reTrigger( req, res ) {
2859
2876
  await createAssignAudit( assignUserRecord );
2860
2877
 
2861
2878
  if ( req.audit.status !== 'completed' ) {
2862
- const userRecord ={
2879
+ const userRecord = {
2863
2880
  auditStatus: 'skipped',
2864
2881
  isDraft: false,
2865
2882
  };
2866
- const query ={
2883
+ const query = {
2867
2884
  storeId: inputData.storeId,
2868
2885
  zoneName: inputData.zoneName,
2869
2886
  fileDate: inputData.fileDate,
@@ -2873,7 +2890,7 @@ export async function reTrigger( req, res ) {
2873
2890
  await updateOneUserAudit( query, userRecord );
2874
2891
  }
2875
2892
 
2876
- const storeRecord ={
2893
+ const storeRecord = {
2877
2894
  status: 'assigned',
2878
2895
  };
2879
2896
 
@@ -2923,13 +2940,13 @@ export async function overViewCard( req, res ) {
2923
2940
  };
2924
2941
 
2925
2942
  const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
2926
- let filters =[
2943
+ let filters = [
2927
2944
  { fileDateISO: { $gte: dateRange.start } },
2928
2945
  { fileDateISO: { $lte: dateRange.end } },
2929
2946
  { clientId: { $in: inputData.clientId } },
2930
2947
  ];
2931
2948
 
2932
- const query =[
2949
+ const query = [
2933
2950
  {
2934
2951
  $match: {
2935
2952
  $and: filters,
@@ -2984,7 +3001,7 @@ export async function overViewCard( req, res ) {
2984
3001
  },
2985
3002
  ];
2986
3003
 
2987
- const binaryQuery =[
3004
+ const binaryQuery = [
2988
3005
  {
2989
3006
  $match: {
2990
3007
  $and: filters,
@@ -3048,7 +3065,7 @@ export async function overViewCard( req, res ) {
3048
3065
  // Function to merge objects by summing their values
3049
3066
  const mergedResult = mergeAndSum( combinedArray );
3050
3067
 
3051
- return res.sendSuccess( { result: Object.keys( mergedResult ).length > 0?mergedResult: temp } );
3068
+ return res.sendSuccess( { result: Object.keys( mergedResult ).length > 0 ? mergedResult : temp } );
3052
3069
  } catch ( error ) {
3053
3070
  const err = error.message || 'Internal Server Error';
3054
3071
  logger.error( { error: error, message: req.body, function: 'overViewCard' } );
@@ -3079,14 +3096,14 @@ export async function summarySplit( req, res ) {
3079
3096
  };
3080
3097
 
3081
3098
  const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
3082
- let filters =[
3099
+ let filters = [
3083
3100
  { fileDateISO: { $gte: dateRange.start } },
3084
3101
  { fileDateISO: { $lte: dateRange.end } },
3085
3102
  { moduleType: { $eq: inputData.moduleType } },
3086
3103
  { clientId: { $in: inputData.clientId } },
3087
3104
  ];
3088
3105
 
3089
- const query =[
3106
+ const query = [
3090
3107
  {
3091
3108
  $match: {
3092
3109
  $and: filters,
@@ -3178,7 +3195,7 @@ export async function summarySplit( req, res ) {
3178
3195
  const result = await aggregateStoreAudit( query );
3179
3196
  const totalCount = await aggregateAuditStoreData( totalQuery );
3180
3197
  result[0].totalCount = totalCount[0]?.total;
3181
- return res.sendSuccess( { result: result.length> 0? result[0]: temp } );
3198
+ return res.sendSuccess( { result: result.length > 0 ? result[0] : temp } );
3182
3199
  } catch ( error ) {
3183
3200
  const err = error.message || 'Internal Server Error';
3184
3201
  logger.error( { error: error, message: req.body, function: 'summarySplit' } );
@@ -3192,7 +3209,7 @@ export async function overviewTable( req, res ) {
3192
3209
  const limit = inputData.limit || 10;
3193
3210
  const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
3194
3211
  const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
3195
- let filters =[
3212
+ let filters = [
3196
3213
  { fileDateISO: { $gte: dateRange.start } },
3197
3214
  { fileDateISO: { $lte: dateRange.end } },
3198
3215
  { moduleType: { $eq: inputData.moduleType } },
@@ -3208,7 +3225,7 @@ export async function overviewTable( req, res ) {
3208
3225
  filters.push( { status: { $in: inputData.filterByStatus } } );
3209
3226
  }
3210
3227
 
3211
- const query =[
3228
+ const query = [
3212
3229
  {
3213
3230
  $match: {
3214
3231
  $and: filters,
@@ -3274,7 +3291,7 @@ export async function overviewTable( req, res ) {
3274
3291
  },
3275
3292
  },
3276
3293
  ];
3277
- if ( inputData.searchValue && inputData.searchValue!== '' ) {
3294
+ if ( inputData.searchValue && inputData.searchValue !== '' ) {
3278
3295
  query.push( {
3279
3296
  $match: {
3280
3297
  $or: [
@@ -3391,12 +3408,16 @@ export async function totalNotAssignedCount( req, res ) {
3391
3408
  },
3392
3409
  {
3393
3410
  $project: {
3394
- totalZoneCount: { $cond: [
3395
- { $eq: [ '$moduleType', 'zone' ] }, 1, 0,
3396
- ] },
3397
- totalTrafficCount: { $cond: [
3398
- { $eq: [ '$moduleType', 'traffic' ] }, 1, 0,
3399
- ] },
3411
+ totalZoneCount: {
3412
+ $cond: [
3413
+ { $eq: [ '$moduleType', 'zone' ] }, 1, 0,
3414
+ ],
3415
+ },
3416
+ totalTrafficCount: {
3417
+ $cond: [
3418
+ { $eq: [ '$moduleType', 'traffic' ] }, 1, 0,
3419
+ ],
3420
+ },
3400
3421
 
3401
3422
  },
3402
3423
  },
@@ -3426,18 +3447,26 @@ export async function totalNotAssignedCount( req, res ) {
3426
3447
  },
3427
3448
  {
3428
3449
  $project: {
3429
- totalUnattendedCustomerCount: { $cond: [
3430
- { $eq: [ '$moduleType', 'unattended-customer' ] }, 1, 0,
3431
- ] },
3432
- totalLeftInMiddleCount: { $cond: [
3433
- { $eq: [ '$moduleType', 'left-in-middle' ] }, 1, 0,
3434
- ] },
3435
- totalUniformDetectionCount: { $cond: [
3436
- { $eq: [ '$moduleType', 'uniform-detection' ] }, 1, 0,
3437
- ] },
3438
- totalMobileDetectionCount: { $cond: [
3439
- { $eq: [ '$moduleType', 'mobile-detection' ] }, 1, 0,
3440
- ] },
3450
+ totalUnattendedCustomerCount: {
3451
+ $cond: [
3452
+ { $eq: [ '$moduleType', 'unattended-customer' ] }, 1, 0,
3453
+ ],
3454
+ },
3455
+ totalLeftInMiddleCount: {
3456
+ $cond: [
3457
+ { $eq: [ '$moduleType', 'left-in-middle' ] }, 1, 0,
3458
+ ],
3459
+ },
3460
+ totalUniformDetectionCount: {
3461
+ $cond: [
3462
+ { $eq: [ '$moduleType', 'uniform-detection' ] }, 1, 0,
3463
+ ],
3464
+ },
3465
+ totalMobileDetectionCount: {
3466
+ $cond: [
3467
+ { $eq: [ '$moduleType', 'mobile-detection' ] }, 1, 0,
3468
+ ],
3469
+ },
3441
3470
 
3442
3471
  },
3443
3472
  },
@@ -3464,7 +3493,7 @@ export async function totalNotAssignedCount( req, res ) {
3464
3493
  },
3465
3494
  },
3466
3495
  ];
3467
- const inprogressCount=[
3496
+ const inprogressCount = [
3468
3497
  {
3469
3498
  $match: {
3470
3499
  fileDate: { $eq: latestDate },
@@ -3477,12 +3506,16 @@ export async function totalNotAssignedCount( req, res ) {
3477
3506
  { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] }, 1, 0,
3478
3507
  ],
3479
3508
  },
3480
- inprogressZoneCount: { $cond: [
3481
- { $and: [ { $eq: [ '$moduleType', 'zone' ] }, { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] } ] }, 1, 0,
3482
- ] },
3483
- inprogressTrafficCount: { $cond: [
3484
- { $and: [ { $eq: [ '$moduleType', 'traffic' ] }, { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] } ] }, 1, 0,
3485
- ] },
3509
+ inprogressZoneCount: {
3510
+ $cond: [
3511
+ { $and: [ { $eq: [ '$moduleType', 'zone' ] }, { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] } ] }, 1, 0,
3512
+ ],
3513
+ },
3514
+ inprogressTrafficCount: {
3515
+ $cond: [
3516
+ { $and: [ { $eq: [ '$moduleType', 'traffic' ] }, { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] } ] }, 1, 0,
3517
+ ],
3518
+ },
3486
3519
 
3487
3520
  },
3488
3521
  },
@@ -3623,18 +3656,18 @@ export async function totalNotAssignedCount( req, res ) {
3623
3656
  return res.sendError( 'No Data Found', 204 );
3624
3657
  }
3625
3658
  logger.info( { getTotalTraxAudit: getTotalTraxAudit } );
3626
- if ( getInprogressCount.length >0 ) {
3627
- notAssignedCount.totalAuditCount =getTotalcount[0].totalAuditCount > getInprogressCount[0]?.inprogressAuditCount? getTotalcount[0].totalAuditCount - getInprogressCount[0]?.inprogressAuditCount : 0;
3628
- notAssignedCount.totalZoneCount = getTotalcount[0].totalZoneCount > getInprogressCount[0]?.inprogressZoneCount? getTotalcount[0].totalZoneCount - getInprogressCount[0]?.inprogressZoneCount: 0;
3629
- notAssignedCount.totalTrafficCount = getTotalcount[0].totalTrafficCount > getInprogressCount[0]?.inprogressTrafficCount? getTotalcount[0].totalTrafficCount - getInprogressCount[0]?.inprogressTrafficCount: 0;
3659
+ if ( getInprogressCount.length > 0 ) {
3660
+ notAssignedCount.totalAuditCount = getTotalcount[0].totalAuditCount > getInprogressCount[0]?.inprogressAuditCount ? getTotalcount[0].totalAuditCount - getInprogressCount[0]?.inprogressAuditCount : 0;
3661
+ notAssignedCount.totalZoneCount = getTotalcount[0].totalZoneCount > getInprogressCount[0]?.inprogressZoneCount ? getTotalcount[0].totalZoneCount - getInprogressCount[0]?.inprogressZoneCount : 0;
3662
+ notAssignedCount.totalTrafficCount = getTotalcount[0].totalTrafficCount > getInprogressCount[0]?.inprogressTrafficCount ? getTotalcount[0].totalTrafficCount - getInprogressCount[0]?.inprogressTrafficCount : 0;
3630
3663
  } else {
3631
3664
  notAssignedCount.totalAuditCount = getTotalcount[0].totalAuditCount;
3632
3665
  notAssignedCount.totalZoneCount = getTotalcount[0].totalZoneCount;
3633
- notAssignedCount.totalTrafficCount= getTotalcount[0].totalTrafficCount;
3666
+ notAssignedCount.totalTrafficCount = getTotalcount[0].totalTrafficCount;
3634
3667
  }
3635
3668
 
3636
3669
  if ( getBinaryInprogressCount.length > 0 ) {
3637
- notAssignedCount.totalTraxAuditCount = getTotalTraxAudit[0].totalTraxAuditCount > getBinaryInprogressCount[0].inprogressTraxCount? getTotalTraxAudit[0].totalTraxAuditCount - getBinaryInprogressCount[0].inprogressTraxCount: 0;
3670
+ notAssignedCount.totalTraxAuditCount = getTotalTraxAudit[0].totalTraxAuditCount > getBinaryInprogressCount[0].inprogressTraxCount ? getTotalTraxAudit[0].totalTraxAuditCount - getBinaryInprogressCount[0].inprogressTraxCount : 0;
3638
3671
  } else {
3639
3672
  notAssignedCount.totalTraxAuditCount = getTotalTraxAudit[0]?.totalTraxAuditCount;
3640
3673
  }
@@ -3662,8 +3695,8 @@ export async function auditImages( req, res ) {
3662
3695
  return res.sendError( 'No Data Found', 204 );
3663
3696
  }
3664
3697
 
3665
- if ( inputData.export==true ) {
3666
- auditImageDownload= await zipDownloadImage( auditImageBC );
3698
+ if ( inputData.export == true ) {
3699
+ auditImageDownload = await zipDownloadImage( auditImageBC );
3667
3700
  // Set response headers for the zip file
3668
3701
  res.set( 'Content-Type', 'application/zip' );
3669
3702
  res.set( 'Content-Disposition', `attachment; filename=${inputData.storeId}_${inputData.fileDate}_${inputData.imageType}.zip` );
@@ -3692,8 +3725,8 @@ export async function auditImages( req, res ) {
3692
3725
  }
3693
3726
 
3694
3727
  logger.info( { auditImageAC: auditImageAC, filterData: filterData, reauditImg: reauditImg } );
3695
- if ( inputData.export==true ) {
3696
- auditImageDownload= await zipDownloadImage( reauditImg );
3728
+ if ( inputData.export == true ) {
3729
+ auditImageDownload = await zipDownloadImage( reauditImg );
3697
3730
  // Set response headers for the zip file
3698
3731
  res.set( 'Content-Type', 'application/zip' );
3699
3732
  res.set( 'Content-Disposition', `attachment; filename=${inputData.storeId}_${inputData.fileDate}_${inputData.imageType}.zip` );
@@ -3709,10 +3742,563 @@ export async function auditImages( req, res ) {
3709
3742
  }
3710
3743
 
3711
3744
 
3712
- // return res.sendSuccess( { result: inputData } );
3745
+ // return res.sendSuccess( { result: inputData } );
3713
3746
  } catch ( error ) {
3714
3747
  const err = error.message || 'Internal Server Error';
3715
3748
  logger.info( { error: error, message: req.body, function: 'auditImages' } );
3716
3749
  return res.sendError( err, 500 );
3717
3750
  }
3718
3751
  }
3752
+
3753
+ export async function getUserAuditCount( req, res ) {
3754
+ try {
3755
+ const userWallet = JSON.parse( process.env.USER_WALLET );
3756
+ const previousDate = dayjs( new Date() ).subtract( 1, 'days' );
3757
+ const fileDate = dayjs( previousDate ).format( 'DD-MM-YYYY' );
3758
+ const configTimeSpentSec = userWallet.minTimeSpentSec; // 18000;
3759
+ const beforeCountX = userWallet.beforeCountCreditPerc; // 0.0335;
3760
+ const afterCountY = userWallet.afterCountCreditPerc; // 0.0035;
3761
+ const reduce = userWallet.reductionPerc; // 0.05;
3762
+ const userCountQuery= [
3763
+ {
3764
+ $match: {
3765
+ $and: [
3766
+ { fileDate: { $eq: fileDate } },
3767
+ { auditStatus: { $eq: 'completed' } },
3768
+ ],
3769
+ },
3770
+ },
3771
+ {
3772
+ $project: {
3773
+
3774
+ trafficUserCount: { $cond: [
3775
+ { $eq: [ '$moduleType', 'traffic' ] }, '$userId', '$$REMOVE',
3776
+ ] },
3777
+ zoneUserCount: { $cond: [
3778
+ { $eq: [ '$moduleType', 'zone' ] }, '$userId', '$$REMOVE',
3779
+ ] },
3780
+
3781
+ trafficFilesCount: { $cond: [
3782
+ { $eq: [ '$moduleType', 'traffic' ] }, '$beforeCount', '$$REMOVE',
3783
+ ] },
3784
+ zoneFilesCount: { $cond: [
3785
+ { $eq: [ '$moduleType', 'zone' ] }, '$beforeCount', '$$REMOVE',
3786
+ ] },
3787
+
3788
+ },
3789
+ },
3790
+ {
3791
+ $group: {
3792
+ _id: '$moduleType',
3793
+ trafficUserCount: { $addToSet: '$trafficUserCount' },
3794
+ zoneUserCount: { $addToSet: '$zoneUserCount' },
3795
+ trafficFilesCount: { $sum: '$trafficFilesCount' },
3796
+ zoneFilesCount: { $sum: '$zoneFilesCount' },
3797
+ },
3798
+ },
3799
+ ];
3800
+
3801
+ // const storeDataQuery= [
3802
+ // {
3803
+ // $match: {
3804
+ // fileDate: { $eq: fileDate },
3805
+ // },
3806
+ // },
3807
+ // {
3808
+ // $project: {
3809
+
3810
+ // trafficFilesCount: { $cond: [
3811
+ // { $eq: [ '$moduleType', 'traffic' ] }, '$totalFilesCount', '$$REMOVE',
3812
+ // ] },
3813
+ // zoneFilesCount: { $cond: [
3814
+ // { $eq: [ '$moduleType', 'zone' ] }, '$totalFilesCount', '$$REMOVE',
3815
+ // ] },
3816
+
3817
+ // },
3818
+ // },
3819
+ // {
3820
+ // $group: {
3821
+ // _id: '$moduleType',
3822
+ // trafficFilesCount: { $sum: '$trafficFilesCount' },
3823
+ // zoneFilesCount: { $sum: '$zoneFilesCount' },
3824
+ // },
3825
+ // },
3826
+ // {
3827
+ // $project: {
3828
+ // _id: 0,
3829
+ // trafficFilesCount: { $ifNull: [ '$trafficFilesCount', 0 ] },
3830
+ // zoneFilesCount: { $ifNull: [ '$zoneFilesCount', 0 ] },
3831
+ // },
3832
+ // },
3833
+ // ];
3834
+ const userCount = await aggregateUserAuditCount( userCountQuery );
3835
+
3836
+ // const totalStoreData = await aggregateAuditStoreData( storeDataQuery );
3837
+ if ( userCount?.length == 0 ) {
3838
+ return res.sendError( 'No Data Found', 204 );
3839
+ }
3840
+ const zoneMinimumTarget =userCount[0]?.zoneUserCount?.length !== 0 && userCount[0]?.zoneFilesCount !==0 ? Math.floor( userCount[0].zoneFilesCount/userCount[0].zoneUserCount.length ) : 0;
3841
+ const trafficMinimumTarget =userCount[0]?.trafficUserCount?.length !== 0 && userCount[0]?.trafficFilesCount !==0 ? Math.floor( userCount[0].trafficFilesCount/userCount[0].trafficUserCount.length ) : 0;
3842
+ const query = [
3843
+ {
3844
+ $match: {
3845
+ $and: [
3846
+ { fileDate: { $eq: fileDate } },
3847
+ {
3848
+ auditStatus: { $eq: 'completed' },
3849
+ },
3850
+ ],
3851
+ },
3852
+ },
3853
+ {
3854
+ $set: { minimumTarget: { $cond: [ { $eq: [ '$moduleType', 'traffic' ] }, trafficMinimumTarget, zoneMinimumTarget ] } },
3855
+ },
3856
+ {
3857
+ $project: {
3858
+ userId: 1,
3859
+ moduleType: 1,
3860
+ beforeCount: 1,
3861
+ afterCount: 1,
3862
+ timeSpent: 1,
3863
+ fileDate: 1,
3864
+ fileDateISO: 1,
3865
+ minimumTarget: 1,
3866
+ totalEarn: { $ifNull: [ 0, 0 ] },
3867
+ totalReducedAmount: { $ifNull: [ 0, 0 ] },
3868
+ totalCredit: { $ifNull: [ 0, 0 ] },
3869
+ },
3870
+ },
3871
+ {
3872
+ $group: {
3873
+ _id: { user: '$userId', type: '$moduleType' },
3874
+ totalCount: { $sum: 1 },
3875
+ moduleType: { $first: '$moduleType' },
3876
+ totalBeforeCount: { $sum: '$beforeCount' },
3877
+ totalAfterCount: { $sum: '$afterCount' },
3878
+ totalTimeSpent: { $sum: '$timeSpent' },
3879
+ fileDate: { $first: '$fileDate' },
3880
+ fileDateISO: { $first: '$fileDateISO' },
3881
+ userId: { $first: '$userId' },
3882
+ minimumTarget: { $first: '$minimumTarget' },
3883
+ totalEarn: { $first: '$totalEarn' },
3884
+ totalReducedAmount: { $first: '$totalReducedAmount' },
3885
+ totalCredit: { $first: '$totalCredit' },
3886
+ },
3887
+ },
3888
+ {
3889
+ $project: {
3890
+ totalCount: 1,
3891
+ userId: 1,
3892
+ moduleType: 1,
3893
+ totalBeforeCount: 1,
3894
+ totalAfterCount: 1,
3895
+ totalTimeSpent: 1,
3896
+ fileDate: 1,
3897
+ fileDateISO: 1,
3898
+ minimumTarget: 1,
3899
+ totalEarn: { $add: [ { $multiply: [ '$totalBeforeCount', beforeCountX ] }, { $multiply: [ '$totalAfterCount', afterCountY ] } ] },
3900
+ totalReducedAmount: 1,
3901
+ totalCredit: 1,
3902
+
3903
+ },
3904
+ },
3905
+ {
3906
+ $project: {
3907
+ totalCount: 1,
3908
+ userId: 1,
3909
+ moduleType: 1,
3910
+ totalBeforeCount: 1,
3911
+ totalAfterCount: 1,
3912
+ totalTimeSpent: 1,
3913
+ fileDate: 1,
3914
+ fileDateISO: 1,
3915
+ minimumTarget: 1,
3916
+ totalEarn: 1,
3917
+ totalReducedAmount: { $cond: [
3918
+ { $lt: [ '$totalTimeSpent', configTimeSpentSec ] },
3919
+ { $add: [ '$totalReducedAmount', { $multiply: [ reduce, '$totalEarn' ] } ] }, '$totalReducedAmount',
3920
+ ] },
3921
+ totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
3922
+
3923
+ },
3924
+ },
3925
+
3926
+ {
3927
+ $project: {
3928
+ totalCount: 1,
3929
+ userId: 1,
3930
+ moduleType: 1,
3931
+ totalBeforeCount: 1,
3932
+ totalAfterCount: 1,
3933
+ totalTimeSpent: 1,
3934
+ fileDate: 1,
3935
+ fileDateISO: 1,
3936
+ minimumTarget: 1,
3937
+ totalEarn: { $add: [ { $multiply: [ '$totalBeforeCount', beforeCountX ] }, { $multiply: [ '$totalAfterCount', afterCountY ] } ] },
3938
+ totalReducedAmount:
3939
+ { $cond: [
3940
+ { $lt: [ '$totalBeforeCount', '$minimumTarget' ] },
3941
+ { $add: [ '$totalReducedAmount', { $multiply: [ reduce, '$totalEarn' ] } ] }, '$totalReducedAmount',
3942
+ ] },
3943
+
3944
+ totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
3945
+ },
3946
+ },
3947
+ {
3948
+ $lookup: {
3949
+ from: 'users',
3950
+ let: { userId: '$userId' }, // Assuming userId is the linking field
3951
+ pipeline: [
3952
+ {
3953
+ $match: {
3954
+ $expr: { $eq: [ '$_id', '$$userId' ] },
3955
+ },
3956
+ },
3957
+ {
3958
+ $project: {
3959
+ _id: 0, // Exclude _id if not needed
3960
+ email: 1,
3961
+ userName: 1,
3962
+ },
3963
+ },
3964
+ ],
3965
+ as: 'users',
3966
+ },
3967
+ },
3968
+ { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
3969
+ {
3970
+ $project: {
3971
+ _id: 0,
3972
+ totalFilesCount: { $round: [ '$totalCount', 2 ] },
3973
+ moduleType: 1,
3974
+ totalAfterCount: { $round: [ '$totalAfterCount', 2 ] },
3975
+ totalBeforeCount: { $round: [ '$totalBeforeCount', 2 ] },
3976
+ timeSpent: { $round: [ '$totalTimeSpent', 2 ] },
3977
+ fileDate: 1,
3978
+ fileDateISO: 1,
3979
+ totalReducedAmount: { $round: [ '$totalReducedAmount', 2 ] },
3980
+ totalCredit: { $round: [ { $subtract: [ '$totalEarn', '$totalReducedAmount' ] }, 2 ] },
3981
+ totalEarn: { $round: [ '$totalEarn', 2 ] },
3982
+ minimumTarget: 1,
3983
+ userEmail: '$users.email', // Access first element from array
3984
+ userName: '$users.userName', // Access first element from array
3985
+ },
3986
+ },
3987
+ ];
3988
+ const consolidatedData = await aggregateUserAuditCount( query );
3989
+ if ( consolidatedData.length == 0 ) {
3990
+ return res.sendError( 'No Data found', 204 );
3991
+ }
3992
+ const result = await createAuditUserWallet( consolidatedData );
3993
+ if ( result.length == 0 ) {
3994
+ return res.sendError( 'No Record Inserted', 500 );
3995
+ }
3996
+ return res.sendSuccess( { message: 'Inserted successfully!' } );
3997
+ } catch ( error ) {
3998
+ const err = error.message || 'Internal Server Error';
3999
+ logger.info( { error: error, message: req.body, function: 'getUserAuditCount' } );
4000
+ return res.sendError( err, 500 );
4001
+ }
4002
+ }
4003
+
4004
+ export async function getUserAuditCountMTD( req, res ) {
4005
+ try {
4006
+ const userWallet = JSON.parse( process.env.USER_WALLET );
4007
+ let today = new Date(); // Current date
4008
+ let yesterday = new Date( today );
4009
+ yesterday.setDate( today.getDate() - 1 );
4010
+ let firstDayOfMonth = new Date( yesterday.getFullYear(), today.getMonth(), 1 );
4011
+ const previousDate = dayjs( new Date() ).subtract( 1, 'days' );
4012
+ // let firstDayOfMonth = new Date( previousDate.getFullYear(), previousDate.getMonth(), 1 );
4013
+ logger.info( { previousDate: previousDate, firstDayOfMonth: firstDayOfMonth } );
4014
+ const dateRange = await getDate( firstDayOfMonth, new Date( dayjs( previousDate ).format( 'YYYY-MM-DD' ) ) );
4015
+ logger.info( { dateRange: dateRange } );
4016
+ const configTimeSpentSec = userWallet.minTimeSpentSec; // 18000;
4017
+ const beforeCountX = userWallet.beforeCountCreditPerc; // 0.0335;
4018
+ const afterCountY = userWallet.afterCountCreditPerc; // 0.0035;
4019
+ const reduce = userWallet.reductionPerc; // 0.05;
4020
+ // const userCountQuery= [
4021
+ // {
4022
+ // $match: {
4023
+ // $and: [
4024
+ // {
4025
+ // $match: {
4026
+ // fileDateISO: { $gte: dateRange?.start },
4027
+ // fileDateISO: { $gte: dateRange?.start },
4028
+ // },
4029
+ // },
4030
+ // ],
4031
+ // },
4032
+ // },
4033
+ // {
4034
+ // $project: {
4035
+
4036
+ // trafficUserCount: { $cond: [
4037
+ // { $eq: [ '$moduleType', 'traffic' ] }, '$userId', '$$REMOVE',
4038
+ // ] },
4039
+ // zoneUserCount: { $cond: [
4040
+ // { $eq: [ '$moduleType', 'zone' ] }, '$userId', '$$REMOVE',
4041
+ // ] },
4042
+
4043
+ // },
4044
+ // },
4045
+ // {
4046
+ // $group: {
4047
+ // _id: '$moduleType',
4048
+ // trafficUserCount: { $addToSet: '$trafficUserCount' },
4049
+ // zoneUserCount: { $addToSet: '$zoneUserCount' },
4050
+ // },
4051
+ // },
4052
+ // ];
4053
+
4054
+ // const storeDataQuery= [
4055
+ // {
4056
+ // $match: {
4057
+ // fileDate: { $eq: fileDate },
4058
+ // },
4059
+ // },
4060
+ // {
4061
+ // $project: {
4062
+
4063
+ // trafficFilesCount: { $cond: [
4064
+ // { $eq: [ '$moduleType', 'traffic' ] }, '$totalFilesCount', '$$REMOVE',
4065
+ // ] },
4066
+ // zoneFilesCount: { $cond: [
4067
+ // { $eq: [ '$moduleType', 'zone' ] }, '$totalFilesCount', '$$REMOVE',
4068
+ // ] },
4069
+
4070
+ // },
4071
+ // },
4072
+ // {
4073
+ // $group: {
4074
+ // _id: '$moduleType',
4075
+ // trafficFilesCount: { $sum: '$trafficFilesCount' },
4076
+ // zoneFilesCount: { $sum: '$zoneFilesCount' },
4077
+ // },
4078
+ // },
4079
+ // {
4080
+ // $project: {
4081
+ // _id: 0,
4082
+ // trafficFilesCount: { $ifNull: [ '$trafficFilesCount', 0 ] },
4083
+ // zoneFilesCount: { $ifNull: [ '$zoneFilesCount', 0 ] },
4084
+ // },
4085
+ // },
4086
+ // ];
4087
+ // const userCount = await aggregateUserAuditCount( userCountQuery );
4088
+
4089
+ // const totalStoreData = await aggregateAuditStoreData( storeDataQuery );
4090
+ // if ( userCount?.length == 0 || totalStoreData?.length == 0 ) {
4091
+ // return res.sendError( 'No Data Found', 204 );
4092
+ // }
4093
+ // const zoneMinimumTarget =userCount[0]?.zoneUserCount?.length !== 0 && totalStoreData[0]?.zoneFilesCount !==0 ? Math.floor( totalStoreData[0].zoneFilesCount/userCount[0].zoneUserCount.length ) : 0;
4094
+ // const trafficMinimumTarget =userCount[0]?.trafficUserCount?.length !== 0 && totalStoreData[0]?.trafficFilesCount !==0 ? Math.floor( totalStoreData[0].trafficFilesCount/userCount[0].trafficUserCount.length ) : 0;
4095
+ const query = [
4096
+ {
4097
+ $match: {
4098
+ $and: [
4099
+ { fileDateISO: { $gte: dateRange.start } },
4100
+ { fileDateISO: { $lte: dateRange.end } },
4101
+ {
4102
+ auditStatus: { $eq: 'completed' },
4103
+ },
4104
+ ],
4105
+ },
4106
+ },
4107
+ // {
4108
+ // $set: { minimumTarget: { $cond: [ { $eq: [ '$moduleType', 'traffic' ] }, trafficMinimumTarget, zoneMinimumTarget ] } },
4109
+ // },
4110
+ {
4111
+ $project: {
4112
+ userId: 1,
4113
+ moduleType: 1,
4114
+ beforeCount: 1,
4115
+ afterCount: 1,
4116
+ timeSpent: 1,
4117
+ fileDate: 1,
4118
+ fileDateISO: 1,
4119
+ minimumTarget: 1,
4120
+ totalEarn: { $ifNull: [ 0, 0 ] },
4121
+ totalReducedAmount: { $ifNull: [ 0, 0 ] },
4122
+ totalCredit: { $ifNull: [ 0, 0 ] },
4123
+ trafficUserCount: { $cond: [
4124
+ { $eq: [ '$moduleType', 'traffic' ] }, '$userId', '$$REMOVE',
4125
+ ] },
4126
+ zoneUserCount: { $cond: [
4127
+ { $eq: [ '$moduleType', 'zone' ] }, '$userId', '$$REMOVE',
4128
+ ] },
4129
+
4130
+ },
4131
+ },
4132
+ {
4133
+ $group: {
4134
+ _id: { type: '$moduleType', fileDate: '$fileDate' },
4135
+ trafficUserCount: { $addToSet: '$trafficUserCount' },
4136
+ zoneUserCount: { $addToSet: '$zoneUserCount' },
4137
+ moduleType: { $first: '$moduleType' },
4138
+ beforeCount: { $first: '$beforeCount' },
4139
+ afterCount: { $sum: '$afterCount' },
4140
+ timeSpent: { $sum: '$timeSpent' },
4141
+ fileDate: { $first: '$fileDate' },
4142
+ fileDateISO: { $first: '$fileDateISO' },
4143
+ userId: { $first: '$userId' },
4144
+ minimumTarget: { $first: '$minimumTarget' },
4145
+ totalEarn: { $first: '$totalEarn' },
4146
+ totalReducedAmount: { $first: '$totalReducedAmount' },
4147
+ totalCredit: { $first: '$totalCredit' },
4148
+
4149
+ },
4150
+ },
4151
+ {
4152
+ $group: {
4153
+ _id: { user: '$userId', type: '$moduleType', fileDate: '$fileDate' },
4154
+ totalCount: { $sum: 1 },
4155
+ moduleType: { $first: '$moduleType' },
4156
+ trafficUserCount: { $first: '$trafficUserCount' },
4157
+ zoneUserCount: { $first: '$zoneUserCount' },
4158
+ totalBeforeCount: { $sum: '$beforeCount' },
4159
+ totalAfterCount: { $sum: '$afterCount' },
4160
+ totalTimeSpent: { $sum: '$timeSpent' },
4161
+ fileDate: { $first: '$fileDate' },
4162
+ fileDateISO: { $first: '$fileDateISO' },
4163
+ userId: { $first: '$userId' },
4164
+ minimumTarget: { $first: '$minimumTarget' },
4165
+ totalEarn: { $first: '$totalEarn' },
4166
+ totalReducedAmount: { $first: '$totalReducedAmount' },
4167
+ totalCredit: { $first: '$totalCredit' },
4168
+ },
4169
+ },
4170
+ // {
4171
+ // $lookup: {
4172
+ // from: 'userAudit',
4173
+ // let:{fileDate: "$fileDate"},
4174
+ // pipeline: [
4175
+ // {
4176
+ // $match: {
4177
+ // $expr: {
4178
+ // $and: [
4179
+ // {
4180
+ // fi
4181
+ // },
4182
+ // ],
4183
+ // },
4184
+ // },
4185
+ // },
4186
+ // ],as:"userAudit"
4187
+ // },
4188
+ // },
4189
+ {
4190
+ $project: {
4191
+ totalCount: 1,
4192
+ userId: 1,
4193
+ moduleType: 1,
4194
+ totalBeforeCount: 1,
4195
+ totalAfterCount: 1,
4196
+ totalTimeSpent: 1,
4197
+ fileDate: 1,
4198
+ fileDateISO: 1,
4199
+ minimumTarget: 1,
4200
+ totalEarn: { $add: [ { $multiply: [ '$totalBeforeCount', beforeCountX ] }, { $multiply: [ '$totalAfterCount', afterCountY ] } ] },
4201
+ totalReducedAmount: 1,
4202
+ totalCredit: 1,
4203
+
4204
+ },
4205
+ },
4206
+ {
4207
+ $project: {
4208
+ totalCount: 1,
4209
+ userId: 1,
4210
+ moduleType: 1,
4211
+ totalBeforeCount: 1,
4212
+ totalAfterCount: 1,
4213
+ totalTimeSpent: 1,
4214
+ fileDate: 1,
4215
+ fileDateISO: 1,
4216
+ minimumTarget: 1,
4217
+ totalEarn: 1,
4218
+ totalReducedAmount: { $cond: [
4219
+ { $lt: [ '$totalTimeSpent', configTimeSpentSec ] },
4220
+ { $add: [ '$totalReducedAmount', { $multiply: [ reduce, '$totalEarn' ] } ] }, '$totalReducedAmount',
4221
+ ] },
4222
+ totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
4223
+
4224
+ },
4225
+ },
4226
+
4227
+ {
4228
+ $project: {
4229
+ totalCount: 1,
4230
+ userId: 1,
4231
+ moduleType: 1,
4232
+ totalBeforeCount: 1,
4233
+ totalAfterCount: 1,
4234
+ totalTimeSpent: 1,
4235
+ fileDate: 1,
4236
+ fileDateISO: 1,
4237
+ minimumTarget: 1,
4238
+ totalEarn: { $add: [ { $multiply: [ '$totalBeforeCount', beforeCountX ] }, { $multiply: [ '$totalAfterCount', afterCountY ] } ] },
4239
+ totalReducedAmount:
4240
+ { $cond: [
4241
+ { $lt: [ '$totalBeforeCount', '$minimumTarget' ] },
4242
+ { $add: [ '$totalReducedAmount', { $multiply: [ reduce, '$totalEarn' ] } ] }, '$totalReducedAmount',
4243
+ ] },
4244
+
4245
+ totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
4246
+ },
4247
+ },
4248
+ {
4249
+ $lookup: {
4250
+ from: 'users',
4251
+ let: { userId: '$userId' }, // Assuming userId is the linking field
4252
+ pipeline: [
4253
+ {
4254
+ $match: {
4255
+ $expr: { $eq: [ '$_id', '$$userId' ] },
4256
+ },
4257
+ },
4258
+ {
4259
+ $project: {
4260
+ _id: 0, // Exclude _id if not needed
4261
+ email: 1,
4262
+ userName: 1,
4263
+ },
4264
+ },
4265
+ ],
4266
+ as: 'users',
4267
+ },
4268
+ },
4269
+ { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
4270
+ {
4271
+ $project: {
4272
+ _id: 0,
4273
+ totalFilesCount: { $round: [ '$totalCount', 2 ] },
4274
+ moduleType: 1,
4275
+ totalAfterCount: { $round: [ '$totalAfterCount', 2 ] },
4276
+ totalBeforeCount: { $round: [ '$totalBeforeCount', 2 ] },
4277
+ timeSpent: { $round: [ '$totalTimeSpent', 2 ] },
4278
+ fileDate: 1,
4279
+ fileDateISO: 1,
4280
+ totalReducedAmount: { $round: [ '$totalReducedAmount', 2 ] },
4281
+ totalCredit: { $round: [ { $subtract: [ '$totalEarn', '$totalReducedAmount' ] }, 2 ] },
4282
+ totalEarn: { $round: [ '$totalEarn', 2 ] },
4283
+ minimumTarget: 1,
4284
+ userEmail: '$users.email', // Access first element from array
4285
+ userName: '$users.userName', // Access first element from array
4286
+ },
4287
+ },
4288
+ ];
4289
+ const consolidatedData = await aggregateUserAuditCount( query );
4290
+ if ( consolidatedData.length == 0 ) {
4291
+ return res.sendError( 'No Data found', 204 );
4292
+ }
4293
+ const result = await createAuditUserWallet( consolidatedData );
4294
+ if ( result.length == 0 ) {
4295
+ return res.sendError( 'No Record Inserted', 500 );
4296
+ }
4297
+ return res.sendSuccess( { message: 'Inserted successfully!' } );
4298
+ } catch ( error ) {
4299
+ const err = error.message || 'Internal Server Error';
4300
+ logger.info( { error: error, message: req.body, function: 'getUserAuditCount' } );
4301
+ return res.sendError( err, 500 );
4302
+ }
4303
+ }
4304
+