tango-app-api-audit 3.4.0-alpha.9 → 3.4.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-audit",
3
- "version": "3.4.0-alpha.9",
3
+ "version": "3.4.0",
4
4
  "description": "audit & audit metrics apis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -38,3 +38,4 @@
38
38
  "eslint-plugin-promise": "^6.2.0"
39
39
  }
40
40
  }
41
+
@@ -58,6 +58,9 @@ import {
58
58
  aggregateAuditStoreData,
59
59
  findOneStoreData,
60
60
  } from '../service/auditStoreData.service.js';
61
+ import { aggregateStoreEmpDetection } from '../service/storeEmpDetection.service.js';
62
+ import { aggregateBinaryAudit } from '../service/binaryAudit.service.js';
63
+ import { aggregateTraxAuditData, findOneTraxAuditData } from '../service/traxAuditData.service.js';
61
64
 
62
65
  /* < -- *** Configuration *** --> */
63
66
 
@@ -195,7 +198,8 @@ export async function getAuditFile( req, res ) {
195
198
  const bucket = JSON.parse( process.env.BUCKET );
196
199
  const openSearch = JSON.parse( process.env.OPENSEARCH );
197
200
  const inputData = req.query;
198
- const data = await getDate( new Date(), new Date() );
201
+ const previousDate = new Date( new Date() - 1 );
202
+ const data = await getDate( previousDate, new Date() );
199
203
  logger.info( { data: data, function: 'data', newDate: new Date() } );
200
204
  logger.info( { userId: req.user._id, queueName: inputData.queueName, moduleType: inputData.moduleType } );
201
205
  const start = data.start;
@@ -750,10 +754,9 @@ export async function workSpace( req, res ) {
750
754
  const filter =[
751
755
  { status: { $in: [ 'active', 'hold' ] } },
752
756
  { 'auditConfigs.audit': { $eq: true } },
757
+ { clientId: { $in: inputData.clientId } },
753
758
  ];
754
- if ( inputData?.clientId?.length > 0 ) {
755
- filter.push( { clientId: { $in: inputData.clientId } } );
756
- }
759
+
757
760
  const temp = [];
758
761
  const clientQuery = [
759
762
  {
@@ -805,7 +808,7 @@ export async function workSpace( req, res ) {
805
808
 
806
809
  const clientDetails = await aggregateClient( clientQuery );
807
810
  const clientList = clientDetails.map( ( i ) => i.clientId );
808
- const fileDate = dayjs( dateRange.start ).subtract( 1, 'days' );
811
+ const fileDate = dayjs( dateRange.start ).subtract( 1, 'days' ); // dayjs( dateRange.start );
809
812
  const getStoreData = await findOneStoreData(
810
813
  {},
811
814
  { fileDate: 1 },
@@ -1453,6 +1456,7 @@ export async function userAuditHistory( req, res ) {
1453
1456
  {
1454
1457
  moduleType: { $eq: inputData.moduleType },
1455
1458
  },
1459
+ { clientId: { $in: inputData.clientId } },
1456
1460
  ];
1457
1461
 
1458
1462
  if ( inputData.dateType == 'fileDate' ) {
@@ -1484,9 +1488,6 @@ export async function userAuditHistory( req, res ) {
1484
1488
  auditStatus: { $in: inputData.filterByStatus },
1485
1489
  } );
1486
1490
  }
1487
- if ( inputData?.clientId?.length > 0 ) {
1488
- filter.push( { clientId: { $in: inputData.clientId } } );
1489
- }
1490
1491
  const query = [
1491
1492
  {
1492
1493
  $match: {
@@ -1923,11 +1924,9 @@ export async function storeMetrics( req, res ) {
1923
1924
  { fileDateISO: { $gte: dateRange.start } },
1924
1925
  { fileDateISO: { $lte: dateRange.end } },
1925
1926
  { moduleType: { $eq: inputData.moduleType } },
1927
+ { clientId: { $in: inputData.filterByClientId } },
1926
1928
 
1927
1929
  ];
1928
- if ( inputData.filterByClientId && inputData.filterByClientId?.length > 0 ) {
1929
- filter.push( { clientId: { $in: inputData.filterByClientId } } );
1930
- }
1931
1930
 
1932
1931
  if ( inputData.filterByStoreId && inputData.filterByStoreId?.length > 0 ) {
1933
1932
  filter.push( { storeId: { $in: inputData.filterByStoreId } } );
@@ -2479,13 +2478,10 @@ export async function pendingSummaryTable( req, res ) {
2479
2478
  { fileDateISO: { $lte: dateRange.end } },
2480
2479
  { auditStatus: { $ne: 'completed' } },
2481
2480
  { moduleType: { $eq: inputData.moduleType } },
2481
+ { clientId: { $in: inputData.clientId } },
2482
2482
 
2483
2483
  ];
2484
2484
 
2485
- if ( inputData.clientId.length > 0 ) {
2486
- filters.push( { clientId: { $in: inputData.clientId } } );
2487
- }
2488
-
2489
2485
  if ( inputData.filterByAuditType.length > 0 ) {
2490
2486
  filters.push( { auditType: { $in: inputData.filterByAuditType } } );
2491
2487
  }
@@ -2666,12 +2662,10 @@ export async function overAllAuditSummary( req, res ) {
2666
2662
  let filters =[
2667
2663
  { fileDateISO: { $gte: dateRange.start } },
2668
2664
  { fileDateISO: { $lte: dateRange.end } },
2669
- { moduleType: { $eq: inputData.moduleType },
2670
- },
2665
+ { moduleType: { $eq: inputData.moduleType } },
2666
+ { clientId: { $in: inputData.clientId } },
2671
2667
  ];
2672
- if ( inputData.clientId.length > 0 ) {
2673
- filters.push( { clientId: { $in: inputData.clientId } } );
2674
- }
2668
+
2675
2669
  const storeQuery = inputData.clientId.length? { 'edge.firstFile': true, 'clientId': { $in: inputData. clientId } } : { 'edge.firstFile': true };
2676
2670
  temp.installedStores = await countDocumentsStore( storeQuery );
2677
2671
  const auditClientDataQuery =[
@@ -2931,10 +2925,9 @@ export async function overViewCard( req, res ) {
2931
2925
  let filters =[
2932
2926
  { fileDateISO: { $gte: dateRange.start } },
2933
2927
  { fileDateISO: { $lte: dateRange.end } },
2928
+ { clientId: { $in: inputData.clientId } },
2934
2929
  ];
2935
- if ( inputData.clientId.length > 0 ) {
2936
- filters.push( { clientId: { $in: inputData.clientId } } );
2937
- }
2930
+
2938
2931
  const query =[
2939
2932
  {
2940
2933
  $match: {
@@ -2989,15 +2982,87 @@ export async function overViewCard( req, res ) {
2989
2982
  },
2990
2983
  },
2991
2984
  ];
2985
+
2986
+ const binaryQuery =[
2987
+ {
2988
+ $match: {
2989
+ $and: filters,
2990
+ },
2991
+
2992
+ },
2993
+ {
2994
+ $project: {
2995
+ auditFileCount: {
2996
+ $cond: [
2997
+ { $eq: [ '$auditType', 'Audit' ] }, 1, 0,
2998
+ ],
2999
+ },
3000
+ ReAuditFileCount: {
3001
+ $cond: [
3002
+ { $eq: [ '$auditType', 'ReAudit' ] }, 1, 0,
3003
+ ],
3004
+ },
3005
+ inprogressCount: {
3006
+ $cond: [
3007
+ { $ne: [ '$auditStatus', 'completed' ] }, 1, 0,
3008
+ ],
3009
+ },
3010
+ completedCount: {
3011
+ $cond: [
3012
+ { $eq: [ '$auditStatus', 'completed' ] }, 1, 0,
3013
+ ],
3014
+ },
3015
+ },
3016
+ },
3017
+ {
3018
+ $group: {
3019
+ _id: { fileDate: '$fileDate', clientId: '$clientId' },
3020
+ totalCount: { $sum: 1 },
3021
+ auditFileCount: { $sum: '$auditFileCount' },
3022
+ ReAuditFileCount: { $sum: '$ReAuditFileCount' },
3023
+ inprogressCount: { $sum: '$inprogressCount' },
3024
+ completedCount: { $sum: '$completedCount' },
3025
+
3026
+ },
3027
+ },
3028
+ {
3029
+ $project: {
3030
+ _id: 0,
3031
+ totalCount: 1,
3032
+ auditFileCount: 1,
3033
+ ReAuditFileCount: 1,
3034
+ inprogressCount: 1,
3035
+ completedCount: 1,
3036
+ notAssigned: { $subtract: [ '$totalCount', { $add: [ '$inprogressCount', '$completedCount' ] } ] },
3037
+ },
3038
+ },
3039
+ ];
2992
3040
  const result = await aggregateStoreAudit( query );
2993
- logger.info( { result: result } );
2994
- return res.sendSuccess( { result: result.length> 0? result[0]: temp } );
3041
+ const empDetection = await aggregateStoreEmpDetection( query );
3042
+ const binaryAudit = await aggregateBinaryAudit( binaryQuery );
3043
+
3044
+ // Combine all arrays into one
3045
+ const combinedArray = [ ...result, ...empDetection, ...binaryAudit ];
3046
+
3047
+ // Function to merge objects by summing their values
3048
+ const mergedResult = mergeAndSum( combinedArray );
3049
+
3050
+ return res.sendSuccess( { result: Object.keys( mergedResult ).length > 0?mergedResult: temp } );
2995
3051
  } catch ( error ) {
2996
3052
  const err = error.message || 'Internal Server Error';
2997
3053
  logger.error( { error: error, message: req.body, function: 'overViewCard' } );
2998
3054
  return res.sendError( err, 500 );
2999
3055
  }
3000
3056
  }
3057
+ export const mergeAndSum = ( arr ) => {
3058
+ return arr.reduce( ( acc, obj ) => {
3059
+ for ( const [ key, value ] of Object.entries( obj ) ) {
3060
+ // Add the value to the existing key or initialize it if it doesn't exist
3061
+ acc[key] = ( acc[key] || 0 ) + value;
3062
+ }
3063
+ return acc;
3064
+ }, {} );
3065
+ };
3001
3066
 
3002
3067
  export async function summarySplit( req, res ) {
3003
3068
  try {
@@ -3017,10 +3082,9 @@ export async function summarySplit( req, res ) {
3017
3082
  { fileDateISO: { $gte: dateRange.start } },
3018
3083
  { fileDateISO: { $lte: dateRange.end } },
3019
3084
  { moduleType: { $eq: inputData.moduleType } },
3085
+ { clientId: { $in: inputData.clientId } },
3020
3086
  ];
3021
- if ( inputData.clientId.length > 0 ) {
3022
- filters.push( { clientId: { $in: inputData.clientId } } );
3023
- }
3087
+
3024
3088
  const query =[
3025
3089
  {
3026
3090
  $match: {
@@ -3131,13 +3195,10 @@ export async function overviewTable( req, res ) {
3131
3195
  { fileDateISO: { $gte: dateRange.start } },
3132
3196
  { fileDateISO: { $lte: dateRange.end } },
3133
3197
  { moduleType: { $eq: inputData.moduleType } },
3198
+ { clientId: { $in: inputData.clientId } },
3134
3199
 
3135
3200
  ];
3136
3201
 
3137
- if ( inputData.clientId.length > 0 ) {
3138
- filters.push( { clientId: { $in: inputData.clientId } } );
3139
- }
3140
-
3141
3202
  if ( inputData.filterByAuditType.length > 0 ) {
3142
3203
  filters.push( { auditType: { $in: inputData.filterByAuditType } } );
3143
3204
  }
@@ -3312,6 +3373,15 @@ export async function totalNotAssignedCount( req, res ) {
3312
3373
  { createdAt: -1 },
3313
3374
  );
3314
3375
  const latestDate = getStoreData?.fileDate || dayjs( fileDate ).format( 'DD-MM-YYYY' );
3376
+
3377
+ const geTraxData = await findOneTraxAuditData(
3378
+ {},
3379
+ { fileDate: 1 },
3380
+ { createdAt: -1 },
3381
+ );
3382
+ const latestTraxDate = geTraxData?.fileDate || dayjs( fileDate ).format( 'DD-MM-YYYY' );
3383
+ logger.info( { geTraxData: geTraxData } );
3384
+
3315
3385
  const query = [
3316
3386
  {
3317
3387
  $match: {
@@ -3347,6 +3417,52 @@ export async function totalNotAssignedCount( req, res ) {
3347
3417
  },
3348
3418
  },
3349
3419
  ];
3420
+ const traxQuery = [
3421
+ {
3422
+ $match: {
3423
+ fileDate: { $eq: latestTraxDate },
3424
+ },
3425
+ },
3426
+ {
3427
+ $project: {
3428
+ totalUnattendedCustomerCount: { $cond: [
3429
+ { $eq: [ '$moduleType', 'unattended-customer' ] }, 1, 0,
3430
+ ] },
3431
+ totalLeftInMiddleCount: { $cond: [
3432
+ { $eq: [ '$moduleType', 'left-in-middle' ] }, 1, 0,
3433
+ ] },
3434
+ totalUniformDetectionCount: { $cond: [
3435
+ { $eq: [ '$moduleType', 'uniform-detection' ] }, 1, 0,
3436
+ ] },
3437
+ totalMobileDetectionCount: { $cond: [
3438
+ { $eq: [ '$moduleType', 'mobile-detection' ] }, 1, 0,
3439
+ ] },
3440
+
3441
+ },
3442
+ },
3443
+ {
3444
+ $group: {
3445
+ _id: null,
3446
+ totalTraxAuditCount: { $sum: 1 },
3447
+ totalUnattendedCustomerCount: { $sum: '$totalUnattendedCustomerCount' },
3448
+ totalLeftInMiddleCount: { $sum: '$totalLeftInMiddleCount' },
3449
+ totalUniformDetectionCount: { $sum: '$totalUniformDetectionCount' },
3450
+ totalMobileDetectionCount: { $sum: '$totalMobileDetectionCount' },
3451
+
3452
+
3453
+ },
3454
+ },
3455
+ {
3456
+ $project: {
3457
+ _id: 0,
3458
+ totalTraxAuditCount: 1,
3459
+ totalUnattendedCustomerCount: 1,
3460
+ totalLeftInMiddleCount: 1,
3461
+ totalUniformDetectionCount: 1,
3462
+ totalMobileDetectionCount: 1,
3463
+ },
3464
+ },
3465
+ ];
3350
3466
  const inprogressCount=[
3351
3467
  {
3352
3468
  $match: {
@@ -3387,22 +3503,140 @@ export async function totalNotAssignedCount( req, res ) {
3387
3503
  },
3388
3504
  },
3389
3505
  ];
3390
- const getTotalcount = await aggregateAuditStoreData( query );
3506
+ const binaryInprogressCount = [
3507
+ {
3508
+ $match: {
3509
+ fileDate: { $eq: latestTraxDate },
3510
+ },
3511
+ },
3512
+ {
3513
+ $project: {
3514
+ inprogressTraxCount: {
3515
+ $cond: [
3516
+ { $in: [ '$auditStatus', [ 'completed' ] ] }, 1, 0,
3517
+ ],
3518
+ },
3519
+ },
3520
+ },
3521
+ {
3522
+ $group: {
3523
+ _id: null,
3524
+ inprogressTraxCount: { $sum: '$inprogressTraxCount' },
3525
+
3526
+ },
3527
+ },
3528
+ {
3529
+ $project: {
3530
+ _id: 0,
3531
+ inprogressTraxCount: { $ifNull: [ '$inprogressTraxCount', 0 ] },
3391
3532
 
3533
+ },
3534
+ },
3535
+ ];
3536
+ // const binaryInprogressCount=[
3537
+ // {
3538
+ // $match: {
3539
+ // fileDate: { $eq: latestDate },
3540
+ // },
3541
+ // },
3542
+ // {
3543
+ // $project: {
3544
+ // inprogressAuditCount: {
3545
+ // $cond: [
3546
+ // { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] }, 1, 0,
3547
+ // ],
3548
+ // },
3549
+ // inprogressZoneCount: { $cond: [
3550
+ // { $and: [ { $eq: [ '$moduleType', 'zone' ] }, { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] } ] }, 1, 0,
3551
+ // ] },
3552
+ // inprogressTrafficCount: { $cond: [
3553
+ // { $and: [ { $eq: [ '$moduleType', 'traffic' ] }, { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] } ] }, 1, 0,
3554
+ // ] },
3555
+
3556
+ // },
3557
+ // },
3558
+ // {
3559
+ // $group: {
3560
+ // _id: null,
3561
+ // inprogressAuditCount: { $sum: '$inprogressAuditCount' },
3562
+ // inprogressZoneCount: { $sum: '$inprogressZoneCount' },
3563
+ // inprogressTrafficCount: { $sum: '$inprogressTrafficCount' },
3564
+
3565
+ // },
3566
+ // },
3567
+ // {
3568
+ // $project: {
3569
+ // _id: 0,
3570
+ // inprogressAuditCount: { $ifNull: [ '$inprogressAuditCount', 0 ] },
3571
+ // inprogressZoneCount: { $ifNull: [ '$inprogressZoneCount', 0 ] },
3572
+ // inprogressTrafficCount: { $ifNull: [ '$inprogressTrafficCount', 0 ] },
3573
+ // },
3574
+ // },
3575
+ // ];
3576
+ // const mappingInprogressCount=[
3577
+ // {
3578
+ // $match: {
3579
+ // fileDate: { $eq: latestDate },
3580
+ // },
3581
+ // },
3582
+ // {
3583
+ // $project: {
3584
+ // inprogressAuditCount: {
3585
+ // $cond: [
3586
+ // { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] }, 1, 0,
3587
+ // ],
3588
+ // },
3589
+ // inprogressZoneCount: { $cond: [
3590
+ // { $and: [ { $eq: [ '$moduleType', 'zone' ] }, { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] } ] }, 1, 0,
3591
+ // ] },
3592
+ // inprogressTrafficCount: { $cond: [
3593
+ // { $and: [ { $eq: [ '$moduleType', 'traffic' ] }, { $in: [ '$status', [ 'completed', 'not_assign', 'skipped' ] ] } ] }, 1, 0,
3594
+ // ] },
3595
+
3596
+ // },
3597
+ // },
3598
+ // {
3599
+ // $group: {
3600
+ // _id: null,
3601
+ // inprogressAuditCount: { $sum: '$inprogressAuditCount' },
3602
+ // inprogressZoneCount: { $sum: '$inprogressZoneCount' },
3603
+ // inprogressTrafficCount: { $sum: '$inprogressTrafficCount' },
3604
+
3605
+ // },
3606
+ // },
3607
+ // {
3608
+ // $project: {
3609
+ // _id: 0,
3610
+ // inprogressAuditCount: { $ifNull: [ '$inprogressAuditCount', 0 ] },
3611
+ // inprogressZoneCount: { $ifNull: [ '$inprogressZoneCount', 0 ] },
3612
+ // inprogressTrafficCount: { $ifNull: [ '$inprogressTrafficCount', 0 ] },
3613
+ // },
3614
+ // },
3615
+ // ];
3616
+ const getTotalcount = await aggregateAuditStoreData( query );
3617
+ const getTotalTraxAudit = await aggregateTraxAuditData( traxQuery );
3392
3618
  const getInprogressCount = await aggregateStoreAudit( inprogressCount );
3393
- if ( getTotalcount.length == 0 ) {
3619
+ const getBinaryInprogressCount = await aggregateBinaryAudit( binaryInprogressCount );
3620
+ // const getMappingInprogressCount = await aggregateStoreEmpDetection( mappingInprogressCount );
3621
+ if ( getTotalcount.length == 0 && getTotalTraxAudit.length == 0 ) {
3394
3622
  return res.sendError( 'No Data Found', 204 );
3395
3623
  }
3396
-
3624
+ logger.info( { getTotalTraxAudit: getTotalTraxAudit } );
3397
3625
  if ( getInprogressCount.length >0 ) {
3398
- notAssignedCount.totalAuditCount = getTotalcount[0].totalAuditCount - getInprogressCount[0]?.inprogressAuditCount;
3399
- notAssignedCount.totalZoneCount = getTotalcount[0].totalZoneCount - getInprogressCount[0]?.inprogressZoneCount;
3400
- notAssignedCount.totalTrafficCount = getTotalcount[0].totalTrafficCount - getInprogressCount[0]?.inprogressTrafficCount;
3626
+ notAssignedCount.totalAuditCount =getTotalcount[0].totalAuditCount > getInprogressCount[0]?.inprogressAuditCount? getTotalcount[0].totalAuditCount - getInprogressCount[0]?.inprogressAuditCount : 0;
3627
+ notAssignedCount.totalZoneCount = getTotalcount[0].totalZoneCount > getInprogressCount[0]?.inprogressZoneCount? getTotalcount[0].totalZoneCount - getInprogressCount[0]?.inprogressZoneCount: 0;
3628
+ notAssignedCount.totalTrafficCount = getTotalcount[0].totalTrafficCount > getInprogressCount[0]?.inprogressTrafficCount? getTotalcount[0].totalTrafficCount - getInprogressCount[0]?.inprogressTrafficCount: 0;
3401
3629
  } else {
3402
3630
  notAssignedCount.totalAuditCount = getTotalcount[0].totalAuditCount;
3403
3631
  notAssignedCount.totalZoneCount = getTotalcount[0].totalZoneCount;
3404
3632
  notAssignedCount.totalTrafficCount= getTotalcount[0].totalTrafficCount;
3405
3633
  }
3634
+
3635
+ if ( getBinaryInprogressCount.length > 0 ) {
3636
+ notAssignedCount.totalTraxAuditCount = getTotalTraxAudit[0].totalTraxAuditCount > getBinaryInprogressCount[0].inprogressTraxCount? getTotalTraxAudit[0].totalTraxAuditCount - getBinaryInprogressCount[0].inprogressTraxCount: 0;
3637
+ } else {
3638
+ notAssignedCount.totalTraxAuditCount = getTotalTraxAudit[0]?.totalTraxAuditCount;
3639
+ }
3406
3640
  return res.sendSuccess( { result: notAssignedCount } );
3407
3641
  } catch ( error ) {
3408
3642
  const err = error.message || 'Internal Server Error';