tango-app-api-audit 3.4.3-alpha.2 → 3.4.3-alpha.20

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.
@@ -9,6 +9,7 @@ import {
9
9
  signedUrl,
10
10
  getDate, getDateWithCustomizeTime,
11
11
  getUTC,
12
+ getOpenSearchData,
12
13
  } from 'tango-app-api-middleware';
13
14
  import {
14
15
  aggregateUserAudit,
@@ -63,6 +64,7 @@ import { aggregateStoreEmpDetection } from '../service/storeEmpDetection.service
63
64
  import { aggregateBinaryAudit } from '../service/binaryAudit.service.js';
64
65
  import { aggregateTraxAuditData, findOneTraxAuditData } from '../service/traxAuditData.service.js';
65
66
  import { createAuditUserWallet } from '../service/auditUserWallet.service.js';
67
+ import { aggregateAuditUsers } from '../service/auditUsers.service.js';
66
68
 
67
69
  /* < -- *** Configuration *** --> */
68
70
 
@@ -354,6 +356,7 @@ export async function getAuditFile( req, res ) {
354
356
  auditId: userDetails[0]._id,
355
357
  moduleType: userDetails[0].moduleType,
356
358
  zoneName: userDetails[0].zoneName,
359
+ beforeCount: userDetails[0].beforeCount,
357
360
  },
358
361
  createdAt: new Date(),
359
362
  };
@@ -456,6 +459,7 @@ export async function getAuditFile( req, res ) {
456
459
  auditId: reauditInsert._id,
457
460
  zoneName: msg.zone_id,
458
461
  moduleType: inputData.moduleType,
462
+ beforeCount: msg.total_count,
459
463
  },
460
464
  createdAt: new Date(),
461
465
  };
@@ -608,6 +612,7 @@ export async function getAuditFile( req, res ) {
608
612
  auditId: insertData._id,
609
613
  zoneName: msg.zone_id,
610
614
  moduleType: inputData.moduleType,
615
+ beforeCount: msg.total_count,
611
616
  },
612
617
  createdAt: new Date(),
613
618
  };
@@ -1101,9 +1106,9 @@ export async function workSpace( req, res ) {
1101
1106
  }
1102
1107
 
