tango-app-api-audit 3.6.55 → 3.6.56

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.6.55",
3
+ "version": "3.6.56",
4
4
  "description": "audit & audit metrics apis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -1993,77 +1993,112 @@ export async function save( req, res ) {
1993
1993
  inputData.auditType == 'Audit' &&
1994
1994
  inputData.moduleType == 'traffic'
1995
1995
  ) {
1996
- logger.info(
1997
- `Hit in ReAudit pushing queue Store Id=${inputData.storeId},File Date=${inputData.fileDate}, /n \nQueue Name = ${clientData[0].auditConfigs.queueName}`,
1998
- 'ReAudit',
1999
- );
2000
- const queueName = inputData.moduleType == 'zone' ? clientData[0].auditConfigs.zoneQueueName : clientData[0].auditConfigs.trafficQueueName;
2001
- if ( !queueName ) {
2002
- logger.error( { error: 'queueName is undefined', queueName: queueName, moduleType: inputData.moduleType, function: 'save' } );
2003
- }
2004
- const sqsProduceQueue = {
2005
- QueueUrl: `${sqs.url}${queueName}`,
2006
- MessageBody: JSON.stringify( {
2007
- store_id: inputData.storeId,
2008
- zone_id: inputData.zoneName,
2009
- audit_type: 'ReAudit',
2010
- curr_date: inputData.fileDate,
2011
- total_count: inputData.customerCount,
2012
- before_count: storeAuditData.beforeCount,
2013
- } ),
2014
- };
2015
- const sqsQueue = await sendMessageToQueue(
2016
- sqsProduceQueue.QueueUrl,
2017
- sqsProduceQueue.MessageBody,
2018
- );
2019
-
2020
- if ( sqsQueue.statusCode ) {
2021
- logger.error( {
2022
- error: `${sqsQueue}`,
2023
- type: 'UPLOAD_ERROR',
2024
- } );
2025
- return res.sendError( mappingUpload, 500 );
2026
- }
2027
-
2028
- await updateOneStoreAudit(
2029
- {
2030
- storeId: inputData.storeId,
2031
- fileDate: inputData.fileDate,
2032
- moduleType: inputData.moduleType,
2033
- zoneName: inputData.zoneName,
2034
- },
2035
- {
2036
- status: 'not_assign',
2037
- afterCount: inputData.customerCount,
2038
- auditType: 'ReAudit',
2039
- timeSpent: inputData.timeSpent,
1996
+ // Skip re-pushing the reAudit queue if a reAuditPush log with the
1997
+ // same audit identifiers already exists (prevents duplicate triggers).
1998
+ const reAuditPushQuery = {
1999
+ size: 1,
2000
+ query: {
2001
+ bool: {
2002
+ must: [
2003
+ { term: { 'logSubType.keyword': 'reAuditPush' } },
2004
+ { term: { 'logData.fileDate.keyword': req.userAudit?.fileDate } },
2005
+ { term: { 'logData.auditType.keyword': req.userAudit?.auditType } },
2006
+ { term: { 'logData.storeId.keyword': req.userAudit?.storeId } },
2007
+ { term: { 'logData.moduleType.keyword': req.userAudit?.moduleType } },
2008
+ { term: { 'logData.zoneName.keyword': req.userAudit?.zoneName } },
2009
+ { term: { 'logData.startTime': req.userAudit?.startTime } },
2010
+ { term: { 'logData.auditId.keyword': inputData.auditId } },
2011
+ ],
2040
2012
  },
2041
- );
2013
+ },
2014
+ };
2015
+ const existingReAuditPush = await getOpenSearchData( openSearch.auditLog, reAuditPushQuery );
2016
+ const reAuditPushExists = existingReAuditPush?.body?.hits?.total?.value || 0;
2042
2017
 
2043
- const logData = {
2044
- userId: req.user._id,
2045
- userName: req.user.userName,
2046
- logType: 'audit',
2047
- logSubType: 'reAuditPush',
2048
- logData: {
2018
+ if ( reAuditPushExists ) {
2019
+ logger.info( {
2020
+ message: 'reAuditPush already exists, skipping queue push',
2021
+ storeId: req.userAudit?.storeId,
2049
2022
  fileDate: req.userAudit?.fileDate,
2050
2023
  auditType: req.userAudit?.auditType,
2051
- storeId: req.userAudit?.storeId,
2052
2024
  moduleType: req.userAudit?.moduleType,
2053
2025
  zoneName: req.userAudit?.zoneName,
2054
- queueName: req.userAudit?.queueName,
2055
- clientId: req.userAudit?.clientId,
2056
- beforeCount: req.userAudit?.beforeCount,
2057
- afterCount: inputData.customerCount,
2058
2026
  startTime: req.userAudit?.startTime,
2059
- endTime: Date.now(),
2060
- timeSpent: inputData.timeSpent,
2061
- auditId: new mongoose.Types.ObjectId( inputData.auditId ),
2062
- },
2063
- createdAt: Date.now(),
2064
- };
2027
+ function: 'save',
2028
+ } );
2029
+ } else {
2030
+ logger.info(
2031
+ `Hit in ReAudit pushing queue Store Id=${inputData.storeId},File Date=${inputData.fileDate}, /n \nQueue Name = ${clientData[0].auditConfigs.queueName}`,
2032
+ 'ReAudit',
2033
+ );
2034
+ const queueName = inputData.moduleType == 'zone' ? clientData[0].auditConfigs.zoneQueueName : clientData[0].auditConfigs.trafficQueueName;
2035
+ if ( !queueName ) {
2036
+ logger.error( { error: 'queueName is undefined', queueName: queueName, moduleType: inputData.moduleType, function: 'save' } );
2037
+ }
2038
+ const sqsProduceQueue = {
2039
+ QueueUrl: `${sqs.url}${queueName}`,
2040
+ MessageBody: JSON.stringify( {
2041
+ store_id: inputData.storeId,
2042
+ zone_id: inputData.zoneName,
2043
+ audit_type: 'ReAudit',
2044
+ curr_date: inputData.fileDate,
2045
+ total_count: inputData.customerCount,
2046
+ before_count: storeAuditData.beforeCount,
2047
+ } ),
2048
+ };
2049
+ const sqsQueue = await sendMessageToQueue(
2050
+ sqsProduceQueue.QueueUrl,
2051
+ sqsProduceQueue.MessageBody,
2052
+ );
2065
2053
 
2066
- await insertOpenSearchData( openSearch.auditLog, logData );
2054
+ if ( sqsQueue.statusCode ) {
2055
+ logger.error( {
2056
+ error: `${sqsQueue}`,
2057
+ type: 'UPLOAD_ERROR',
2058
+ } );
2059
+ return res.sendError( mappingUpload, 500 );
2060
+ }
2061
+
2062
+ await updateOneStoreAudit(
2063
+ {
2064
+ storeId: inputData.storeId,
2065
+ fileDate: inputData.fileDate,
2066
+ moduleType: inputData.moduleType,
2067
+ zoneName: inputData.zoneName,
2068
+ },
2069
+ {
2070
+ status: 'not_assign',
2071
+ afterCount: inputData.customerCount,
2072
+ auditType: 'ReAudit',
2073
+ timeSpent: inputData.timeSpent,
2074
+ },
2075
+ );
2076
+
2077
+ const logData = {
2078
+ userId: req.user._id,
2079
+ userName: req.user.userName,
2080
+ logType: 'audit',
2081
+ logSubType: 'reAuditPush',
2082
+ logData: {
2083
+ fileDate: req.userAudit?.fileDate,
2084
+ auditType: req.userAudit?.auditType,
2085
+ storeId: req.userAudit?.storeId,
2086
+ moduleType: req.userAudit?.moduleType,
2087
+ zoneName: req.userAudit?.zoneName,
2088
+ queueName: req.userAudit?.queueName,
2089
+ clientId: req.userAudit?.clientId,
2090
+ beforeCount: req.userAudit?.beforeCount,
2091
+ afterCount: inputData.customerCount,
2092
+ startTime: req.userAudit?.startTime,
2093
+ endTime: Date.now(),
2094
+ timeSpent: inputData.timeSpent,
2095
+ auditId: new mongoose.Types.ObjectId( inputData.auditId ),
2096
+ },
2097
+ createdAt: Date.now(),
2098
+ };
2099
+
2100
+ await insertOpenSearchData( openSearch.auditLog, logData );
2101
+ }
2067
2102
  } else {
2068
2103
  /* openSearch logs start*/
2069
2104
  let id = `${inputData.moduleType}_${inputData.auditId}_${inputData.storeId}_${inputData.moduleType}_${inputData.auditType}_save_Info_${req?.user?.email}_${Date.now()}`;
@@ -3751,7 +3786,7 @@ export async function reTrigger( req, res ) {
3751
3786
  }
3752
3787
  break;
3753
3788
  case 'user':
3754
- if ( req.audit.status === 'not_assign' ) {
3789
+ if ( req.audit.status === 'not_assign' ) {
3755
3790
  await releaseReTriggerLock( lockKey );
3756
3791
  return res.sendError( 'This store has already assigned to the user', 400 );
3757
3792
  }
@@ -45,11 +45,14 @@ export async function validateUserAudit( req, res, next ) {
45
45
  const userAuditDetails = await findOneUserAudit( { _id: inputData.auditId } );
46
46
  if ( !userAuditDetails ) {
47
47
  return res.sendError( 'No Data Available' );
48
+ } else if ( userAuditDetails && userAuditDetails.auditStatus == 'completed' ) {
49
+ return res.sendError( 'The Audit file has been already submitted', 403 );
48
50
  } else if ( userAuditDetails && userAuditDetails.auditStatus == 'skipped' ) {
49
51
  return res.sendError( 'The Audit file Assigned to some one else', 203 );
52
+ } else {
53
+ req.userAudit = userAuditDetails;
54
+ next();
50
55
  }
51
- req.userAudit = userAuditDetails;
52
- next();
53
56
  } catch ( error ) {
54
57
  const err = error.message || 'Internal Server Error';
55
58
  logger.error( { error: error, message: req.query, function: 'validateUserAudit' } );