tango-app-api-audit 3.5.4 → 3.5.6

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.6",
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,
@@ -537,7 +538,40 @@ export async function getAuditFile( req, res ) {
537
538
  }
538
539
  }
539
540
 
540
- const getQuery = {
541
+ const getQuery = msg.audit_type =='Audit'?
542
+ {
543
+ size: inputData?.limit || 2,
544
+ _source: [ 'module', 'status', 'date', 'store_id', 'personPath', 'REIDCluster', 'EmployeeStatusProposed', 'isJunk', 'EmployeeStatusFinal' ], // Only fetch necessary fields
545
+
546
+ query: {
547
+ bool: {
548
+ 'must': [
549
+ {
550
+ term: { 'store_id.keyword': msg.store_id },
551
+ },
552
+ {
553
+ term: { 'date.keyword': msg.curr_date },
554
+ },
555
+ {
556
+ term: { 'module.keyword': 'CUSTOMER' },
557
+ },
558
+ {
559
+ term: { 'status.keyword': 'PP_CLUSTER_FORMED' },
560
+ },
561
+ {
562
+ terms: { EmployeeStatusProposed: [ 1, 2 ] },
563
+ },
564
+ ],
565
+ // 'should': [
566
+ // { 'term': { 'isJunk': false } },
567
+ // { 'bool': { 'must_not': { 'exists': { 'field': 'isJunk' } } } },
568
+ // ],
569
+ // 'minimum_should_match': 1,
570
+ },
571
+ },
572
+
573
+ }:
574
+ {
541
575
  size: inputData?.limit || 2,
542
576
  _source: [ 'module', 'status', 'date', 'store_id', 'outputCluster', 'personPath', 'isEmployee', 'isJunk', 'EmployeeStatusFinal' ], // Only fetch necessary fields
543
577
 
@@ -581,7 +615,7 @@ export async function getAuditFile( req, res ) {
581
615
  for ( let i = 0; i < folderPath.length; i++ ) {
582
616
  const img =msg.zone_id === 'track' ?folderPath[i]?._source?.personPath?.split( '/' ): folderPath[i].Key.split( '/' );
583
617
  const image = img[3]?.split( '.' );
584
- const indexes = msg.zone_id === 'track' ? folderPath[i]?._source?.outputCluster : image[0]?.split( '_' );
618
+ const indexes = msg.zone_id === 'track' ? folderPath[i]?._source?.outputCluster || folderPath[i]?._source?.REIDCluster : image[0]?.split( '_' );
585
619
  fetchData.file_path = msg.zone_id === 'track' ?folderPath[i]?._source?.personPath:folderPath[i].Key;
586
620
  const data =msg.zone_id === 'track' ? `${url.trackInput}${folderPath[i]?._source?.personPath}` :await signedUrl( fetchData );
587
621
  const mapimg = {
@@ -1416,101 +1450,115 @@ export async function save( req, res ) {
1416
1450
  }
1417
1451
  } else {
1418
1452
  // Junk Mapping
1453
+ let junkCount = [];
1454
+ let empCount = [];
1455
+ // let customerCount = [];
1419
1456
  if ( inputData.junkCount >0 ) {
1420
1457
  const chunkedJunkMappingData = await filteredMap( inputData.junk );
1421
- logger.info( { chunkedJunkMappingData: chunkedJunkMappingData } );
1422
- const getQuery = {
1423
- size: 10000,
1424
- _source: false, // do not fetch full source
1425
- stored_fields: [], // exclude all stored fields
1426
- query: {
1427
- 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
- ],
1442
- },
1458
+ junkCount = chunkedJunkMappingData;
1459
+ let filter = [
1460
+ {
1461
+ term: { 'store_id.keyword': inputData.storeId },
1462
+ },
1463
+ {
1464
+ term: { 'date.keyword': inputData.fileDate },
1465
+ },
1466
+ {
1467
+ term: { 'module.keyword': 'CUSTOMER' },
1443
1468
  },
1469
+ ];
1470
+ inputData.auditType == 'Audit'? filter.push(
1471
+ {
1472
+ terms: { REIDCluster: chunkedJunkMappingData },
1473
+ },
1474
+ ):
1475
+ filter.push(
1476
+ {
1477
+ terms: { outputCluster: chunkedJunkMappingData },
1478
+ },
1479
+ );
1480
+ logger.info( { chunkedJunkMappingData: chunkedJunkMappingData } );
1481
+ const getQuery = {
1482
+ size: 10000,
1483
+ _source: false, // do not fetch full source
1484
+ stored_fields: [], // exclude all stored fields
1485
+ query: {
1486
+ bool: {
1487
+ must: filter,
1488
+ },
1489
+ },
1444
1490
 
1445
- };
1446
- const getId = await getOpenSearchData( 'tracker_process', getQuery );
1447
- const ids = getId?.body?.hits?.total?.value || null;
1448
- if ( ids ) {
1449
- const temp = getId?.body?.hits?.hits?.map( ( item ) => item._id );
1450
- const updatedField = { isJunk: true }; // your field updates
1451
- const body = temp.flatMap( ( id ) => [
1452
- { update: { _index: 'tracker_process', _id: id } },
1453
- { doc: updatedField },
1454
- ] );
1455
- const re= await bulkUpdate( body );
1456
- logger.info( { getId: getId, re: re } );
1457
- }
1491
+ };
1492
+ const getId = await getOpenSearchData( openSearch.trackerInput, getQuery );
1493
+ const ids = getId?.body?.hits?.total?.value || null;
1494
+ if ( ids ) {
1495
+ const temp = getId?.body?.hits?.hits?.map( ( item ) => item._id );
1496
+ const updatedField = { isJunk: true }; // your field updates
1497
+ const body = temp.flatMap( ( id ) => [
1498
+ { update: { _index: openSearch.trackerInput, _id: id } },
1499
+ { doc: updatedField },
1500
+ ] );
1501
+ const re= await bulkUpdate( body );
1502
+ logger.info( { getId: getId, re: re, storeId: inputData.storeId, fileDate: inputData.fileDate, function: 'tracking save - junk' } );
1503
+ }
1458
1504
  }
1459
1505
 
1506
+
1460
1507
  // Employee Mapping
1461
- if ( inputData.employeeCount >0 ) {
1462
- const chunkedEmpMappingData = await filteredMap( inputData.employee );
1463
- logger.info( { chunkedEmpMappingData: chunkedEmpMappingData } );
1464
- const getQuery = {
1465
- size: 10000,
1466
- _source: false, // do not fetch full source
1467
- stored_fields: [], // exclude all stored fields
1468
- query: {
1469
- bool: {
1470
- must: [
1471
- {
1472
- term: { 'store_id.keyword': inputData.storeId },
1473
- },
1474
- {
1475
- terms: { outputCluster: chunkedEmpMappingData },
1476
- },
1477
- {
1478
- term: { 'date.keyword': inputData.fileDate },
1479
- },
1480
- {
1481
- term: { 'module.keyword': 'CUSTOMER' },
1482
- },
1483
- ],
1508
+ {
1509
+ if ( inputData.employeeCount >0 ) {
1510
+ const chunkedEmpMappingData = await filteredMap( inputData.employee );
1511
+ empCount = chunkedEmpMappingData;
1512
+ const getQuery = {
1513
+ size: 10000,
1514
+ _source: false, // do not fetch full source
1515
+ stored_fields: [], // exclude all stored fields
1516
+ query: {
1517
+ bool: {
1518
+ must: [
1519
+ {
1520
+ term: { 'store_id.keyword': inputData.storeId },
1521
+ },
1522
+ {
1523
+ terms: { outputCluster: chunkedEmpMappingData },
1524
+ },
1525
+ {
1526
+ term: { 'date.keyword': inputData.fileDate },
1527
+ },
1528
+ {
1529
+ term: { 'module.keyword': 'CUSTOMER' },
1530
+ },
1531
+ ],
1532
+ },
1484
1533
  },
1485
- },
1486
1534
 
1487
- };
1488
- const getId = await getOpenSearchData( 'tracker_process', getQuery );
1489
- const ids = getId?.body?.hits?.total?.value || null;
1490
- if ( ids ) {
1491
- const temp = getId?.body?.hits?.hits?.map( ( item ) => item._id );
1492
- const updatedField = { EmployeeStatusFinal: 0 }; // your field updates
1493
- const body = temp.flatMap( ( id ) => [
1494
- { update: { _index: 'tracker_process', _id: id } },
1495
- { doc: updatedField },
1496
- ] );
1497
- const re= await bulkUpdate( body );
1498
- logger.info( { getId: getId, re: re } );
1535
+ };
1536
+ const getId = await getOpenSearchData( openSearch.trackerInput, getQuery );
1537
+ const ids = getId?.body?.hits?.total?.value || null;
1538
+ if ( ids ) {
1539
+ const temp = getId?.body?.hits?.hits?.map( ( item ) => item._id );
1540
+
1541
+ const updatedField = { EmployeeStatusFinal: 0 }; // your field updates
1542
+ const body = temp.flatMap( ( id ) => [
1543
+ { update: { _index: openSearch.trackerInput, _id: id } },
1544
+ { doc: updatedField },
1545
+ ] );
1546
+ const re= await bulkUpdate( body );
1547
+ logger.info( { getId: getId, re: re, storeId: inputData.storeId, fileDate: inputData.fileDate, function: 'tracking save - Employee' } );
1548
+ }
1499
1549
  }
1500
1550
  }
1501
1551
 
1502
1552
  // Customer Mapping
1503
1553
  if ( inputData?.customer?.length > 0 ) {
1504
- logger.info( { lenght: inputData?.customer?.length } );
1505
1554
  const filterCustomerData = await filteredCustomerMap( inputData.customer );
1506
1555
  const chunkedMappingData = await chunkArray( filterCustomerData, 10 );
1507
1556
 
1508
1557
  chunkedMappingData.map( async ( chunk ) => {
1509
- logger.info( { chunk: chunk } );
1510
-
1511
1558
  chunk.map( async ( item ) => {
1512
1559
  const imgName= item.img_name;
1513
1560
  const mappedId= item.mappedid.map( ( m ) => m.img_name );
1561
+ // customerCount=mappedId;
1514
1562
  const searchQuery = {
1515
1563
  size: 10000,
1516
1564
  _source: false, // do not fetch full source
@@ -1535,21 +1583,152 @@ export async function save( req, res ) {
1535
1583
  },
1536
1584
 
1537
1585
  };
1538
- const getData =await getOpenSearchData( 'tracker_process', searchQuery );
1586
+ const getData =await getOpenSearchData( openSearch.trackerInput, searchQuery );
1539
1587
  if ( getData?.body?.hits?.hits?.length > 0 ) {
1540
1588
  const temp = getData?.body?.hits?.hits?.map( ( item ) => item._id );
1541
1589
  const body = temp.flatMap( ( id ) => [
1542
- { update: { _index: 'tracker_process', _id: id } },
1590
+ { update: { _index: openSearch.trackerInput, _id: id } },
1543
1591
  { doc: { outputCluster: imgName, EmployeeStatusFinal: 1, isJunk: false } },
1544
1592
  ] );
1545
1593
  const re= await bulkUpdate( body );
1546
- logger.info( { re: re } );
1594
+ logger.info( { re: re, storeId: inputData.storeId, fileDate: inputData.fileDate, function: 'tracking save - customer' } );
1547
1595
  }
1548
1596
  } );
1549
1597
  } );
1550
1598
  }
1599
+
1600
+ if ( inputData.auditType == 'Audit' ) {
1601
+ // junk Mapping
1602
+ const getJunkQuery = {
1603
+ size: 10000,
1604
+ _source: false, // do not fetch full source
1605
+ stored_fields: [], // exclude all stored fields
1606
+ query: {
1607
+ bool: {
1608
+ must_not: {
1609
+ terms: {
1610
+ REIDCluster: junkCount, // how can i c=handle if incase junkCount === []
1611
+ },
1612
+ },
1613
+ must: [
1614
+ {
1615
+ term: { 'store_id.keyword': inputData.storeId },
1616
+ },
1617
+ {
1618
+ term: { 'date.keyword': inputData.fileDate },
1619
+ },
1620
+ {
1621
+ term: { 'module.keyword': 'CUSTOMER' },
1622
+ },
1623
+ ],
1624
+ },
1625
+ },
1626
+
1627
+ };
1628
+
1629
+ const getJunkId = await getOpenSearchData( openSearch.trackerInput, getJunkQuery );
1630
+ const junkIds = getJunkId?.body?.hits?.total?.value || null;
1631
+ if ( junkIds ) {
1632
+ const temp = getJunkId?.body?.hits?.hits?.map( ( item ) => item._id );
1633
+ const updatedField = { isJunk: false }; // your field updates
1634
+ const body = temp.flatMap( ( id ) => [
1635
+ { update: { _index: openSearch.trackerInput, _id: id } },
1636
+ { doc: updatedField },
1637
+ ] );
1638
+ const re= await bulkUpdate( body );
1639
+ logger.info( { getJunkId: getJunkId, re: re, storeId: inputData.storeId, auditType: inputData.auditType, fileDate: inputData.fileDate, function: 'tracking save - junk false mapping' } );
1640
+ }
1641
+
1642
+
1643
+ // Employee Mapping
1644
+ const getEmployeeQuery = {
1645
+ size: 10000,
1646
+ _source: false, // do not fetch full source
1647
+ stored_fields: [], // exclude all stored fields
1648
+ query: {
1649
+ bool: {
1650
+ must_not: {
1651
+ terms: {
1652
+ REIDCluster: empCount, // how can i c=handle if incase empCount === []
1653
+ },
1654
+ },
1655
+ must: [
1656
+ {
1657
+ term: { 'store_id.keyword': inputData.storeId },
1658
+ },
1659
+ {
1660
+ term: { 'date.keyword': inputData.fileDate },
1661
+ },
1662
+ {
1663
+ term: { 'module.keyword': 'CUSTOMER' },
1664
+ },
1665
+ {
1666
+ term: { EmployeeStatusProposed: 1 },
1667
+ },
1668
+ ],
1669
+ },
1670
+ },
1671
+
1672
+ };
1673
+
1674
+ const getEmpId = await getOpenSearchData( openSearch.trackerInput, getEmployeeQuery );
1675
+ const empIds = getEmpId?.body?.hits?.total?.value || null;
1676
+ if ( empIds ) {
1677
+ const temp = getEmpId?.body?.hits?.hits?.map( ( item ) => item._id );
1678
+ const updatedField = { EmployeeStatusFinal: 1 }; // your field updates
1679
+ const body = temp.flatMap( ( id ) => [
1680
+ { update: { _index: openSearch.trackerInput, _id: id } },
1681
+ { doc: updatedField },
1682
+ ] );
1683
+ const re= await bulkUpdate( body );
1684
+ logger.info( { getEmpId: getEmpId, re: re, storeId: inputData.storeId, auditType: inputData.auditType, fileDate: inputData.fileDate, function: 'tracking save - junk false mapping' } );
1685
+ }
1686
+
1687
+ // customer Mapping
1688
+ // const getCusQuery = {
1689
+ // size: 10000,
1690
+ // _source: true, // do not fetch full source
1691
+ // stored_fields: [], // exclude all stored fields
1692
+ // query: {
1693
+ // bool: {
1694
+ // must_not: {
1695
+ // terms: {
1696
+ // outputCluster: customerCount, // how can i c=handle if incase empCount === []
1697
+ // },
1698
+ // },
1699
+ // must: [
1700
+ // {
1701
+ // term: { 'store_id.keyword': inputData.storeId },
1702
+ // },
1703
+ // {
1704
+ // term: { 'date.keyword': inputData.fileDate },
1705
+ // },
1706
+ // {
1707
+ // term: { 'module.keyword': 'CUSTOMER' },
1708
+ // },
1709
+ // ],
1710
+ // },
1711
+ // },
1712
+
1713
+ // };
1714
+
1715
+ // const getCusId = await getOpenSearchData( openSearch.trackerInput, getCusQuery );
1716
+ // const cusIds = getCusId?.body?.hits?.total?.value || null;
1717
+ // if ( cusIds ) {
1718
+ // const temp = getCusId?.body?.hits?.hits; // .map( ( item ) => {id:item._id,reidCluster:item._source.REIDCluster } );
1719
+ // logger.info( { custoemrTemp: temp } );
1720
+ // // const updatedField = ; // your field updates
1721
+ // const body = temp.flatMap( ( id ) => [
1722
+ // { update: { _index: openSearch.trackerInput, _id: id._id } },
1723
+ // { doc: { outputCluster: id._source.REIDCluster } },
1724
+ // ] );
1725
+ // const re= await bulkUpdate( body );
1726
+ // logger.info( { getCusId: getCusId, re: re, storeId: inputData.storeId, auditType: inputData.auditType, fileDate: inputData.fileDate, function: 'tracking save - junk false mapping' } );
1727
+ // }
1728
+ }
1551
1729
  }
1552
1730
 
1731
+
1553
1732
  await updateOneUserAudit(
1554
1733
  { _id: new mongoose.Types.ObjectId( inputData.auditId ) },
1555
1734
  {
@@ -1679,33 +1858,82 @@ export async function save( req, res ) {
1679
1858
  timeSpent: inputData.timeSpent,
1680
1859
  },
1681
1860
  );
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() };
1861
+ const getQuery = [
1862
+ {
1863
+ $match: {
1864
+ $and: [
1865
+ {
1866
+ storeId: {
1867
+ $eq: inputData.storeId,
1706
1868
 
1869
+ },
1707
1870
 
1708
- await upsertOpenSearchData( 'tracker_process', `${inputData.storeId}_${inputData.fileDate}_23:00`, { doc: query } );
1871
+ },
1872
+ {
1873
+ fileDate: {
1874
+ $eq: inputData.fileDate,
1875
+ },
1876
+ },
1877
+ {
1878
+ moduleType: {
1879
+ $eq: 'track',
1880
+ },
1881
+ },
1882
+ ],
1883
+ },
1884
+ },
1885
+ {
1886
+ $project: {
1887
+ _id: 0,
1888
+ sqs: '$sqs.Body',
1889
+ },
1890
+ },
1891
+ ];
1892
+ const getBody = await aggregateAuditStoreData( getQuery );
1893
+ const streams = getBody? getBody?.sqs?.streams : null;
1894
+ const sqsProduceQueue = inputData.moduleType === 'track'?
1895
+ {
1896
+ QueueUrl: `${sqs.url}${sqs.trackOutput}`,
1897
+ MessageBody: JSON.stringify( {
1898
+ store_id: inputData.storeId,
1899
+ zone_id: [ 'traffic_zone' ],
1900
+ streams: streams || [],
1901
+ store_date: inputData.fileDate,
1902
+ bucket_name: 'tango-yolo-v2-output',
1903
+ time: '23:00',
1904
+ retry: 1,
1905
+ } ),
1906
+ MessageGroupId: 'my-group-1', // Required for FIFO queues
1907
+ MessageDeduplicationId: Date.now().toString(), // Or a UUID, if content-based deduplication is off
1908
+ }:
1909
+ {
1910
+ QueueUrl: ( auditConfig?.isCheckMinAfterCount ==1 && inputData.customerCount > auditConfig?.minAfterCount ) ? `${sqs.url}${sqs.feature_new}`: `${sqs.url}${sqs.feature}`,
1911
+ MessageBody: JSON.stringify( {
1912
+ store_id: inputData.storeId,
1913
+ store_date: inputData.fileDate,
1914
+ bucket_name: `${bucket.auditUploadBucket}`,
1915
+ zone_id: inputData.zoneName,
1916
+ process_type: 'audit',
1917
+ } ),
1918
+ };
1919
+ logger.info( { sqsProduceQueue: sqsProduceQueue } );
1920
+ const sqsQueue = inputData.moduleType === 'track'? await sendMessageToFIFOQueue( sqsProduceQueue ) : await sendMessageToQueue(
1921
+ sqsProduceQueue.QueueUrl,
1922
+ sqsProduceQueue.MessageBody,
1923
+
1924
+ );
1925
+
1926
+ logger.info( { sqsQueue: sqsQueue } );
1927
+ if ( inputData.moduleType === 'track' ) {
1928
+ const query =
1929
+ {
1930
+ module: 'REID-TRACKER-V1',
1931
+ store_date_time: `${inputData.storeId}_${inputData.fileDate}_23:00`,
1932
+ status: 'POSTPROCESS_AUDIT_SUCCESS',
1933
+ status_time: Math.floor( Date.now() / 1000 ),
1934
+ timestamp: new Date(),
1935
+ };
1936
+ await upsertOpenSearchData( openSearch.trackerInput, `${inputData.storeId}_${inputData.fileDate}_23:00`, { doc: query } );
1709
1937
  }
1710
1938
  }
1711
1939
 
@@ -1867,9 +2095,6 @@ export async function userAuditHistory( req, res ) {
1867
2095
  timezone: 'Asia/Kolkata',
1868
2096
  },
1869
2097
  },
1870
- // timeSpent: { $round: [
1871
- // { $divide: [ '$timeSpent', 60 ] }, 2,
1872
- // ] },
1873
2098
  timeSpent: {
1874
2099
 
1875
2100
  $cond: [
@@ -3253,7 +3478,7 @@ export async function reTrigger( req, res ) {
3253
3478
  };
3254
3479
 
3255
3480
 
3256
- await updateOpenSearchData( 'tracker_process', `${inputData.storeId}_${inputData.fileDate}_23:00`, { doc: updateQuery } );
3481
+ await updateOpenSearchData( openSearch.trackerInput, `${inputData.storeId}_${inputData.fileDate}_23:00`, { doc: updateQuery } );
3257
3482
  return res.sendSuccess( { result: 'The File has been Re-Triggered Succesfully' } );
3258
3483
  } catch ( error ) {
3259
3484
  const err = error.message;