tango-app-api-audit 3.4.3-alpha.9 → 3.4.4

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.
@@ -64,6 +64,7 @@ import { aggregateStoreEmpDetection } from '../service/storeEmpDetection.service
64
64
  import { aggregateBinaryAudit } from '../service/binaryAudit.service.js';
65
65
  import { aggregateTraxAuditData, findOneTraxAuditData } from '../service/traxAuditData.service.js';
66
66
  import { createAuditUserWallet } from '../service/auditUserWallet.service.js';
67
+ import { aggregateAuditUsers } from '../service/auditUsers.service.js';
67
68
 
68
69
  /* < -- *** Configuration *** --> */
69
70
 
@@ -355,6 +356,7 @@ export async function getAuditFile( req, res ) {
355
356
  auditId: userDetails[0]._id,
356
357
  moduleType: userDetails[0].moduleType,
357
358
  zoneName: userDetails[0].zoneName,
359
+ beforeCount: userDetails[0].beforeCount,
358
360
  },
359
361
  createdAt: new Date(),
360
362
  };
@@ -457,6 +459,7 @@ export async function getAuditFile( req, res ) {
457
459
  auditId: reauditInsert._id,
458
460
  zoneName: msg.zone_id,
459
461
  moduleType: inputData.moduleType,
462
+ beforeCount: msg.total_count,
460
463
  },
461
464
  createdAt: new Date(),
462
465
  };
@@ -609,6 +612,7 @@ export async function getAuditFile( req, res ) {
609
612
  auditId: insertData._id,
610
613
  zoneName: msg.zone_id,
611
614
  moduleType: inputData.moduleType,
615
+ beforeCount: msg.total_count,
612
616
  },
613
617
  createdAt: new Date(),
614
618
  };
@@ -1102,9 +1106,9 @@ export async function workSpace( req, res ) {
1102
1106
  }
1103
1107
 
