tango-app-api-audit 1.0.52 → 1.0.54

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.
@@ -1022,7 +1022,7 @@ export async function workSpace( req, res ) {
1022
1022
  );
1023
1023
  let totalStores = 0;
1024
1024
  for ( let i = 0; i < merge.length; i++ ) {
1025
- totalStores = merge[i]?.totalFilesCount || 0;
1025
+ totalStores = merge[i]?.totalCount || 0;
1026
1026
 
1027
1027
  const pending = await listQueue( merge[i].queueName );
1028
1028
 
@@ -1041,7 +1041,7 @@ export async function workSpace( req, res ) {
1041
1041
  Number( merge[i].clientAsignedCount ) :
1042
1042
  0 ) :
1043
1043
  0,
1044
- totalCount: merge[i].totalFilesCount,
1044
+ totalCount: merge[i].totalCount,
1045
1045
  queueName: merge[i].queueName,
1046
1046
  isDraft: merge[i]?.isDrafted || false,
1047
1047
  isEnable:
@@ -1051,11 +1051,11 @@ export async function workSpace( req, res ) {
1051
1051
  ( merge[i].userInprogressCount && merge[i].userInprogressCount > 0 ) ?
1052
1052
  1 :
1053
1053
  0,
1054
- completedRatio: merge[i].totalFilesCount ?
1054
+ completedRatio: merge[i].totalCount ?
1055
1055
  merge[i].completedStoresCount ?
1056
1056
  Math.round(
1057
1057
  ( Number( merge[i].completedStoresCount ) /
1058
- Number( merge[i].totalFilesCount ) ) *
1058
+ Number( merge[i].totalCount ) ) *
1059
1059
  100,
1060
1060
  ) :
1061
1061
  0 :
@@ -1660,3 +1660,763 @@ export async function userAuditHistory( req, res ) {
1660
1660
  return res.sendError( err, 500 );
1661
1661
  }
1662
1662
  }
