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.
@@ -2,13 +2,13 @@ import { checkFileExist, chunkArray, download, getDate, getDateWithCustomizeTime
2
2
  import { aggregateBinaryAudit, createBinaryAudit, updateOneBinaryAuditModel } from '../service/binaryAudit.service.js';
3
3
  import { countDocumentsStore, findOneStore } from '../service/store.service.js';
4
4
  import { findOneUser } from '../service/user.service.js';
5
- import { aggregateUserEmpDetection, createUserEmpDetection, updateOneUserEmpDetection } from '../service/userEmpDetection.service.js';
5
+ import { aggregateUserEmpDetection, countDocumentsUserEmpDetection, createUserEmpDetection, updateOneUserEmpDetection } from '../service/userEmpDetection.service.js';
6
6
  import { aggregateTraxAuditData, findOneTraxAuditData } from '../service/traxAuditData.service.js';
7
- import { aggregateClient } from '../service/client.service.js';
7
+ import { aggregateClient, findOneClient } from '../service/client.service.js';
8
8
  import dayjs from 'dayjs';
9
9
  import { aggregateAssignAudit } from '../service/assignAudit.service.js';
10
10
  import { aggregateStoreEmpDetection, updateManyStoreEmpDetection, updateOneStoreEmpDetection } from '../service/storeEmpDetection.service.js';
11
- import { listQueue } from 'tango-app-api-middleware/src/utils/aws/sqs.js';
11
+ import { listQueue, sendMessageToQueue } from 'tango-app-api-middleware/src/utils/aws/sqs.js';
12
12
  import _ from 'lodash';
13
13
  import mongoose from 'mongoose';
14
14
  import { findOneAuditLog, createAuditLog,
@@ -16,7 +16,7 @@ import { findOneAuditLog, createAuditLog,
16
16
  import {
17
17
  findOneUserEmpDetection,
18
18
  } from '../service/userEmpDetection.service.js';
19
-
19
+ import { updateOneEmpDetectionOutput } from '../service/empDetectionOutput.service.js';
20
20
  // export async function getDetectionAuditFile( req, res ) {
21
21
  // try {
22
22
  // const bucket = JSON.parse( process.env.BUCKET );
@@ -337,15 +337,16 @@ import {
337
337
  // const data = await signedUrl( fetchData );
338
338
  // const mapimg = {
339
339
  // img_path: data,
340
- // img_name: image[0],
341
- // img_id: indexes[1],
340
+ // img_name: inputData.moduleType=='mobile-detection'?`${indexes[0]}-${indexes[3]}` :image[0],
341
+ // img_id: img[3],
342
342
  // };
343
343
  // files.push( {
344
344
  // img_path: data,
345
- // img_name: image[0],
346
- // img_id: indexes[1],
345
+ // img_name: inputData.moduleType=='mobile-detection'?`${indexes[0]}-${indexes[3]}` :image[0],
346
+ // img_id: img[3],
347
347
  // selected: false,
348
348
  // dropped: false,
349
+ // singleDetection: false,
349
350
  // count: 1,
350
351
  // mappedid: [ mapimg ],
351
352
  // } );
@@ -1402,6 +1403,7 @@ export async function getUpdatedFile( req, res ) {
1402
1403
 
1403
1404
  export async function saveBinary( req, res ) {
1404
1405
  try {
1406
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
1405
1407
  const inputData = req.body;
1406
1408
  const isoDate = req.userAudit?.startTime;
1407
1409
  const currentTime = dayjs();
@@ -1475,6 +1477,31 @@ export async function saveBinary( req, res ) {
1475
1477
  };
1476
1478
  let updatDatacamera= await updateOneBinaryAuditModel( cameraquery, cameraupdate );
1477
1479
  if ( updatDatacamera.modifiedCount==0 ) {
1480
+ const logData = {
1481
+ userId: req.user._id,
1482
+ userName: req.user.userName,
1483
+ logType: 'traxAudit',
1484
+ logSubType: 'auditDone',
1485
+ logData: {
1486
+ fileDate: req.userAudit?.fileDate,
1487
+ auditType: req.userAudit?.auditType,
1488
+ storeId: req.userAudit?.storeId,
1489
+ moduleType: req.userAudit?.moduleType,
1490
+ queueName: req.userAudit?.queueName,
1491
+ clientId: req.userAudit?.clientId,
1492
+ beforeCount: req.userAudit?.beforeCount,
1493
+ afterCount: inputData.employeeCount,
1494
+ startTime: req.userAudit?.startTime,
1495
+ streamName: req.userAudit?.streamName,
1496
+ tempId: req.userAudit?.tempId,
1497
+ question: req.userAudit?.question,
1498
+ endTime: Date.now(),
1499
+ timeSpent: timeDifferenceInMinutes,
1500
+ auditId: new mongoose.Types.ObjectId( inputData.auditId ),
1501
+ },
1502
+ createdAt: Date.now(),
1503
+ };
1504
+ await insertOpenSearchData( openSearch.auditLog, logData );
1478
1505
  return res.sendError( 'Update failed, it may be invalid request', 400 );
1479
1506
  }
1480
1507
  logger.info( 'update in binary data', { query: cameraquery, update: cameraupdate } );
@@ -1713,33 +1740,35 @@ export async function userAuditHistory( req, res ) {
1713
1740
  const temp ={
1714
1741
 
1715
1742
  'File Date': element.fileDate,
1716
- 'Brand ID': element.clientId,
1717
1743
  'Brand Name': element.clientName,
1718
- 'Store ID': element.storeId,
1744
+ 'Brand ID': element.clientId,
1719
1745
  'Store Name': element.storeName,
1746
+ 'Store ID': element.storeId,
1720
1747
  'Audit Type': element.auditType,
1721
1748
  'Product Type': element.value,
1749
+ 'Audited Date': dayjs( element.createdAt ).format( 'YYYY,MMM D' ),
1722
1750
  'Start Time': element.startTime,
1723
1751
  'End Time': element.endTime || '',
1724
1752
  'Time Spent': element.timeSpent,
1725
- 'Status': element.auditStatus,
1726
- 'Audited Date': dayjs( element.createdAt ).format( 'YYYY,MMM D' ),
1727
- 'Result': element.answer,
1753
+ 'Status': toCamelCase( element.auditStatus ),
1728
1754
 
1729
1755
 
1730
1756
  };
1731
1757
  switch ( inputData.moduleType ) {
1732
1758
  case 'camera-angle-change':
1733
1759
  temp['stream Name'] = element.streamName;
1760
+ temp['Result'] = element.answer;
1734
1761
  temp['User Comments'] = element.userComments;
1735
1762
  break;
1736
1763
  case 'unattended-customer':
1737
1764
  temp['Customer ID'] = element.tempId;
1738
1765
  temp['Question'] = element.question;
1766
+ temp['Result'] = element.answer;
1739
1767
  temp['User Comments'] = element.userComments;
1740
1768
  break;
1741
1769
  case 'left-in-middle':
1742
1770
  temp['Question'] = element.question;
1771
+ temp['Result'] = element.answer;
1743
1772
  temp['User Comments'] = element.userComments;
1744
1773
  break;
1745
1774
  case 'uniform-detection':
@@ -1922,6 +1951,125 @@ export async function clientMetrics( req, res ) {
1922
1951
  },
1923
1952
  ];
1924
1953
 
1954
+ const MappingQuery =[
1955
+ {
1956
+ $match: {
1957
+ $and: filter,
1958
+ },
1959
+ },
1960
+ {
1961
+ $group: {
1962
+ _id: { clientId: '$clientId', fileDate: '$fileDate', moduleType: '$moduleType' },
1963
+ fileDate: { $last: '$fileDate' },
1964
+ clientId: { $last: '$clientId' },
1965
+ clientName: { $last: '$clientName' },
1966
+ moduleType: { $last: '$moduleType' },
1967
+ installedStore: { $last: '$installedStore' },
1968
+ queueName: { $last: '$queueName' },
1969
+ totalFilesCount: { $sum: 1 },
1970
+ },
1971
+ },
1972
+ {
1973
+ $project: {
1974
+ fileDate: 1,
1975
+ clientId: 1,
1976
+ clientName: 1,
1977
+ queueName: 1,
1978
+ installedStore: 1,
1979
+ totalFilesCount: 1,
1980
+ moduleType: 1,
1981
+ notAssignedCount: { $ifNull: [ 0, 0 ] },
1982
+ clientStatus: { $ifNull: [ '', '' ] },
1983
+ },
1984
+ },
1985
+ {
1986
+ $lookup: {
1987
+ 'from': 'storeEmpDetection',
1988
+ 'let': { clientId: '$clientId', fileDate: '$fileDate', totalAuditFiles: '$totalFilesCount' },
1989
+ 'pipeline': [
1990
+
1991
+ {
1992
+ $match: {
1993
+ $expr: {
1994
+ $and: storeAuditFilter,
1995
+ },
1996
+ },
1997
+ },
1998
+ {
1999
+ $project: {
2000
+ completedStores: { $cond: [ { $eq: [ '$status', 'completed' ] }, 1, 0 ] },
2001
+ notAssignedStores: { $cond: [ { $eq: [ '$status', 'not_assign' ] }, 1, 0 ] },
2002
+ inprogressStores: { $cond: [ { $in: [ '$status', [ 'inprogress', 'drafted', 'assigned' ] ] }, 1, 0 ] },
2003
+ },
2004
+ },
2005
+ {
2006
+ $group: {
2007
+ _id: { clientId: '$clientId', fileDate: '$fileDate' },
2008
+ completedStores: { $sum: '$completedStores' },
2009
+ inprogressStores: { $sum: '$inprogressStores' },
2010
+ notAssignedStores: { $sum: '$notAssignedStores' },
2011
+ fileCount: { $sum: 1 },
2012
+ },
2013
+ },
2014
+ {
2015
+ $project: {
2016
+ _id: 0,
2017
+ completedStores: 1,
2018
+ inprogressStores: 1,
2019
+ notAssignedStores: 1,
2020
+ fileCount: { $ifNull: [ '$fileCount', 0 ] },
2021
+ completionPercentage: { $ifNull: [
2022
+ {
2023
+ $round: [ {
2024
+ $multiply: [
2025
+ 100, {
2026
+ $divide: [
2027
+ '$completedStores', '$$totalAuditFiles',
2028
+ ],
2029
+ },
2030
+ ],
2031
+ }, 1 ],
2032
+
2033
+ },
2034
+ 0,
2035
+ ],
2036
+ },
2037
+ },
2038
+ },
2039
+ ], 'as': 'binaryAudit',
2040
+ },
2041
+ },
2042
+ {
2043
+ $unwind: {
2044
+ path: '$binaryAudit',
2045
+ preserveNullAndEmptyArrays: true,
2046
+ },
2047
+ },
2048
+ {
2049
+ $project: {
2050
+ _id: 0,
2051
+ fileDate: 1,
2052
+ clientId: 1,
2053
+ clientName: 1,
2054
+ totalFilesCount: 1,
2055
+ moduleType: 1,
2056
+ value: {
2057
+ $replaceAll: { input: '$moduleType', find: '-', replacement: ' ' }, // Replacing "-" with " "
2058
+ },
2059
+ installedStore: 1,
2060
+ // notAssignedStores: '$binaryAudit.notAssignedStores',
2061
+ inprogressStoresCount: '$binaryAudit.inprogressStores',
2062
+ // fileCount: '$binaryAudit.fileCount',
2063
+ notAssignedCount: { $subtract: [ '$totalFilesCount', { $subtract: [ { $ifNull: [ '$binaryAudit.fileCount', 0 ] }, { $ifNull: [ '$binaryAudit.notAssignedStores', 0 ] } ] } ] },
2064
+ clientStatus: { $cond: [ { $eq: [ { $ifNull: [ '$binaryAudit.fileCount', 0 ] }, 0 ] }, 'not-taken', { $cond: [ { $gte: [ '$binaryAudit.completedStores', '$totalFilesCount' ] }, 'completed', 'pending' ] } ] }, // { $cond: [ { $or: [ { $gt: [ '$notAssignedCount', 0 ] }, { $gt: [ '$binaryAudit.notAssignedStores', 0 ] } ] }
2065
+ completedStores: '$binaryAudit.completedStores',
2066
+ completionPercentage: '$binaryAudit.completionPercentage',
2067
+
2068
+ },
2069
+ },
2070
+ ];
2071
+
2072
+
1925
2073
  if ( inputData?.filterByStatus?.length> 0 ) {
1926
2074
  query.push(
1927
2075
  {
@@ -1953,7 +2101,7 @@ export async function clientMetrics( req, res ) {
1953
2101
  );
1954
2102
  }
1955
2103
 
1956
- const count = await aggregateTraxAuditData( query );
2104
+ const count = [ 'uniform-detection', 'mobile-detection' ].includes( inputData.moduleType )? await aggregateTraxAuditData( MappingQuery ) : await aggregateTraxAuditData( query );
1957
2105
  logger.info( { count: count } );
1958
2106
  if ( count.length == 0 ) {
1959
2107
  return res.sendError( 'No Data Found', 204 );
@@ -1969,16 +2117,16 @@ export async function clientMetrics( req, res ) {
1969
2117
  $project: {
1970
2118
  '_id': 0,
1971
2119
  'File Date': '$fileDate',
1972
- 'Client Name': '$clientName',
1973
- 'Client Id': '$clientId',
2120
+ 'Brand Name': '$clientName',
2121
+ 'Brand Id': '$clientId',
1974
2122
  'Product Type': '$value',
1975
- 'Completed Percentage': '$completionPercentage',
2123
+ // 'Completed Percentage': '$completionPercentage',
1976
2124
  'Installed Stores': '$installedStore',
1977
2125
  'Audit Stores': '$totalFilesCount',
1978
2126
  'Inprogress Stores': '$inprogressStoresCount',
1979
- 'Not Assigned': '$notAssignedCount',
2127
+ 'Not Assigned Stores': '$notAssignedCount',
1980
2128
  // 'Not Assigned Stores': '$notAssignedStores',
1981
- 'Completed': '$completedStores',
2129
+ 'Completed Stores': '$completedStores',
1982
2130
  'Status': '$clientStatus',
1983
2131
  },
1984
2132
  },
@@ -1986,7 +2134,7 @@ export async function clientMetrics( req, res ) {
1986
2134
  }
1987
2135
 
1988
2136
  logger.info( { query: query } );
1989
- const result = await aggregateTraxAuditData( query );
2137
+ const result = [ 'uniform-detection', 'mobile-detection' ].includes( inputData.moduleType )? await aggregateTraxAuditData( MappingQuery ) : await aggregateTraxAuditData( query );
1990
2138
  if ( result.length == 0 ) {
1991
2139
  return res.sendError( 'No Data Found', 204 );
1992
2140
  }
@@ -2082,10 +2230,10 @@ export async function storeMetrics( req, res ) {
2082
2230
  question: 1,
2083
2231
  questionType: 1,
2084
2232
  answer: 1,
2085
- // userId: {
2086
- // $arrayElemAt: [ '$userId', { $subtract: [ { $size: '$userId' }, 1 ] } ], //need to check if no data
2087
- // },
2088
- userId: 1,
2233
+ userId: {
2234
+ $arrayElemAt: [ '$userId', { $subtract: [ { $size: '$userId' }, 1 ] } ], // need to check if no data
2235
+ },
2236
+ // userId: 1,
2089
2237
  auditType: 1,
2090
2238
  beforeCount: 1,
2091
2239
  afterCount: { $ifNull: [ '$afterCount', null ] },
@@ -2429,8 +2577,10 @@ export async function storeMetrics( req, res ) {
2429
2577
  },
2430
2578
  );
2431
2579
  }
2580
+ logger.info( { keyss: [ 'uniform-detection', 'mobile-detection' ].includes( inputData.moduleType ) } );
2432
2581
  const count = [ 'uniform-detection', 'mobile-detection' ].includes( inputData.moduleType )?await aggregateStoreEmpDetection( query ) : await aggregateBinaryAudit( binaryQuery );
2433
2582
 
2583
+
2434
2584
  if ( count.length == 0 ) {
2435
2585
  return res.sendError( 'No data Found', 204 );
2436
2586
  }
@@ -2444,7 +2594,7 @@ export async function storeMetrics( req, res ) {
2444
2594
  } else {
2445
2595
  query.push( { $limit: 10000 } );
2446
2596
  }
2447
- const result = [ 'uniform-detection', 'mobile-detection' ].includes( inputData.moduleType )?await aggregateUserEmpDetection( query ) : await aggregateBinaryAudit( query );
2597
+ const result = [ 'uniform-detection', 'mobile-detection' ].includes( inputData.moduleType )?await aggregateStoreEmpDetection( query ) : await aggregateBinaryAudit( query );
2448
2598
  if ( inputData.isExport ) {
2449
2599
  const exportdata = [];
2450
2600
  result.forEach( ( element ) => {
@@ -2458,7 +2608,6 @@ export async function storeMetrics( req, res ) {
2458
2608
  'User Email': element.userEmail,
2459
2609
  'Audit Type': element.auditType,
2460
2610
  'Time Spent': element.timeSpent,
2461
- 'Result': element.answer,
2462
2611
  };
2463
2612
  switch ( inputData.moduleType ) {
2464
2613
  case 'camera-angle-change':
@@ -2473,18 +2622,18 @@ export async function storeMetrics( req, res ) {
2473
2622
  case 'left-in-middle':
2474
2623
  temp['Question'] = element.question;
2475
2624
  temp['User Comments'] = element.userComments;
2476
- temp['Status'] = element.auditStatus;
2625
+ temp['Status'] = toCamelCase( element.auditStatus );
2477
2626
  break;
2478
2627
  case 'mobile-detection':
2479
2628
  temp['Before Count'] = element.beforeCount;
2480
2629
  temp['After Count'] = element.afterCount;
2481
- temp['Status'] = element.status;
2630
+ temp['Status'] = toCamelCase( element.status );
2482
2631
  temp['Accuracy'] = element.accuracy;
2483
2632
  break;
2484
2633
  case 'uniform-detection':
2485
2634
  temp['Before Count'] = element.beforeCount;
2486
2635
  temp['After Count'] = element.afterCount;
2487
- temp['Status'] = element.status;
2636
+ temp['Status'] = toCamelCase( element.status );
2488
2637
  temp['Accuracy'] = element.accuracy;
2489
2638
  break;
2490
2639
  }
@@ -2555,7 +2704,7 @@ export async function userMetrics( req, res ) {
2555
2704
  totalCompletedFiles: { $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, 1, 0 ] },
2556
2705
  beforeCount: 1,
2557
2706
  afterCount: { $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, '$afterCount', 0 ] },
2558
-
2707
+ createdAt: 1,
2559
2708
  value: {
2560
2709
  $replaceAll: { input: '$moduleType', find: '-', replacement: ' ' },
2561
2710
  } },
@@ -2572,6 +2721,7 @@ export async function userMetrics( req, res ) {
2572
2721
  checkOutTime: { $last: '$endTime' },
2573
2722
  moduleType: { $last: '$moduleType' },
2574
2723
  value: { $last: '$value' },
2724
+ createdAt: { $first: '$createdAt' },
2575
2725
  },
2576
2726
  },
2577
2727
  {
@@ -2584,6 +2734,7 @@ export async function userMetrics( req, res ) {
2584
2734
  value: 1,
2585
2735
  beforeCount: 1,
2586
2736
  afterCount: 1,
2737
+ createdAt: 1,
2587
2738
  // mappingPerc: { $round: [
2588
2739
  // { $multiply: [
2589
2740
  // { $divide: [ '$afterCount', '$beforeCount' ] }, 100,
@@ -2611,6 +2762,7 @@ export async function userMetrics( req, res ) {
2611
2762
  hours: { $floor: { $divide: [ '$differenceInSeconds', 3600 ] } },
2612
2763
  minutes: { $floor: { $divide: [ { $mod: [ '$differenceInSeconds', 3600 ] }, 60 ] } },
2613
2764
  seconds: { $mod: [ '$differenceInSeconds', 60 ] },
2765
+ createdAt: 1,
2614
2766
  },
2615
2767
  },
2616
2768
  {
@@ -2625,6 +2777,7 @@ export async function userMetrics( req, res ) {
2625
2777
  checkOutTime: 1,
2626
2778
  moduleType: 1,
2627
2779
  value: 1,
2780
+ createdAt: 1,
2628
2781
  // mappingPerc: 1,
2629
2782
  hours: {
2630
2783
  $cond: {
@@ -2660,6 +2813,7 @@ export async function userMetrics( req, res ) {
2660
2813
  checkIntime: 1,
2661
2814
  checkOutTime: 1,
2662
2815
  moduleType: 1,
2816
+ createdAt: 1,
2663
2817
  value: {
2664
2818
  $replaceAll: { input: '$moduleType', find: '-', replacement: ' ' },
2665
2819
  },
@@ -2723,6 +2877,7 @@ export async function userMetrics( req, res ) {
2723
2877
  timezone: 'Asia/Kolkata',
2724
2878
  },
2725
2879
  },
2880
+ createdAt: 1,
2726
2881
  workingHours: 1,
2727
2882
  },
2728
2883
  },
@@ -2759,26 +2914,28 @@ export async function userMetrics( req, res ) {
2759
2914
  if ( inputData.isExport ) {
2760
2915
  const detection = {
2761
2916
  '_id': 0,
2762
- 'File Date': '$fileDate',
2763
2917
  'User Name': '$userName',
2764
- 'Total Completed Files Count': '$totalCompletedFiles',
2918
+ 'File Date': '$fileDate',
2919
+ 'Completed Files': '$totalCompletedFiles',
2920
+ 'Product Type': '$value',
2765
2921
  'Total Before Count': '$beforeCount',
2766
2922
  'Total After Count': '$afterCount',
2767
- 'check Intime': '$checkIntime',
2768
- 'check OutTime': '$checkOutTime',
2769
- 'working Hours': '$workingHours',
2770
- 'Product Type': '$value',
2923
+ 'Check Intime': '$checkIntime',
2924
+ 'Check OutTime': '$checkOutTime',
2925
+ 'Working Hours': '$workingHours',
2926
+ 'Audited Date': { $dateToString: { format: '%Y,%b %d', date: '$createdAt' } },
2771
2927
  // 'Mapping Percentage': '$mappingPerc',
2772
2928
  };
2773
2929
  const YesNo = {
2774
2930
  '_id': 0,
2775
- 'File Date': '$fileDate',
2776
2931
  'User Name': '$userName',
2777
- 'Total Completed Files Count': '$totalCompletedFiles',
2778
- 'check Intime': '$checkIntime',
2779
- 'check OutTime': '$checkOutTime',
2780
- 'working Hours': '$workingHours',
2932
+ 'File Date': '$fileDate',
2933
+ 'Completed Files': '$totalCompletedFiles',
2781
2934
  'Product Type': '$value',
2935
+ 'Check Intime': '$checkIntime',
2936
+ 'Check OutTime': '$checkOutTime',
2937
+ 'Working Hours': '$workingHours',
2938
+ 'Audited Date': { $dateToString: { format: '%Y,%b %d', date: '$createdAt' } },
2782
2939
  // 'Mapping Percentage': '$mappingPerc',
2783
2940
  };
2784
2941
  [ 'uniform-detection', 'mobile-detection' ].includes( inputData.moduleType )?
@@ -2967,7 +3124,7 @@ export async function pendingSummaryTable( req, res ) {
2967
3124
  'Start Time': element.startTime,
2968
3125
  'User Name': element.userName,
2969
3126
  'User Email': element.userEmail,
2970
- 'Status': element.auditStatus,
3127
+ 'Status': toCamelCase( element.auditStatus ),
2971
3128
  };
2972
3129
  switch ( inputData.moduleType ) {
2973
3130
  case 'camera-angle-change':
@@ -3191,6 +3348,63 @@ export async function summarySplit( req, res ) {
3191
3348
  { clientId: { $in: inputData.clientId } },
3192
3349
  ];
3193
3350
 
3351
+
3352
+ const binaryAuditQuery =[
3353
+ {
3354
+ $match: {
3355
+ $and: filters,
3356
+ },
3357
+
3358
+ },
3359
+ {
3360
+ $project: {
3361
+ auditInprogress: {
3362
+ $cond: [
3363
+ { $and: [ { $eq: [ '$auditType', 'Audit' ] }, { $eq: [ '$auditStatus', 'inprogress' ] } ] }, 1, 0,
3364
+ ],
3365
+ },
3366
+ reAuditInprogress: {
3367
+ $cond: [
3368
+ { $and: [ { $eq: [ '$auditType', 'ReAudit' ] }, { $eq: [ '$auditStatus', 'inprogress' ] } ] }, 1, 0,
3369
+ ],
3370
+ },
3371
+
3372
+ reAuditCompleted: {
3373
+ $cond: [
3374
+ { $and: [ { $eq: [ '$auditType', 'ReAudit' ] }, { $eq: [ '$auditStatus', 'completed' ] } ] }, 1, 0,
3375
+ ],
3376
+ },
3377
+ auditCompleted: {
3378
+ $cond: [
3379
+ { $and: [ { $eq: [ '$auditType', 'Audit' ] }, { $eq: [ '$auditStatus', 'completed' ] } ] }, 1, 0,
3380
+ ],
3381
+ },
3382
+ },
3383
+ },
3384
+ {
3385
+ $group: {
3386
+ _id: null,
3387
+ auditInprogress: { $sum: '$auditInprogress' },
3388
+ reAuditInprogress: { $sum: '$reAuditInprogress' },
3389
+ reAuditCompleted: { $sum: '$reAuditCompleted' },
3390
+ auditCompleted: { $sum: '$auditCompleted' },
3391
+
3392
+ },
3393
+ },
3394
+ {
3395
+ $project: {
3396
+ _id: 0,
3397
+ totalInprogress: 1,
3398
+ auditInprogress: 1,
3399
+ reAuditInprogress: 1,
3400
+ reAuditCompleted: 1,
3401
+ auditCompleted: 1,
3402
+ totalCount: { $ifNull: [ 0, 0 ] },
3403
+
3404
+ },
3405
+ },
3406
+ ];
3407
+
3194
3408
  const query =[
3195
3409
  {
3196
3410
  $match: {
@@ -3280,7 +3494,7 @@ export async function summarySplit( req, res ) {
3280
3494
  },
3281
3495
  },
3282
3496
  ];
3283
- const result = [ 'uniform-detection', 'mobile-detection' ].includes( inputData.moduleType )? await aggregateUserEmpDetection( query ) : await aggregateBinaryAudit( query );
3497
+ const result = [ 'uniform-detection', 'mobile-detection' ].includes( inputData.moduleType )? await aggregateStoreEmpDetection( query ) : await aggregateBinaryAudit( binaryAuditQuery );
3284
3498
  const totalCount = await aggregateTraxAuditData( totalQuery );
3285
3499
  result[0].totalCount = totalCount[0]?.total;
3286
3500
  return res.sendSuccess( { result: result.length> 0? result[0]: temp } );
@@ -3310,7 +3524,7 @@ export async function overviewTable( req, res ) {
3310
3524
  }
3311
3525
 
3312
3526
  if ( inputData.filterByStatus.length > 0 ) {
3313
- filters.push( { status: { $in: inputData.filterByStatus } } );
3527
+ [ 'uniform-detection', 'mobile-detection' ].includes( inputData.moduleType )? filters.push( { status: { $in: inputData.filterByStatus } } ):filters.push( { auditStatus: { $in: inputData.filterByStatus } } );
3314
3528
  }
3315
3529
 
3316
3530
  const query =[
@@ -3447,39 +3661,42 @@ export async function overviewTable( req, res ) {
3447
3661
  const exportdata = [];
3448
3662
  result.forEach( ( element ) => {
3449
3663
  const temp ={
3450
- 'Store ID': element.storeId,
3451
- 'Brand ID': element.clientId,
3452
3664
  'File Date': element.fileDate,
3453
- 'Result': element.answer,
3454
- 'Product Type': element.value,
3455
- 'Audit Type': element.auditType,
3456
- // 'User Name': element.userName,
3457
- // 'User Email': element.userEmail,
3458
- 'Status': element.status,
3459
- 'Audit Status': element.status,
3665
+ 'Brand ID': element.clientId,
3666
+ 'Store ID': element.storeId,
3460
3667
  };
3461
3668
 
3462
3669
  switch ( inputData.moduleType ) {
3463
3670
  case 'camera-angle-change':
3464
3671
  temp['stream Name'] = element.streamName;
3465
3672
  temp['User Comments'] = element.userComments;
3673
+ temp['Audit Type'] = element.auditType;
3674
+ temp['Status'] = toCamelCase( element.auditStatus );
3466
3675
  break;
3467
3676
  case 'unattended-customer':
3468
3677
  temp['Customer ID'] = element.tempId;
3469
3678
  temp['Question'] = element.question;
3470
3679
  temp['User Comments'] = element.userComments;
3680
+ temp['Audit Type'] = element.auditType;
3681
+ temp['Status'] = toCamelCase( element.auditStatus );
3471
3682
  break;
3472
3683
  case 'left-in-middle':
3473
3684
  temp['Question'] = element.question;
3474
3685
  temp['User Comments'] = element.userComments;
3686
+ temp['Audit Type'] = element.auditType;
3687
+ temp['Status'] = toCamelCase( element.auditStatus );
3475
3688
  break;
3476
3689
  case 'mobile-detection':
3477
3690
  temp['Before Count'] = element.beforeCount;
3478
3691
  temp['After Count'] = element.afterCount;
3692
+ temp['Audit Type'] = element.auditType;
3693
+ temp['Status'] = toCamelCase( element.status );
3479
3694
  break;
3480
3695
  case 'uniform-detection':
3481
3696
  temp['Before Count'] = element.beforeCount;
3482
3697
  temp['After Count'] = element.afterCount;
3698
+ temp['Audit Type'] = element.auditType;
3699
+ temp['Status'] = toCamelCase( element.status );
3483
3700
  break;
3484
3701
  }
3485
3702
  exportdata.push( temp );
@@ -3815,15 +4032,16 @@ export async function getDetectionAuditFile( req, res ) {
3815
4032
  const data = await signedUrl( fetchData );
3816
4033
  const mapimg = {
3817
4034
  img_path: data,
3818
- img_name: image[0],
3819
- img_id: indexes[1],
4035
+ img_name: inputData.moduleType=='mobile-detection'?`${indexes[0]}-${indexes[3]}` :image[0],
4036
+ img_id: img[3],
3820
4037
  };
3821
4038
  files.push( {
3822
4039
  img_path: data,
3823
- img_name: image[0],
3824
- img_id: indexes[1],
4040
+ img_name: inputData.moduleType=='mobile-detection'?`${indexes[0]}-${indexes[3]}` :image[0],
4041
+ img_id: img[3],
3825
4042
  selected: false,
3826
4043
  dropped: false,
4044
+ singleDetection: false,
3827
4045
  count: 1,
3828
4046
  mappedid: [ mapimg ],
3829
4047
  } );
@@ -3840,7 +4058,7 @@ export async function getDetectionAuditFile( req, res ) {
3840
4058
  userId: req.user._id,
3841
4059
  storeId: msg.storeId,
3842
4060
  clientId: msg.storeId.split( '-' )[0],
3843
- auditType: 'Audit',
4061
+ auditType: msg.auditType || 'Audit',
3844
4062
  fileDate: msg.fileDate,
3845
4063
  inputBucketName: msg.inputBucketName,
3846
4064
  folderPath: msg.folderPath,
@@ -3861,7 +4079,7 @@ export async function getDetectionAuditFile( req, res ) {
3861
4079
  userId: req.user._id,
3862
4080
  storeId: msg.storeId,
3863
4081
  clientId: msg.storeId.split( '-' )[0],
3864
- auditType: 'Audit',
4082
+ auditType: msg.auditType || 'Audit',
3865
4083
  fileDate: msg.fileDate,
3866
4084
  folderPath: msg.folderPath,
3867
4085
  inputBucketName: msg.inputBucketName,
@@ -3924,7 +4142,7 @@ export async function getDetectionAuditFile( req, res ) {
3924
4142
  queueName: inputData.queueName,
3925
4143
  storeId: storeId,
3926
4144
  fileDate: fileDate,
3927
- auditType: 'Audit',
4145
+ auditType: msg.auditType || 'Audit',
3928
4146
  auditId: insertData._id,
3929
4147
  userId: insertData.userId,
3930
4148
  moduleType: inputData.moduleType,
@@ -4029,6 +4247,7 @@ export async function getDraftedData( req, res ) {
4029
4247
  storeId: inputData.storeId,
4030
4248
  userId: userId,
4031
4249
  moduleType: inputData.moduleType,
4250
+ auditId: inputData.auditId,
4032
4251
  };
4033
4252
  const result = await findOneAuditLog( query, {}, { createdAt: -1 }, 1 );
4034
4253
 
@@ -4049,9 +4268,53 @@ export async function getDraftedData( req, res ) {
4049
4268
 
4050
4269
  export async function save( req, res ) {
4051
4270
  try {
4271
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
4052
4272
  const inputData = req.body;
4053
- const data = await createEmpDetection( inputData );
4273
+ const isoDate = req.userAudit?.startTime;
4274
+ const currentTime = dayjs();
4275
+ const isoDateTime = dayjs( isoDate );
4276
+ const timeDifferenceInMinutes = currentTime.diff( isoDateTime, 'seconds' );
4277
+ const getUserAuditData = await findOneUserEmpDetection( { _id: inputData.auditId } );
4278
+ inputData.clientId = getUserAuditData.clientId;
4279
+ const query ={ storeId: inputData.storeId, moduleType: inputData.moduleType, fileDate: inputData.fileDate };
4280
+ const data = await updateOneEmpDetectionOutput( query, { $set: inputData }, { upsert: true } );
4054
4281
  if ( data ) {
4282
+ const userUpdated ={
4283
+ auditStatus: 'completed',
4284
+ afterCount: inputData.employeeCount,
4285
+ endTime: Date.now(),
4286
+ isDraft: false,
4287
+ timeSpent: timeDifferenceInMinutes,
4288
+ };
4289
+ const storeUpdated ={
4290
+ status: 'completed',
4291
+ afterCount: inputData.employeeCount,
4292
+ timeSpent: timeDifferenceInMinutes,
4293
+ };
4294
+ await updateOneUserEmpDetection( { _id: inputData.auditId }, userUpdated );
4295
+ await updateOneStoreEmpDetection( query, storeUpdated );
4296
+ const logData = {
4297
+ userId: req.user._id,
4298
+ userName: req.user.userName,
4299
+ logType: 'traxAudit',
4300
+ logSubType: 'auditDone',
4301
+ logData: {
4302
+ fileDate: getUserAuditData?.fileDate,
4303
+ auditType: getUserAuditData?.auditType,
4304
+ storeId: getUserAuditData?.storeId,
4305
+ moduleType: getUserAuditData?.moduleType,
4306
+ queueName: req.userAudit?.queueName,
4307
+ clientId: req.userAudit?.clientId,
4308
+ beforeCount: req.userAudit?.beforeCount,
4309
+ afterCount: inputData.employeeCount,
4310
+ startTime: req.userAudit?.startTime,
4311
+ endTime: Date.now(),
4312
+ timeSpent: timeDifferenceInMinutes,
4313
+ auditId: new mongoose.Types.ObjectId( inputData.auditId ),
4314
+ },
4315
+ createdAt: Date.now(),
4316
+ };
4317
+ await insertOpenSearchData( openSearch.auditLog, logData );
4055
4318
  return res.sendSuccess( { result: 'Updated Successfully' } );
4056
4319
  } else {
4057
4320
  return res.sendError( 'something went wrong', 403 );
@@ -4066,3 +4329,105 @@ export async function save( req, res ) {
4066
4329
  return res.sendError( err, 500 );
4067
4330
  }
4068
4331
  }
4332
+
4333
+ export async function reTrigger( req, res ) {
4334
+ try {
4335
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
4336
+ const inputData = req.body;
4337
+ const message = req.sqs;
4338
+ const sqs = JSON.parse( process.env.SQS );
4339
+ const getQueueName = await findOneClient( { clientId: req.audit.clientId }, { 'auditConfigs': 1 } );
4340
+ const queueName = inputData.moduleType == 'uniform-detection' ? getQueueName?.auditConfigs?.traxQueueName?.uniformDetection : getQueueName?.auditConfigs?.traxQueueName?.mobileDetection;
4341
+ if ( !getQueueName || queueName == '' ) {
4342
+ return res.sendError( 'queueName does not create', 400 );
4343
+ }
4344
+ const getRecord = await countDocumentsUserEmpDetection( { storeId: message.storeId,
4345
+ fileDate: message.fileDate,
4346
+ moduleType: message.moduleType, auditStatus: 'completed' } );
4347
+ const msg = {
4348
+ storeId: message.storeId,
4349
+ fileDate: message.fileDate,
4350
+ moduleType: message.moduleType,
4351
+ inputBucketName: message.inputBucketName,
4352
+ questionType: message.questionType,
4353
+ question: message.question,
4354
+ folderPath: message.folderPath,
4355
+ fileCount: message.fileCount,
4356
+ auditType: getRecord > 0? 'ReAudit':'Audit',
4357
+ };
4358
+ const sqsProduceQueue = {
4359
+ QueueUrl: `${sqs.url}${queueName}`,
4360
+ MessageBody: JSON.stringify( msg ),
4361
+ };
4362
+ const sqsQueue = await sendMessageToQueue( sqsProduceQueue.QueueUrl, sqsProduceQueue.MessageBody );
4363
+ if ( sqsQueue?.errorCode ) {
4364
+ const error = sqsQueue?.errorMsg;
4365
+ const code = sqsQueue?.errorCode;
4366
+ return res.sendError( error, code );
4367
+ } else {
4368
+ if ( req.audit.status !== 'completed' ) {
4369
+ const userRecord = {
4370
+ auditStatus: 'skipped',
4371
+ isDraft: false,
4372
+ };
4373
+ const query = {
4374
+ storeId: inputData.storeId,
4375
+ fileDate: inputData.fileDate,
4376
+ auditStatus: req.audit.status,
4377
+ moduleType: inputData.moduleType,
4378
+
4379
+ };
4380
+ await updateOneUserEmpDetection( query, userRecord );
4381
+ }
4382
+
4383
+ const storeQuery = {
4384
+ storeId: inputData.storeId,
4385
+ fileDate: inputData.fileDate,
4386
+ moduleType: inputData.moduleType,
4387
+
4388
+ };
4389
+
4390
+ const storeRecord = {
4391
+ status: 'not_assign',
4392
+ };
4393
+
4394
+ await updateOneStoreEmpDetection( storeQuery, storeRecord );
4395
+ const logData = {
4396
+ userId: req.user._id,
4397
+ userName: req.user.userName,
4398
+ logType: 'traxAudit',
4399
+ logSubType: 'reTrigger',
4400
+ logData: {
4401
+ beforeCount: message.fileCount,
4402
+ afterCount: '',
4403
+ fileDate: inputData.fileDate,
4404
+ auditType: req.audit.auditType,
4405
+ storeId: inputData.storeId,
4406
+ moduleType: inputData.moduleType,
4407
+ queueName: queueName,
4408
+ triggerBy: req.user.userName,
4409
+ triggerId: req.user._id,
4410
+ },
4411
+ createdAt: new Date(),
4412
+ };
4413
+ await insertOpenSearchData( openSearch.auditLog, logData );
4414
+ }
4415
+ return res.sendSuccess( { result: 'The File has been Re-Triggered Succesfully' } );
4416
+ } catch ( error ) {
4417
+ const err = error.message;
4418
+ logger.error( { error: error, message: req.body, function: 'reTrigger' } );
4419
+ return res.sendError( err, 500 );
4420
+ }
4421
+ }
4422
+
4423
+ export function toCamelCase( str ) {
4424
+ logger.info( { str: str } );
4425
+ return str
4426
+ .split( '-' ) // Split the string by spaces
4427
+ .map( ( word, index ) =>
4428
+ // index === 0 ?
4429
+ // word.toLowerCase() : // Lowercase the first word
4430
+ word.charAt( 0 ).toUpperCase() + word.slice( 1 ).toLowerCase(), // Capitalize the first letter of subsequent words
4431
+ )
4432
+ .join( '' ); // Join the words without spaces
4433
+ }