tango-app-api-audit 3.5.4 → 3.5.7-stage.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.5.4",
3
+ "version": "3.5.7-stage.0",
4
4
  "description": "audit & audit metrics apis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -27,7 +27,7 @@
27
27
  "nodemon": "^3.1.3",
28
28
  "swagger-ui-express": "^5.0.1",
29
29
  "tango-api-schema": "^2.2.113",
30
- "tango-app-api-middleware": "^3.1.76",
30
+ "tango-app-api-middleware": "^3.1.78",
31
31
  "winston": "^3.13.0",
32
32
  "winston-daily-rotate-file": "^5.0.0"
33
33
  },
@@ -31,6 +31,7 @@ import {
31
31
  listQueue,
32
32
  sqsReceive,
33
33
  sendMessageToQueue,
34
+ sendMessageToFIFOQueue,
34
35
  } from 'tango-app-api-middleware/src/utils/aws/sqs.js';
35
36
  import {
36
37
  aggregateStoreAudit,
@@ -536,10 +537,10 @@ export async function getAuditFile( req, res ) {
536
537
  fetchData.ContinuationToken = decodeURIComponent( inputData.nextId );
537
538
  }
538
539
  }
539
-
540
- const getQuery = {
540
+ const getQuery = msg.audit_type =='ReAudit'?
541
+ {
541
542
  size: inputData?.limit || 2,
542
- _source: [ 'module', 'status', 'date', 'store_id', 'outputCluster', 'personPath', 'isEmployee', 'isJunk', 'EmployeeStatusFinal' ], // Only fetch necessary fields
543
+ _source: [ 'module', 'status', 'date', 'store_id', 'outputCluster', 'personPath', 'REIDCluster', 'isEmployee', 'isJunk', 'EmployeeStatusFinal' ], // Only fetch necessary fields
543
544
 
544
545
  query: {
545
546
  bool: {
@@ -568,6 +569,33 @@ export async function getAuditFile( req, res ) {
568
569
  },
569
570
  },
570
571
 
572
+ }:
573
+ {
574
+ size: inputData?.limit || 2,
575
+ _source: [ 'module', 'status', 'date', 'store_id', 'personPath', 'REIDCluster', 'EmployeeStatusProposed', 'isJunk', 'EmployeeStatusFinal' ], // Only fetch necessary fields
576
+
577
+ query: {
578
+ bool: {
579
+ 'must': [
580
+ {
581
+ term: { 'store_id.keyword': msg.store_id },
582
+ },
583
+ {
584
+ term: { 'date.keyword': msg.curr_date },
585
+ },
586
+ {
587
+ term: { 'module.keyword': 'CUSTOMER' },
588
+ },
589
+ {
590
+ term: { 'status.keyword': 'PP_CLUSTER_FORMED' },
591
+ },
592
+ {
593
+ terms: { EmployeeStatusProposed: [ 1, 2 ] },
594
+ },
595
+ ],
596
+ },
597
+ },
598
+
571
599
  };
572
600
  const list = msg.zone_id === 'track' ? inputData.nextId? await scrollResponse( inputData.nextId ):await searchOpenSearchData( msg.index_name, getQuery ): await listFileByPath( fetchData );
573
601
  const folderPath = msg.zone_id === 'track' ? list?.body?.hits?.hits : list.data;
@@ -581,7 +609,7 @@ export async function getAuditFile( req, res ) {
581
609
  for ( let i = 0; i < folderPath.length; i++ ) {
582
610
  const img =msg.zone_id === 'track' ?folderPath[i]?._source?.personPath?.split( '/' ): folderPath[i].Key.split( '/' );
583
611
  const image = img[3]?.split( '.' );
584
- const indexes = msg.zone_id === 'track' ? folderPath[i]?._source?.outputCluster : image[0]?.split( '_' );
612
+ 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( '_' );
585
613
  fetchData.file_path = msg.zone_id === 'track' ?folderPath[i]?._source?.personPath:folderPath[i].Key;
586
614
  const data =msg.zone_id === 'track' ? `${url.trackInput}${folderPath[i]?._source?.personPath}` :await signedUrl( fetchData );
587
615
  const mapimg = {
@@ -1416,8 +1444,27 @@ export async function save( req, res ) {
1416
1444
  }
1417
1445
  } else {
1418
1446
  // Junk Mapping
1447
+ let junkCount = [];
1448
+ let empCount = [];
1449
+ let customerCount = [];
1419
1450
  if ( inputData.junkCount >0 ) {
1420
1451
  const chunkedJunkMappingData = await filteredMap( inputData.junk );
1452
+ junkCount = chunkedJunkMappingData;
1453
+ let filter = [
1454
+ {
1455
+ term: { 'store_id.keyword': inputData.storeId },
1456
+ },
1457
+ {
1458
+ term: { 'date.keyword': inputData.fileDate },
1459
+ },
1460
+ {
1461
+ term: { 'module.keyword': 'CUSTOMER' },
1462
+ },
1463
+ {
1464
+ terms: { REIDCluster: chunkedJunkMappingData },
1465
+ },
1466
+ ];
1467
+
1421
1468
  logger.info( { chunkedJunkMappingData: chunkedJunkMappingData } );
1422
1469
  const getQuery = {
1423
1470
  size: 10000,
@@ -1425,42 +1472,31 @@ export async function save( req, res ) {
1425
1472
  stored_fields: [], // exclude all stored fields
1426
1473
  query: {
1427
1474
  bool: {
1428
- must: [
1429
- {
1430
- term: { 'store_id.keyword': inputData.storeId },
1431
- },
1432
- {
1433
- terms: { outputCluster: chunkedJunkMappingData },
1434
- },
1435
- {
1436
- term: { 'date.keyword': inputData.fileDate },
1437
- },
1438
- {
1439
- term: { 'module.keyword': 'CUSTOMER' },
1440
- },
1441
- ],
1475
+ must: filter,
1442
1476
  },
1443
1477
  },
1444
1478
 
1445
1479
  };
1446
- const getId = await getOpenSearchData( 'tracker_process', getQuery );
1480
+ const getId = await getOpenSearchData( openSearch.trackerInput, getQuery );
1447
1481
  const ids = getId?.body?.hits?.total?.value || null;
1448
1482
  if ( ids ) {
1449
1483
  const temp = getId?.body?.hits?.hits?.map( ( item ) => item._id );
1450
1484
  const updatedField = { isJunk: true }; // your field updates
1451
1485
  const body = temp.flatMap( ( id ) => [
1452
- { update: { _index: 'tracker_process', _id: id } },
1486
+ { update: { _index: openSearch.trackerInput, _id: id } },
1453
1487
  { doc: updatedField },
1454
1488
  ] );
1455
1489
  const re= await bulkUpdate( body );
1456
- logger.info( { getId: getId, re: re } );
1490
+ logger.info( { getId: getId, re: re, storeId: inputData.storeId, fileDate: inputData.fileDate, function: 'tracking save - junk' } );
1457
1491
  }
1458
1492
  }
1459
1493
 
1494
+
1460
1495
  // Employee Mapping
1496
+
1461
1497
  if ( inputData.employeeCount >0 ) {
1462
1498
  const chunkedEmpMappingData = await filteredMap( inputData.employee );
1463
- logger.info( { chunkedEmpMappingData: chunkedEmpMappingData } );
1499
+ empCount = chunkedEmpMappingData;
1464
1500
  const getQuery = {
1465
1501
  size: 10000,
1466
1502
  _source: false, // do not fetch full source
@@ -1472,7 +1508,7 @@ export async function save( req, res ) {
1472
1508
  term: { 'store_id.keyword': inputData.storeId },
1473
1509
  },
1474
1510
  {
1475
- terms: { outputCluster: chunkedEmpMappingData },
1511
+ terms: { REIDCluster: chunkedEmpMappingData },
1476
1512
  },
1477
1513
  {
1478
1514
  term: { 'date.keyword': inputData.fileDate },
@@ -1485,32 +1521,31 @@ export async function save( req, res ) {
1485
1521
  },
1486
1522
 
1487
1523
  };
1488
- const getId = await getOpenSearchData( 'tracker_process', getQuery );
1524
+ const getId = await getOpenSearchData( openSearch.trackerInput, getQuery );
1489
1525
  const ids = getId?.body?.hits?.total?.value || null;
1490
1526
  if ( ids ) {
1491
1527
  const temp = getId?.body?.hits?.hits?.map( ( item ) => item._id );
1528
+
1492
1529
  const updatedField = { EmployeeStatusFinal: 0 }; // your field updates
1493
1530
  const body = temp.flatMap( ( id ) => [
1494
- { update: { _index: 'tracker_process', _id: id } },
1531
+ { update: { _index: openSearch.trackerInput, _id: id } },
1495
1532
  { doc: updatedField },
1496
1533
  ] );
1497
1534
  const re= await bulkUpdate( body );
1498
- logger.info( { getId: getId, re: re } );
1535
+ logger.info( { getId: getId, re: re, storeId: inputData.storeId, fileDate: inputData.fileDate, function: 'tracking save - Employee' } );
1499
1536
  }
1500
1537
  }
1501
1538
 
1502
1539
  // Customer Mapping
1503
1540
  if ( inputData?.customer?.length > 0 ) {
1504
- logger.info( { lenght: inputData?.customer?.length } );
1505
1541
  const filterCustomerData = await filteredCustomerMap( inputData.customer );
1506
1542
  const chunkedMappingData = await chunkArray( filterCustomerData, 10 );
1507
1543
 
1508
1544
  chunkedMappingData.map( async ( chunk ) => {
1509
- logger.info( { chunk: chunk } );
1510
-
1511
1545
  chunk.map( async ( item ) => {
1512
1546
  const imgName= item.img_name;
1513
1547
  const mappedId= item.mappedid.map( ( m ) => m.img_name );
1548
+ customerCount.push( ...mappedId );
1514
1549
  const searchQuery = {
1515
1550
  size: 10000,
1516
1551
  _source: false, // do not fetch full source
@@ -1522,7 +1557,7 @@ export async function save( req, res ) {
1522
1557
  term: { 'store_id.keyword': inputData.storeId },
1523
1558
  },
1524
1559
  {
1525
- terms: { outputCluster: mappedId },
1560
+ terms: { REIDCluster: mappedId },
1526
1561
  },
1527
1562
  {
1528
1563
  term: { 'date.keyword': inputData.fileDate },
@@ -1535,21 +1570,151 @@ export async function save( req, res ) {
1535
1570
  },
1536
1571
 
1537
1572
  };
1538
- const getData =await getOpenSearchData( 'tracker_process', searchQuery );
1573
+ const getData =await getOpenSearchData( openSearch.trackerInput, searchQuery );
1539
1574
  if ( getData?.body?.hits?.hits?.length > 0 ) {
1540
1575
  const temp = getData?.body?.hits?.hits?.map( ( item ) => item._id );
1541
1576
  const body = temp.flatMap( ( id ) => [
1542
- { update: { _index: 'tracker_process', _id: id } },
1577
+ { update: { _index: openSearch.trackerInput, _id: id } },
1543
1578
  { doc: { outputCluster: imgName, EmployeeStatusFinal: 1, isJunk: false } },
1544
1579
  ] );
1545
1580
  const re= await bulkUpdate( body );
1546
- logger.info( { re: re } );
1581
+ logger.info( { re: re, storeId: inputData.storeId, fileDate: inputData.fileDate, function: 'tracking save - customer' } );
1547
1582
  }
1548
1583
  } );
1549
1584
  } );
1550
1585
  }
1586
+
1587
+ if ( inputData.auditType !== 'ReAudit' ) {
1588
+ // junk Mapping
1589
+ const getJunkQuery = {
1590
+ size: 10000,
1591
+ _source: false, // do not fetch full source
1592
+ stored_fields: [], // exclude all stored fields
1593
+ query: {
1594
+ bool: {
1595
+ must_not: {
1596
+ terms: {
1597
+ REIDCluster: junkCount, // how can i c=handle if incase junkCount === []
1598
+ },
1599
+ },
1600
+ must: [
1601
+ {
1602
+ term: { 'store_id.keyword': inputData.storeId },
1603
+ },
1604
+ {
1605
+ term: { 'date.keyword': inputData.fileDate },
1606
+ },
1607
+ {
1608
+ term: { 'module.keyword': 'CUSTOMER' },
1609
+ },
1610
+ ],
1611
+ },
1612
+ },
1613
+
1614
+ };
1615
+
1616
+ const getJunkId = await getOpenSearchData( openSearch.trackerInput, getJunkQuery );
1617
+ const junkIds = getJunkId?.body?.hits?.total?.value || null;
1618
+ if ( junkIds ) {
1619
+ const temp = getJunkId?.body?.hits?.hits?.map( ( item ) => item._id );
1620
+ const updatedField = { isJunk: false }; // your field updates
1621
+ const body = temp.flatMap( ( id ) => [
1622
+ { update: { _index: openSearch.trackerInput, _id: id } },
1623
+ { doc: updatedField },
1624
+ ] );
1625
+ const re= await bulkUpdate( body );
1626
+ logger.info( { getJunkId: getJunkId, re: re, storeId: inputData.storeId, auditType: inputData.auditType, fileDate: inputData.fileDate, function: 'tracking save - junk false mapping' } );
1627
+ }
1628
+
1629
+
1630
+ // Employee Mapping
1631
+ const getEmployeeQuery = {
1632
+ size: 10000,
1633
+ _source: false, // do not fetch full source
1634
+ stored_fields: [], // exclude all stored fields
1635
+ query: {
1636
+ bool: {
1637
+ must_not: {
1638
+ terms: {
1639
+ REIDCluster: empCount, // how can i c=handle if incase empCount === []
1640
+ },
1641
+ },
1642
+ must: [
1643
+ {
1644
+ term: { 'store_id.keyword': inputData.storeId },
1645
+ },
1646
+ {
1647
+ term: { 'date.keyword': inputData.fileDate },
1648
+ },
1649
+ {
1650
+ term: { 'module.keyword': 'CUSTOMER' },
1651
+ },
1652
+ {
1653
+ terms: { EmployeeStatusProposed: [ 1, 2 ] },
1654
+ },
1655
+ ],
1656
+ },
1657
+ },
1658
+
1659
+ };
1660
+
1661
+ const getEmpId = await getOpenSearchData( openSearch.trackerInput, getEmployeeQuery );
1662
+ const empIds = getEmpId?.body?.hits?.total?.value || null;
1663
+ if ( empIds ) {
1664
+ const temp = getEmpId?.body?.hits?.hits?.map( ( item ) => item._id );
1665
+ const updatedField = { EmployeeStatusFinal: 1 }; // your field updates
1666
+ const body = temp.flatMap( ( id ) => [
1667
+ { update: { _index: openSearch.trackerInput, _id: id } },
1668
+ { doc: updatedField },
1669
+ ] );
1670
+ const re= await bulkUpdate( body );
1671
+ logger.info( { getEmpId: getEmpId, re: re, storeId: inputData.storeId, auditType: inputData.auditType, fileDate: inputData.fileDate, function: 'tracking save - junk false mapping' } );
1672
+ }
1673
+
1674
+ // customer Mapping
1675
+ const getCusQuery = {
1676
+ size: 10000,
1677
+ _source: false, // do not fetch full source
1678
+ stored_fields: [], // exclude all stored fields
1679
+ query: {
1680
+ bool: {
1681
+ must_not: {
1682
+ terms: {
1683
+ outputCluster: customerCount, // how can i c=handle if incase empCount === []
1684
+ },
1685
+ },
1686
+ must: [
1687
+ {
1688
+ term: { 'store_id.keyword': inputData.storeId },
1689
+ },
1690
+ {
1691
+ term: { 'date.keyword': inputData.fileDate },
1692
+ },
1693
+ {
1694
+ term: { 'module.keyword': 'CUSTOMER' },
1695
+ },
1696
+ ],
1697
+ },
1698
+ },
1699
+
1700
+ };
1701
+
1702
+ const getCusId = await getOpenSearchData( openSearch.trackerInput, getCusQuery );
1703
+ const cusIds = getCusId?.body?.hits?.total?.value || null;
1704
+ if ( cusIds ) {
1705
+ const temp = getCusId?.body?.hits?.hits.map( ( item ) => item._id );
1706
+ logger.info( { custoemrTemp: temp } );
1707
+ const body = temp.flatMap( ( id ) => [
1708
+ { update: { _index: openSearch.trackerInput, _id: id } },
1709
+ { doc: { outputCluster: 50000 } },
1710
+ ] );
1711
+ const re= await bulkUpdate( body );
1712
+ logger.info( { getCusId: getCusId, re: re, storeId: inputData.storeId, auditType: inputData.auditType, fileDate: inputData.fileDate, function: 'tracking save - junk false mapping' } );
1713
+ }
1714
+ }
1551
1715
  }
1552
1716
 
1717
+
1553
1718
  await updateOneUserAudit(
1554
1719
  { _id: new mongoose.Types.ObjectId( inputData.auditId ) },
1555
1720
  {
@@ -1589,7 +1754,7 @@ export async function save( req, res ) {
1589
1754
  const ratio = ( ( storeAuditData.beforeCount - inputData.customerCount ) / storeAuditData.beforeCount );
1590
1755
  if ( ratio < clientData[0].auditConfigs.ratio &&
1591
1756
  inputData.auditType == 'Audit' &&
1592
- inputData.moduleType == 'traffic'
1757
+ inputData.moduleType == 'traffic1'
1593
1758
  ) {
1594
1759
  logger.info(
1595
1760
  `Hit in Reaudit pushing queue Store Id=${inputData.storeId},File Date=${inputData.fileDate}, /n \nQueue Name = ${clientData[0].auditConfigs.queueName}`,
@@ -1679,33 +1844,82 @@ export async function save( req, res ) {
1679
1844
  timeSpent: inputData.timeSpent,
1680
1845
  },
1681
1846
  );
1682
- if ( inputData.moduleType !== 'track' ) {
1683
- const sqsProduceQueue = {
1684
- QueueUrl: ( auditConfig?.isCheckMinAfterCount ==1 && inputData.customerCount > auditConfig?.minAfterCount ) ? `${sqs.url}${sqs.feature_new}`: `${sqs.url}${sqs.feature}`,
1685
- MessageBody: JSON.stringify( {
1686
- store_id: inputData.storeId,
1687
- store_date: inputData.fileDate,
1688
- bucket_name: `${bucket.auditUploadBucket}`,
1689
- zone_id: inputData.zoneName,
1690
- process_type: 'audit',
1691
- } ),
1692
- };
1693
- logger.info( { sqsProduceQueue: sqsProduceQueue } );
1694
- const sqsQueue = await sendMessageToQueue(
1695
- sqsProduceQueue.QueueUrl,
1696
- sqsProduceQueue.MessageBody,
1697
- );
1698
- logger.info( { sqsQueue: sqsQueue } );
1699
- } else {
1700
- const query =
1701
- { 'module': 'REID-TRACKER-V1',
1702
- 'store_date_time': `${inputData.storeId}_${inputData.fileDate}_23:00`,
1703
- 'status': 'POSTPROCESS_AUDIT_SUCCESS',
1704
- 'status_time': Math.floor( Date.now() / 1000 ),
1705
- 'timestamp': new Date() };
1847
+ const getQuery = [
1848
+ {
1849
+ $match: {
1850
+ $and: [
1851
+ {
1852
+ storeId: {
1853
+ $eq: inputData.storeId,
1706
1854
 
1855
+ },
1707
1856
 
1708
- await upsertOpenSearchData( 'tracker_process', `${inputData.storeId}_${inputData.fileDate}_23:00`, { doc: query } );
1857
+ },
1858
+ {
1859
+ fileDate: {
1860
+ $eq: inputData.fileDate,
1861
+ },
1862
+ },
1863
+ {
1864
+ moduleType: {
1865
+ $eq: 'track',
1866
+ },
1867
+ },
1868
+ ],
1869
+ },
1870
+ },
1871
+ {
1872
+ $project: {
1873
+ _id: 0,
1874
+ sqs: '$sqs.Body',
1875
+ },
1876
+ },
1877
+ ];
1878
+ const getBody = await aggregateAuditStoreData( getQuery );
1879
+ const streams = getBody? getBody?.sqs?.streams : null;
1880
+ const sqsProduceQueue = inputData.moduleType === 'track'?
1881
+ {
1882
+ QueueUrl: `${sqs.url}${sqs.trackOutput}`,
1883
+ MessageBody: JSON.stringify( {
1884
+ store_id: inputData.storeId,
1885
+ zone_id: [ 'traffic_zone' ],
1886
+ streams: streams || [],
1887
+ store_date: inputData.fileDate,
1888
+ bucket_name: 'tango-yolo-v2-output',
1889
+ time: '23:00',
1890
+ retry: 1,
1891
+ } ),
1892
+ MessageGroupId: 'my-group-1', // Required for FIFO queues
1893
+ MessageDeduplicationId: Date.now().toString(), // Or a UUID, if content-based deduplication is off
1894
+ }:
1895
+ {
1896
+ QueueUrl: ( auditConfig?.isCheckMinAfterCount ==1 && inputData.customerCount > auditConfig?.minAfterCount ) ? `${sqs.url}${sqs.feature_new}`: `${sqs.url}${sqs.feature}`,
1897
+ MessageBody: JSON.stringify( {
1898
+ store_id: inputData.storeId,
1899
+ store_date: inputData.fileDate,
1900
+ bucket_name: `${bucket.auditUploadBucket}`,
1901
+ zone_id: inputData.zoneName,
1902
+ process_type: 'audit',
1903
+ } ),
1904
+ };
1905
+ logger.info( { sqsProduceQueue: sqsProduceQueue } );
1906
+ const sqsQueue = inputData.moduleType === 'track'? await sendMessageToFIFOQueue( sqsProduceQueue ) : await sendMessageToQueue(
1907
+ sqsProduceQueue.QueueUrl,
1908
+ sqsProduceQueue.MessageBody,
1909
+
1910
+ );
1911
+
1912
+ logger.info( { sqsQueue: sqsQueue } );
1913
+ if ( inputData.moduleType === 'track' ) {
1914
+ const query =
1915
+ {
1916
+ module: 'REID-TRACKER-V1',
1917
+ store_date_time: `${inputData.storeId}_${inputData.fileDate}_23:00`,
1918
+ status: 'POSTPROCESS_AUDIT_SUCCESS',
1919
+ status_time: Math.floor( Date.now() / 1000 ),
1920
+ timestamp: new Date(),
1921
+ };
1922
+ await upsertOpenSearchData( openSearch.trackerInput, `${inputData.storeId}_${inputData.fileDate}_23:00`, { doc: query } );
1709
1923
  }
1710
1924
  }
1711
1925
 
@@ -1867,9 +2081,6 @@ export async function userAuditHistory( req, res ) {
1867
2081
  timezone: 'Asia/Kolkata',
1868
2082
  },
1869
2083
  },
1870
- // timeSpent: { $round: [
1871
- // { $divide: [ '$timeSpent', 60 ] }, 2,
1872
- // ] },
1873
2084
  timeSpent: {
1874
2085
 
1875
2086
  $cond: [
@@ -3253,7 +3464,7 @@ export async function reTrigger( req, res ) {
3253
3464
  };
3254
3465
 
3255
3466
 
3256
- await updateOpenSearchData( 'tracker_process', `${inputData.storeId}_${inputData.fileDate}_23:00`, { doc: updateQuery } );
3467
+ await updateOpenSearchData( openSearch.trackerInput, `${inputData.storeId}_${inputData.fileDate}_23:00`, { doc: updateQuery } );
3257
3468
  return res.sendSuccess( { result: 'The File has been Re-Triggered Succesfully' } );
3258
3469
  } catch ( error ) {
3259
3470
  const err = error.message;