1663
+
1664
+ export async function clientMetrics( req, res ) {
1665
+ try {
1666
+ const inputData = req.body;
1667
+ const limit = inputData.limit || 10;
1668
+ const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
1669
+ const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
1670
+ let filter = [
1671
+ { fileDateISO: { $gte: dateRange.start } },
1672
+ { fileDateISO: { $lte: dateRange.end } },
1673
+ ];
1674
+
1675
+ let storeAuditFilter = [
1676
+ { $eq: [ '$clientId', '$$clientId' ] },
1677
+ { $eq: [ '$fileDate', '$$fileDate' ] },
1678
+
1679
+ ];
1680
+
1681
+ if ( inputData?.filterByModuleType?.length> 0 ) {
1682
+ filter.push(
1683
+ { moduleType: { $in: inputData.filterByModuleType } },
1684
+ );
1685
+ storeAuditFilter.push( {
1686
+ $in: [ '$moduleType', inputData.filterByModuleType ],
1687
+ } );
1688
+ }
1689
+
1690
+ if ( inputData?.filterByClient?.length> 0 ) {
1691
+ filter.push(
1692
+ { clientId: { $in: inputData.filterByClient } },
1693
+ );
1694
+ }
1695
+ const query = [
1696
+ {
1697
+ $match: {
1698
+ $and: filter,
1699
+ },
1700
+ },
1701
+ {
1702
+ $group: {
1703
+ _id: { clientId: '$clientId', fileDate: '$fileDate', moduleType: '$moduleType' },
1704
+ fileDate: { $last: '$fileDate' },
1705
+ clientId: { $last: '$clientId' },
1706
+ clientName: { $last: '$clientName' },
1707
+ moduleType: { $last: '$moduleType' },
1708
+ installedStore: { $last: '$installedStore' },
1709
+ queueName: { $last: '$queueName' },
1710
+ totalFilesCount: { $sum: 1 },
1711
+ },
1712
+ },
1713
+ {
1714
+ $project: {
1715
+ fileDate: 1,
1716
+ clientId: 1,
1717
+ clientName: 1,
1718
+ queueName: 1,
1719
+ installedStore: 1,
1720
+ totalFilesCount: 1,
1721
+ moduleType: 1,
1722
+ notAssignedCount: { $ifNull: [ 0, 0 ] },
1723
+ clientStatus: { $ifNull: [ '', '' ] },
1724
+ },
1725
+ },
1726
+ {
1727
+ $lookup: {
1728
+ 'from': 'storeAudit',
1729
+ 'let': { clientId: '$clientId', fileDate: '$fileDate', totalAuditFiles: '$totalFilesCount' },
1730
+ 'pipeline': [
1731
+
1732
+ {
1733
+ $match: {
1734
+ $expr: {
1735
+ $and: storeAuditFilter,
1736
+ },
1737
+ },
1738
+ },
1739
+ {
1740
+ $project: {
1741
+ completedStores: { $cond: [ { $eq: [ '$status', 'completed' ] }, 1, 0 ] },
1742
+ notAssignedStores: { $cond: [ { $eq: [ '$status', 'not_assign' ] }, 1, 0 ] },
1743
+ inprogressStores: { $cond: [ { $in: [ '$status', [ 'inprogress', 'drafted', 'assigned' ] ] }, 1, 0 ] },
1744
+ },
1745
+ },
1746
+ {
1747
+ $group: {
1748
+ _id: { clientId: '$clientId', fileDate: '$fileDate' },
1749
+ completedStores: { $sum: '$completedStores' },
1750
+ inprogressStores: { $sum: '$inprogressStores' },
1751
+ notAssignedStores: { $sum: '$notAssignedStores' },
1752
+ fileCount: { $sum: 1 },
1753
+ },
1754
+ },
1755
+ {
1756
+ $project: {
1757
+ _id: 0,
1758
+ completedStores: 1,
1759
+ inprogressStores: 1,
1760
+ notAssignedStores: 1,
1761
+ fileCount: 1,
1762
+ completionPercentage: { $ifNull: [
1763
+ {
1764
+ $round: [ {
1765
+ $multiply: [
1766
+ 100, {
1767
+ $divide: [
1768
+ '$completedStores', '$$totalAuditFiles',
1769
+ ],
1770
+ },
1771
+ ],
1772
+ }, 1 ],
1773
+
1774
+ },
1775
+ 0,
1776
+ ],
1777
+ },
1778
+ },
1779
+ },
1780
+ ], 'as': 'storeAudit',
1781
+ },
1782
+ },
1783
+ {
1784
+ $unwind: {
1785
+ path: '$storeAudit',
1786
+ preserveNullAndEmptyArrays: true,
1787
+ },
1788
+ },
1789
+ {
1790
+ $project: {
1791
+ _id: 0,
1792
+ fileDate: 1,
1793
+ clientId: 1,
1794
+ clientName: 1,
1795
+ totalFilesCount: 1,
1796
+ moduleType: 1,
1797
+ installedStore: 1,
1798
+ // notAssignedStores: '$storeAudit.notAssignedStores',
1799
+ inprogressStoresCount: '$storeAudit.inprogressStores',
1800
+ // fileCount: '$storeAudit.fileCount',
1801
+ notAssignedCount: 1,
1802
+ clientStatus: { $cond: [ { $eq: [ '$storeAudit.fileCount', 0 ] }, 'not-taken', { $cond: [ { $or: [ { $gt: [ '$notAssignedCount', 0 ] }, { $gt: [ '$storeAudit.notAssignedStores', 0 ] } ] }, 'pending', 'completed' ] } ] },
1803
+ completedStores: '$storeAudit.completedStores',
1804
+ completionPercentage: '$storeAudit.completionPercentage',
1805
+
1806
+ },
1807
+ },
1808
+ ];
1809
+
1810
+ if ( inputData?.filterByStatus?.length> 0 ) {
1811
+ query.push(
1812
+ {
1813
+ $match: { clientStatus: { $in: inputData.filterByStatus } },
1814
+ },
1815
+
1816
+ );
1817
+ }
1818
+
1819
+ if ( inputData.searchValue && inputData.searchValue!== '' ) {
1820
+ query.push( {
1821
+ $match: {
1822
+ $or: [
1823
+ { clientId: { $regex: inputData.searchValue, $options: 'i' } },
1824
+ { clientName: { $regex: inputData.searchValue, $options: 'i' } },
1825
+ { moduleType: { $regex: inputData.searchValue, $options: 'i' } },
1826
+ { clientStatus: { $regex: inputData.searchValue, $options: 'i' } },
1827
+ ],
1828
+
1829
+ },
1830
+ } );
1831
+ }
1832
+ if ( inputData.sortColumnName ) {
1833
+ const sortBy = inputData.sortBy || -1;
1834
+ query.push( {
1835
+ $sort: { [inputData.sortColumnName]: sortBy },
1836
+ },
1837
+ );
1838
+ }
1839
+
1840
+ const count = await aggregateAuditStoreDataSchema( query );
1841
+ if ( count.length == 0 ) {
1842
+ return res.sendError( 'No Data Found', 204 );
1843
+ }
1844
+
1845
+ query.push(
1846
+ { $skip: offset },
1847
+ { $limit: limit },
1848
+ );
1849
+ if ( inputData.isExport ) {
1850
+ query.push(
1851
+ {
1852
+ $project: {
1853
+ '_id': 0,
1854
+ 'File Date': '$fileDate',
1855
+ 'Client Name': '$clientName',
1856
+ 'Client Id': '$clientId',
1857
+ 'Module Type': '$moduleType',
1858
+ 'Completed Percentage': '$completionPercentage',
1859
+ 'Installed Stores': '$installedStore',
1860
+ 'Audit Stores': '$totalFilesCount',
1861
+ 'Inprogress Stores': '$inprogressStoresCount',
1862
+ 'Not Assigned': '$notAssignedCount',
1863
+ // 'Not Assigned Stores': '$notAssignedStores',
1864
+ 'Completed': '$completedStores',
1865
+ 'Status': '$clientStatus',
1866
+ },
1867
+ },
1868
+ );
1869
+ }
1870
+
1871
+ logger.info( { query: query } );
1872
+ const result = await aggregateAuditStoreDataSchema( query );
1873
+ if ( result.length == 0 ) {
1874
+ return res.sendError( 'No Data Found', 204 );
1875
+ }
1876
+ if ( inputData.isExport ) {
1877
+ await download( result, res );
1878
+ return;
1879
+ }
1880
+ return res.sendSuccess( { result: result, count: count.length } );
1881
+ } catch ( error ) {
1882
+ const err = error.error || 'Internal Server Error';
1883
+ logger.info( { error: error, message: req.body, function: 'clientMetrics' } );
1884
+ return res.sendError( err, 500 );
1885
+ }
1886
+ }
1887
+
1888
+ export async function storeMetrics( req, res ) {
1889
+ try {
1890
+ const inputData = req.body;
1891
+ const limit = inputData.limit || 10;
1892
+ const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
1893
+ const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
1894
+ let filter = [
1895
+ { fileDateISO: { $gte: dateRange.start } },
1896
+ { fileDateISO: { $lte: dateRange.end } },
1897
+
1898
+ ];
1899
+ if ( inputData.filterByClientId && inputData.filterByClientId?.length > 0 ) {
1900
+ filter.push( { clientId: { $in: inputData.filterByClientId } } );
1901
+ }
1902
+
1903
+ if ( inputData.filterByStoreId && inputData.filterByStoreId?.length > 0 ) {
1904
+ filter.push( { storeId: { $in: inputData.filterByStoreId } } );
1905
+ }
1906
+
1907
+ if ( inputData.filterByStatus && inputData.filterByStatus?.length > 0 ) {
1908
+ filter.push( { status: { $in: inputData.filterByStatus } } );
1909
+ }
1910
+
1911
+ if ( inputData.filterByAuditType && inputData.filterByAuditType?.length > 0 ) {
1912
+ filter.push( { auditType: { $in: inputData.filterByAuditType } } );
1913
+ }
1914
+
1915
+ if ( inputData.filterByModuleType && inputData.filterByModuleType?.length > 0 ) {
1916
+ filter.push( { moduleType: { $in: inputData.filterByModuleType } } );
1917
+ }
1918
+
1919
+
1920
+ const query = [
1921
+ {
1922
+ $match: {
1923
+ $and: filter,
1924
+ },
1925
+ },
1926
+
1927
+ {
1928
+ $lookup: {
1929
+ from: 'stores',
1930
+ let: { storeId: '$storeId' },
1931
+ pipeline: [
1932
+ {
1933
+ $match: {
1934
+ $expr: {
1935
+ $eq: [ '$storeId', '$$storeId' ],
1936
+ },
1937
+ },
1938
+ },
1939
+ {
1940
+ $project: {
1941
+ _id: 0,
1942
+ storeName: 1,
1943
+ },
1944
+ },
1945
+ ], as: 'stores',
1946
+ },
1947
+ },
1948
+ {
1949
+ $unwind: {
1950
+ path: '$stores',
1951
+ preserveNullAndEmptyArrays: true,
1952
+ },
1953
+ },
1954
+ {
1955
+ $project: {
1956
+ _id: 0,
1957
+ fileDate: 1,
1958
+ storeId: 1,
1959
+ storeName: '$store.storeName',
1960
+ clientName: '',
1961
+ clientId: 1,
1962
+ zoneName: 1,
1963
+ moduleType: 1,
1964
+ userId: {
1965
+ $arrayElemAt: [ '$userId', { $subtract: [ { $size: '$userId' }, 1 ] } ],
1966
+ },
1967
+ auditType: 1,
1968
+ beforeCount: 1,
1969
+ afterCount: { $ifNull: [ '$afterCount', null ] },
1970
+ accuracy: { $round: [
1971
+ { $divide: [
1972
+ { $multiply: [ { $ifNull: [ '$afterCount', 0 ] }, '$beforeCount' ] }, 100,
1973
+ ] }, 2,
1974
+ ],
1975
+
1976
+ },
1977
+ timeSpent: 1,
1978
+ status: 1,
1979
+ },
1980
+ },
1981
+ {
1982
+ $lookup: {
1983
+ from: 'users',
1984
+ let: { userId: '$userId' },
1985
+ pipeline: [
1986
+ {
1987
+ $match: {
1988
+ $expr: {
1989
+ $eq: [ '$_id', '$$userId' ],
1990
+ },
1991
+ },
1992
+ },
1993
+ {
1994
+ $project: {
1995
+ _id: 0,
1996
+ userName: 1,
1997
+ userEmail: '$email',
1998
+ },
1999
+ },
2000
+ ], as: 'users',
2001
+ },
2002
+ },
2003
+ {
2004
+ $unwind: {
2005
+ path: '$users',
2006
+ preserveNullAndEmptyArrays: true,
2007
+ },
2008
+ },
2009
+ {
2010
+ $lookup: {
2011
+ from: 'clients',
2012
+ let: { clientId: '$clientId' },
2013
+ pipeline: [
2014
+ {
2015
+ $match: {
2016
+ $expr: {
2017
+ $eq: [ '$clientId', '$$clientId' ],
2018
+ },
2019
+ },
2020
+ },
2021
+ {
2022
+ $project: {
2023
+ _id: 0,
2024
+ clientName: 1,
2025
+ },
2026
+ },
2027
+ ], as: 'client',
2028
+ },
2029
+ },
2030
+ {
2031
+ $unwind: {
2032
+ path: '$client',
2033
+ preserveNullAndEmptyArrays: true,
2034
+ },
2035
+ },
2036
+ {
2037
+ $project: {
2038
+ _id: 0,
2039
+ fileDate: 1,
2040
+ storeId: 1,
2041
+ storeName: '$stores.storeName',
2042
+ userName: '$users.userName',
2043
+ userEmail: '$users.userEmail',
2044
+ clientId: 1,
2045
+ clientName: '$client.clientName',
2046
+ zoneName: 1,
2047
+ moduleType: 1,
2048
+ auditType: 1,
2049
+ beforeCount: 1,
2050
+ afterCount: { $ifNull: [ '$afterCount', null ] },
2051
+ accuracy: { $round: [
2052
+ { $divide: [
2053
+ { $multiply: [ { $ifNull: [ '$afterCount', 0 ] }, '$beforeCount' ] }, 100,
2054
+ ] }, 1,
2055
+ ],
2056
+
2057
+ },
2058
+ timeSpent: {
2059
+
2060
+ $cond: [
2061
+ { $lt: [ '$timeSpent', 60 ] }, // Case 1: If less than 60 seconds
2062
+ { $concat: [ { $toString: '$timeSpent' }, ' sec' ] },
2063
+ {
2064
+ $cond: [
2065
+ { $lt: [ '$timeSpent', 3600 ] }, // Case 2: If less than 60 minutes (3600 seconds)
2066
+ {
2067
+ $concat: [
2068
+ { $toString: { $round: [ { $divide: [ '$timeSpent', 60 ] }, 1 ] } }, // Convert to minutes
2069
+ ' min',
2070
+ ],
2071
+ },
2072
+ {
2073
+ $concat: [
2074
+ { $toString: { $round: [ { $divide: [ '$timeSpent', 3600 ] }, 1 ] } }, // Convert to hours
2075
+ ' hr',
2076
+ ],
2077
+ },
2078
+ ],
2079
+ },
2080
+ ],
2081
+
2082
+ },
2083
+ status: 1,
2084
+ },
2085
+ },
2086
+
2087
+ ];
2088
+
2089
+ if ( inputData.searchValue && inputData.searchValue !== '' ) {
2090
+ query.push( {
2091
+ $match: {
2092
+ $or: [
2093
+ { clientId: { $regex: inputData.searchValue, $options: 'i' } },
2094
+ { clientName: { $regex: inputData.searchValue, $options: 'i' } },
2095
+ { storeId: { $regex: inputData.searchValue, $options: 'i' } },
2096
+ { storeName: { $regex: inputData.searchValue, $options: 'i' } },
2097
+ { userName: { $regex: inputData.searchValue, $options: 'i' } },
2098
+ { status: { $regex: inputData.searchValue, $options: 'i' } },
2099
+ { auditType: { $regex: inputData.searchValue, $options: 'i' } },
2100
+ { moduleType: { $regex: inputData.searchValue, $options: 'i' } },
2101
+ ],
2102
+ },
2103
+ } );
2104
+ }
2105
+
2106
+ if ( inputData.sortColumnName ) {
2107
+ const sortBy = inputData.sortBy || -1;
2108
+ query.push( {
2109
+ $sort: { [inputData.sortColumName]: sortBy },
2110
+ },
2111
+ );
2112
+ }
2113
+ const count = await aggregateStoreAudit( query );
2114
+
2115
+ if ( count.length == 0 ) {
2116
+ return res.sendError( 'No data Found', 204 );
2117
+ }
2118
+ if ( !inputData.isExport ) {
2119
+ query.push( {
2120
+ $skip: offset,
2121
+ },
2122
+ {
2123
+ $limit: limit,
2124
+ } );
2125
+ } else {
2126
+ query.push( { $limit: 10000 } );
2127
+ }
2128
+ const result = await aggregateStoreAudit( query );
2129
+ if ( inputData.isExport ) {
2130
+ const exportdata = [];
2131
+ result.forEach( ( element ) => {
2132
+ exportdata.push( {
2133
+ 'File Date': element.fileDate,
2134
+ 'Store Id': element.storeId,
2135
+ 'Store Name': element.storeName,
2136
+ 'Client Id': element.clientId,
2137
+ 'Client Name': element.clientName,
2138
+ 'User Name': element.userName,
2139
+ 'User Email': element.userEmail,
2140
+ 'Audit Type': element.auditType,
2141
+ 'Accuracy': element.accuracy,
2142
+ 'Time Spent': element.timeSpent,
2143
+ 'Status': element.status,
2144
+ } );
2145
+ } );
2146
+ await download( exportdata, res );
2147
+ return;
2148
+ }
2149
+ return res.sendSuccess( { result: result, count: count.length } );
2150
+ } catch ( error ) {
2151
+ const err = error.message || 'Internal Server Error';
2152
+ logger.error( { error: error, data: req.body, function: 'storeMetrics' } );
2153
+ return res.sendError( err, 500 );
2154
+ }
2155
+ }
2156
+
2157
+ export async function userMetrics( req, res ) {
2158
+ try {
2159
+ const inputData = req.body;
2160
+ const limit = inputData.limit || 10;
2161
+ const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
2162
+ const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
2163
+ let filter = [];
2164
+ if ( inputData.dateType == 'auditedDate' ) {
2165
+ filter.push(
2166
+ { updatedAt: { $gte: dateRange.start } },
2167
+ { updatedAt: { $lte: dateRange.end } },
2168
+
2169
+ );
2170
+ } else {
2171
+ filter.push(
2172
+ { fileDateISO: { $gte: dateRange.start } },
2173
+ { fileDateISO: { $lte: dateRange.end } },
2174
+
2175
+ );
2176
+ }
2177
+ if ( inputData?.filterByModuleType?.length> 0 ) {
2178
+ filter.push(
2179
+ { moduleType: { $in: inputData.filterByModuleType } },
2180
+ );
2181
+ }
2182
+ if ( inputData?.filterByStatus?.length> 0 ) {
2183
+ filter.push(
2184
+ { auditStatus: { $in: inputData.filterByStatus } },
2185
+ );
2186
+ }
2187
+ if ( inputData?.filterByUser?.length> 0 ) {
2188
+ const temp = inputData.filterByUser.map( ( item ) => new mongoose.Types.ObjectId( item ) );
2189
+ logger.info( { temp: temp } );
2190
+ filter.push(
2191
+ { userId: { $in: temp } },
2192
+ );
2193
+ }
2194
+
2195
+ const query = [
2196
+ {
2197
+ $match: {
2198
+ $and: filter,
2199
+ },
2200
+ },
2201
+ {
2202
+ $project: {
2203
+ userId: 1,
2204
+ fileDate: 1,
2205
+ startTime: 1,
2206
+ endTime: 1,
2207
+ totalCompletedFiles: { $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, 1, 0 ] },
2208
+ beforeCount: 1,
2209
+ afterCount: { $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, '$afterCount', 0 ] },
2210
+ },
2211
+ },
2212
+ {
2213
+ $group: {
2214
+ _id: { userId: '$userId', fileDate: '$fileDate' },
2215
+ userId: { $first: '$userId' },
2216
+ fileDate: { $first: '$fileDate' },
2217
+ totalCompletedFiles: { $sum: '$totalCompletedFiles' },
2218
+ beforeCount: { $sum: '$beforeCount' },
2219
+ afterCount: { $sum: '$afterCount' },
2220
+ checkIntime: { $first: '$startTime' },
2221
+ checkOutTime: { $last: '$endTime' },
2222
+
2223
+ },
2224
+ },
2225
+ {
2226
+ $project: {
2227
+ _id: 0,
2228
+ userId: 1,
2229
+ fileDate: 1,
2230
+ totalCompletedFiles: 1,
2231
+ beforeCount: 1,
2232
+ afterCount: 1,
2233
+ mappingPerc: { $round: [
2234
+ { $multiply: [
2235
+ { $divide: [ '$afterCount', '$beforeCount' ] }, 100,
2236
+ ] }, 2,
2237
+ ] },
2238
+ checkIntime: 1,
2239
+ checkOutTime: 1,
2240
+ differenceInSeconds: {
2241
+ $divide: [ { $subtract: [ '$checkOutTime', '$checkIntime' ] }, 1000 ],
2242
+ },
2243
+ },
2244
+ },
2245
+ {
2246
+ $project: {
2247
+ userId: 1,
2248
+ fileDate: 1,
2249
+ totalCompletedFiles: 1,
2250
+ beforeCount: 1,
2251
+ afterCount: 1,
2252
+ checkIntime: 1,
2253
+ checkOutTime: 1,
2254
+ mappingPerc: 1,
2255
+ hours: { $floor: { $divide: [ '$differenceInSeconds', 3600 ] } },
2256
+ minutes: { $floor: { $divide: [ { $mod: [ '$differenceInSeconds', 3600 ] }, 60 ] } },
2257
+ seconds: { $mod: [ '$differenceInSeconds', 60 ] },
2258
+ },
2259
+ },
2260
+ {
2261
+ $project: {
2262
+ event: 1,
2263
+ userId: 1,
2264
+ fileDate: 1,
2265
+ totalCompletedFiles: 1,
2266
+ beforeCount: 1,
2267
+ afterCount: 1,
2268
+ checkIntime: 1,
2269
+ checkOutTime: 1,
2270
+ hours: {
2271
+ $cond: {
2272
+ if: { $lt: [ '$hours', 10 ] },
2273
+ then: { $concat: [ '0', { $toString: { $round: [ '$hours', 0 ] } } ] },
2274
+ else: { $toString: { $round: [ '$hours', 0 ] } },
2275
+ },
2276
+ },
2277
+ minutes: {
2278
+ $cond: {
2279
+ if: { $lt: [ '$minutes', 10 ] },
2280
+ then: { $concat: [ '0', { $toString: { $round: [ '$minutes', 0 ] } } ] },
2281
+ else: { $toString: { $round: [ '$minutes', 0 ] } },
2282
+ },
2283
+ },
2284
+ seconds: {
2285
+ $cond: {
2286
+ if: { $lt: [ '$seconds', 10 ] },
2287
+ then: { $concat: [ '0', { $toString: { $round: [ '$seconds', 0 ] } } ] },
2288
+ else: { $toString: { $round: [ '$seconds', 0 ] } },
2289
+ },
2290
+ },
2291
+
2292
+ },
2293
+ },
2294
+ {
2295
+ $project: {
2296
+ event: 1,
2297
+ userId: 1,
2298
+ fileDate: 1,
2299
+ totalCompletedFiles: 1,
2300
+ beforeCount: 1,
2301
+ afterCount: 1,
2302
+ checkIntime: 1,
2303
+ zoneName: 1,
2304
+ checkOutTime: 1,
2305
+ workingHours: {
2306
+ $concat: [ '$hours', ':', '$minutes', ':', '$seconds' ],
2307
+ },
2308
+ },
2309
+ },
2310
+ {
2311
+ $lookup: {
2312
+ from: 'users',
2313
+ let: { userId: '$userId' },
2314
+ pipeline: [
2315
+ {
2316
+ $match: {
2317
+ $expr: {
2318
+ $eq: [ '$_id', '$$userId' ],
2319
+ },
2320
+ },
2321
+ },
2322
+ {
2323
+ $project: {
2324
+ _id: 0,
2325
+ userName: 1,
2326
+ },
2327
+ },
2328
+ ], as: 'userInfo',
2329
+ },
2330
+ },
2331
+ {
2332
+ $unwind: {
2333
+ path: '$userInfo', preserveNullAndEmptyArrays: true,
2334
+ },
2335
+ },
2336
+ {
2337
+ $project: {
2338
+ event: 1,
2339
+ userName: '$userInfo.userName',
2340
+ userId: 1,
2341
+ fileDate: 1,
2342
+ totalCompletedFiles: 1,
2343
+ beforeCount: 1,
2344
+ afterCount: 1,
2345
+ checkIntime: {
2346
+ $dateToString: {
2347
+ format: '%Y-%m-%d %H:%M:%S',
2348
+ date: '$checkIntime',
2349
+ timezone: 'Asia/Kolkata',
2350
+ },
2351
+ },
2352
+ checkOutTime: {
2353
+ $dateToString: {
2354
+ format: '%Y-%m-%d %H:%M:%S',
2355
+ date: '$checkOutTime',
2356
+ timezone: 'Asia/Kolkata',
2357
+ },
2358
+ },
2359
+ workingHours: 1,
2360
+ },
2361
+ },
2362
+ ];
2363
+ if ( inputData.searchValue && inputData.searchValue!== '' ) {
2364
+ query.push( {
2365
+ $match: {
2366
+ $or: [
2367
+ { userName: { $regex: inputData.searchValue, $options: 'i' } },
2368
+ { clientName: { $regex: inputData.searchValue, $options: 'i' } },
2369
+ ],
2370
+
2371
+ },
2372
+ } );
2373
+ }
2374
+ if ( inputData.sortColumnName ) {
2375
+ const sortBy = inputData.sortBy || -1;
2376
+ query.push( {
2377
+ $sort: { [inputData.sortColumName]: sortBy },
2378
+ },
2379
+ );
2380
+ }
2381
+
2382
+ const count = await aggregateUserAudit( query );
2383
+ if ( count.length == 0 ) {
2384
+ return res.sendError( 'No Data Found', 204 );
2385
+ }
2386
+
2387
+ query.push(
2388
+ { $skip: offset },
2389
+ { $limit: limit },
2390
+ );
2391
+ if ( inputData.isExport ) {
2392
+ query.push(
2393
+ {
2394
+ $project: {
2395
+ '_id': 0,
2396
+ 'File Date': '$fileDate',
2397
+ 'User Name': '$userName',
2398
+ 'User Id': '$userId',
2399
+ 'Total Completed Files Count': '$totalCompletedFiles',
2400
+ 'Total efore Count': '$beforeCount',
2401
+ 'Total After Count': '$afterCount',
2402
+ 'check Intime': '$checkIntime',
2403
+ 'check OutTime': '$checkOutTime',
2404
+ 'working Hours': '$workingHours',
2405
+ },
2406
+ },
2407
+ );
2408
+ }
2409
+
2410
+
2411
+ const result = await aggregateUserAudit( query );
2412
+ if ( inputData.isExport ) {
2413
+ await download( result, res );
2414
+ return;
2415
+ }
2416
+ return res.sendSuccess( { result: result, count: count.length } );
2417
+ } catch ( error ) {
2418
+ const err = error.error || 'Internal Server Error';
2419
+ logger.info( { error: error, message: req.body, function: 'clientMetrics' } );
2420
+ return res.sendError( err, 500 );
2421
+ }
2422
+ }