tango-app-api-audit 1.0.53 → 1.0.55

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.
@@ -39,7 +39,7 @@ import {
39
39
  createAuditLog,
40
40
  findOneAuditLog,
41
41
  } from '../service/auditLog.service.js';
42
- import { aggregateStore, findOneStore } from '../service/store.service.js';
42
+ import { aggregateStore, countDocumentsStore, findOneStore } from '../service/store.service.js';
43
43
  import {
44
44
  mapCustomer,
45
45
  mapEmployee,
@@ -1884,3 +1884,1006 @@ export async function clientMetrics( req, res ) {
1884
1884
  return res.sendError( err, 500 );
1885
1885
  }
1886
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
+ }
2423
+
2424
+ export async function overViewTable( req, res ) {
2425
+ try {
2426
+ const inputData = req.body;
2427
+ const limit = inputData.limit || 10;
2428
+ const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
2429
+ const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
2430
+ let filters =[
2431
+ { fileDateISO: { $gte: dateRange.start } },
2432
+ { fileDateISO: { $lte: dateRange.end } },
2433
+ { auditStatus: { $in: inputData.filterByStatus } },
2434
+
2435
+ ];
2436
+
2437
+ if ( inputData.clientId.length > 0 ) {
2438
+ filters.push( { clientId: { $in: inputData.clientId } } );
2439
+ }
2440
+
2441
+ if ( inputData.filterByModuleType.length > 0 ) {
2442
+ filters.push( { moduleType: { $in: inputData.filterByModuleType } } );
2443
+ }
2444
+
2445
+ if ( inputData.filterByAuditType.length > 0 ) {
2446
+ filters.push( { auditType: { $in: inputData.filterByAuditType } } );
2447
+ }
2448
+ const query =[
2449
+ {
2450
+ $match: {
2451
+ $and: filters,
2452
+ },
2453
+
2454
+ },
2455
+ {
2456
+ $project: {
2457
+ _id: 0,
2458
+ storeId: 1,
2459
+ zoneName: 1,
2460
+ fileDate: 1,
2461
+ moduleType: 1,
2462
+ beforeCount: 1,
2463
+ startTime: 1,
2464
+ auditType: 1,
2465
+ auditStatus: 1,
2466
+ userId: 1,
2467
+ },
2468
+ },
2469
+ {
2470
+ $lookup: {
2471
+ from: 'users',
2472
+ let: { userId: '$userId' },
2473
+ pipeline: [
2474
+ {
2475
+ $match: {
2476
+ $expr: {
2477
+ $eq: [ '$_id', '$$userId' ],
2478
+ },
2479
+ },
2480
+ },
2481
+ {
2482
+ $project: {
2483
+ userName: 1,
2484
+ userEmail: '$email',
2485
+ },
2486
+ },
2487
+ ], as: 'user',
2488
+ },
2489
+ },
2490
+ {
2491
+ $unwind: {
2492
+ path: '$user',
2493
+ preserveNullAndEmptyArrays: true,
2494
+ },
2495
+ },
2496
+ {
2497
+ $project: {
2498
+ storeId: 1,
2499
+ zoneName: 1,
2500
+ fileDate: 1,
2501
+ moduleType: 1,
2502
+ beforeCount: 1,
2503
+ startTime: 1,
2504
+ auditType: 1,
2505
+ auditStatus: 1,
2506
+ userName: '$user.userName',
2507
+ userEmail: '$user.userEmail',
2508
+
2509
+ },
2510
+ },
2511
+ ];
2512
+ if ( inputData.searchValue && inputData.searchValue!== '' ) {
2513
+ query.push( {
2514
+ $match: {
2515
+ $or: [
2516
+ { storeId: { $regex: inputData.searchValue, $options: 'i' } },
2517
+ { fileDate: { $regex: inputData.searchValue, $options: 'i' } },
2518
+ { auditType: { $regex: inputData.searchValue, $options: 'i' } },
2519
+ { auditStatus: { $regex: inputData.searchValue, $options: 'i' } },
2520
+ { userName: { $regex: inputData.searchValue, $options: 'i' } },
2521
+ { userEmail: { $regex: inputData.searchValue, $options: 'i' } },
2522
+ ],
2523
+
2524
+ },
2525
+ } );
2526
+ }
2527
+ if ( inputData.sortColumnName ) {
2528
+ const sortBy = inputData.sortBy || -1;
2529
+ query.push( {
2530
+ $sort: { [inputData.sortColumName]: sortBy },
2531
+ },
2532
+ );
2533
+ }
2534
+ const count = await aggregateUserAudit( query );
2535
+ if ( count.length == 0 ) {
2536
+ return res.sendError( 'No Data Found', 204 );
2537
+ }
2538
+
2539
+ if ( inputData.isExport ) {
2540
+ query.push( { $limit: 10000 } );
2541
+ } else {
2542
+ query.push( {
2543
+ $skip: offset,
2544
+ },
2545
+ {
2546
+ $limit: limit,
2547
+ } );
2548
+ }
2549
+
2550
+ const result = await aggregateUserAudit( query );
2551
+ if ( inputData.isExport ) {
2552
+ const exportdata = [];
2553
+ result.forEach( ( element ) => {
2554
+ exportdata.push( {
2555
+ 'Store Id': element.storeId,
2556
+ 'zoneName': element.zoneName,
2557
+ 'File Date': element.fileDate,
2558
+ 'Module Type': element.moduleType,
2559
+ 'Audit Type': element.auditType,
2560
+ 'Before Count': element.beforeCount,
2561
+ 'Start Time': element.startTime,
2562
+ 'User Name': element.userName,
2563
+ 'User Email': element.userEmail,
2564
+ 'Status': element.auditStatus,
2565
+ } );
2566
+ } );
2567
+ await download( exportdata, res );
2568
+ return;
2569
+ }
2570
+ return res.sendSuccess( { result: result, count: count.length } );
2571
+ } catch ( error ) {
2572
+ const err = error.message || 'Internal Server Error';
2573
+ logger.error( { error: error, message: req.body, function: 'summarySplit' } );
2574
+ return res.sendError( err, 500 );
2575
+ }
2576
+ }
2577
+
2578
+ export async function overAllAuditSummary( req, res ) {
2579
+ try {
2580
+ const inputData = req.body;
2581
+ let temp = {
2582
+ installedStores: 0,
2583
+ auditStores: 0,
2584
+ inprogreseStores: 0,
2585
+ completedStores: 0,
2586
+ auditInprogress: 0,
2587
+ reAuditInprogress: 0,
2588
+ draft: 0,
2589
+ assigned: 0,
2590
+ notAssigned: 0,
2591
+ auditCompleted: 0,
2592
+ reAuditCompleted: 0,
2593
+
2594
+ };
2595
+ const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
2596
+ let filters =[
2597
+ { fileDateISO: { $gte: dateRange.start } },
2598
+ { fileDateISO: { $lte: dateRange.end } },
2599
+ { moduleType: { $in: inputData.moduleType },
2600
+ },
2601
+ ];
2602
+ if ( inputData.clientId.length > 0 ) {
2603
+ filters.push( { clientId: { $in: inputData.clientId } } );
2604
+ }
2605
+ const storeQuery = inputData.clientId.length? { 'edge.firstFile': true, 'clientId': { $in: inputData. clientId } } : { 'edge.firstFile': true };
2606
+ temp.installedStores = await countDocumentsStore( storeQuery );
2607
+ const auditClientDataQuery =[
2608
+ {
2609
+ $match: {
2610
+ $and: filters,
2611
+ },
2612
+
2613
+ },
2614
+ {
2615
+ $group: {
2616
+ _id: null,
2617
+ totalFilesCount: { $sum: 1 },
2618
+ },
2619
+ },
2620
+ {
2621
+ $project: {
2622
+ _id: 0,
2623
+ totalFilesCount: 1,
2624
+ },
2625
+ },
2626
+ ];
2627
+ const auditClientData = await aggregateAuditStoreDataSchema( auditClientDataQuery );
2628
+ if ( auditClientData.length >0 ) {
2629
+ temp.auditStores = auditClientData[0].totalFilesCount;
2630
+ }
2631
+ const storeAuditQuery =[
2632
+ {
2633
+ $match: {
2634
+ $and: filters,
2635
+ },
2636
+
2637
+ },
2638
+ {
2639
+ $project: {
2640
+ completedStores: { $cond: [ { $eq: [ '$status', 'completed' ] }, 1, 0 ] },
2641
+ auditInprogress: { $cond: [ { $and: [ { $in: [ '$status', [ 'inprogress' ] ] }, { $eq: [ '$auditType', 'Audit' ] } ] }, 1, 0 ] },
2642
+ reAuditInprogress: { $cond: [ { $and: [ { $in: [ '$status', [ 'inprogress' ] ] }, { $eq: [ '$auditType', 'ReAudit' ] } ] }, 1, 0 ] },
2643
+ draft: { $cond: [ { $eq: [ '$status', 'drafted' ] }, 1, 0 ] },
2644
+ assigned: { $cond: [ { $eq: [ '$status', 'drafted' ] }, 1, 0 ] },
2645
+ exceptNoAssigned: { $cond: [ { $in: [ '$status', [ 'skipped', 'not_assign' ] ] }, 1, 0 ] },
2646
+ auditCompleted: { $cond: [ { $and: [ { $in: [ '$status', [ 'completed' ] ] }, { $eq: [ '$auditType', 'Audit' ] } ] }, 1, 0 ] },
2647
+ reAuditCompleted: { $cond: [ { $and: [ { $in: [ '$status', [ 'completed' ] ] }, { $eq: [ '$auditType', 'ReAudit' ] } ] }, 1, 0 ] },
2648
+
2649
+
2650
+ },
2651
+ },
2652
+ {
2653
+ $group: {
2654
+ _id: null,
2655
+ completedStores: { $sum: '$completedStores' },
2656
+ auditInprogress: { $sum: '$auditInprogress' },
2657
+ reAuditInprogress: { $sum: '$reAuditInprogress' },
2658
+ draft: { $sum: '$draft' },
2659
+ assigned: { $sum: '$assigned' },
2660
+ exceptNoAssigned: { $sum: '$exceptNoAssigned' },
2661
+ auditCompleted: { $sum: '$auditCompleted' },
2662
+ reAuditCompleted: { $sum: '$reAuditCompleted' },
2663
+ },
2664
+ },
2665
+ {
2666
+ $project: {
2667
+ completedStores: 1,
2668
+ inprogressStores: { $subtract: [ temp.auditStores, '$completedStores' ] },
2669
+ auditInprogress: 1,
2670
+ reAuditInprogress: 1,
2671
+ draft: 1,
2672
+ assigned: 1,
2673
+ exceptNoAssigned1: 1,
2674
+ exceptNoAssigned: { $subtract: [ temp.auditStores, '$exceptNoAssigned' ] },
2675
+ auditCompleted: 1,
2676
+ reAuditCompleted: 1,
2677
+ },
2678
+ },
2679
+ ];
2680
+ const storeAudit = await aggregateStoreAudit( storeAuditQuery );
2681
+ if ( storeAudit.length > 0 ) {
2682
+ temp.inprogreseStores= storeAudit[0].inprogreseStores;
2683
+ temp.completedStores = storeAudit[0].completedStores;
2684
+ temp.auditInprogress = storeAudit[0].auditInprogress;
2685
+ temp.reAuditInprogress = storeAudit[0].reAuditInprogress;
2686
+ temp.draft = storeAudit[0].draft;
2687
+ temp.assigned = storeAudit[0].assigned;
2688
+ temp.notAssigned = storeAudit[0].exceptNoAssigned;
2689
+ temp.auditCompleted = storeAudit[0].auditCompleted;
2690
+ temp.reAuditCompleted = storeAudit[0].reAuditCompleted;
2691
+ }
2692
+
2693
+ return res.sendSuccess( { result: temp } );
2694
+ } catch ( error ) {
2695
+ const err = error.message || 'Internal Server Error';
2696
+ logger.error( { error: error, message: req.body, function: 'summarySplit' } );
2697
+ return res.sendError( err, 500 );
2698
+ }
2699
+ }
2700
+
2701
+ export async function overViewCard( req, res ) {
2702
+ try {
2703
+ const inputData = req.body;
2704
+ const temp = {
2705
+ totalCount: 0,
2706
+ auditFileCount: 0,
2707
+ ReAuditFileCount: 0,
2708
+ inprogressCount: 0,
2709
+ completedCount: 0,
2710
+ };
2711
+
2712
+ const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
2713
+ let filters =[
2714
+ { fileDateISO: { $gte: dateRange.start } },
2715
+ { fileDateISO: { $lte: dateRange.end } },
2716
+ ];
2717
+ if ( inputData.clientId.length > 0 ) {
2718
+ filters.push( { clientId: { $in: inputData.clientId } } );
2719
+ }
2720
+ const query =[
2721
+ {
2722
+ $match: {
2723
+ $and: filters,
2724
+ },
2725
+
2726
+ },
2727
+ {
2728
+ $project: {
2729
+ auditFileCount: {
2730
+ $cond: [
2731
+ { $eq: [ '$auditType', 'Audit' ] }, 1, 0,
2732
+ ],
2733
+ },
2734
+ ReAuditFileCount: {
2735
+ $cond: [
2736
+ { $eq: [ '$auditType', 'ReAudit' ] }, 1, 0,
2737
+ ],
2738
+ },
2739
+ inprogressCount: {
2740
+ $cond: [
2741
+ { $ne: [ '$status', 'completed' ] }, 1, 0,
2742
+ ],
2743
+ },
2744
+ completedCount: {
2745
+ $cond: [
2746
+ { $eq: [ '$status', 'completed' ] }, 1, 0,
2747
+ ],
2748
+ },
2749
+ },
2750
+ },
2751
+ {
2752
+ $group: {
2753
+ _id: { fileDate: '$fileDate', clientId: '$clientId' },
2754
+ totalCount: { $sum: 1 },
2755
+ auditFileCount: { $sum: '$auditFileCount' },
2756
+ ReAuditFileCount: { $sum: '$ReAuditFileCount' },
2757
+ inprogressCount: { $sum: '$inprogressCount' },
2758
+ completedCount: { $sum: '$completedCount' },
2759
+
2760
+ },
2761
+ },
2762
+ {
2763
+ $project: {
2764
+ _id: 0,
2765
+ totalCount: 1,
2766
+ auditFileCount: 1,
2767
+ ReAuditFileCount: 1,
2768
+ inprogressCount: 1,
2769
+ completedCount: 1,
2770
+ },
2771
+ },
2772
+ ];
2773
+ const result = await aggregateStoreAudit( query );
2774
+ logger.info( { result: result } );
2775
+ return res.sendSuccess( { result: result.length> 0? result[0]: temp } );
2776
+ } catch ( error ) {
2777
+ const err = error.message || 'Internal Server Error';
2778
+ logger.error( { error: error, message: req.body, function: 'overViewCard' } );
2779
+ return res.sendError( err, 500 );
2780
+ }
2781
+ }
2782
+
2783
+ export async function summarySplit( req, res ) {
2784
+ try {
2785
+ const inputData = req.body;
2786
+ const temp = {
2787
+ totalInprogress: 0,
2788
+ auditInprogress: 0,
2789
+ reAuditInprogress: 0,
2790
+ draft: 0,
2791
+ assigned: 0,
2792
+ auditCompleted: 0,
2793
+ reAuditCompleted: 0,
2794
+ completedCount: 0,
2795
+ };
2796
+
2797
+ const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
2798
+ let filters =[
2799
+ { fileDateISO: { $gte: dateRange.start } },
2800
+ { fileDateISO: { $lte: dateRange.end } },
2801
+ ];
2802
+ if ( inputData.clientId.length > 0 ) {
2803
+ filters.push( { clientId: { $in: inputData.clientId } } );
2804
+ }
2805
+ const query =[
2806
+ {
2807
+ $match: {
2808
+ $and: filters,
2809
+ },
2810
+
2811
+ },
2812
+ {
2813
+ $project: {
2814
+ auditInprogress: {
2815
+ $cond: [
2816
+ { $and: [ { $eq: [ '$auditType', 'Audit' ] }, { $eq: [ '$status', 'inprogress' ] } ] }, 1, 0,
2817
+ ],
2818
+ },
2819
+ reAuditInprogress: {
2820
+ $cond: [
2821
+ { $and: [ { $eq: [ '$auditType', 'ReAudit' ] }, { $eq: [ '$status', 'inprogress' ] } ] }, 1, 0,
2822
+ ],
2823
+ },
2824
+ draft: {
2825
+ $cond: [
2826
+ { $ne: [ '$status', 'drafted' ] }, 1, 0,
2827
+ ],
2828
+ },
2829
+ assigned: {
2830
+ $cond: [
2831
+ { $ne: [ '$status', 'assigned' ] }, 1, 0,
2832
+ ],
2833
+ },
2834
+ completedCount: {
2835
+ $cond: [
2836
+ { $eq: [ '$status', 'completed' ] }, 1, 0,
2837
+ ],
2838
+ },
2839
+ reAuditCompleted: {
2840
+ $cond: [
2841
+ { $and: [ { $eq: [ '$auditType', 'ReAudit' ] }, { $eq: [ '$status', 'completed' ] } ] }, 1, 0,
2842
+ ],
2843
+ },
2844
+ auditCompleted: {
2845
+ $cond: [
2846
+ { $and: [ { $eq: [ '$auditType', 'Audit' ] }, { $eq: [ '$status', 'completed' ] } ] }, 1, 0,
2847
+ ],
2848
+ },
2849
+ },
2850
+ },
2851
+ {
2852
+ $group: {
2853
+ _id: { fileDate: '$fileDate', clientId: '$clientId' },
2854
+ totalInprogress: { $sum: 1 },
2855
+ auditInprogress: { $sum: '$auditInprogress' },
2856
+ reAuditInprogress: { $sum: '$reAuditInprogress' },
2857
+ draft: { $sum: '$draft' },
2858
+ assigned: { $sum: 'assigned' },
2859
+ completedCount: { $sum: '$completedCount' },
2860
+ reAuditCompleted: { $sum: '$reAuditCompleted' },
2861
+ auditCompleted: { $sum: '$auditCompleted' },
2862
+ completedCount: { $sum: '$completedCount' },
2863
+
2864
+ },
2865
+ },
2866
+ {
2867
+ $project: {
2868
+ _id: 0,
2869
+ totalInprogress: 1,
2870
+ auditInprogress: 1,
2871
+ reAuditInprogress: 1,
2872
+ draft: 1,
2873
+ assigned: 1,
2874
+ completedCount: 1,
2875
+ reAuditCompleted: 1,
2876
+ auditCompleted: 1,
2877
+
2878
+ },
2879
+ },
2880
+ ];
2881
+ const result = await aggregateStoreAudit( query );
2882
+ logger.info( { result: result } );
2883
+ return res.sendSuccess( { result: result.length> 0? result[0]: temp } );
2884
+ } catch ( error ) {
2885
+ const err = error.message || 'Internal Server Error';
2886
+ logger.error( { error: error, message: req.body, function: 'summarySplit' } );
2887
+ return res.sendError( err, 500 );
2888
+ }
2889
+ }