1103
1108
  if ( inputData.isExport ) {
1104
- const exportdata = [];
1109
+ const exportData = [];
1105
1110
  temp.forEach( ( element ) => {
1106
- exportdata.push( {
1111
+ exportData.push( {
1107
1112
  'Client Name': element.clientName,
1108
1113
  'Client Id': element.clientId,
1109
1114
  'Completed stores Count': element.completedStoresCount,
@@ -1114,7 +1119,7 @@ export async function workSpace( req, res ) {
1114
1119
  'Assigned': element.Assignedcount,
1115
1120
  } );
1116
1121
  } );
1117
- await download( exportdata, res );
1122
+ await download( exportData, res );
1118
1123
  return;
1119
1124
  } else {
1120
1125
  return res.sendSuccess( {
@@ -1216,6 +1221,7 @@ export async function getDraftedData( req, res ) {
1216
1221
  userId: userId,
1217
1222
  moduleType: inputData.moduleType,
1218
1223
  zoneName: inputData.zoneName,
1224
+ auditId: inputData.auditId,
1219
1225
  };
1220
1226
  const result = await findOneAuditLog( query, {}, { createdAt: -1 }, 1 );
1221
1227
 
@@ -1236,8 +1242,10 @@ export async function getDraftedData( req, res ) {
1236
1242
 
1237
1243
  export async function save( req, res ) {
1238
1244
  try {
1245
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
1239
1246
  const bucket = JSON.parse( process.env.BUCKET );
1240
1247
  const sqs = JSON.parse( process.env.SQS );
1248
+ const auditConfig = JSON.parse( process.env.AUDIT_CONFIG );
1241
1249
  const inputData = req.body;
1242
1250
  let storeAuditData = await findOneStoreAudit( {
1243
1251
  storeId: inputData.storeId,
@@ -1328,12 +1336,29 @@ export async function save( req, res ) {
1328
1336
  },
1329
1337
  );
1330
1338
 
1331
- /** *
1332
- *
1333
- * logs need to be added
1334
- *
1335
- *
1336
- */
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 );
1337
1362
 
1338
1363
  let storeConfig = await findOneStore(
1339
1364
  {
@@ -1398,6 +1423,31 @@ export async function save( req, res ) {
1398
1423
  timeSpent: timeDifferenceInMinutes,
1399
1424
  },
1400
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 );
1401
1451
  } else {
1402
1452
  logger.info( 'Hit in Features', {
1403
1453
  data: _.omit( inputData, [ 'junk', 'employee', 'customer' ] ),
@@ -1416,10 +1466,8 @@ export async function save( req, res ) {
1416
1466
  timeSpent: timeDifferenceInMinutes,
1417
1467
  },
1418
1468
  );
1419
-
1420
1469
  const sqsProduceQueue = {
1421
- QueueUrl: `${sqs.url}${sqs.feature}`,
1422
-
1470
+ QueueUrl: ( auditConfig?.isCheckMinAfterCount ==1 && auditConfig?.minAfterCount ) >1500? `${sqs.url}${sqs.feature_new}`: `${sqs.url}${sqs.feature}`,
1423
1471
  MessageBody: JSON.stringify( {
1424
1472
  store_id: inputData.storeId,
1425
1473
  store_date: inputData.fileDate,
@@ -1657,21 +1705,22 @@ export async function userAuditHistory( req, res ) {
1657
1705
  chunk.forEach( ( element ) => {
1658
1706
  exportData.push( {
1659
1707
  'File Date': element.fileDate,
1660
- 'Brand ID': element.clientId,
1661
1708
  'Brand Name': element.clientName,
1662
- 'Store ID': element.storeId,
1709
+ 'Brand ID': element.clientId,
1663
1710
  'Store Name': element.storeName,
1711
+ 'Store ID': element.storeId,
1664
1712
  'Zone Name': element.moduleType == 'zone' ? element.zoneName : '',
1665
1713
  'Audit Type': element.auditType,
1666
1714
  'Product Type': element.moduleType,
1667
1715
  'Before Count': element.beforeCount,
1668
1716
  'After Count': element.afterCount || '',
1669
1717
  'Accuracy': element.accuracy || '',
1718
+ 'Audited Date': dayjs( element.createdAt ).format( 'YYYY,MMM D' ),
1670
1719
  'Start Time': element.startTime,
1671
1720
  'End Time': element.endTime || '',
1672
1721
  'Time Spent': element.timeSpent,
1673
- 'Audit Status': element.auditStatus,
1674
- 'Audited Date': dayjs( element.createdAt ).format( 'YYYY,MMM D' ),
1722
+ 'Status': element.auditStatus,
1723
+
1675
1724
 
1676
1725
  } );
1677
1726
  } );
@@ -1888,16 +1937,16 @@ export async function clientMetrics( req, res ) {
1888
1937
  $project: {
1889
1938
  '_id': 0,
1890
1939
  'File Date': '$fileDate',
1891
- 'Client Name': '$clientName',
1892
- 'Client Id': '$clientId',
1940
+ 'Brand Name': '$clientName',
1941
+ 'Brand Id': '$clientId',
1893
1942
  'Product Type': '$moduleType',
1894
- 'Completed Percentage': '$completionPercentage',
1943
+ // 'Completed Percentage': '$completionPercentage',
1895
1944
  'Installed Stores': '$installedStore',
1896
1945
  'Audit Stores': '$totalFilesCount',
1897
1946
  'Inprogress Stores': '$inprogressStoresCount',
1898
- 'Not Assigned': '$notAssignedCount',
1947
+ 'Not Assigned stores': '$notAssignedCount',
1899
1948
  // 'Not Assigned Stores': '$notAssignedStores',
1900
- 'Completed': '$completedStores',
1949
+ 'Completed Stores': '$completedStores',
1901
1950
  'Status': '$clientStatus',
1902
1951
  },
1903
1952
  },
@@ -2163,44 +2212,46 @@ export async function storeMetrics( req, res ) {
2163
2212
  }
2164
2213
  const result = await aggregateStoreAudit( query );
2165
2214
  if ( inputData.isExport ) {
2166
- const exportdata = [];
2215
+ const exportData = [];
2167
2216
  result.forEach( ( element ) => {
2168
2217
  if ( element.moduleType == 'zone' ) {
2169
- exportdata.push( {
2218
+ exportData.push( {
2170
2219
  'File Date': element.fileDate,
2171
- 'Store ID': element.storeId,
2172
2220
  'Store Name': element.storeName,
2173
- 'Client Id': element.clientId,
2174
- 'Client Name': element.clientName,
2221
+ 'Store ID': element.storeId,
2222
+ // 'Client Id': element.clientId,
2223
+ // 'Client Name': element.clientName,
2224
+ 'Product Type': element.moduleType,
2175
2225
  'Zone Name': element.zoneName,
2176
2226
  'User Name': element.userName,
2177
2227
  'User Email': element.userEmail,
2178
2228
  'Before Count': element.beforeCount,
2179
2229
  'After Count': element.afterCount,
2180
- 'Audit Type': element.auditType,
2181
2230
  'Accuracy': element.accuracy,
2182
2231
  'Time Spent': element.timeSpent,
2232
+ 'Audit Type': element.auditType,
2183
2233
  'Status': element.status,
2184
2234
  } );
2185
2235
  } else {
2186
- exportdata.push( {
2236
+ exportData.push( {
2187
2237
  'File Date': element.fileDate,
2188
- 'Store ID': element.storeId,
2189
2238
  'Store Name': element.storeName,
2190
- 'Client Id': element.clientId,
2191
- 'Client Name': element.clientName,
2239
+ 'Store ID': element.storeId,
2240
+ // 'Client Id': element.clientId,
2241
+ // 'Client Name': element.clientName,
2242
+ 'Product Type': element.moduleType,
2192
2243
  'User Name': element.userName,
2193
2244
  'User Email': element.userEmail,
2194
2245
  'Before Count': element.beforeCount,
2195
2246
  'After Count': element.afterCount,
2196
- 'Audit Type': element.auditType,
2197
2247
  'Accuracy': element.accuracy,
2198
2248
  'Time Spent': element.timeSpent,
2249
+ 'Audit Type': element.auditType,
2199
2250
  'Status': element.status,
2200
2251
  } );
2201
2252
  }
2202
2253
  } );
2203
- await download( exportdata, res );
2254
+ await download( exportData, res );
2204
2255
  return;
2205
2256
  }
2206
2257
  return res.sendSuccess( { result: result, count: count.length } );
@@ -2262,6 +2313,7 @@ export async function userMetrics( req, res ) {
2262
2313
  totalCompletedFiles: { $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, 1, 0 ] },
2263
2314
  beforeCount: 1,
2264
2315
  afterCount: { $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, '$afterCount', 0 ] },
2316
+ createdAt: 1,
2265
2317
  },
2266
2318
  },
2267
2319
  {
@@ -2274,6 +2326,7 @@ export async function userMetrics( req, res ) {
2274
2326
  afterCount: { $sum: '$afterCount' },
2275
2327
  checkIntime: { $first: '$startTime' },
2276
2328
  checkOutTime: { $last: '$endTime' },
2329
+ createdAt: { $first: '$createdAt' },
2277
2330
 
2278
2331
  },
2279
2332
  },
@@ -2299,6 +2352,7 @@ export async function userMetrics( req, res ) {
2299
2352
  differenceInSeconds: {
2300
2353
  $divide: [ { $subtract: [ '$checkOutTime', '$checkIntime' ] }, 1000 ],
2301
2354
  },
2355
+ createdAt: 1,
2302
2356
  },
2303
2357
  },
2304
2358
  {
@@ -2314,6 +2368,7 @@ export async function userMetrics( req, res ) {
2314
2368
  hours: { $floor: { $divide: [ '$differenceInSeconds', 3600 ] } },
2315
2369
  minutes: { $floor: { $divide: [ { $mod: [ '$differenceInSeconds', 3600 ] }, 60 ] } },
2316
2370
  seconds: { $mod: [ '$differenceInSeconds', 60 ] },
2371
+ createdAt: 1,
2317
2372
  },
2318
2373
  },
2319
2374
  {
@@ -2348,7 +2403,7 @@ export async function userMetrics( req, res ) {
2348
2403
  else: { $toString: { $round: [ '$seconds', 0 ] } },
2349
2404
  },
2350
2405
  },
2351
-
2406
+ createdAt: 1,
2352
2407
  },
2353
2408
  },
2354
2409
  {
@@ -2366,6 +2421,7 @@ export async function userMetrics( req, res ) {
2366
2421
  workingHours: {
2367
2422
  $concat: [ '$hours', ':', '$minutes', ':', '$seconds' ],
2368
2423
  },
2424
+ createdAt: 1,
2369
2425
  },
2370
2426
  },
2371
2427
  {
@@ -2419,6 +2475,7 @@ export async function userMetrics( req, res ) {
2419
2475
  },
2420
2476
  },
2421
2477
  workingHours: 1,
2478
+ createdAt: 1,
2422
2479
  },
2423
2480
  },
2424
2481
  ];
@@ -2456,15 +2513,17 @@ export async function userMetrics( req, res ) {
2456
2513
  {
2457
2514
  $project: {
2458
2515
  '_id': 0,
2459
- 'File Date': '$fileDate',
2460
2516
  'User Name': '$userName',
2461
- 'Total Completed Files Count': '$totalCompletedFiles',
2517
+ 'File Date': '$fileDate',
2518
+ 'Completed Files': '$totalCompletedFiles',
2519
+ 'Product Type': '$moduleType',
2462
2520
  'Total Before Count': '$beforeCount',
2463
2521
  'Total After Count': '$afterCount',
2464
- 'check Intime': '$checkIntime',
2465
- 'check OutTime': '$checkOutTime',
2466
- 'working Hours': '$workingHours',
2467
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' } },
2468
2527
  },
2469
2528
  },
2470
2529
  );
@@ -2619,10 +2678,10 @@ export async function pendingSummaryTable( req, res ) {
2619
2678
 
2620
2679
  const result = await aggregateUserAudit( query );
2621
2680
  if ( inputData.isExport ) {
2622
- const exportdata = [];
2681
+ const exportData = [];
2623
2682
  result.forEach( ( element ) => {
2624
2683
  if ( element.moduleType == 'zone' ) {
2625
- exportdata.push( {
2684
+ exportData.push( {
2626
2685
  'Store ID': element.storeId,
2627
2686
  'zoneName': element.zoneName,
2628
2687
  'File Date': element.fileDate,
@@ -2635,7 +2694,7 @@ export async function pendingSummaryTable( req, res ) {
2635
2694
  'Status': element.auditStatus,
2636
2695
  } );
2637
2696
  } else {
2638
- exportdata.push( {
2697
+ exportData.push( {
2639
2698
  'Store ID': element.storeId,
2640
2699
  'File Date': element.fileDate,
2641
2700
  'Product Type': element.moduleType,
@@ -2648,7 +2707,7 @@ export async function pendingSummaryTable( req, res ) {
2648
2707
  } );
2649
2708
  }
2650
2709
  } );
2651
- await download( exportdata, res );
2710
+ await download( exportData, res );
2652
2711
  return;
2653
2712
  }
2654
2713
  return res.sendSuccess( { result: result, count: count.length } );
@@ -2937,6 +2996,7 @@ export async function overViewCard( req, res ) {
2937
2996
  inprogressCount: 0,
2938
2997
  completedCount: 0,
2939
2998
  pendingCount: 0,
2999
+ notAssigned: 0,
2940
3000
  };
2941
3001
 
2942
3002
  const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
@@ -2967,7 +3027,7 @@ export async function overViewCard( req, res ) {
2967
3027
  },
2968
3028
  inprogressCount: {
2969
3029
  $cond: [
2970
- { $ne: [ '$status', 'completed' ] }, 1, 0,
3030
+ { $in: [ '$status', [ 'inprogress', 'assigned', 'drafted' ] ] }, 1, 0,
2971
3031
  ],
2972
3032
  },
2973
3033
  completedCount: {
@@ -3194,7 +3254,10 @@ export async function summarySplit( req, res ) {
3194
3254
  ];
3195
3255
  const result = await aggregateStoreAudit( query );
3196
3256
  const totalCount = await aggregateAuditStoreData( totalQuery );
3197
- result[0].totalCount = totalCount[0]?.total;
3257
+ if ( totalCount.length> 0 ) {
3258
+ result[0].totalCount = totalCount[0]?.total;
3259
+ }
3260
+
3198
3261
  return res.sendSuccess( { result: result.length > 0 ? result[0] : temp } );
3199
3262
  } catch ( error ) {
3200
3263
  const err = error.message || 'Internal Server Error';
@@ -3335,38 +3398,32 @@ export async function overviewTable( req, res ) {
3335
3398
 
3336
3399
  const result = await aggregateStoreAudit( query );
3337
3400
  if ( inputData.isExport ) {
3338
- const exportdata = [];
3401
+ const exportData = [];
3339
3402
  result.forEach( ( element ) => {
3340
3403
  if ( element.moduleType == 'zone' ) {
3341
- exportdata.push( {
3342
- 'Store ID': element.storeId,
3404
+ exportData.push( {
3405
+ 'File Date': element.fileDate,
3343
3406
  'Brand ID': element.clientId,
3407
+ 'Store ID': element.storeId,
3344
3408
  'Zone Name': element.zoneName,
3345
- 'File Date': element.fileDate,
3346
- 'Product Type': element.moduleType,
3347
- 'Audit Type': element.auditType,
3348
3409
  'Before Count': element.beforeCount,
3349
3410
  'After Count': element.afterCount,
3350
- // 'User Name': element.userName,
3351
- // 'User Email': element.userEmail,
3352
- 'Status': element.status,
3411
+ 'Audit Type': element.auditType,
3412
+ 'Status': toCamelCase( element.status ),
3353
3413
  } );
3354
3414
  } else {
3355
- exportdata.push( {
3356
- 'Store ID': element.storeId,
3357
- 'Brand ID': element.clientId,
3415
+ exportData.push( {
3358
3416
  'File Date': element.fileDate,
3359
- 'Product Type': element.moduleType,
3360
- 'Audit Type': element.auditType,
3417
+ 'Brand ID': element.clientId,
3418
+ 'Store ID': element.storeId,
3361
3419
  'Before Count': element.beforeCount,
3362
3420
  'After Count': element.afterCount,
3363
- // 'User Name': element.userName,
3364
- // 'User Email': element.userEmail,
3365
- 'Status': element.status,
3421
+ 'Audit Type': element.auditType,
3422
+ 'Status': toCamelCase( element.status ),
3366
3423
  } );
3367
3424
  }
3368
3425
  } );
3369
- await download( exportdata, res );
3426
+ await download( exportData, res );
3370
3427
  return;
3371
3428
  }
3372
3429
  return res.sendSuccess( { result: result, count: count.length } );
@@ -3377,6 +3434,18 @@ export async function overviewTable( req, res ) {
3377
3434
  }
3378
3435
  }
3379
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
+
3380
3449
  export async function totalNotAssignedCount( req, res ) {
3381
3450
  try {
3382
3451
  let notAssignedCount = {
@@ -3567,6 +3636,43 @@ export async function totalNotAssignedCount( req, res ) {
3567
3636
  },
3568
3637
  },
3569
3638
  ];
3639
+
3640
+ const mappingInprogressCount = [
3641
+ {
3642
+ $match: {
3643
+ fileDate: { $eq: latestTraxDate },
3644
+ },
3645
+ },
3646
+ {
3647
+ $project: {
3648
+ inprogressAuditCount: {
3649
+ $cond: [
3650
+ { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] }, 1, 0,
3651
+ ],
3652
+ },
3653
+ inprogressTraxCount: {
3654
+ $cond: [
3655
+ { $and: [ { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] } ] }, 1, 0,
3656
+ ],
3657
+ },
3658
+
3659
+ },
3660
+ },
3661
+ {
3662
+ $group: {
3663
+ _id: null,
3664
+ inprogressAuditCount: { $sum: '$inprogressAuditCount' },
3665
+ inprogressTraxCount: { $sum: '$inprogressTraxCount' },
3666
+ },
3667
+ },
3668
+ {
3669
+ $project: {
3670
+ _id: 0,
3671
+ inprogressAuditCount: { $ifNull: [ '$inprogressAuditCount', 0 ] },
3672
+ inprogressTraxCount: { $ifNull: [ '$inprogressTraxCount', 0 ] },
3673
+ },
3674
+ },
3675
+ ];
3570
3676
  // const binaryInprogressCount=[
3571
3677
  // {
3572
3678
  // $match: {
@@ -3651,6 +3757,8 @@ export async function totalNotAssignedCount( req, res ) {
3651
3757
  const getTotalTraxAudit = await aggregateTraxAuditData( traxQuery );
3652
3758
  const getInprogressCount = await aggregateStoreAudit( inprogressCount );
3653
3759
  const getBinaryInprogressCount = await aggregateBinaryAudit( binaryInprogressCount );
3760
+ const getMappingInprogressCount = await aggregateStoreEmpDetection( mappingInprogressCount );
3761
+ logger.info( { getMappingInprogressCount: getMappingInprogressCount } );
3654
3762
  // const getMappingInprogressCount = await aggregateStoreEmpDetection( mappingInprogressCount );
3655
3763
  if ( getTotalcount.length == 0 && getTotalTraxAudit.length == 0 ) {
3656
3764
  return res.sendError( 'No Data Found', 204 );
@@ -3671,6 +3779,12 @@ export async function totalNotAssignedCount( req, res ) {
3671
3779
  } else {
3672
3780
  notAssignedCount.totalTraxAuditCount = getTotalTraxAudit[0]?.totalTraxAuditCount;
3673
3781
  }
3782
+
3783
+ if ( getMappingInprogressCount.length > 0 ) {
3784
+ notAssignedCount.totalTraxAuditCount = getTotalTraxAudit[0].totalTraxAuditCount > getMappingInprogressCount[0].inprogressTraxCount ? getTotalTraxAudit[0].totalTraxAuditCount - getMappingInprogressCount[0].inprogressTraxCount : 0;
3785
+ } else {
3786
+ notAssignedCount.totalTraxAuditCount = getTotalTraxAudit[0]?.totalTraxAuditCount;
3787
+ }
3674
3788
  return res.sendSuccess( { result: notAssignedCount } );
3675
3789
  } catch ( error ) {
3676
3790
  const err = error.message || 'Internal Server Error';
@@ -3753,18 +3867,45 @@ export async function auditImages( req, res ) {
3753
3867
  export async function getUserAuditCount( req, res ) {
3754
3868
  try {
3755
3869
  const userWallet = JSON.parse( process.env.USER_WALLET );
3756
- const previousDate = dayjs( new Date() ).subtract( 1, 'days' );
3870
+ const previousDate = dayjs( new Date() ).subtract( 19, 'days' );
3757
3871
  const fileDate = dayjs( previousDate ).format( 'DD-MM-YYYY' );
3758
3872
  const configTimeSpentSec = userWallet.minTimeSpentSec; // 18000;
3759
- const beforeCountX = userWallet.beforeCountCreditPerc; // 0.0335;
3760
- const afterCountY = userWallet.afterCountCreditPerc; // 0.0035;
3873
+ // const beforeCountX = userWallet.beforeCountCreditPerc; // 0.0335;
3874
+ // const afterCountY = userWallet.afterCountCreditPerc; // 0.0035;
3761
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
+ }
3762
3902
  const userCountQuery= [
3763
3903
  {
3764
3904
  $match: {
3765
3905
  $and: [
3766
3906
  { fileDate: { $eq: fileDate } },
3767
3907
  { auditStatus: { $eq: 'completed' } },
3908
+ { userId: { $in: auditUserList[0].userList } },
3768
3909
  ],
3769
3910
  },
3770
3911
  },
@@ -3839,6 +3980,11 @@ export async function getUserAuditCount( req, res ) {
3839
3980
  }
3840
3981
  const zoneMinimumTarget =userCount[0]?.zoneUserCount?.length !== 0 && userCount[0]?.zoneFilesCount !==0 ? Math.floor( userCount[0].zoneFilesCount/userCount[0].zoneUserCount.length ) : 0;
3841
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
+
3842
3988
  const query = [
3843
3989
  {
3844
3990
  $match: {
@@ -3847,11 +3993,13 @@ export async function getUserAuditCount( req, res ) {
3847
3993
  {
3848
3994
  auditStatus: { $eq: 'completed' },
3849
3995
  },
3996
+ { userId: { $in: auditUserList[0].userList } },
3850
3997
  ],
3851
3998
  },
3852
3999
  },
3853
4000
  {
3854
- $set: { minimumTarget: { $cond: [ { $eq: [ '$moduleType', 'traffic' ] }, trafficMinimumTarget, zoneMinimumTarget ] } },
4001
+ $set: { minimumTarget: { $cond: [ { $eq: [ '$moduleType', 'traffic' ] }, trafficMinimumTarget, zoneMinimumTarget ] },
4002
+ afterCountCredit: afterCountCredit },
3855
4003
  },
3856
4004
  {
3857
4005
  $project: {
@@ -3863,9 +4011,13 @@ export async function getUserAuditCount( req, res ) {
3863
4011
  fileDate: 1,
3864
4012
  fileDateISO: 1,
3865
4013
  minimumTarget: 1,
4014
+ afterCountCredit: 1,
3866
4015
  totalEarn: { $ifNull: [ 0, 0 ] },
4016
+ singleImageAmount: { $ifNull: [ 0, 0 ] },
3867
4017
  totalReducedAmount: { $ifNull: [ 0, 0 ] },
3868
4018
  totalCredit: { $ifNull: [ 0, 0 ] },
4019
+ deductionBytimeSpent: { $ifNull: [ 0, 0 ] },
4020
+ deductionByMinimumTarget: { $ifNull: [ 0, 0 ] },
3869
4021
  },
3870
4022
  },
3871
4023
  {
@@ -3880,9 +4032,13 @@ export async function getUserAuditCount( req, res ) {
3880
4032
  fileDateISO: { $first: '$fileDateISO' },
3881
4033
  userId: { $first: '$userId' },
3882
4034
  minimumTarget: { $first: '$minimumTarget' },
4035
+ afterCountCredit: { $first: '$afterCountCredit' },
3883
4036
  totalEarn: { $first: '$totalEarn' },
3884
4037
  totalReducedAmount: { $first: '$totalReducedAmount' },
3885
4038
  totalCredit: { $first: '$totalCredit' },
4039
+ deductionByMinimumTarget: { $first: '$deductionByMinimumTarget' },
4040
+ deductionBytimeSpent: { $first: '$deductionBytimeSpent' },
4041
+ singleImageAmount: { $first: '$singleImageAmount' },
3886
4042
  },
3887
4043
  },
3888
4044
  {
@@ -3896,7 +4052,33 @@ export async function getUserAuditCount( req, res ) {
3896
4052
  fileDate: 1,
3897
4053
  fileDateISO: 1,
3898
4054
  minimumTarget: 1,
3899
- 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
+ ] },
3900
4082
  totalReducedAmount: 1,
3901
4083
  totalCredit: 1,
3902
4084
 
@@ -3910,16 +4092,36 @@ export async function getUserAuditCount( req, res ) {
3910
4092
  totalBeforeCount: 1,
3911
4093
  totalAfterCount: 1,
3912
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
+ },
3913
4103
  fileDate: 1,
3914
4104
  fileDateISO: 1,
3915
4105
  minimumTarget: 1,
4106
+ singleImageAmount: { $cond: [ { $eq: [ '$moduleType', 'traffic' ] }, beforeCountTraffic, beforeCountZone ] },
4107
+ beforeCountCredit: 1,
4108
+ afterCountCredit: 1,
3916
4109
  totalEarn: 1,
3917
- totalReducedAmount: { $cond: [
4110
+ deductionBytimeSpent: { $cond: [
3918
4111
  { $lt: [ '$totalTimeSpent', configTimeSpentSec ] },
3919
- { $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',
3920
4119
  ] },
4120
+ mappingperc: 1,
4121
+ deductionByMapping: 1,
3921
4122
  totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
3922
4123
 
4124
+
3923
4125
  },
3924
4126
  },
3925
4127
 
@@ -3930,18 +4132,39 @@ export async function getUserAuditCount( req, res ) {
3930
4132
  moduleType: 1,
3931
4133
  totalBeforeCount: 1,
3932
4134
  totalAfterCount: 1,
3933
- 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
+ },
3934
4154
  fileDate: 1,
3935
4155
  fileDateISO: 1,
3936
4156
  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
-
4157
+ beforeCountCredit: 1,
4158
+ afterCountCredit: 1,
4159
+ totalEarn: 1,
4160
+ deductionByMinimumTarget: 1,
4161
+ singleImageAmount: 1,
4162
+ deductionBytimeSpent: 1,
4163
+ mappingperc: 1,
4164
+ deductionByMapping: 1,
3944
4165
  totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
4166
+ totalReducedAmount: { $round: [ { $add: [ '$deductionBytimeSpent', '$deductionByMinimumTarget', '$deductionByMapping' ] }, 2 ] },
4167
+
3945
4168
  },
3946
4169
  },
3947
4170
  {
@@ -3973,15 +4196,23 @@ export async function getUserAuditCount( req, res ) {
3973
4196
  moduleType: 1,
3974
4197
  totalAfterCount: { $round: [ '$totalAfterCount', 2 ] },
3975
4198
  totalBeforeCount: { $round: [ '$totalBeforeCount', 2 ] },
3976
- timeSpent: { $round: [ '$totalTimeSpent', 2 ] },
4199
+ timeSpent: '$totalTimeSpent',
3977
4200
  fileDate: 1,
3978
4201
  fileDateISO: 1,
3979
- totalReducedAmount: { $round: [ '$totalReducedAmount', 2 ] },
3980
- totalCredit: { $round: [ { $subtract: [ '$totalEarn', '$totalReducedAmount' ] }, 2 ] },
4202
+ beforeCountCredit: 1,
4203
+ afterCountCredit: 1,
3981
4204
  totalEarn: { $round: [ '$totalEarn', 2 ] },
3982
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 ] },
3983
4213
  userEmail: '$users.email', // Access first element from array
3984
- userName: '$users.userName', // Access first element from array
4214
+ userName: '$users.userName', // Access first element from array,
4215
+
3985
4216
  },
3986
4217
  },
3987
4218
  ];
@@ -4304,30 +4535,82 @@ export async function getUserAuditCountMTD( req, res ) {
4304
4535
 
4305
4536
  export async function auditViewLogs( req, res ) {
4306
4537
  try {
4538
+ const inputData = req.query;
4307
4539
  const parsedOpenSearch = JSON.parse( process.env.OPENSEARCH );
4308
- let downTimeQuery = {
4540
+ const logData =[];
4541
+ let logsQuery = {
4309
4542
  'size': 100,
4310
4543
  'query': {
4311
4544
  'bool': {
4312
4545
  'must': [
4313
4546
  {
4314
4547
  'term': {
4315
- 'doc.date.keyword': dayjs( inputQuery.filedate ).format( 'DD-MM-YYYY' ),
4548
+ 'logData.fileDate.keyword': inputData.fileDate,
4549
+ },
4550
+ },
4551
+ {
4552
+ 'term': {
4553
+ 'logData.storeId.keyword': inputData.storeId,
4316
4554
  },
4317
4555
  },
4318
4556
  {
4319
4557
  'term': {
4320
- 'doc.store_id.keyword': inputQuery.storeId,
4558
+ 'logData.moduleType.keyword': inputData.moduleType,
4559
+ },
4560
+ },
4561
+ {
4562
+ 'terms': {
4563
+ 'logSubType.keyword': [ 'auditDone', 'reAuditPush', 'reTrigger' ],
4321
4564
  },
4322
4565
  },
4323
4566
  ],
4324
-
4325
4567
  },
4326
4568
  },
4327
4569
  };
4328
- const downtime = await getOpenSearchData( parsedOpenSearch.activityLog, downTimeQuery );
4329
- return res.sendSuccess( downtime );
4330
- } catch ( err ) {
4570
+ const logs = await getOpenSearchData( parsedOpenSearch.auditLog, logsQuery );
4571
+ if ( logs.body.hits.hits.length > 0 ) {
4572
+ if ( logs.body.hits.hits[0]._source.logSubType == 'auditDone' ) {
4573
+ const convertedTime = await convertTimestampToDateTime( logs.body.hits.hits[0]._source.logData.endTime );
4574
+ logData.push( {
4575
+ ...logs.body.hits.hits[0]._source, // Spread the _source object
4576
+ time: convertedTime, // Add the converted time
4577
+ } );
4578
+ } else {
4579
+ const convertedTime = await convertTimestampToDateTime( logs.body.hits.hits[0]._source.createdAt );
4580
+ logData.push( {
4581
+ ...logs.body.hits.hits[0]._source, // Spread the _source object
4582
+ time: convertedTime, // Add the converted time
4583
+ } );
4584
+ }
4585
+ return res.sendSuccess( logData );
4586
+ } else {
4587
+ return res.sendError( 'No Record Inserted', 204 );
4588
+ }
4589
+ } catch ( error ) {
4590
+ const err = error.message || 'Internal Server Error';
4591
+ logger.info( { error: error, message: req.body, function: 'auditViewLogs' } );
4592
+ return res.sendError( err, 500 );
4593
+ }
4594
+ }
4331
4595
 
4596
+ export async function convertTimestampToDateTime( timestamp ) {
4597
+ // Check if the timestamp is too large (nanoseconds), then convert it to milliseconds
4598
+ if ( timestamp > 9999999999999 ) {
4599
+ timestamp = Math.floor( timestamp / 1000 ); // Convert to milliseconds if needed
4332
4600
  }
4601
+
4602
+ // Create a new Date object
4603
+ const date = new Date( timestamp );
4604
+
4605
+ // Format the date and time components
4606
+ const year = date.getFullYear();
4607
+ const month = ( '0' + ( date.getMonth() + 1 ) ).slice( -2 ); // Months are zero-based
4608
+ const day = ( '0' + date.getDate() ).slice( -2 );
4609
+
4610
+ const hours = ( '0' + date.getHours() ).slice( -2 );
4611
+ const minutes = ( '0' + date.getMinutes() ).slice( -2 );
4612
+ const seconds = ( '0' + date.getSeconds() ).slice( -2 );
4613
+
4614
+ // Return formatted date and time string
4615
+ return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
4333
4616
  }