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.
@@ -12,715 +12,6 @@ import { getReauditImg } from './audit.controllers.js';
12
12
  // import { listQueue } from 'tango-app-api-middleware/src/utils/aws/sqs.js';
13
13
 
14
14
 
15
- export async function storeMetrics( req, res ) {
16
- try {
17
- const inputData = req.body;
18
- const limit = inputData.limit || 10;
19
- const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
20
- const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
21
- let filter = [
22
- { fileDateISO: { $gte: dateRange.start } },
23
- { fileDateISO: { $lte: dateRange.end } },
24
- ];
25
- if ( inputData.filterByClientId && inputData.filterByClientId?.length > 0 ) {
26
- filter.push( { clientId: { $in: inputData.filterByClientId } } );
27
- }
28
-
29
- if ( inputData.filterByStoreId && inputData.filterByStoreId?.length > 0 ) {
30
- filter.push( { storeId: { $in: inputData.filterByStoreId } } );
31
- }
32
-
33
- if ( inputData.filterByStatus && inputData.filterByStatus?.length > 0 ) {
34
- filter.push( { status: { $in: inputData.filterByStatus } } );
35
- }
36
-
37
- if ( inputData.filterByType && inputData.filterByType?.length > 0 ) {
38
- filter.push( { auditType: { $in: inputData.filterByType } } );
39
- }
40
-
41
-
42
- const query = [
43
- {
44
- $match: {
45
- $and: filter,
46
- },
47
- },
48
-
49
- {
50
- $lookup: {
51
- from: 'stores',
52
- let: { storeId: '$storeId' },
53
- pipeline: [
54
- {
55
- $match: {
56
- $expr: {
57
- $eq: [ '$storeId', '$$storeId' ],
58
- },
59
- },
60
- },
61
- {
62
- $project: {
63
- _id: 0,
64
- storeName: 1,
65
- },
66
- },
67
- ], as: 'stores',
68
- },
69
- },
70
- {
71
- $unwind: {
72
- path: '$stores',
73
- preserveNullAndEmptyArrays: true,
74
- },
75
- },
76
- {
77
- $project: {
78
- _id: 0,
79
- fileDate: 1,
80
- storeId: 1,
81
- storeName: '$store.storeName',
82
- clientName: '',
83
- clientId: 1,
84
- userId: {
85
- $arrayElemAt: [ '$userId', { $subtract: [ { $size: '$userId' }, 1 ] } ],
86
- },
87
- auditType: 1,
88
- beforeCount: 1,
89
- afterCount: { $ifNull: [ '$afterCount', null ] },
90
- accuracy: { $round: [
91
- { $divide: [
92
- { $multiply: [ { $ifNull: [ '$afterCount', 0 ] }, '$beforeCount' ] }, 100,
93
- ] }, 2,
94
- ],
95
-
96
- },
97
- timeSpent: 1,
98
- status: 1,
99
- },
100
- },
101
- {
102
- $lookup: {
103
- from: 'users',
104
- let: { userId: '$userId' },
105
- pipeline: [
106
- {
107
- $match: {
108
- $expr: {
109
- $eq: [ '$_id', '$$userId' ],
110
- },
111
- },
112
- },
113
- {
114
- $project: {
115
- _id: 0,
116
- userName: 1,
117
- userEmail: '$email',
118
- },
119
- },
120
- ], as: 'users',
121
- },
122
- },
123
- {
124
- $unwind: {
125
- path: '$users',
126
- preserveNullAndEmptyArrays: true,
127
- },
128
- },
129
- {
130
- $lookup: {
131
- from: 'clients',
132
- let: { clientId: '$clientId' },
133
- pipeline: [
134
- {
135
- $match: {
136
- $expr: {
137
- $eq: [ '$clientId', '$$clientId' ],
138
- },
139
- },
140
- },
141
- {
142
- $project: {
143
- _id: 0,
144
- clientName: 1,
145
- },
146
- },
147
- ], as: 'client',
148
- },
149
- },
150
- {
151
- $unwind: {
152
- path: '$client',
153
- preserveNullAndEmptyArrays: true,
154
- },
155
- },
156
- {
157
- $project: {
158
- _id: 0,
159
- fileDate: 1,
160
- storeId: 1,
161
- storeName: '$stores.storeName',
162
- userName: '$users.userName',
163
- userEmail: '$users.userEmail',
164
- clientId: 1,
165
- clientName: '$client.clientName',
166
- auditType: 1,
167
- beforeCount: 1,
168
- afterCount: { $ifNull: [ '$afterCount', null ] },
169
- accuracy: { $round: [
170
- { $divide: [
171
- { $multiply: [ { $ifNull: [ '$afterCount', 0 ] }, '$beforeCount' ] }, 100,
172
- ] }, 1,
173
- ],
174
-
175
- },
176
- timeSpent: {
177
-
178
- $cond: [
179
- { $lt: [ '$timeSpent', 60 ] }, // Case 1: If less than 60 seconds
180
- { $concat: [ { $toString: '$timeSpent' }, ' sec' ] },
181
- {
182
- $cond: [
183
- { $lt: [ '$timeSpent', 3600 ] }, // Case 2: If less than 60 minutes (3600 seconds)
184
- {
185
- $concat: [
186
- { $toString: { $round: [ { $divide: [ '$timeSpent', 60 ] }, 1 ] } }, // Convert to minutes
187
- ' min',
188
- ],
189
- },
190
- {
191
- $concat: [
192
- { $toString: { $round: [ { $divide: [ '$timeSpent', 3600 ] }, 1 ] } }, // Convert to hours
193
- ' hr',
194
- ],
195
- },
196
- ],
197
- },
198
- ],
199
-
200
- },
201
- status: 1,
202
- },
203
- },
204
-
205
- ];
206
-
207
- if ( inputData.searchValue && inputData.searchValue !== '' ) {
208
- query.push( {
209
- $match: {
210
- $or: [
211
- { clientId: { $regex: inputData.searchValue, $options: 'i' } },
212
- { clientName: { $regex: inputData.searchValue, $options: 'i' } },
213
- { storeId: { $regex: inputData.searchValue, $options: 'i' } },
214
- { storeName: { $regex: inputData.searchValue, $options: 'i' } },
215
- { userName: { $regex: inputData.searchValue, $options: 'i' } },
216
- { status: { $regex: inputData.searchValue, $options: 'i' } },
217
- { auditType: { $regex: inputData.searchValue, $options: 'i' } },
218
- ],
219
- },
220
- } );
221
- }
222
-
223
- if ( inputData.sortColumnName ) {
224
- const sortBy = inputData.sortBy || -1;
225
- query.push( {
226
- $sort: { [inputData.sortColumName]: sortBy },
227
- },
228
- );
229
- }
230
- const count = await aggregateStoreAudit( query );
231
-
232
- if ( count.length == 0 ) {
233
- return res.sendError( 'No data Found', 204 );
234
- }
235
- if ( !inputData.isExport ) {
236
- query.push( {
237
- $skip: offset,
238
- },
239
- {
240
- $limit: limit,
241
- } );
242
- } else {
243
- query.push( { $limit: 10000 } );
244
- }
245
- const result = await aggregateStoreAudit( query );
246
- if ( inputData.isExport ) {
247
- const exportdata = [];
248
- result.forEach( ( element ) => {
249
- exportdata.push( {
250
- 'File Date': element.fileDate,
251
- 'Store Id': element.storeId,
252
- 'Store Name': element.storeName,
253
- 'Client Id': element.clientId,
254
- 'Client Name': element.clientName,
255
- 'User Name': element.userName,
256
- 'User Email': element.userEmail,
257
- 'Audit Type': element.auditType,
258
- 'Accuracy': element.accuracy,
259
- 'Time Spent': element.timeSpent,
260
- 'Status': element.status,
261
- } );
262
- } );
263
- await download( exportdata, res );
264
- return;
265
- }
266
- return res.sendSuccess( { result: result, count: count.length } );
267
- } catch ( error ) {
268
- const err = error.message || 'Internal Server Error';
269
- logger.error( { error: error, data: req.body, function: 'storeMetrics' } );
270
- return res.sendError( err, 500 );
271
- }
272
- }
273
-
274
- export async function clientMetrics( req, res ) {
275
- try {
276
- const inputData = req.body;
277
- const limit = inputData.limit || 10;
278
- const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
279
- const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
280
- let filter = [
281
- { fileDateISO: { $gte: dateRange.start } },
282
- { fileDateISO: { $lte: dateRange.end } },
283
- { moduleType: { $eq: 'traffic' } },
284
-
285
- ];
286
- if ( inputData?.filterByClient?.length> 0 ) {
287
- filter.push(
288
- { clientId: { $in: inputData.filterByClient } },
289
- );
290
- }
291
- const query = [
292
- {
293
- $match: {
294
- $and: filter,
295
- },
296
- },
297
- {
298
- $project: {
299
- fileDate: 1,
300
- clientId: 1,
301
- clientName: 1,
302
- queueName: 1,
303
- installedStore: 1,
304
- totalFilesCount: 1,
305
- notAssignedCount: { $ifNull: [ 0, 0 ] },
306
- clientStatus: { $ifNull: [ '', '' ] },
307
- },
308
- },
309
- {
310
- $lookup: {
311
- 'from': 'storeAudit',
312
- 'let': { clientId: '$clientId', fileDate: '$fileDate', totalAuditFiles: '$totalAuditFiles' },
313
- 'pipeline': [
314
-
315
- {
316
- $match: {
317
- $expr: {
318
- $and: [
319
- { $eq: [ '$clientId', '$$clientId' ] },
320
- { $eq: [ '$fileDate', '$$fileDate' ] },
321
- ],
322
- },
323
- },
324
- },
325
- {
326
- $project: {
327
- completedStores: { $cond: [ { $eq: [ '$status', 'completed' ] }, 1, 0 ] },
328
- notAssignedStores: { $cond: [ { $eq: [ '$status', 'not_assign' ] }, 1, 0 ] },
329
- inprogressStores: { $cond: [ { $in: [ '$status', [ 'inprogress', 'drafted', 'assigned' ] ] }, 1, 0 ] },
330
- },
331
- },
332
- {
333
- $group: {
334
- _id: { clientId: '$clientId', fileDate: '$fileDate' },
335
- completedStores: { $sum: '$completedStores' },
336
- inprogressStores: { $sum: '$inprogressStores' },
337
- notAssignedStores: { $sum: '$notAssignedStores' },
338
- fileCount: { $sum: 1 },
339
- },
340
- },
341
- {
342
- $project: {
343
- _id: 0,
344
- completedStores: 1,
345
- inprogressStores: 1,
346
- notAssignedStores: 1,
347
- fileCount: 1,
348
- completionPercentage: { $ifNull: [
349
- {
350
- $round: [ {
351
- $multiply: [
352
- 100, {
353
- $divide: [
354
- '$completedStores', '$$totalAuditFiles',
355
- ],
356
- },
357
- ],
358
- }, 1 ],
359
-
360
- },
361
- 0,
362
- ],
363
- },
364
- },
365
- },
366
- ], 'as': 'storeAudit',
367
- },
368
- },
369
- {
370
- $unwind: {
371
- path: '$storeAudit',
372
- preserveNullAndEmptyArrays: true,
373
- },
374
- },
375
- {
376
- $project: {
377
- fileDate: 1,
378
- clientId: 1,
379
- clientName: 1,
380
- totalFilesCount: 1,
381
- installedStore: 1,
382
- notAssignedStores: '$storeAudit.notAssignedStores',
383
- inprogressStoresCount: '$storeAudit.inprogressStores',
384
- fileCount: '$storeAudit.fileCount',
385
- notAssignedCount: 1,
386
- clientStatus: { $cond: [ { $eq: [ '$storeAudit.fileCount', 0 ] }, 'not-taken', { $cond: [ { $or: [ { $gt: [ '$notAssignedCount', 0 ] }, { $gt: [ '$storeAudit.notAssignedStores', 0 ] } ] }, 'pending', 'completed' ] } ] },
387
- completedStores: '$storeAudit.completedStores',
388
- completionPercentage: '$storeAudit.completionPercentage',
389
-
390
- },
391
- },
392
- ];
393
-
394
- if ( inputData.searchValue && inputData.searchValue!== '' ) {
395
- query.push( {
396
- $match: {
397
- $or: [
398
- { clientId: { $regex: inputData.searchValue, $options: 'i' } },
399
- { clientName: { $regex: inputData.searchValue, $options: 'i' } },
400
- ],
401
-
402
- },
403
- } );
404
- }
405
- if ( inputData.sortColumnName ) {
406
- const sortBy = inputData.sortBy || -1;
407
- query.push( {
408
- $sort: { [inputData.sortColumName]: sortBy },
409
- },
410
- );
411
- }
412
- logger.info( { query: query } );
413
- const count = [];// await aggregateAuditClientData( query );
414
- if ( count.length == 0 ) {
415
- return res.sendError( 'No Data Found', 204 );
416
- }
417
-
418
- query.push(
419
- { $skip: offset },
420
- { $limit: limit },
421
- );
422
- if ( inputData.isExport ) {
423
- query.push(
424
- {
425
- $project: {
426
- '_id': 0,
427
- 'File Date': '$fileDate',
428
- 'Client Name': '$clientName',
429
- 'Client Id': '$clientId',
430
- 'Completed Percentage': '$completionPercentage',
431
- 'Installed Stores': '$installedStore',
432
- 'Audit Stores': '$totalFilesCount',
433
- 'Inprogress Stores': '$inprogressStoresCount',
434
- 'Not Assigned': '$notAssignedCount',
435
- 'Not Assigned Stores': '$notAssignedStores',
436
- 'Completed': '$completedStores',
437
- 'Status': '$clientStatus',
438
- },
439
- },
440
- );
441
- }
442
-
443
- logger.info( { query: query } );
444
- const result = [];// await aggregateAuditClientData( query );
445
- if ( inputData.isExport ) {
446
- await download( result, res );
447
- return;
448
- }
449
- return res.sendSuccess( { result: result, count: count.length } );
450
- } catch ( error ) {
451
- const err = error.error || 'Internal Server Error';
452
- logger.info( { error: error, message: req.body, function: 'clientMetrics' } );
453
- return res.sendError( err, 500 );
454
- }
455
- }
456
-
457
- export async function userMetrics( req, res ) {
458
- try {
459
- const inputData = req.body;
460
- const limit = inputData.limit || 10;
461
- const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
462
- const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
463
- let filter = [];
464
- if ( inputData.fileType == 'auditedDate' ) {
465
- filter.push(
466
- { updatedAt: { $gte: dateRange.start } },
467
- { updatedAt: { $lte: dateRange.end } },
468
-
469
- );
470
- } else {
471
- filter.push(
472
- { fileDateISO: { $gte: dateRange.start } },
473
- { fileDateISO: { $lte: dateRange.end } },
474
-
475
- );
476
- }
477
- if ( inputData?.filterByAuditType?.length> 0 ) {
478
- filter.push(
479
- { moduleType: { $in: inputData.filterByAuditType } },
480
- );
481
- }
482
- if ( inputData?.filterByStatus?.length> 0 ) {
483
- filter.push(
484
- { auditStatus: { $in: inputData.filterByStatus } },
485
- );
486
- }
487
- if ( inputData?.filterByUser?.length> 0 ) {
488
- const temp = inputData.filterByUser.map( ( item ) => new mongoose.Types.ObjectId( item ) );
489
- logger.info( { temp: temp } );
490
- filter.push(
491
- { userId: { $in: temp } },
492
- );
493
- }
494
-
495
- const query = [
496
- {
497
- $match: {
498
- $and: filter,
499
- },
500
- },
501
- {
502
- $project: {
503
- userId: 1,
504
- fileDate: 1,
505
- startTime: 1,
506
- endTime: 1,
507
- totalCompletedFiles: { $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, 1, 0 ] },
508
- beforeCount: 1,
509
- afterCount: { $cond: [ { $eq: [ '$auditStatus', 'completed' ] }, '$afterCount', 0 ] },
510
- },
511
- },
512
- {
513
- $group: {
514
- _id: { userId: '$userId', fileDate: '$fileDate' },
515
- userId: { $first: '$userId' },
516
- fileDate: { $first: '$fileDate' },
517
- totalCompletedFiles: { $sum: '$totalCompletedFiles' },
518
- beforeCount: { $sum: '$beforeCount' },
519
- afterCount: { $sum: '$afterCount' },
520
- checkIntime: { $first: '$startTime' },
521
- checkOutTime: { $last: '$endTime' },
522
-
523
- },
524
- },
525
- {
526
- $project: {
527
- _id: 0,
528
- userId: 1,
529
- fileDate: 1,
530
- totalCompletedFiles: 1,
531
- beforeCount: 1,
532
- afterCount: 1,
533
- mappingPerc: { $round: [
534
- { $multiply: [
535
- { $divide: [ '$afterCount', '$beforeCount' ] }, 100,
536
- ] }, 2,
537
- ] },
538
- checkIntime: 1,
539
- checkOutTime: 1,
540
- differenceInSeconds: {
541
- $divide: [ { $subtract: [ '$checkOutTime', '$checkIntime' ] }, 1000 ],
542
- },
543
- },
544
- },
545
- {
546
- $project: {
547
- userId: 1,
548
- fileDate: 1,
549
- totalCompletedFiles: 1,
550
- beforeCount: 1,
551
- afterCount: 1,
552
- checkIntime: 1,
553
- checkOutTime: 1,
554
- mappingPerc: 1,
555
- hours: { $floor: { $divide: [ '$differenceInSeconds', 3600 ] } },
556
- minutes: { $floor: { $divide: [ { $mod: [ '$differenceInSeconds', 3600 ] }, 60 ] } },
557
- seconds: { $mod: [ '$differenceInSeconds', 60 ] },
558
- },
559
- },
560
- {
561
- $project: {
562
- event: 1,
563
- userId: 1,
564
- fileDate: 1,
565
- totalCompletedFiles: 1,
566
- beforeCount: 1,
567
- afterCount: 1,
568
- checkIntime: 1,
569
- checkOutTime: 1,
570
- hours: {
571
- $cond: {
572
- if: { $lt: [ '$hours', 10 ] },
573
- then: { $concat: [ '0', { $toString: { $round: [ '$hours', 0 ] } } ] },
574
- else: { $toString: { $round: [ '$hours', 0 ] } },
575
- },
576
- },
577
- minutes: {
578
- $cond: {
579
- if: { $lt: [ '$minutes', 10 ] },
580
- then: { $concat: [ '0', { $toString: { $round: [ '$minutes', 0 ] } } ] },
581
- else: { $toString: { $round: [ '$minutes', 0 ] } },
582
- },
583
- },
584
- seconds: {
585
- $cond: {
586
- if: { $lt: [ '$seconds', 10 ] },
587
- then: { $concat: [ '0', { $toString: { $round: [ '$seconds', 0 ] } } ] },
588
- else: { $toString: { $round: [ '$seconds', 0 ] } },
589
- },
590
- },
591
-
592
- },
593
- },
594
- {
595
- $project: {
596
- event: 1,
597
- userId: 1,
598
- fileDate: 1,
599
- totalCompletedFiles: 1,
600
- beforeCount: 1,
601
- afterCount: 1,
602
- checkIntime: 1,
603
- zoneName: 1,
604
- checkOutTime: 1,
605
- workingHours: {
606
- $concat: [ '$hours', ':', '$minutes', ':', '$seconds' ],
607
- },
608
- },
609
- },
610
- {
611
- $lookup: {
612
- from: 'users',
613
- let: { userId: '$userId' },
614
- pipeline: [
615
- {
616
- $match: {
617
- $expr: {
618
- $eq: [ '$_id', '$$userId' ],
619
- },
620
- },
621
- },
622
- {
623
- $project: {
624
- _id: 0,
625
- userName: 1,
626
- },
627
- },
628
- ], as: 'userInfo',
629
- },
630
- },
631
- {
632
- $unwind: {
633
- path: '$userInfo', preserveNullAndEmptyArrays: true,
634
- },
635
- },
636
- {
637
- $project: {
638
- event: 1,
639
- userName: '$userInfo.userName',
640
- userId: 1,
641
- fileDate: 1,
642
- totalCompletedFiles: 1,
643
- beforeCount: 1,
644
- afterCount: 1,
645
- checkIntime: {
646
- $dateToString: {
647
- format: '%Y-%m-%d %H:%M:%S',
648
- date: '$checkIntime',
649
- timezone: 'Asia/Kolkata',
650
- },
651
- },
652
- checkOutTime: {
653
- $dateToString: {
654
- format: '%Y-%m-%d %H:%M:%S',
655
- date: '$checkOutTime',
656
- timezone: 'Asia/Kolkata',
657
- },
658
- },
659
- workingHours: 1,
660
- },
661
- },
662
- ];
663
- if ( inputData.searchValue && inputData.searchValue!== '' ) {
664
- query.push( {
665
- $match: {
666
- $or: [
667
- { userName: { $regex: inputData.searchValue, $options: 'i' } },
668
- { clientName: { $regex: inputData.searchValue, $options: 'i' } },
669
- ],
670
-
671
- },
672
- } );
673
- }
674
- if ( inputData.sortColumnName ) {
675
- const sortBy = inputData.sortBy || -1;
676
- query.push( {
677
- $sort: { [inputData.sortColumName]: sortBy },
678
- },
679
- );
680
- }
681
-
682
- const count = await aggregateUserAudit( query );
683
- if ( count.length == 0 ) {
684
- return res.sendError( 'No Data Found', 204 );
685
- }
686
-
687
- query.push(
688
- { $skip: offset },
689
- { $limit: limit },
690
- );
691
- if ( inputData.isExport ) {
692
- query.push(
693
- {
694
- $project: {
695
- '_id': 0,
696
- 'File Date': '$fileDate',
697
- 'User Name': '$userName',
698
- 'User Id': '$userId',
699
- 'Total Completed Files Count': '$totalCompletedFiles',
700
- 'Total efore Count': '$beforeCount',
701
- 'Total After Count': '$afterCount',
702
- 'check Intime': '$checkIntime',
703
- 'check OutTime': '$checkOutTime',
704
- 'working Hours': '$workingHours',
705
- },
706
- },
707
- );
708
- }
709
-
710
-
711
- const result = await aggregateUserAudit( query );
712
- if ( inputData.isExport ) {
713
- await download( result, res );
714
- return;
715
- }
716
- return res.sendSuccess( { result: result, count: count.length } );
717
- } catch ( error ) {
718
- const err = error.error || 'Internal Server Error';
719
- logger.info( { error: error, message: req.body, function: 'clientMetrics' } );
720
- return res.sendError( err, 500 );
721
- }
722
- }
723
-
724
15
  export async function overViewCard( req, res ) {
725
16
  try {
726
17
  const inputData = req.body;