1104
1108
  if ( inputData.isExport ) {
1105
- const exportdata = [];
1109
+ const exportData = [];
1106
1110
  temp.forEach( ( element ) => {
1107
- exportdata.push( {
1111
+ exportData.push( {
1108
1112
  'Client Name': element.clientName,
1109
1113
  'Client Id': element.clientId,
1110
1114
  'Completed stores Count': element.completedStoresCount,
@@ -1115,7 +1119,7 @@ export async function workSpace( req, res ) {
1115
1119
  'Assigned': element.Assignedcount,
1116
1120
  } );
1117
1121
  } );
1118
- await download( exportdata, res );
1122
+ await download( exportData, res );
1119
1123
  return;
1120
1124
  } else {
1121
1125
  return res.sendSuccess( {
@@ -1217,6 +1221,7 @@ export async function getDraftedData( req, res ) {
1217
1221
  userId: userId,
1218
1222
  moduleType: inputData.moduleType,
1219
1223
  zoneName: inputData.zoneName,
1224
+ auditId: inputData.auditId,
1220
1225
  };
1221
1226
  const result = await findOneAuditLog( query, {}, { createdAt: -1 }, 1 );
1222
1227
 
@@ -1237,8 +1242,10 @@ export async function getDraftedData( req, res ) {
1237
1242
 
1238
1243
  export async function save( req, res ) {
1239
1244
  try {
1245
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
1240
1246
  const bucket = JSON.parse( process.env.BUCKET );
1241
1247
  const sqs = JSON.parse( process.env.SQS );
1248
+ const auditConfig = JSON.parse( process.env.AUDIT_CONFIG );
1242
1249
  const inputData = req.body;
1243
1250
  let storeAuditData = await findOneStoreAudit( {
1244
1251
  storeId: inputData.storeId,
@@ -1329,12 +1336,29 @@ export async function save( req, res ) {
1329
1336
  },
1330
1337
  );
1331
1338
 
1332
- /** *
1333
- *
1334
- * logs need to be added
1335
- *
1336
- *
1337
- */
1339
+ const logData = {
1340
+ userId: req.user._id,
1341
+ userName: req.user.userName,
1342
+ logType: 'audit',
1343
+ logSubType: 'auditDone',
1344
+ logData: {
1345
+ fileDate: req.userAudit?.fileDate,
1346
+ auditType: req.userAudit?.auditType,
1347
+ storeId: req.userAudit?.storeId,
1348
+ zoneName: req.userAudit?.zoneName,
1349
+ moduleType: req.userAudit?.moduleType,
1350
+ queueName: req.userAudit?.queueName,
1351
+ clientId: req.userAudit?.clientId,
1352
+ beforeCount: req.userAudit?.beforeCount,
1353
+ afterCount: inputData.customerCount,
1354
+ startTime: req.userAudit?.startTime,
1355
+ endTime: Date.now(),
1356
+ timeSpent: timeDifferenceInMinutes,
1357
+ auditId: new mongoose.Types.ObjectId( inputData.auditId ),
1358
+ },
1359
+ createdAt: Date.now(),
1360
+ };
1361
+ await insertOpenSearchData( openSearch.auditLog, logData );
1338
1362
 
1339
1363
  let storeConfig = await findOneStore(
1340
1364
  {
@@ -1399,6 +1423,31 @@ export async function save( req, res ) {
1399
1423
  timeSpent: timeDifferenceInMinutes,
1400
1424
  },
1401
1425
  );
1426
+
1427
+ const logData = {
1428
+ userId: req.user._id,
1429
+ userName: req.user.userName,
1430
+ logType: 'audit',
1431
+ logSubType: 'reAuditPush',
1432
+ logData: {
1433
+ fileDate: req.userAudit?.fileDate,
1434
+ auditType: req.userAudit?.auditType,
1435
+ storeId: req.userAudit?.storeId,
1436
+ moduleType: req.userAudit?.moduleType,
1437
+ zoneName: req.userAudit?.zoneName,
1438
+ queueName: req.userAudit?.queueName,
1439
+ clientId: req.userAudit?.clientId,
1440
+ beforeCount: req.userAudit?.beforeCount,
1441
+ afterCount: inputData.customerCount,
1442
+ startTime: req.userAudit?.startTime,
1443
+ endTime: Date.now(),
1444
+ timeSpent: timeDifferenceInMinutes,
1445
+ auditId: new mongoose.Types.ObjectId( inputData.auditId ),
1446
+ },
1447
+ createdAt: Date.now(),
1448
+ };
1449
+
1450
+ await insertOpenSearchData( openSearch.auditLog, logData );
1402
1451
  } else {
1403
1452
  logger.info( 'Hit in Features', {
1404
1453
  data: _.omit( inputData, [ 'junk', 'employee', 'customer' ] ),
@@ -1417,10 +1466,8 @@ export async function save( req, res ) {
1417
1466
  timeSpent: timeDifferenceInMinutes,
1418
1467
  },
1419
1468
  );
1420
-
1421
1469
  const sqsProduceQueue = {
1422
- QueueUrl: `${sqs.url}${sqs.feature}`,
1423
-
1470
+ QueueUrl: ( auditConfig?.isCheckMinAfterCount ==1 && inputData.customerCount > auditConfig?.minAfterCount ) ? `${sqs.url}${sqs.feature_new}`: `${sqs.url}${sqs.feature}`,
1424
1471
  MessageBody: JSON.stringify( {
1425
1472
  store_id: inputData.storeId,
1426
1473
  store_date: inputData.fileDate,
@@ -1658,21 +1705,22 @@ export async function userAuditHistory( req, res ) {
1658
1705
  chunk.forEach( ( element ) => {
1659
1706
  exportData.push( {
1660
1707
  'File Date': element.fileDate,
1661
- 'Brand ID': element.clientId,
1662
1708
  'Brand Name': element.clientName,
1663
- 'Store ID': element.storeId,
1709
+ 'Brand ID': element.clientId,
1664
1710
  'Store Name': element.storeName,
1711
+ 'Store ID': element.storeId,
1665
1712
  'Zone Name': element.moduleType == 'zone' ? element.zoneName : '',
1666
1713
  'Audit Type': element.auditType,
1667
1714
  'Product Type': element.moduleType,
1668
1715
  'Before Count': element.beforeCount,
1669
1716
  'After Count': element.afterCount || '',
1670
1717
  'Accuracy': element.accuracy || '',
1718
+ 'Audited Date': dayjs( element.createdAt ).format( 'YYYY,MMM D' ),
1671
1719
  'Start Time': element.startTime,
1672
1720
  'End Time': element.endTime || '',
1673
1721
  'Time Spent': element.timeSpent,
1674
- 'Audit Status': element.auditStatus,
1675
- 'Audited Date': dayjs( element.createdAt ).format( 'YYYY,MMM D' ),
1722
+ 'Status': element.auditStatus,
1723
+
1676
1724
 
1677
1725
  } );
1678
1726
  } );
@@ -1889,16 +1937,16 @@ export async function clientMetrics( req, res ) {
1889
1937
  $project: {
1890
1938
  '_id': 0,
1891
1939
  'File Date': '$fileDate',
1892
- 'Client Name': '$clientName',
1893
- 'Client Id': '$clientId',
1940
+ 'Brand Name': '$clientName',
1941
+ 'Brand Id': '$clientId',
1894
1942
  'Product Type': '$moduleType',
1895
- 'Completed Percentage': '$completionPercentage',
1943
+ // 'Completed Percentage': '$completionPercentage',
1896
1944
  'Installed Stores': '$installedStore',
1897
1945
  'Audit Stores': '$totalFilesCount',
1898
1946
  'Inprogress Stores': '$inprogressStoresCount',
1899
- 'Not Assigned': '$notAssignedCount',
1947
+ 'Not Assigned stores': '$notAssignedCount',
1900
1948
  // 'Not Assigned Stores': '$notAssignedStores',
1901
- 'Completed': '$completedStores',
1949
+ 'Completed Stores': '$completedStores',
1902
1950
  'Status': '$clientStatus',
1903
1951
  },
1904
1952
  },
@@ -2164,44 +2212,46 @@ export async function storeMetrics( req, res ) {
2164
2212
  }
2165
2213
  const result = await aggregateStoreAudit( query );
2166
2214
  if ( inputData.isExport ) {
2167
- const exportdata = [];
2215
+ const exportData = [];
2168
2216
  result.forEach( ( element ) => {
2169
2217
  if ( element.moduleType == 'zone' ) {
2170
- exportdata.push( {
2218
+ exportData.push( {
2171
2219
  'File Date': element.fileDate,
2172
- 'Store ID': element.storeId,
2173
2220
  'Store Name': element.storeName,
2174
- 'Client Id': element.clientId,
2175
- 'Client Name': element.clientName,
2221
+ 'Store ID': element.storeId,
2222
+ // 'Client Id': element.clientId,
2223
+ // 'Client Name': element.clientName,
2224
+ 'Product Type': element.moduleType,
2176
2225
  'Zone Name': element.zoneName,
2177
2226
  'User Name': element.userName,
2178
2227
  'User Email': element.userEmail,
2179
2228
  'Before Count': element.beforeCount,
2180
2229
  'After Count': element.afterCount,
2181
- 'Audit Type': element.auditType,
2182
2230
  'Accuracy': element.accuracy,
2183
2231
  'Time Spent': element.timeSpent,
2232
+ 'Audit Type': element.auditType,
2184
2233
  'Status': element.status,
2185
2234
  } );
2186
2235
  } else {
2187
- exportdata.push( {
2236
+ exportData.push( {
2188
2237
  'File Date': element.fileDate,
2189
- 'Store ID': element.storeId,
2190
2238
  'Store Name': element.storeName,
2191
- 'Client Id': element.clientId,
2192
- 'Client Name': element.clientName,
2239
+ 'Store ID': element.storeId,
2240
+ // 'Client Id': element.clientId,
2241
+ // 'Client Name': element.clientName,
2242
+ 'Product Type': element.moduleType,
2193
2243
  'User Name': element.userName,
2194
2244
  'User Email': element.userEmail,
2195
2245
  'Before Count': element.beforeCount,
2196
2246
  'After Count': element.afterCount,
2197
- 'Audit Type': element.auditType,
2198
2247
  'Accuracy': element.accuracy,
2199
2248
  'Time Spent': element.timeSpent,
2249
+ 'Audit Type': element.auditType,
2200
2250
  'Status': element.status,
2201
2251
  } );
2202
2252
  }
2203
2253
  } );
2204
- await download( exportdata, res );
2254
+ await download( exportData, res );
2205
2255
  return;
2206
2256
  }
2207
2257
  return res.sendSuccess( { result: result, count: count.length } );
@@ -2263,6 +2313,7 @@ export async function userMetrics( req, res ) {
2263
2313
  totalCompletedFiles: { $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, 1, 0 ] },
2264
2314
  beforeCount: 1,
2265
2315
  afterCount: { $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, '$afterCount', 0 ] },
2316
+ createdAt: 1,
2266
2317
  },
2267
2318
  },
2268
2319
  {
@@ -2275,6 +2326,7 @@ export async function userMetrics( req, res ) {
2275
2326
  afterCount: { $sum: '$afterCount' },
2276
2327
  checkIntime: { $first: '$startTime' },
2277
2328
  checkOutTime: { $last: '$endTime' },
2329
+ createdAt: { $first: '$createdAt' },
2278
2330
 
2279
2331
  },
2280
2332
  },
@@ -2300,6 +2352,7 @@ export async function userMetrics( req, res ) {
2300
2352
  differenceInSeconds: {
2301
2353
  $divide: [ { $subtract: [ '$checkOutTime', '$checkIntime' ] }, 1000 ],
2302
2354
  },
2355
+ createdAt: 1,
2303
2356
  },
2304
2357
  },
2305
2358
  {
@@ -2315,6 +2368,7 @@ export async function userMetrics( req, res ) {
2315
2368
  hours: { $floor: { $divide: [ '$differenceInSeconds', 3600 ] } },
2316
2369
  minutes: { $floor: { $divide: [ { $mod: [ '$differenceInSeconds', 3600 ] }, 60 ] } },
2317
2370
  seconds: { $mod: [ '$differenceInSeconds', 60 ] },
2371
+ createdAt: 1,
2318
2372
  },
2319
2373
  },
2320
2374
  {
@@ -2349,7 +2403,7 @@ export async function userMetrics( req, res ) {
2349
2403
  else: { $toString: { $round: [ '$seconds', 0 ] } },
2350
2404
  },
2351
2405
  },
2352
-
2406
+ createdAt: 1,
2353
2407
  },
2354
2408
  },
2355
2409
  {
@@ -2367,6 +2421,7 @@ export async function userMetrics( req, res ) {
2367
2421
  workingHours: {
2368
2422
  $concat: [ '$hours', ':', '$minutes', ':', '$seconds' ],
2369
2423
  },
2424
+ createdAt: 1,
2370
2425
  },
2371
2426
  },
2372
2427
  {
@@ -2420,6 +2475,7 @@ export async function userMetrics( req, res ) {
2420
2475
  },
2421
2476
  },
2422
2477
  workingHours: 1,
2478
+ createdAt: 1,
2423
2479
  },
2424
2480
  },
2425
2481
  ];
@@ -2457,15 +2513,17 @@ export async function userMetrics( req, res ) {
2457
2513
  {
2458
2514
  $project: {
2459
2515
  '_id': 0,
2460
- 'File Date': '$fileDate',
2461
2516
  'User Name': '$userName',
2462
- 'Total Completed Files Count': '$totalCompletedFiles',
2517
+ 'File Date': '$fileDate',
2518
+ 'Completed Files': '$totalCompletedFiles',
2519
+ 'Product Type': '$moduleType',
2463
2520
  'Total Before Count': '$beforeCount',
2464
2521
  'Total After Count': '$afterCount',
2465
- 'check Intime': '$checkIntime',
2466
- 'check OutTime': '$checkOutTime',
2467
- 'working Hours': '$workingHours',
2468
2522
  'Mapping Percentage': '$mappingPerc',
2523
+ 'Check Intime': '$checkIntime',
2524
+ 'Check OutTime': '$checkOutTime',
2525
+ 'Working Hours': '$workingHours',
2526
+ 'Audited Date': { $dateToString: { format: '%Y,%b %d', date: '$createdAt' } },
2469
2527
  },
2470
2528
  },
2471
2529
  );
@@ -2620,10 +2678,10 @@ export async function pendingSummaryTable( req, res ) {
2620
2678
 
2621
2679
  const result = await aggregateUserAudit( query );
2622
2680
  if ( inputData.isExport ) {
2623
- const exportdata = [];
2681
+ const exportData = [];
2624
2682
  result.forEach( ( element ) => {
2625
2683
  if ( element.moduleType == 'zone' ) {
2626
- exportdata.push( {
2684
+ exportData.push( {
2627
2685
  'Store ID': element.storeId,
2628
2686
  'zoneName': element.zoneName,
2629
2687
  'File Date': element.fileDate,
@@ -2636,7 +2694,7 @@ export async function pendingSummaryTable( req, res ) {
2636
2694
  'Status': element.auditStatus,
2637
2695
  } );
2638
2696
  } else {
2639
- exportdata.push( {
2697
+ exportData.push( {
2640
2698
  'Store ID': element.storeId,
2641
2699
  'File Date': element.fileDate,
2642
2700
  'Product Type': element.moduleType,
@@ -2649,7 +2707,7 @@ export async function pendingSummaryTable( req, res ) {
2649
2707
  } );
2650
2708
  }
2651
2709
  } );
2652
- await download( exportdata, res );
2710
+ await download( exportData, res );
2653
2711
  return;
2654
2712
  }
2655
2713
  return res.sendSuccess( { result: result, count: count.length } );
@@ -2938,6 +2996,7 @@ export async function overViewCard( req, res ) {
2938
2996
  inprogressCount: 0,
2939
2997
  completedCount: 0,
2940
2998
  pendingCount: 0,
2999
+ notAssigned: 0,
2941
3000
  };
2942
3001
 
2943
3002
  const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
@@ -2968,7 +3027,7 @@ export async function overViewCard( req, res ) {
2968
3027
  },
2969
3028
  inprogressCount: {
2970
3029
  $cond: [
2971
- { $ne: [ '$status', 'completed' ] }, 1, 0,
3030
+ { $in: [ '$status', [ 'inprogress', 'assigned', 'drafted' ] ] }, 1, 0,
2972
3031
  ],
2973
3032
  },
2974
3033
  completedCount: {
@@ -3195,7 +3254,10 @@ export async function summarySplit( req, res ) {
3195
3254
  ];
3196
3255
  const result = await aggregateStoreAudit( query );
3197
3256
  const totalCount = await aggregateAuditStoreData( totalQuery );
3198
- result[0].totalCount = totalCount[0]?.total;
3257
+ if ( totalCount.length> 0 ) {
3258
+ result[0].totalCount = totalCount[0]?.total;
3259
+ }
3260
+
3199
3261
  return res.sendSuccess( { result: result.length > 0 ? result[0] : temp } );
3200
3262
  } catch ( error ) {
3201
3263
  const err = error.message || 'Internal Server Error';
@@ -3336,38 +3398,32 @@ export async function overviewTable( req, res ) {
3336
3398
 
3337
3399
  const result = await aggregateStoreAudit( query );
3338
3400
  if ( inputData.isExport ) {
3339
- const exportdata = [];
3401
+ const exportData = [];
3340
3402
  result.forEach( ( element ) => {
3341
3403
  if ( element.moduleType == 'zone' ) {
3342
- exportdata.push( {
3343
- 'Store ID': element.storeId,
3404
+ exportData.push( {
3405
+ 'File Date': element.fileDate,
3344
3406
  'Brand ID': element.clientId,
3407
+ 'Store ID': element.storeId,
3345
3408
  'Zone Name': element.zoneName,
3346
- 'File Date': element.fileDate,
3347
- 'Product Type': element.moduleType,
3348
- 'Audit Type': element.auditType,
3349
3409
  'Before Count': element.beforeCount,
3350
3410
  'After Count': element.afterCount,
3351
- // 'User Name': element.userName,
3352
- // 'User Email': element.userEmail,
3353
- 'Status': element.status,
3411
+ 'Audit Type': element.auditType,
3412
+ 'Status': toCamelCase( element.status ),
3354
3413
  } );
3355
3414
  } else {
3356
- exportdata.push( {
3357
- 'Store ID': element.storeId,
3358
- 'Brand ID': element.clientId,
3415
+ exportData.push( {
3359
3416
  'File Date': element.fileDate,
3360
- 'Product Type': element.moduleType,
3361
- 'Audit Type': element.auditType,
3417
+ 'Brand ID': element.clientId,
3418
+ 'Store ID': element.storeId,
3362
3419
  'Before Count': element.beforeCount,
3363
3420
  'After Count': element.afterCount,
3364
- // 'User Name': element.userName,
3365
- // 'User Email': element.userEmail,
3366
- 'Status': element.status,
3421
+ 'Audit Type': element.auditType,
3422
+ 'Status': toCamelCase( element.status ),
3367
3423
  } );
3368
3424
  }
3369
3425
  } );
3370
- await download( exportdata, res );
3426
+ await download( exportData, res );
3371
3427
  return;
3372
3428
  }
3373
3429
  return res.sendSuccess( { result: result, count: count.length } );
@@ -3378,6 +3434,18 @@ export async function overviewTable( req, res ) {
3378
3434
  }
3379
3435
  }
3380
3436
 
3437
+ export function toCamelCase( str ) {
3438
+ logger.info( { str: str } );
3439
+ return str
3440
+ .split( '-' ) // Split the string by spaces
3441
+ .map( ( word, index ) =>
3442
+ // index === 0 ?
3443
+ // word.toLowerCase() : // Lowercase the first word
3444
+ word.charAt( 0 ).toUpperCase() + word.slice( 1 ).toLowerCase(), // Capitalize the first letter of subsequent words
3445
+ )
3446
+ .join( '' ); // Join the words without spaces
3447
+ }
3448
+
3381
3449
  export async function totalNotAssignedCount( req, res ) {
3382
3450
  try {
3383
3451
  let notAssignedCount = {
@@ -3572,7 +3640,7 @@ export async function totalNotAssignedCount( req, res ) {
3572
3640
  const mappingInprogressCount = [
3573
3641
  {
3574
3642
  $match: {
3575
- fileDate: { $eq: latestDate },
3643
+ fileDate: { $eq: latestTraxDate },
3576
3644
  },
3577
3645
  },
3578
3646
  {
@@ -3594,17 +3662,14 @@ export async function totalNotAssignedCount( req, res ) {
3594
3662
  $group: {
3595
3663
  _id: null,
3596
3664
  inprogressAuditCount: { $sum: '$inprogressAuditCount' },
3597
- inprogressZoneCount: { $sum: '$inprogressZoneCount' },
3598
- inprogressTrafficCount: { $sum: '$inprogressTrafficCount' },
3599
-
3665
+ inprogressTraxCount: { $sum: '$inprogressTraxCount' },
3600
3666
  },
3601
3667
  },
3602
3668
  {
3603
3669
  $project: {
3604
3670
  _id: 0,
3605
3671
  inprogressAuditCount: { $ifNull: [ '$inprogressAuditCount', 0 ] },
3606
- inprogressZoneCount: { $ifNull: [ '$inprogressZoneCount', 0 ] },
3607
- inprogressTrafficCount: { $ifNull: [ '$inprogressTrafficCount', 0 ] },
3672
+ inprogressTraxCount: { $ifNull: [ '$inprogressTraxCount', 0 ] },
3608
3673
  },
3609
3674
  },
3610
3675
  ];
@@ -3693,6 +3758,7 @@ export async function totalNotAssignedCount( req, res ) {
3693
3758
  const getInprogressCount = await aggregateStoreAudit( inprogressCount );
3694
3759
  const getBinaryInprogressCount = await aggregateBinaryAudit( binaryInprogressCount );
3695
3760
  const getMappingInprogressCount = await aggregateStoreEmpDetection( mappingInprogressCount );
3761
+ logger.info( { getMappingInprogressCount: getMappingInprogressCount } );
3696
3762
  // const getMappingInprogressCount = await aggregateStoreEmpDetection( mappingInprogressCount );
3697
3763
  if ( getTotalcount.length == 0 && getTotalTraxAudit.length == 0 ) {
3698
3764
  return res.sendError( 'No Data Found', 204 );
@@ -3801,18 +3867,45 @@ export async function auditImages( req, res ) {
3801
3867
  export async function getUserAuditCount( req, res ) {
3802
3868
  try {
3803
3869
  const userWallet = JSON.parse( process.env.USER_WALLET );
3804
- const previousDate = dayjs( new Date() ).subtract( 1, 'days' );
3870
+ const previousDate = dayjs( new Date() ).subtract( 19, 'days' );
3805
3871
  const fileDate = dayjs( previousDate ).format( 'DD-MM-YYYY' );
3806
3872
  const configTimeSpentSec = userWallet.minTimeSpentSec; // 18000;
3807
- const beforeCountX = userWallet.beforeCountCreditPerc; // 0.0335;
3808
- const afterCountY = userWallet.afterCountCreditPerc; // 0.0035;
3873
+ // const beforeCountX = userWallet.beforeCountCreditPerc; // 0.0335;
3874
+ // const afterCountY = userWallet.afterCountCreditPerc; // 0.0035;
3809
3875
  const reduce = userWallet.reductionPerc; // 0.05;
3876
+ const auditUserQuery=[
3877
+ {
3878
+ $match: {
3879
+ $and: [
3880
+ { type: { $eq: 'consultant' } },
3881
+ { isActive: { $eq: true } },
3882
+ ],
3883
+ },
3884
+ },
3885
+ {
3886
+ $group: {
3887
+ _id: null,
3888
+ userList: { $push: '$userId' },
3889
+ },
3890
+ },
3891
+ {
3892
+ $project: {
3893
+ _id: 0,
3894
+ userList: 1,
3895
+ },
3896
+ },
3897
+ ];
3898
+ const auditUserList = await aggregateAuditUsers( auditUserQuery );
3899
+ if ( auditUserList.length==0 ) {
3900
+ return res.sendError( 'No users available', 204 );
3901
+ }
3810
3902
  const userCountQuery= [
3811
3903
  {
3812
3904
  $match: {
3813
3905
  $and: [
3814
3906
  { fileDate: { $eq: fileDate } },
3815
3907
  { auditStatus: { $eq: 'completed' } },
3908
+ { userId: { $in: auditUserList[0].userList } },
3816
3909
  ],
3817
3910
  },
3818
3911
  },
@@ -3887,6 +3980,11 @@ export async function getUserAuditCount( req, res ) {
3887
3980
  }
3888
3981
  const zoneMinimumTarget =userCount[0]?.zoneUserCount?.length !== 0 && userCount[0]?.zoneFilesCount !==0 ? Math.floor( userCount[0].zoneFilesCount/userCount[0].zoneUserCount.length ) : 0;
3889
3982
  const trafficMinimumTarget =userCount[0]?.trafficUserCount?.length !== 0 && userCount[0]?.trafficFilesCount !==0 ? Math.floor( userCount[0].trafficFilesCount/userCount[0].trafficUserCount.length ) : 0;
3983
+
3984
+ const beforeCountTraffic = 544/trafficMinimumTarget;
3985
+ const beforeCountZone = 544/zoneMinimumTarget; // 0.0035;
3986
+ const afterCountCredit = 136;
3987
+
3890
3988
  const query = [
3891
3989
  {
3892
3990
  $match: {
@@ -3895,11 +3993,13 @@ export async function getUserAuditCount( req, res ) {
3895
3993
  {
3896
3994
  auditStatus: { $eq: 'completed' },
3897
3995
  },
3996
+ { userId: { $in: auditUserList[0].userList } },
3898
3997
  ],
3899
3998
  },
3900
3999
  },
3901
4000
  {
3902
- $set: { minimumTarget: { $cond: [ { $eq: [ '$moduleType', 'traffic' ] }, trafficMinimumTarget, zoneMinimumTarget ] } },
4001
+ $set: { minimumTarget: { $cond: [ { $eq: [ '$moduleType', 'traffic' ] }, trafficMinimumTarget, zoneMinimumTarget ] },
4002
+ afterCountCredit: afterCountCredit },
3903
4003
  },
3904
4004
  {
3905
4005
  $project: {
@@ -3911,9 +4011,13 @@ export async function getUserAuditCount( req, res ) {
3911
4011
  fileDate: 1,
3912
4012
  fileDateISO: 1,
3913
4013
  minimumTarget: 1,
4014
+ afterCountCredit: 1,
3914
4015
  totalEarn: { $ifNull: [ 0, 0 ] },
4016
+ singleImageAmount: { $ifNull: [ 0, 0 ] },
3915
4017
  totalReducedAmount: { $ifNull: [ 0, 0 ] },
3916
4018
  totalCredit: { $ifNull: [ 0, 0 ] },
4019
+ deductionBytimeSpent: { $ifNull: [ 0, 0 ] },
4020
+ deductionByMinimumTarget: { $ifNull: [ 0, 0 ] },
3917
4021
  },
3918
4022
  },
3919
4023
  {
@@ -3928,9 +4032,13 @@ export async function getUserAuditCount( req, res ) {
3928
4032
  fileDateISO: { $first: '$fileDateISO' },
3929
4033
  userId: { $first: '$userId' },
3930
4034
  minimumTarget: { $first: '$minimumTarget' },
4035
+ afterCountCredit: { $first: '$afterCountCredit' },
3931
4036
  totalEarn: { $first: '$totalEarn' },
3932
4037
  totalReducedAmount: { $first: '$totalReducedAmount' },
3933
4038
  totalCredit: { $first: '$totalCredit' },
4039
+ deductionByMinimumTarget: { $first: '$deductionByMinimumTarget' },
4040
+ deductionBytimeSpent: { $first: '$deductionBytimeSpent' },
4041
+ singleImageAmount: { $first: '$singleImageAmount' },
3934
4042
  },
3935
4043
  },
3936
4044
  {
@@ -3944,7 +4052,33 @@ export async function getUserAuditCount( req, res ) {
3944
4052
  fileDate: 1,
3945
4053
  fileDateISO: 1,
3946
4054
  minimumTarget: 1,
3947
- totalEarn: { $add: [ { $multiply: [ '$totalBeforeCount', beforeCountX ] }, { $multiply: [ '$totalAfterCount', afterCountY ] } ] },
4055
+ singleImageAmount: { $cond: [ { $eq: [ '$moduleType', 'traffic' ] }, beforeCountTraffic, beforeCountZone ] },
4056
+ beforeCountCredit: { $multiply: [ '$totalBeforeCount', { $cond: [ { $eq: [ '$moduleType', 'traffic' ] }, beforeCountTraffic, beforeCountZone ] } ] },
4057
+ // afterCountCredit: { $cond: [ { $and: [
4058
+ // { $gte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.4 ] }, { $lte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4059
+ // ] }, 136,
4060
+ // {
4061
+ // $cond: [ { $gt: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4062
+ // { $subtract: [ 136, { $multiply: [ { $subtract: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] }, 136 ] } ] },
4063
+ // { $subtract: [ 136, { $multiply: [ { $subtract: [ 0.4, { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] } ] }, 136 ] } ] },
4064
+ // ],
4065
+ // },
4066
+ // ] },
4067
+ afterCountCredit: afterCountCredit,
4068
+ totalEarn: { $add: [ { $multiply: [ '$totalBeforeCount', { $cond: [ { $eq: [ '$moduleType', 'traffic' ] }, beforeCountTraffic, beforeCountZone ] } ] }, 136 ] },
4069
+ deductionBytimeSpent: 1,
4070
+ deductionByMinimumTarget: 1,
4071
+ mappingperc: { $multiply: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 100 ] },
4072
+ deductionByMapping: { $cond: [ { $and: [
4073
+ { $gte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.4 ] }, { $lte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4074
+ ] }, 0,
4075
+ {
4076
+ $cond: [ { $gt: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4077
+ { $multiply: [ { $subtract: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] }, 136 ] },
4078
+ { $multiply: [ { $subtract: [ 0.4, { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] } ] }, 136 ] },
4079
+ ],
4080
+ },
4081
+ ] },
3948
4082
  totalReducedAmount: 1,
3949
4083
  totalCredit: 1,
3950
4084
 
@@ -3958,16 +4092,36 @@ export async function getUserAuditCount( req, res ) {
3958
4092
  totalBeforeCount: 1,
3959
4093
  totalAfterCount: 1,
3960
4094
  totalTimeSpent: 1,
4095
+ hours: { $floor: { $divide: [ '$totalTimeSpent', 3600 ] } },
4096
+ minutes: {
4097
+ $floor: {
4098
+ $divide: [
4099
+ { $mod: [ '$totalTimeSpent', 3600 ] }, // Get remaining minutes after hours
4100
+ 60,
4101
+ ] },
4102
+ },
3961
4103
  fileDate: 1,
3962
4104
  fileDateISO: 1,
3963
4105
  minimumTarget: 1,
4106
+ singleImageAmount: { $cond: [ { $eq: [ '$moduleType', 'traffic' ] }, beforeCountTraffic, beforeCountZone ] },
4107
+ beforeCountCredit: 1,
4108
+ afterCountCredit: 1,
3964
4109
  totalEarn: 1,
3965
- totalReducedAmount: { $cond: [
4110
+ deductionBytimeSpent: { $cond: [
3966
4111
  { $lt: [ '$totalTimeSpent', configTimeSpentSec ] },
3967
- { $add: [ '$totalReducedAmount', { $multiply: [ reduce, '$totalEarn' ] } ] }, '$totalReducedAmount',
4112
+ { $multiply: [ reduce, '$totalEarn' ] }, '$deductionBytimeSpent',
4113
+ ] },
4114
+
4115
+ deductionByMinimumTarget:
4116
+ { $cond: [
4117
+ { $lt: [ '$totalBeforeCount', '$minimumTarget' ] },
4118
+ { $multiply: [ reduce, '$totalEarn' ] }, '$deductionByMinimumTarget',
3968
4119
  ] },
4120
+ mappingperc: 1,
4121
+ deductionByMapping: 1,
3969
4122
  totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
3970
4123
 
4124
+
3971
4125
  },
3972
4126
  },
3973
4127
 
@@ -3978,18 +4132,39 @@ export async function getUserAuditCount( req, res ) {
3978
4132
  moduleType: 1,
3979
4133
  totalBeforeCount: 1,
3980
4134
  totalAfterCount: 1,
3981
- totalTimeSpent: 1,
4135
+ totalTimeSpent: {
4136
+ $cond: [ { $eq: [ '$hours', 0 ] },
4137
+ { $concat: [
4138
+ '00:', { $toString: '$minutes' }, ':00',
4139
+ ] },
4140
+ { $cond: [ { $eq: [ '$minutes', 0 ] },
4141
+ { $concat: [
4142
+ { $toString: '$hours' }, ':00:00',
4143
+ ] },
4144
+ {
4145
+ $concat: [
4146
+ { $toString: '$hours' }, ':',
4147
+ { $toString: '$minutes' }, ':00',
4148
+ ],
4149
+ },
4150
+ ] },
4151
+ ],
4152
+
4153
+ },
3982
4154
  fileDate: 1,
3983
4155
  fileDateISO: 1,
3984
4156
  minimumTarget: 1,
3985
- totalEarn: { $add: [ { $multiply: [ '$totalBeforeCount', beforeCountX ] }, { $multiply: [ '$totalAfterCount', afterCountY ] } ] },
3986
- totalReducedAmount:
3987
- { $cond: [
3988
- { $lt: [ '$totalBeforeCount', '$minimumTarget' ] },
3989
- { $add: [ '$totalReducedAmount', { $multiply: [ reduce, '$totalEarn' ] } ] }, '$totalReducedAmount',
3990
- ] },
3991
-
4157
+ beforeCountCredit: 1,
4158
+ afterCountCredit: 1,
4159
+ totalEarn: 1,
4160
+ deductionByMinimumTarget: 1,
4161
+ singleImageAmount: 1,
4162
+ deductionBytimeSpent: 1,
4163
+ mappingperc: 1,
4164
+ deductionByMapping: 1,
3992
4165
  totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
4166
+ totalReducedAmount: { $round: [ { $add: [ '$deductionBytimeSpent', '$deductionByMinimumTarget', '$deductionByMapping' ] }, 2 ] },
4167
+
3993
4168
  },
3994
4169
  },
3995
4170
  {
@@ -4021,15 +4196,23 @@ export async function getUserAuditCount( req, res ) {
4021
4196
  moduleType: 1,
4022
4197
  totalAfterCount: { $round: [ '$totalAfterCount', 2 ] },
4023
4198
  totalBeforeCount: { $round: [ '$totalBeforeCount', 2 ] },
4024
- timeSpent: { $round: [ '$totalTimeSpent', 2 ] },
4199
+ timeSpent: '$totalTimeSpent',
4025
4200
  fileDate: 1,
4026
4201
  fileDateISO: 1,
4027
- totalReducedAmount: { $round: [ '$totalReducedAmount', 2 ] },
4028
- totalCredit: { $round: [ { $subtract: [ '$totalEarn', '$totalReducedAmount' ] }, 2 ] },
4202
+ beforeCountCredit: 1,
4203
+ afterCountCredit: 1,
4029
4204
  totalEarn: { $round: [ '$totalEarn', 2 ] },
4030
4205
  minimumTarget: 1,
4206
+ singleImageAmount: 1,
4207
+ deductionByMinimumTarget: { $round: [ '$deductionByMinimumTarget', 2 ] },
4208
+ deductionBytimeSpent: { $round: [ '$deductionBytimeSpent', 2 ] },
4209
+ mappingperc: 1,
4210
+ deductionByMapping: 1,
4211
+ totalReducedAmount: 1,
4212
+ totalCredit: { $round: [ { $subtract: [ '$totalEarn', '$totalReducedAmount' ] }, 2 ] },
4031
4213
  userEmail: '$users.email', // Access first element from array
4032
- userName: '$users.userName', // Access first element from array
4214
+ userName: '$users.userName', // Access first element from array,
4215
+
4033
4216
  },
4034
4217
  },
4035
4218
  ];
@@ -4354,35 +4537,81 @@ export async function auditViewLogs( req, res ) {
4354
4537
  try {
4355
4538
  const inputData = req.query;
4356
4539
  const parsedOpenSearch = JSON.parse( process.env.OPENSEARCH );
4357
- let downTimeQuery = {
4540
+ const logData =[];
4541
+ let logsQuery = {
4358
4542
  'size': 100,
4359
4543
  'query': {
4360
4544
  'bool': {
4361
4545
  'must': [
4362
4546
  {
4363
4547
  'term': {
4364
- 'doc.logData.fileDate.keyword': dayjs( inputData.fileDate ).format( 'DD-MM-YYYY' ),
4548
+ 'logData.fileDate.keyword': inputData.fileDate,
4365
4549
  },
4366
4550
  },
4367
4551
  {
4368
4552
  'term': {
4369
- 'doc.logData.storeId.keyword': inputData.storeId,
4553
+ 'logData.storeId.keyword': inputData.storeId,
4370
4554
  },
4371
4555
  },
4372
4556
  {
4373
4557
  'term': {
4374
- 'doc.logData.moduleType.keyword': inputData.moduleType,
4558
+ 'logData.moduleType.keyword': inputData.moduleType,
4559
+ },
4560
+ },
4561
+ {
4562
+ 'terms': {
4563
+ 'logSubType.keyword': [ 'auditDone', 'reAuditPush', 'reTrigger' ],
4375
4564
  },
4376
4565
  },
4377
4566
  ],
4378
-
4379
4567
  },
4380
4568
  },
4381
4569
  };
4382
- const downtime = await getOpenSearchData( parsedOpenSearch.auditLog, downTimeQuery );
4383
- return res.sendSuccess( downtime );
4384
- } catch ( err ) {
4385
-
4570
+ const logs = await getOpenSearchData( parsedOpenSearch.auditLog, logsQuery );
4571
+ logger.info( { logs: logs.body.hits, inputData: inputData, auditLog: parsedOpenSearch.auditLog } );
4572
+ if ( logs.body.hits.hits.length > 0 ) {
4573
+ if ( logs.body.hits.hits[0]._source.logSubType == 'auditDone' ) {
4574
+ const convertedTime = await convertTimestampToDateTime( logs.body.hits.hits[0]._source.logData.endTime );
4575
+ logData.push( {
4576
+ ...logs.body.hits.hits[0]._source, // Spread the _source object
4577
+ time: convertedTime, // Add the converted time
4578
+ } );
4579
+ } else {
4580
+ const convertedTime = await convertTimestampToDateTime( logs.body.hits.hits[0]._source.createdAt );
4581
+ logData.push( {
4582
+ ...logs.body.hits.hits[0]._source, // Spread the _source object
4583
+ time: convertedTime, // Add the converted time
4584
+ } );
4585
+ }
4586
+ return res.sendSuccess( logData );
4587
+ } else {
4588
+ return res.sendError( 'No Record Inserted', 204 );
4589
+ }
4590
+ } catch ( error ) {
4591
+ const err = error.message || 'Internal Server Error';
4592
+ logger.info( { error: error, message: req.body, function: 'auditViewLogs' } );
4593
+ return res.sendError( err, 500 );
4386
4594
  }
4387
4595
  }
4388
4596
 
4597
+ export async function convertTimestampToDateTime( timestamp ) {
4598
+ // Check if the timestamp is too large (nanoseconds), then convert it to milliseconds
4599
+ if ( timestamp > 9999999999999 ) {
4600
+ timestamp = Math.floor( timestamp / 1000 ); // Convert to milliseconds if needed
4601
+ }
4602
+
4603
+ // Create a new Date object
4604
+ const date = new Date( timestamp );
4605
+
4606
+ // Format the date and time components
4607
+ const year = date.getFullYear();
4608
+ const month = ( '0' + ( date.getMonth() + 1 ) ).slice( -2 ); // Months are zero-based
4609
+ const day = ( '0' + date.getDate() ).slice( -2 );
4610
+
4611
+ const hours = ( '0' + date.getHours() ).slice( -2 );
4612
+ const minutes = ( '0' + date.getMinutes() ).slice( -2 );
4613
+ const seconds = ( '0' + date.getSeconds() ).slice( -2 );
4614
+
4615
+ // Return formatted date and time string
4616
+ return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
4617
+ }