tango-app-api-audit 3.5.13 → 3.5.14

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.5.13",
3
+ "version": "3.5.14",
4
4
  "description": "audit & audit metrics apis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -256,8 +256,7 @@ export async function getAuditFile( req, res ) {
256
256
  ];
257
257
  }
258
258
  const auditInsertData = inserQuery? await aggregateAuditStoreData( inserQuery ): null;
259
- const auditStatus =
260
- userDetails && userDetails.length > 0 ? userDetails[0].auditStatus : null;
259
+ const auditStatus = userDetails && userDetails.length > 0 ? userDetails[0].auditStatus : null;
261
260
  switch ( auditStatus ) {
262
261
  case null:
263
262
  const query = [
@@ -397,7 +396,7 @@ export async function getAuditFile( req, res ) {
397
396
  address: '$storeProfile.address',
398
397
  };
399
398
  const storeDetails = await findOneStore( storeQuery, storeFields );
400
- res.sendSuccess( {
399
+ return res.sendSuccess( {
401
400
  result: result,
402
401
  storeId: storeDetails?.storeId,
403
402
  storeName: storeDetails?.storeName,
@@ -537,7 +536,6 @@ export async function getAuditFile( req, res ) {
537
536
  fetchData.ContinuationToken = decodeURIComponent( inputData.nextId );
538
537
  }
539
538
  }
540
- logger.info( { msg: msg, leve: '............1' } );
541
539
  const getQuery = msg.audit_type =='ReAudit'?
542
540
  {
543
541
  size: inputData?.limit || 500,
@@ -600,7 +598,6 @@ export async function getAuditFile( req, res ) {
600
598
  };
601
599
  logger.info( { fetchData: fetchData, msg: msg, inputData: inputData, type: 'debug-bucketNameIssue' } );
602
600
  const list = msg.zone_id === 'track' ? inputData.nextId? await scrollResponse( inputData.nextId ):await searchOpenSearchData( msg.index_name, getQuery ): await listFileByPath( fetchData );
603
- logger.info( { list: list, msg: msg, level: '.........2' } );
604
601
  const folderPath = msg.zone_id === 'track' ? list?.body?.hits?.hits : list.data;
605
602
  const nextQuery =msg.zone_id !== 'track' ? list.pageToken: '';
606
603
  if ( msg.zone_id === 'track' ) {
@@ -608,10 +605,12 @@ export async function getAuditFile( req, res ) {
608
605
  await clearScroll( list?.body?._scroll_id );
609
606
  }
610
607
  }
608
+
611
609
  if ( folderPath?.length > 0 ) {
612
610
  for ( let i = 0; i < folderPath.length; i++ ) {
613
611
  const img =msg.zone_id === 'track' ?folderPath[i]?._source?.personPath?.split( '/' ): folderPath[i].Key.split( '/' );
614
612
  const image = img[3]?.split( '.' );
613
+ logger.info( { msg: msg, auditType: msg.audit_type, outputCluster: folderPath[i]?._source?.outputCluster, reidCLu: folderPath[i]?._source?.REIDCluster, level: '................2' } );
615
614
  const indexes = msg.zone_id === 'track' ? msg.audit_type === 'ReAudit'? folderPath[i]?._source?.outputCluster ===50000? folderPath[i]?._source?.REIDCluster:folderPath[i]?._source?.outputCluster:folderPath[i]?._source?.REIDCluster: image[0]?.split( '_' );
616
615
  fetchData.file_path = msg.zone_id === 'track' ?folderPath[i]?._source?.personPath:folderPath[i].Key;
617
616
  const data =msg.zone_id === 'track' ? `${url.trackInput}${folderPath[i]?._source?.personPath}` :await signedUrl( fetchData );
@@ -670,7 +669,7 @@ export async function getAuditFile( req, res ) {
670
669
  fileDate: msg.curr_date,
671
670
  moduleType: inputData.moduleType,
672
671
  };
673
- const storeRecord = {
672
+ const storeRecord = msg.audit_type !== 'ReAudit' ?{
674
673
  userId: req.user._id,
675
674
  storeId: msg.store_id,
676
675
  clientId: msg.store_id.split( '-' )[0],
@@ -683,6 +682,11 @@ export async function getAuditFile( req, res ) {
683
682
  timeSpent: 0,
684
683
  zoneName: msg.zone_id,
685
684
  moduleType: inputData.moduleType,
685
+ }: {
686
+ userId: req.user._id,
687
+ auditType: msg.audit_type || 'ReAudit',
688
+ status: 'inprogress',
689
+ timeSpent: 0,
686
690
  };
687
691
 
688
692
  insertData = await createUserAudit( record );
@@ -763,6 +767,7 @@ export async function getAuditFile( req, res ) {
763
767
  }
764
768
  function addUniqueFile( files, newFile ) {
765
769
  const exists = files.some( ( file ) => file.img_name === newFile.img_name );
770
+ logger.info( { files: files, exists: exists, newFile: newFile, message: '.........1' } );
766
771
  if ( !exists ) {
767
772
  files.push( newFile );
768
773
  }
@@ -1446,146 +1451,145 @@ export async function save( req, res ) {
1446
1451
  return res.sendError( mappingUpload, 500 );
1447
1452
  }
1448
1453
  } else {
1449
- // Junk Mapping
1450
1454
  let junkCount = [];
1451
1455
  let empCount = [];
1452
1456
  let customerCount = [];
1453
- if ( inputData.junkCount >0 ) {
1454
- const chunkedJunkMappingData = await filteredMap( inputData.junk );
1455
- junkCount = chunkedJunkMappingData;
1456
- let filter = [
1457
- {
1458
- term: { 'store_id.keyword': inputData.storeId },
1459
- },
1460
- {
1461
- term: { 'date.keyword': inputData.fileDate },
1462
- },
1463
- {
1464
- term: { 'module.keyword': 'CUSTOMER' },
1465
- },
1466
- {
1467
- terms: { REIDCluster: chunkedJunkMappingData },
1468
- },
1469
- ];
1470
1457
 
1471
- logger.info( { chunkedJunkMappingData: chunkedJunkMappingData } );
1472
- const getQuery = {
1473
- size: 10000,
1474
- _source: false, // do not fetch full source
1475
- stored_fields: [], // exclude all stored fields
1476
- query: {
1477
- bool: {
1478
- must: filter,
1479
- },
1480
- },
1458
+ // Junk Mapping
1459
+ const junkPromise = ( async () => {
1460
+ if ( inputData.junkCount > 0 ) {
1461
+ const chunkedJunkMappingData = await filteredMap( inputData.junk );
1462
+ junkCount = chunkedJunkMappingData;
1463
+
1464
+ const filter = [
1465
+ { term: { 'store_id.keyword': inputData.storeId } },
1466
+ { term: { 'date.keyword': inputData.fileDate } },
1467
+ { term: { 'module.keyword': 'CUSTOMER' } },
1468
+ // { terms: { REIDCluster: chunkedJunkMappingData } },
1469
+ ];
1470
+
1471
+ const getQuery = {
1472
+ size: 10000,
1473
+ _source: false,
1474
+ stored_fields: [],
1475
+ query: { bool: { should: [
1476
+ { terms: { outputCluster: chunkedJunkMappingData } },
1477
+ { terms: { REIDCluster: chunkedJunkMappingData } },
1478
+ ],
1479
+ minimum_should_match: 1, must: filter } },
1480
+ };
1481
1481
 
1482
- };
1483
- const getId = await getOpenSearchData( openSearch.trackerInput, getQuery );
1484
- const ids = getId?.body?.hits?.total?.value || null;
1485
- if ( ids ) {
1486
- const temp = getId?.body?.hits?.hits?.map( ( item ) => item._id );
1487
- const updatedField = { isJunk: true }; // your field updates
1488
- const body = temp.flatMap( ( id ) => [
1489
- { update: { _index: openSearch.trackerInput, _id: id } },
1490
- { doc: updatedField },
1491
- ] );
1492
- const re= await bulkUpdate( body );
1493
- logger.info( { getId: getId, re: re, storeId: inputData.storeId, fileDate: inputData.fileDate, function: 'tracking save - junk' } );
1494
- }
1495
- }
1482
+ const getId = await getOpenSearchData( openSearch.trackerInput, getQuery );
1483
+ const temp = getId?.body?.hits?.hits?.map( ( item ) => item._id ) || [];
1496
1484
 
1485
+ if ( temp.length ) {
1486
+ const body = temp.flatMap( ( id ) => [
1487
+ { update: { _index: openSearch.trackerInput, _id: id } },
1488
+ { doc: { isJunk: true } },
1489
+ ] );
1490
+ await bulkUpdate( body );
1491
+ }
1492
+ }
1493
+ } )();
1497
1494
 
1498
1495
  // Employee Mapping
1499
1496
 
1500
- if ( inputData.employeeCount >0 ) {
1501
- const chunkedEmpMappingData = await filteredMap( inputData.employee );
1502
- empCount = chunkedEmpMappingData;
1503
- const getQuery = {
1504
- size: 10000,
1505
- _source: false, // do not fetch full source
1506
- stored_fields: [], // exclude all stored fields
1507
- query: {
1508
- bool: {
1509
- must: [
1510
- {
1511
- term: { 'store_id.keyword': inputData.storeId },
1512
- },
1513
- {
1514
- terms: { REIDCluster: chunkedEmpMappingData },
1515
- },
1516
- {
1517
- term: { 'date.keyword': inputData.fileDate },
1518
- },
1519
- {
1520
- term: { 'module.keyword': 'CUSTOMER' },
1521
- },
1522
- ],
1497
+ const empPromise = ( async () => {
1498
+ if ( inputData.employeeCount > 0 ) {
1499
+ const chunkedEmpMappingData = await filteredMap( inputData.employee );
1500
+ empCount = chunkedEmpMappingData;
1501
+
1502
+ const getQuery = {
1503
+ size: 10000,
1504
+ _source: false,
1505
+ stored_fields: [],
1506
+ query: {
1507
+ bool: {
1508
+ should: [
1509
+ { terms: { outputCluster: chunkedEmpMappingData } },
1510
+ { terms: { REIDCluster: chunkedEmpMappingData } },
1511
+ ],
1512
+ minimum_should_match: 1,
1513
+ must: [
1514
+ { term: { 'store_id.keyword': inputData.storeId } },
1515
+ { term: { 'date.keyword': inputData.fileDate } },
1516
+ { term: { 'module.keyword': 'CUSTOMER' } },
1517
+ // { terms: { REIDCluster: chunkedEmpMappingData } },
1518
+ ],
1519
+ },
1523
1520
  },
1524
- },
1521
+ };
1525
1522
 
1526
- };
1527
- const getId = await getOpenSearchData( openSearch.trackerInput, getQuery );
1528
- const ids = getId?.body?.hits?.total?.value || null;
1529
- if ( ids ) {
1530
- const temp = getId?.body?.hits?.hits?.map( ( item ) => item._id );
1523
+ const getId = await getOpenSearchData( openSearch.trackerInput, getQuery );
1524
+ const temp = getId?.body?.hits?.hits?.map( ( item ) => item._id ) || [];
1531
1525
 
1532
- const updatedField = { EmployeeStatusFinal: 0 }; // your field updates
1533
- const body = temp.flatMap( ( id ) => [
1534
- { update: { _index: openSearch.trackerInput, _id: id } },
1535
- { doc: updatedField },
1536
- ] );
1537
- const re= await bulkUpdate( body );
1538
- logger.info( { getId: getId, re: re, storeId: inputData.storeId, fileDate: inputData.fileDate, function: 'tracking save - Employee' } );
1526
+ if ( temp.length ) {
1527
+ const body = temp.flatMap( ( id ) => [
1528
+ { update: { _index: openSearch.trackerInput, _id: id } },
1529
+ { doc: { EmployeeStatusFinal: 0 } },
1530
+ ] );
1531
+ await bulkUpdate( body );
1532
+ }
1539
1533
  }
1540
- }
1534
+ } )();
1541
1535
 
1542
1536
  // Customer Mapping
1543
- if ( inputData?.customer?.length > 0 ) {
1544
- const filterCustomerData = await filteredCustomerMap( inputData.customer );
1545
- const chunkedMappingData = await chunkArray( filterCustomerData, 10 );
1546
-
1547
- chunkedMappingData.map( async ( chunk ) => {
1548
- chunk.map( async ( item ) => {
1549
- const imgName= item.img_name;
1550
- const mappedId= item.mappedid.map( ( m ) => m.img_name );
1551
- customerCount.push( ...mappedId );
1552
- const searchQuery = {
1553
- size: 10000,
1554
- _source: false, // do not fetch full source
1555
- stored_fields: [], // exclude all stored fields
1556
- query: {
1557
- bool: {
1558
- must: [
1559
- {
1560
- term: { 'store_id.keyword': inputData.storeId },
1561
- },
1562
- {
1563
- terms: { REIDCluster: mappedId },
1564
- },
1565
- {
1566
- term: { 'date.keyword': inputData.fileDate },
1567
- },
1568
- {
1569
- term: { 'module.keyword': 'CUSTOMER' },
1570
- },
1571
- ],
1537
+
1538
+ const customerPromise = ( async () => {
1539
+ if ( inputData?.customer?.length > 0 ) {
1540
+ const filterCustomerData = await filteredCustomerMap( inputData.customer );
1541
+ const chunkedMappingData = await chunkArray( filterCustomerData, 10 );
1542
+
1543
+ for ( const chunk of chunkedMappingData ) {
1544
+ for ( const item of chunk ) {
1545
+ const imgName = item.img_name;
1546
+ const mappedId = item.mappedid.map( ( m ) => m.img_name );
1547
+ customerCount.push( ...mappedId );
1548
+
1549
+ const searchQuery = {
1550
+ size: 10000,
1551
+ _source: false,
1552
+ stored_fields: [],
1553
+ query: {
1554
+ bool: {
1555
+ should: [
1556
+ { terms: { outputCluster: mappedId } },
1557
+ { terms: { REIDCluster: mappedId } },
1558
+ ],
1559
+ minimum_should_match: 1,
1560
+ must: [
1561
+ { term: { 'store_id.keyword': inputData.storeId } },
1562
+ { term: { 'date.keyword': inputData.fileDate } },
1563
+ { term: { 'module.keyword': 'CUSTOMER' } },
1564
+ ],
1565
+ },
1572
1566
  },
1573
- },
1567
+ };
1574
1568
 
1575
- };
1576
- const getData =await getOpenSearchData( openSearch.trackerInput, searchQuery );
1577
- if ( getData?.body?.hits?.hits?.length > 0 ) {
1578
- const temp = getData?.body?.hits?.hits?.map( ( item ) => item._id );
1579
- const body = temp.flatMap( ( id ) => [
1580
- { update: { _index: openSearch.trackerInput, _id: id } },
1581
- { doc: { outputCluster: imgName, auditOutputCluster: imgName, EmployeeStatusFinal: 1, isJunk: false } },
1582
- ] );
1583
- const re= await bulkUpdate( body );
1584
- logger.info( { re: re, storeId: inputData.storeId, fileDate: inputData.fileDate, function: 'tracking save - customer' } );
1569
+ const getData = await getOpenSearchData( openSearch.trackerInput, searchQuery );
1570
+ const temp = getData?.body?.hits?.hits?.map( ( item ) => item._id ) || [];
1571
+
1572
+ if ( temp.length ) {
1573
+ const body = temp.flatMap( ( id ) => [
1574
+ { update: { _index: openSearch.trackerInput, _id: id } },
1575
+ {
1576
+ doc: {
1577
+ outputCluster: imgName,
1578
+ auditOutputCluster: imgName,
1579
+ EmployeeStatusFinal: 1,
1580
+ isJunk: false,
1581
+ },
1582
+ },
1583
+ ] );
1584
+ await bulkUpdate( body );
1585
+ }
1585
1586
  }
1586
- } );
1587
- } );
1588
- }
1587
+ }
1588
+ }
1589
+ } )();
1590
+
1591
+ // Wait for all above tasks to complete
1592
+ await Promise.all( [ junkPromise, empPromise, customerPromise ] );
1589
1593
 
1590
1594
  if ( inputData.auditType !== 'ReAudit' ) {
1591
1595
  // junk Mapping
@@ -3329,6 +3333,7 @@ export async function reTrigger( req, res ) {
3329
3333
  fileDate: inputData.fileDate,
3330
3334
  zoneName: inputData.zoneName,
3331
3335
  };
3336
+ const getIndex =inputData.moduleType == 'track'? await findOneStoreData( query, { sqs: 1 } ):null;
3332
3337
  const getQueueName = await findOneClient( { clientId: req.audit.clientId }, { 'auditConfigs': 1 } );
3333
3338
  const queueName = inputData.moduleType == 'zone' ? getQueueName?.auditConfigs?.zoneQueueName :inputData.moduleType == 'track'?getQueueName?.auditConfigs?.trackQueueName: getQueueName?.auditConfigs?.trafficQueueName;
3334
3339
  if ( !getQueueName || queueName == '' ) {
@@ -3342,6 +3347,7 @@ export async function reTrigger( req, res ) {
3342
3347
  curr_date: inputData.fileDate,
3343
3348
  audit_type: inputData.auditType,
3344
3349
  total_count: inputData.totalCount,
3350
+ ...( getIndex? { index_name: getIndex?.sqs?.Body?.index_name }:null ),
3345
3351
  };
3346
3352
 
3347
3353
  const sqsProduceQueue = {