tango-app-api-trax 1.0.0-alpha-task.130

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.
Files changed (49) hide show
  1. package/.eslintrc.cjs +41 -0
  2. package/README.md +29 -0
  3. package/fir-51e77-firebase-adminsdk-x3sdp-fd902b74ae.json +14 -0
  4. package/index.js +12 -0
  5. package/package.json +43 -0
  6. package/src/controllers/download.controller.js +544 -0
  7. package/src/controllers/gallery.controller.js +457 -0
  8. package/src/controllers/internalTrax.controller.js +989 -0
  9. package/src/controllers/locus.controller.js +214 -0
  10. package/src/controllers/mobileTrax.controller.js +2327 -0
  11. package/src/controllers/teaxFlag.controller.js +2699 -0
  12. package/src/controllers/trax.controller.js +2214 -0
  13. package/src/controllers/traxDashboard.controllers.js +3523 -0
  14. package/src/dtos/dashboardValidation.dtos.js +143 -0
  15. package/src/dtos/downloadValidation.dtos.js +44 -0
  16. package/src/dtos/validation.dtos.js +138 -0
  17. package/src/hbs/login-otp.hbs +944 -0
  18. package/src/routes/download.router.js +23 -0
  19. package/src/routes/gallery.routes.js +28 -0
  20. package/src/routes/internalTraxApi.router.js +19 -0
  21. package/src/routes/locus.router.js +16 -0
  22. package/src/routes/mobileTrax.routes.js +25 -0
  23. package/src/routes/trax.routes.js +24 -0
  24. package/src/routes/traxDashboard.routes.js +59 -0
  25. package/src/routes/traxFlag.router.js +74 -0
  26. package/src/services/app.service.js +10 -0
  27. package/src/services/approver.service.js +20 -0
  28. package/src/services/authentication.service.js +6 -0
  29. package/src/services/checklist.service.js +31 -0
  30. package/src/services/checklistAssign.service.js +31 -0
  31. package/src/services/checklistQuestion.service.js +31 -0
  32. package/src/services/checklistlog.service.js +35 -0
  33. package/src/services/clientRequest.service.js +5 -0
  34. package/src/services/clients.services.js +23 -0
  35. package/src/services/domain.service.js +28 -0
  36. package/src/services/download.services.js +38 -0
  37. package/src/services/group.service.js +22 -0
  38. package/src/services/lenskartEmployeeMapping.service.js +15 -0
  39. package/src/services/locus.service.js +34 -0
  40. package/src/services/otp.service.js +14 -0
  41. package/src/services/processedTaskConfig.service.js +32 -0
  42. package/src/services/processedTaskList.service.js +30 -0
  43. package/src/services/processedchecklist.services.js +36 -0
  44. package/src/services/processedchecklistconfig.services.js +35 -0
  45. package/src/services/store.service.js +31 -0
  46. package/src/services/tagging.service.js +5 -0
  47. package/src/services/ticket.service.js +15 -0
  48. package/src/services/user.service.js +25 -0
  49. package/src/services/userAssignedstores.service.js +10 -0
@@ -0,0 +1,3523 @@
1
+ import { logger, download } from 'tango-app-api-middleware';
2
+ import * as processedchecklistService from '../services/processedchecklist.services.js';
3
+ import * as processedchecklistconfigService from '../services/processedchecklistconfig.services.js';
4
+ import * as checklistconfigService from '../services/checklist.service.js';
5
+ import mongoose from 'mongoose';
6
+ import dayjs from 'dayjs';
7
+ import utc from 'dayjs/plugin/utc.js';
8
+ dayjs.extend( utc );
9
+
10
+ export const welcome = async ( req, res ) => {
11
+ try {
12
+ let result = {
13
+ 'Message': 'Welcome',
14
+ };
15
+ return res.sendSuccess( result );
16
+ } catch ( error ) {
17
+ logger.error( { error: error, message: req.query, function: 'welcome' } );
18
+ return res.sendError( { error: error }, 500 );
19
+ }
20
+ };
21
+
22
+ export const overallCardsOld = async ( req, res ) => {
23
+ try {
24
+ let requestData = req.body;
25
+ let fromDate = new Date( requestData.fromDate );
26
+ let toDate = new Date( requestData.toDate );
27
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
28
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
29
+ toDate.setUTCHours( 23, 59, 59, 59 );
30
+ let activeUnique = 0;
31
+ let notSubmittedInstances = {
32
+ 'count': '',
33
+ 'open': '',
34
+ 'inprogress': '',
35
+ };
36
+ let totalInstances = {
37
+ 'count': '',
38
+ 'comparisonData': '',
39
+ 'ComparisonFlag': '',
40
+ };
41
+ let completedInstances = {
42
+ 'count': '',
43
+ 'comparisonData': '',
44
+ 'ComparisonFlag': '',
45
+ };
46
+ let flags = {
47
+ 'count': '',
48
+ 'comparisonData': '',
49
+ 'ComparisonFlag': '',
50
+ };
51
+ let completionScore = {
52
+ 'count': '',
53
+ 'comparisonData': '',
54
+ 'ComparisonFlag': '',
55
+ };
56
+ let complianceRate = {
57
+ 'count': '',
58
+ 'comparisonData': '',
59
+ 'ComparisonFlag': '',
60
+ };
61
+ let result = {};
62
+
63
+ let findQuery = [];
64
+ let findUniqueQuery = [];
65
+ let findAndQuery = [];
66
+ findAndQuery.push(
67
+ { client_id: requestData.clientId },
68
+ { store_id: { $in: requestData.storeId } },
69
+ { date_iso: { $gte: fromDate } },
70
+ { date_iso: { $lte: toDate } },
71
+ );
72
+
73
+ findQuery.push( { $match: { $and: findAndQuery } } );
74
+
75
+ findQuery.push( {
76
+ $project: {
77
+ sourceCheckList_id: 1,
78
+ checkListId: 1,
79
+ checklistStatus: 1,
80
+ timeFlag: 1,
81
+ questionFlag: 1,
82
+ mobileDetectionFlag: 1,
83
+ storeOpenCloseFlag: 1,
84
+ uniformDetectionFlag: 1,
85
+ markasread: 1,
86
+ checkListType: 1,
87
+ questionCount: 1,
88
+ },
89
+ } );
90
+
91
+ findQuery.push( {
92
+ $group: {
93
+ _id: '',
94
+ totalChecklist: { $sum: 1 },
95
+ notSubmittedChecklist: {
96
+ $sum: {
97
+ $cond: [
98
+ {
99
+ $and: [
100
+ { $ne: [ '$checklistStatus', 'submit' ] },
101
+ ],
102
+ }, 1, 0 ],
103
+ },
104
+ },
105
+ openChecklist: {
106
+ $sum: {
107
+ $cond: [
108
+ {
109
+ $and: [
110
+ { $eq: [ '$checklistStatus', 'open' ] },
111
+ ],
112
+ }, 1, 0 ],
113
+ },
114
+ },
115
+ inprogressChecklist: {
116
+ $sum: {
117
+ $cond: [
118
+ {
119
+ $and: [
120
+ { $eq: [ '$checklistStatus', 'inprogress' ] },
121
+ ],
122
+ }, 1, 0 ],
123
+ },
124
+ },
125
+ submittedChecklist: {
126
+ $sum: {
127
+ $cond: [
128
+ {
129
+ $and: [
130
+ { $eq: [ '$checklistStatus', 'submit' ] },
131
+ ],
132
+ }, 1, 0 ],
133
+ },
134
+ },
135
+ flaggedChecklist: {
136
+ $sum: {
137
+ $cond: [ {
138
+ $or: [
139
+ { $gt: [ '$timeFlag', 0 ] },
140
+ { $gt: [ '$mobileDetectionFlag', 0 ] },
141
+ { $gt: [ '$storeOpenCloseFlag', 0 ] },
142
+ { $gt: [ '$uniformDetectionFlag', 0 ] },
143
+ ],
144
+ }, 1, 0 ],
145
+ },
146
+ },
147
+ questionFlagCount: {
148
+ $sum: {
149
+ $cond: [
150
+ {
151
+ $and: [
152
+ { $gt: [ '$questionFlag', 0 ] },
153
+ ],
154
+ }, 1, 0 ],
155
+ },
156
+ },
157
+ questionCount: {
158
+ $sum: {
159
+ $cond: [
160
+ {
161
+ $and: [
162
+ { $gt: [ '$questionCount', 0 ] },
163
+ ],
164
+ }, 1, 0 ],
165
+ },
166
+ },
167
+ },
168
+ } );
169
+
170
+ findQuery.push( {
171
+ $project: {
172
+ totalChecklist: 1,
173
+ notSubmittedChecklist: 1,
174
+ openChecklist: 1,
175
+ inprogressChecklist: 1,
176
+ submittedChecklist: 1,
177
+ flaggedChecklist: 1,
178
+ completionScore: {
179
+ $multiply: [
180
+ { $divide: [ '$submittedChecklist', '$totalChecklist' ] },
181
+ 100,
182
+ ],
183
+ },
184
+ questionFlagCount: 1,
185
+ questionCount: 1,
186
+ questionCompletionScore: {
187
+ $multiply: [
188
+ { $divide: [
189
+ { $subtract: [ '$questionCount', '$questionFlagCount' ] },
190
+ '$questionCount',
191
+ ] },
192
+ 100,
193
+ ],
194
+ },
195
+ },
196
+ } );
197
+ let getOverallChecklistData = await processedchecklistService.aggregate( findQuery );
198
+
199
+ findUniqueQuery.push( { $match: { $and: findAndQuery } } );
200
+ findUniqueQuery.push( {
201
+ $project: {
202
+ sourceCheckList_id: 1,
203
+ },
204
+ } );
205
+ findUniqueQuery.push( {
206
+ $group: {
207
+ _id: '$sourceCheckList_id',
208
+ },
209
+ } );
210
+ findUniqueQuery.push( {
211
+ $count: 'totalUniqueChecklist',
212
+ } );
213
+ let getUniqueChecklistData = await processedchecklistService.aggregate( findUniqueQuery );
214
+ if ( !getUniqueChecklistData.length && !getOverallChecklistData.length ) {
215
+ return res.sendError( { error: 'No Data Found' }, 204 );
216
+ }
217
+
218
+ if ( getUniqueChecklistData.length && getUniqueChecklistData.length>0 ) {
219
+ if ( getUniqueChecklistData[0].totalUniqueChecklist ) {
220
+ activeUnique = getUniqueChecklistData[0]?.totalUniqueChecklist;
221
+ }
222
+ }
223
+
224
+ if ( getOverallChecklistData.length && getOverallChecklistData.length>0 ) {
225
+ if ( getOverallChecklistData[0].totalChecklist ) {
226
+ totalInstances.count = getOverallChecklistData[0]?.totalChecklist;
227
+ }
228
+ if ( getOverallChecklistData[0].notSubmittedChecklist ) {
229
+ notSubmittedInstances.count = getOverallChecklistData[0]?.notSubmittedChecklist;
230
+ }
231
+ if ( getOverallChecklistData[0].openChecklist ) {
232
+ notSubmittedInstances.open = getOverallChecklistData[0]?.openChecklist;
233
+ }
234
+ if ( getOverallChecklistData[0].inprogressChecklist ) {
235
+ notSubmittedInstances.inprogress = getOverallChecklistData[0]?.inprogressChecklist;
236
+ }
237
+ if ( getOverallChecklistData[0].submittedChecklist ) {
238
+ completedInstances.count = getOverallChecklistData[0]?.submittedChecklist;
239
+ }
240
+ if ( getOverallChecklistData[0].flaggedChecklist ) {
241
+ flags.count = getOverallChecklistData[0]?.flaggedChecklist;
242
+ }
243
+ if ( getOverallChecklistData[0].completionScore ) {
244
+ completionScore.count = getOverallChecklistData[0]?.completionScore;
245
+ }
246
+ if ( getOverallChecklistData[0].questionCompletionScore ) {
247
+ complianceRate.count = getOverallChecklistData[0]?.questionCompletionScore;
248
+ }
249
+ }
250
+ result.overallCards = {
251
+ 'activeUnique': activeUnique,
252
+ 'totalInstances': totalInstances,
253
+ 'notSubmittedInstances': notSubmittedInstances,
254
+ 'completedInstances': completedInstances,
255
+ 'flags': flags,
256
+ 'completionScore': completionScore,
257
+ 'complianceRate': complianceRate,
258
+ };
259
+ return res.sendSuccess( result );
260
+ } catch ( error ) {
261
+ logger.error( { error: error, message: req.query, function: 'overallCards' } );
262
+ return res.sendError( { error: error }, 500 );
263
+ }
264
+ };
265
+
266
+ export const checklistPerformance = async ( req, res ) => {
267
+ try {
268
+ let requestData = req.body;
269
+ let fromDate = new Date( requestData.fromDate );
270
+ let toDate = new Date( requestData.toDate );
271
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
272
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
273
+ toDate.setUTCHours( 23, 59, 59, 59 );
274
+ let result = {};
275
+
276
+ let findQuery = [];
277
+ let findAndQuery = [];
278
+ findAndQuery.push(
279
+ { checkListType: { $eq: 'custom' } },
280
+ { date_iso: { $gte: fromDate, $lte: toDate } },
281
+ { client_id: requestData.clientId },
282
+ { store_id: { $in: requestData.storeId } },
283
+ );
284
+
285
+ findQuery.push( { $match: { $and: findAndQuery } } );
286
+
287
+ if ( requestData.searchValue && requestData.searchValue != '' ) {
288
+ let checkListSearch = requestData.searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
289
+ let query;
290
+ if ( checkListSearch.length > 1 ) {
291
+ findQuery.push( { $addFields: { cheklistlowercase: { $toLower: '$checkListName' } } } );
292
+ query = { cheklistlowercase: { $in: checkListSearch } };
293
+ } else {
294
+ query = { checkListName: { $regex: requestData.searchValue.trim(), $options: 'i' } };
295
+ }
296
+ findQuery.push( { $match: { $or: [ query ] } } );
297
+ }
298
+
299
+ // findQuery.push( {
300
+ // $project: {
301
+ // sourceCheckList_id: 1,
302
+ // checkListId: 1,
303
+ // checkListName: 1,
304
+ // storeCount: 1,
305
+ // createdBy: 1,
306
+ // createdByName: 1,
307
+ // checklistStatus: 1,
308
+ // timeFlag: 1,
309
+ // questionFlag: 1,
310
+ // checkListType: 1,
311
+ // scheduleRepeatedType: 1,
312
+ // },
313
+ // } );
314
+
315
+ findQuery.push( {
316
+ $group: {
317
+ _id: '$sourceCheckList_id',
318
+ sourceCheckList_id: { $last: '$sourceCheckList_id' },
319
+ checkListName: { $last: '$checkListName' },
320
+ checkListChar: { $last: { $substr: [ '$checkListName', 0, 2 ] } },
321
+ scheduleRepeatedType: { $last: '$scheduleRepeatedType' },
322
+ // storeCount: { $max: '$storeCount' },
323
+ storeCount: { $sum: 1 },
324
+ submittedChecklist: {
325
+ $sum: {
326
+ $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, 1, 0 ],
327
+ },
328
+ },
329
+ timeFlag: { $sum: '$timeFlag' },
330
+ questionFlagCount: { $sum: '$questionFlag' },
331
+ checkListType: { $last: '$checkListType' },
332
+ },
333
+ } );
334
+ findQuery.push( {
335
+ $project: {
336
+ _id: 1,
337
+ sourceCheckList_id: 1,
338
+ checkListName: 1,
339
+ checkListChar: 1,
340
+ scheduleRepeatedType: {
341
+ $concat: [
342
+ { $toUpper: { $substr: [ '$scheduleRepeatedType', 0, 1 ] } }, // Capitalize the first letter
343
+ { $substr: [ '$scheduleRepeatedType', 1, { $strLenCP: '$scheduleRepeatedType' } ] }, // Append the rest of the string
344
+ ],
345
+ },
346
+ storeCount: 1,
347
+ submittedChecklist: 1,
348
+ flaggedChecklist: { $add: [ '$timeFlag', '$questionFlagCount' ] },
349
+ checkListType: 1,
350
+ },
351
+ } );
352
+
353
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
354
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
355
+ } else {
356
+ findQuery.push( { $sort: { ['submittedChecklist']: -1 } } );
357
+ }
358
+
359
+ let limit = parseInt( requestData?.limit ) || 10;
360
+ let skip = limit * ( requestData?.offset ) || 0;
361
+
362
+ findQuery.push( {
363
+ $facet: {
364
+ data: [
365
+ { $skip: skip }, { $limit: limit },
366
+ ],
367
+ count: [
368
+ { $count: 'total' },
369
+ ],
370
+ },
371
+ } );
372
+ let getChecklistPerformanceData = await processedchecklistService.aggregate( findQuery );
373
+ if ( !getChecklistPerformanceData[0].data.length ) {
374
+ return res.sendError( 'no data found', 204 );
375
+ }
376
+
377
+ if ( requestData.export ) {
378
+ const exportdata = [];
379
+ getChecklistPerformanceData[0].data.forEach( ( element ) => {
380
+ exportdata.push( {
381
+ 'Checklist Name': element.checkListName,
382
+ 'Scheduled': element.scheduleRepeatedType,
383
+ 'Assigned To': element.storeCount,
384
+ 'Submitted': element.submittedChecklist,
385
+ 'Flags': element.flaggedChecklist,
386
+ } );
387
+ } );
388
+ return await download( exportdata, res );
389
+ }
390
+
391
+ result.totalCount = getChecklistPerformanceData[0].count[0].total;
392
+ result.checklistPerformance = getChecklistPerformanceData[0].data;
393
+ return res.sendSuccess( result );
394
+ } catch ( error ) {
395
+ console.log( 'error =>', error );
396
+ logger.error( { error: error, message: req.query, function: 'checklistPerformance' } );
397
+ return res.sendError( { error: error }, 500 );
398
+ }
399
+ };
400
+
401
+ export const storePerformance = async ( req, res ) => {
402
+ try {
403
+ let requestData = req.body;
404
+ let fromDate = new Date( requestData.fromDate );
405
+ let toDate = new Date( requestData.toDate );
406
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
407
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
408
+ toDate.setUTCHours( 23, 59, 59, 59 );
409
+ let result = {};
410
+
411
+ let findQuery = [];
412
+ let findAndQuery = [];
413
+ findAndQuery.push(
414
+ { client_id: requestData.clientId },
415
+ { store_id: { $in: requestData.storeId } },
416
+ { date_iso: { $gte: fromDate } },
417
+ { date_iso: { $lte: toDate } },
418
+ { checkListType: { $eq: 'custom' } },
419
+ );
420
+
421
+ findQuery.push( { $match: { $and: findAndQuery } } );
422
+
423
+ // if ( requestData.searchValue && requestData.searchValue != '' ) {
424
+ // findQuery.push( { $match: { $or: [ { storeName: { $regex: requestData.searchValue, $options: 'i' } } ] } } );
425
+ // }
426
+
427
+ if ( requestData.searchValue && requestData.searchValue != '' ) {
428
+ let storeList = requestData.searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
429
+ let query;
430
+ if ( storeList.length > 1 ) {
431
+ findQuery.push( { $addFields: { store: { $toLower: '$storeName' } } } );
432
+ query = { store: { $in: storeList } };
433
+ } else {
434
+ query = { storeName: { $regex: requestData.searchValue.trim(), $options: 'i' } };
435
+ }
436
+ findQuery.push( { $match: { $or: [ query ] } } );
437
+ }
438
+
439
+ findQuery.push( {
440
+ $project: {
441
+ sourceCheckList_id: 1,
442
+ checkListId: 1,
443
+ checkListName: 1,
444
+ storeCount: 1,
445
+ createdBy: 1,
446
+ createdByName: 1,
447
+ checklistStatus: 1,
448
+ timeFlag: 1,
449
+ questionFlag: 1,
450
+ questionCount: 1,
451
+ checkListType: 1,
452
+ scheduleRepeatedType: 1,
453
+ store_id: 1,
454
+ storeName: 1,
455
+ userEmail: 1,
456
+ },
457
+ } );
458
+
459
+ findQuery.push( {
460
+ $group: {
461
+ _id: '$store_id',
462
+ totalChecklist: { $sum: 1 },
463
+ storeName: { $last: '$storeName' },
464
+ userEmail: { $last: '$userEmail' },
465
+ checkListCount: { $sum: '$_id' },
466
+ timeFlag: { $sum: '$timeFlag' },
467
+ submittedChecklist: {
468
+ $sum: {
469
+ $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, 1, 0 ],
470
+ },
471
+ },
472
+ checkListType: { $last: '$checkListType' },
473
+ // questionFlag: { $sum: '$questionFlag' },
474
+ questionFlag: { $sum: { $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, '$questionFlag', 0 ] } },
475
+ questionCount: { $sum: '$questionCount' },
476
+ submitQuestionCount: { $sum: { $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, '$questionCount', 0 ] } },
477
+ },
478
+ } );
479
+
480
+ findQuery.push( {
481
+ $project: {
482
+ store_id: 1,
483
+ storeName: 1,
484
+ userEmail: 1,
485
+ checkListCount: { $sum: '$totalChecklist' },
486
+ flaggedCount: { $add: [ '$timeFlag', '$questionFlag' ] },
487
+ submittedChecklist: 1,
488
+ checkListType: 1,
489
+ questionCount: 1,
490
+ questionFlag: 1,
491
+ timeFlag: 1,
492
+ completion: {
493
+ $round: [ { $multiply: [ { $divide: [ '$submittedChecklist', '$totalChecklist' ] }, 100 ] }, 0 ],
494
+ },
495
+ correctAnswers: { $subtract: [ '$submitQuestionCount', '$questionFlag' ] },
496
+ },
497
+ } );
498
+
499
+ findQuery.push( {
500
+ $project: {
501
+ store_id: 1,
502
+ storeName: 1,
503
+ userEmail: 1,
504
+ checkListCount: 1,
505
+ flaggedCount: 1,
506
+ submittedChecklist: 1,
507
+ checkListType: 1,
508
+ questionCount: 1,
509
+ questionFlag: 1,
510
+ timeFlag: 1,
511
+ completion: 1,
512
+ correctAnswers: 1,
513
+ compliance: {
514
+ $cond: {
515
+ if: { $eq: [ '$questionCount', 0 ] },
516
+ then: 0,
517
+ else: {
518
+ $round: [ { $multiply: [ { $divide: [ '$correctAnswers', '$questionCount' ] }, 100 ] }, 0 ],
519
+ },
520
+ },
521
+ },
522
+ },
523
+ } );
524
+
525
+ findQuery.push( {
526
+ $project: {
527
+ store_id: 1,
528
+ storeName: 1,
529
+ lowercaseStoreName: { $toLower: '$storeName' },
530
+ userEmail: 1,
531
+ checkListCount: 1,
532
+ flaggedCount: 1,
533
+ submittedChecklist: 1,
534
+ checkListType: 1,
535
+ questionCount: 1,
536
+ questionFlag: 1,
537
+ timeFlag: 1,
538
+ correctAnswers: 1,
539
+ completion: 1,
540
+ compliance: 1,
541
+ performance: { $round: [ { $divide: [ { $add: [ '$completion', '$compliance' ] }, 2 ] }, 0 ] },
542
+ },
543
+ } );
544
+
545
+ // let getTotalCount = await processedchecklistService.aggregate( findQuery );
546
+ // if ( !getTotalCount.length ) {
547
+ // return res.sendError( { error: 'No Data Found' }, 204 );
548
+ // }
549
+
550
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
551
+ if ( requestData.sortColumnName == 'storeName' ) {
552
+ findQuery.push( { $sort: { ['lowercaseStoreName']: requestData.sortBy } } );
553
+ } else {
554
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
555
+ }
556
+ } else {
557
+ findQuery.push( { $sort: { ['submittedChecklist']: -1 } } );
558
+ }
559
+
560
+ let limit = parseInt( requestData?.limit ) || 10;
561
+ let skip = limit * ( requestData?.offset ) || 0;
562
+ findQuery.push( {
563
+ $facet: {
564
+ data: [
565
+ { $skip: skip }, { $limit: limit },
566
+ ],
567
+ count: [
568
+ { $count: 'total' },
569
+ ],
570
+ },
571
+ },
572
+ );
573
+
574
+ let getStorePerformanceData = await processedchecklistService.aggregate( findQuery );
575
+ if ( !getStorePerformanceData[0].data.length ) {
576
+ return res.sendError( { error: 'No Data Found' }, 204 );
577
+ }
578
+
579
+
580
+ if ( requestData.export ) {
581
+ const exportdata = [];
582
+ getStorePerformanceData[0].data.forEach( ( element ) => {
583
+ exportdata.push( {
584
+ 'Store Name': element.storeName,
585
+ 'Store Spoc': element.userEmail,
586
+ 'Checklist Assigned': element.checkListCount,
587
+ 'Flags': element.flaggedCount,
588
+ 'Completion': element.completion,
589
+ 'Compliance': element.compliance,
590
+ 'Performance': element.performance,
591
+ } );
592
+ } );
593
+ return await download( exportdata, res );
594
+ }
595
+
596
+ result.totalCount = getStorePerformanceData[0].count[0].total;
597
+ result.storePerformance = getStorePerformanceData[0].data;
598
+ return res.sendSuccess( result );
599
+ } catch ( error ) {
600
+ console.log( 'error =>', error );
601
+ logger.error( { error: error, message: req.query, function: 'storePerformance' } );
602
+ return res.sendError( { error: error }, 500 );
603
+ }
604
+ };
605
+
606
+ export const userPerformance = async ( req, res ) => {
607
+ try {
608
+ let requestData = req.body;
609
+ let fromDate = new Date( requestData.fromDate );
610
+ let toDate = new Date( requestData.toDate );
611
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
612
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
613
+ toDate.setUTCHours( 23, 59, 59, 59 );
614
+ let result = {};
615
+
616
+ let findQuery = [];
617
+ let findAndQuery = [];
618
+ findAndQuery.push(
619
+ { date_iso: { $gte: fromDate, $lte: toDate } },
620
+ { checkListType: { $eq: 'custom' } },
621
+ { client_id: requestData.clientId },
622
+ { store_id: { $in: requestData.storeId } },
623
+ );
624
+
625
+ findQuery.push( { $match: { $and: findAndQuery } } );
626
+
627
+ // if ( requestData.searchValue && requestData.searchValue != '' ) {
628
+ // findQuery.push( { $match: { $or: [ { userName: { $regex: requestData.searchValue, $options: 'i' } } ] } } );
629
+ // }
630
+
631
+ if ( requestData.searchValue && requestData.searchValue != '' ) {
632
+ let userSearch = requestData.searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
633
+ let query;
634
+ if ( userSearch.length > 1 ) {
635
+ findQuery.push( { $addFields: { userNamelowercase: { $toLower: '$userName' } } } );
636
+ query = { userNamelowercase: { $in: userSearch } };
637
+ } else {
638
+ query = { userName: { $regex: requestData.searchValue.trim(), $options: 'i' } };
639
+ }
640
+ findQuery.push( { $match: { $or: [ query ] } } );
641
+ }
642
+
643
+ findQuery.push( {
644
+ $project: {
645
+ sourceCheckList_id: 1,
646
+ checkListId: 1,
647
+ checkListName: 1,
648
+ storeCount: 1,
649
+ createdBy: 1,
650
+ createdByName: 1,
651
+ checklistStatus: 1,
652
+ timeFlag: 1,
653
+ questionFlag: 1,
654
+ mobileDetectionFlag: 1,
655
+ storeOpenCloseFlag: 1,
656
+ uniformDetectionFlag: 1,
657
+ checkListType: 1,
658
+ scheduleRepeatedType: 1,
659
+ store_id: 1,
660
+ storeName: 1,
661
+ userEmail: 1,
662
+ userName: 1,
663
+ questionCount: 1,
664
+ },
665
+ } );
666
+
667
+ findQuery.push( {
668
+ $group: {
669
+ _id: '$userName',
670
+ totalChecklist: { $sum: 1 },
671
+ userEmail: { $last: '$userEmail' },
672
+ checkListCount: { $sum: '$_id' },
673
+ timeFlag: { $sum: '$timeFlag' },
674
+ submittedChecklist: {
675
+ $sum: {
676
+ $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, 1, 0 ],
677
+ },
678
+ },
679
+ checkListType: { $last: '$checkListType' },
680
+ // questionFlag: { $sum: '$questionFlag' },
681
+ questionFlag: { $sum: { $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, '$questionFlag', 0 ] } },
682
+ questionCount: { $sum: '$questionCount' },
683
+ submittedChecklistQuestionCount: { $sum: { $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, '$questionCount', 0 ] } },
684
+ },
685
+ } );
686
+
687
+ findQuery.push( {
688
+ $project: {
689
+ userName: '$_id',
690
+ userEmail: 1,
691
+ checkListCount: { $sum: '$totalChecklist' },
692
+ flaggedCount: { $add: [ '$timeFlag', '$questionFlag' ] },
693
+ submittedChecklist: 1,
694
+ timeFlag: 1,
695
+ checkListType: 1,
696
+ questionCount: 1,
697
+ questionFlag: 1,
698
+ completion: {
699
+ $round: [ { $multiply: [ { $divide: [ '$submittedChecklist', '$totalChecklist' ] }, 100 ] }, 0 ],
700
+ // $multiply: [ { $divide: [ '$submittedChecklist', '$totalChecklist' ] }, 100 ],
701
+ },
702
+ correctAnswers: { $subtract: [ '$submittedChecklistQuestionCount', '$questionFlag' ] },
703
+ },
704
+ } );
705
+
706
+ findQuery.push( {
707
+ $project: {
708
+ userName: 1,
709
+ userEmail: 1,
710
+ checkListCount: 1,
711
+ flaggedCount: 1,
712
+ submittedChecklist: 1,
713
+ checkListType: 1,
714
+ questionCount: 1,
715
+ questionFlag: 1,
716
+ timeFlag: 1,
717
+ completion: 1,
718
+ compliance: 1,
719
+ compliance: {
720
+ $cond: {
721
+ if: { $eq: [ '$questionCount', 0 ] },
722
+ then: 0,
723
+ else: {
724
+ $round: [ { $multiply: [ { $divide: [ '$correctAnswers', '$questionCount' ] }, 100 ] }, 0 ],
725
+ // $multiply: [ { $divide: [ '$correctAnswers', '$questionCount' ] }, 100 ],
726
+ },
727
+ },
728
+ },
729
+ },
730
+ } );
731
+
732
+ findQuery.push( {
733
+ $project: {
734
+ userName: 1,
735
+ lowercaseUserName: { $toLower: '$userName' },
736
+ userEmail: 1,
737
+ checkListCount: 1,
738
+ flaggedCount: 1,
739
+ submittedChecklist: 1,
740
+ checkListType: 1,
741
+ questionCount: 1,
742
+ questionFlag: 1,
743
+ timeFlag: 1,
744
+ correctAnswers: 1,
745
+ completion: 1,
746
+ compliance: 1,
747
+ performance: { $round: [ { $divide: [ { $add: [ '$completion', '$compliance' ] }, 2 ] }, 0 ] },
748
+ // performance: { $divide: [ { $add: [ '$completion', '$compliance' ] }, 2 ] },
749
+ },
750
+ } );
751
+
752
+ // let getTotalCount = await processedchecklistService.aggregate( findQuery );
753
+ // if ( !getTotalCount.length ) {
754
+ // return res.sendError( { error: 'No Data Found' }, 204 );
755
+ // }
756
+
757
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
758
+ if ( requestData.sortColumnName == 'userName' ) {
759
+ findQuery.push( { $sort: { ['lowercaseUserName']: requestData.sortBy } } );
760
+ } else {
761
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
762
+ }
763
+ } else {
764
+ findQuery.push( { $sort: { ['userName']: -1 } } );
765
+ }
766
+
767
+ let limit = parseInt( requestData?.limit ) || 10;
768
+ let skip = limit * ( requestData?.offset ) || 0;
769
+ findQuery.push( {
770
+ $facet: {
771
+ data: [
772
+ { $skip: skip }, { $limit: limit },
773
+ ],
774
+ count: [
775
+ { $count: 'total' },
776
+ ],
777
+ },
778
+ },
779
+ );
780
+
781
+ let getUserPerformanceData = await processedchecklistService.aggregate( findQuery );
782
+
783
+ if ( !getUserPerformanceData[0].data.length ) {
784
+ return res.sendError( { error: 'No Data Found' }, 204 );
785
+ }
786
+
787
+ result.totalCount = getUserPerformanceData[0].count[0].total;
788
+ result.userPerformance = getUserPerformanceData[0].data;
789
+ return res.sendSuccess( result );
790
+ } catch ( error ) {
791
+ console.log( 'error =>', error );
792
+ logger.error( { error: error, message: req.query, function: 'userPerformance' } );
793
+ return res.sendError( { error: error }, 500 );
794
+ }
795
+ };
796
+
797
+ export const checklistDropdownSourceChecklist = async ( req, res ) => {
798
+ try {
799
+ let requestData = req.body;
800
+ let fromDate = new Date( requestData.fromDate );
801
+ let toDate = new Date( requestData.toDate );
802
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
803
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
804
+ toDate.setUTCHours( 23, 59, 59, 59 );
805
+ let result = {};
806
+
807
+ let findQuery = [];
808
+ let findAndQuery = [];
809
+ findAndQuery.push(
810
+ { client_id: requestData.clientId },
811
+ { date_iso: { $gte: fromDate } },
812
+ { date_iso: { $lte: toDate } },
813
+ { checkListType: { $eq: 'custom' } },
814
+ );
815
+
816
+ findQuery.push( { $match: { $and: findAndQuery } } );
817
+
818
+ findQuery.push( {
819
+ $project: {
820
+ sourceCheckList_id: 1,
821
+ checkListName: 1,
822
+ checkListType: 1,
823
+ createdByName: 1,
824
+ storeCount: 1,
825
+ scheduleRepeatedType: 1,
826
+ scheduleStartTime: 1,
827
+ scheduleEndTime: 1,
828
+ },
829
+ } );
830
+
831
+ findQuery.push( {
832
+ $group: {
833
+ _id: '$sourceCheckList_id',
834
+ sourceCheckList_id: { $last: '$sourceCheckList_id' },
835
+ checkListName: { $last: '$checkListName' },
836
+ checkListType: { $last: '$checkListType' },
837
+ createdByName: { $last: '$createdByName' },
838
+ storeCount: { $max: '$storeCount' },
839
+ scheduleRepeatedType: { $last: '$scheduleRepeatedType' },
840
+ scheduleStartTime: { $last: '$scheduleStartTime' },
841
+ scheduleEndTime: { $last: '$scheduleEndTime' },
842
+ },
843
+ } );
844
+
845
+ findQuery.push( {
846
+ $lookup: {
847
+ from: 'checklistconfigs',
848
+ let: { sourceCheckList_id: '$sourceCheckList_id' },
849
+ pipeline: [
850
+ {
851
+ $match: {
852
+ $expr: {
853
+ $and: [
854
+ { $eq: [ '$_id', '$$sourceCheckList_id' ] },
855
+ ],
856
+ },
857
+ },
858
+ },
859
+ {
860
+ $project: {
861
+ checkListName: 1,
862
+ publish: 1,
863
+ },
864
+ },
865
+ ], as: 'checklistconfigs',
866
+ },
867
+ } );
868
+ findQuery.push( { $unwind: { path: '$checklistconfigs', preserveNullAndEmptyArrays: true } } );
869
+ findQuery.push( {
870
+ $project: {
871
+ sourceCheckList_id: 1,
872
+ checkListName: 1,
873
+ checkListType: 1,
874
+ createdByName: 1,
875
+ storeCount: 1,
876
+ scheduleRepeatedType: 1,
877
+ scheduleStartTime: 1,
878
+ scheduleEndTime: 1,
879
+ publish: '$checklistconfigs.publish',
880
+ },
881
+ } );
882
+
883
+ let getTotalCount = await processedchecklistconfigService.aggregate( findQuery );
884
+ if ( !getTotalCount.length ) {
885
+ return res.sendError( { error: 'No Data Found' }, 204 );
886
+ }
887
+
888
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
889
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
890
+ } else {
891
+ findQuery.push( { $sort: { ['checkListName']: -1 } } );
892
+ }
893
+
894
+ let getChecklistData = await processedchecklistconfigService.aggregate( findQuery );
895
+ result.checklistData = getChecklistData;
896
+ return res.sendSuccess( result );
897
+ } catch ( error ) {
898
+ logger.error( { error: error, message: req.query, function: 'checklistDropdown' } );
899
+ return res.sendError( { error: error }, 500 );
900
+ }
901
+ };
902
+
903
+ export const storeDropdown = async ( req, res ) => {
904
+ try {
905
+ let requestData = req.body;
906
+ let fromDate = new Date( requestData.fromDate );
907
+ let toDate = new Date( requestData.toDate );
908
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
909
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
910
+ toDate.setUTCHours( 23, 59, 59, 59 );
911
+ let result = {};
912
+
913
+ let findQuery = [];
914
+ let findAndQuery = [];
915
+ findAndQuery.push(
916
+ { client_id: requestData.clientId },
917
+ { store_id: { $in: requestData.storeId } },
918
+ { date_iso: { $gte: fromDate } },
919
+ { date_iso: { $lte: toDate } },
920
+ );
921
+
922
+ findQuery.push( { $match: { $and: findAndQuery } } );
923
+
924
+ findQuery.push( {
925
+ $project: {
926
+ store_id: 1,
927
+ storeName: 1,
928
+ },
929
+ } );
930
+
931
+ findQuery.push( {
932
+ $group: {
933
+ _id: '$store_id',
934
+ storeName: { $last: '$storeName' },
935
+ },
936
+ } );
937
+ findQuery.push( {
938
+ $project: {
939
+ _id: 0,
940
+ store_id: '$_id',
941
+ storeName: 1,
942
+ },
943
+ } );
944
+ findQuery.push( {
945
+ $lookup: {
946
+ from: 'stores',
947
+ let: { store_id: '$store_id' },
948
+ pipeline: [
949
+ {
950
+ $match: {
951
+ $expr: {
952
+ $and: [
953
+ { $eq: [ '$storeId', '$$store_id' ] },
954
+ ],
955
+ },
956
+ },
957
+ },
958
+ {
959
+ $project: {
960
+ appId: 1,
961
+ status: 1,
962
+ storeId: 1,
963
+ },
964
+ },
965
+ ], as: 'stores',
966
+ },
967
+ } );
968
+ findQuery.push( { $unwind: { path: '$stores', preserveNullAndEmptyArrays: true } } );
969
+ findQuery.push( {
970
+ $project: {
971
+ _id: 0,
972
+ store_id: 1,
973
+ storeName: 1,
974
+ appId: '$stores.appId',
975
+ },
976
+ } );
977
+
978
+ let getTotalCount = await processedchecklistService.aggregate( findQuery );
979
+ if ( !getTotalCount.length ) {
980
+ return res.sendError( { error: 'No Data Found' }, 204 );
981
+ }
982
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
983
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
984
+ } else {
985
+ findQuery.push( { $sort: { ['store_id']: -1 } } );
986
+ }
987
+
988
+ let getstoreData = await processedchecklistService.aggregate( findQuery );
989
+ result.storeData = getstoreData;
990
+ return res.sendSuccess( result );
991
+ } catch ( error ) {
992
+ logger.error( { error: error, message: req.query, function: 'storeDropdown' } );
993
+ return res.sendError( { error: error }, 500 );
994
+ }
995
+ };
996
+ export async function checklistbasedStoreDropdown( req, res ) {
997
+ try {
998
+ let findQuery = [];
999
+ let findAndQuery = [];
1000
+ let fromDate = new Date( req.body.fromDate );
1001
+ let toDate = new Date( req.body.toDate );
1002
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
1003
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
1004
+ toDate.setUTCHours( 23, 59, 59, 59 );
1005
+ findAndQuery.push(
1006
+ { client_id: req.body.clientId },
1007
+ { date_iso: { $gte: fromDate } },
1008
+ { date_iso: { $lte: toDate } },
1009
+ );
1010
+ console.log( findAndQuery );
1011
+ findQuery.push( { $match: { $and: findAndQuery } } );
1012
+ let getTotalCount = await processedchecklistService.aggregate( findQuery );
1013
+ console.log( '----->', getTotalCount );
1014
+ res.sendSuccess( getTotalCount );
1015
+ } catch ( error ) {
1016
+ logger.error( { error: error, message: req.body, function: 'checklistbasedStoreDropdown' } );
1017
+ return res.sendError( { error: error }, 500 );
1018
+ }
1019
+ }
1020
+ export const userDropdown = async ( req, res ) => {
1021
+ try {
1022
+ let requestData = req.body;
1023
+ let fromDate = new Date( requestData.fromDate );
1024
+ let toDate = new Date( requestData.toDate );
1025
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
1026
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
1027
+ toDate.setUTCHours( 23, 59, 59, 59 );
1028
+ let result = {};
1029
+
1030
+ let findQuery = [];
1031
+ let findAndQuery = [];
1032
+ findAndQuery.push(
1033
+ { client_id: requestData.clientId },
1034
+ { store_id: { $in: requestData.storeId } },
1035
+ { date_iso: { $gte: fromDate } },
1036
+ { date_iso: { $lte: toDate } },
1037
+ );
1038
+
1039
+ findQuery.push( { $match: { $and: findAndQuery } } );
1040
+
1041
+ findQuery.push( {
1042
+ $project: {
1043
+ userEmail: 1,
1044
+ userName: 1,
1045
+ },
1046
+ } );
1047
+
1048
+ findQuery.push( {
1049
+ $group: {
1050
+ _id: '$userEmail',
1051
+ userName: { $last: '$userName' },
1052
+ userNameChar: { $last: { $substr: [ '$userName', 0, 2 ] } },
1053
+ },
1054
+ } );
1055
+
1056
+ findQuery.push( {
1057
+ $project: {
1058
+ _id: 0,
1059
+ userEmail: '$_id',
1060
+ userName: 1,
1061
+ userNameChar: 1,
1062
+ },
1063
+ } );
1064
+ findQuery.push( {
1065
+ $lookup: {
1066
+ from: 'users',
1067
+ let: { userEmail: '$userEmail' },
1068
+ pipeline: [
1069
+ {
1070
+ $match: {
1071
+ $expr: {
1072
+ $and: [
1073
+ { $eq: [ '$email', '$$userEmail' ] },
1074
+ ],
1075
+ },
1076
+ },
1077
+ },
1078
+ {
1079
+ $project: {
1080
+ email: 1,
1081
+ userName: 1,
1082
+ role: 1,
1083
+ },
1084
+ },
1085
+ ], as: 'users',
1086
+ },
1087
+ } );
1088
+ findQuery.push( { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } } );
1089
+ findQuery.push( {
1090
+ $project: {
1091
+ _id: 0,
1092
+ userEmail: 1,
1093
+ userName: 1,
1094
+ userNameChar: 1,
1095
+ role: '$users.role',
1096
+ },
1097
+ } );
1098
+ let getTotalCount = await processedchecklistService.aggregate( findQuery );
1099
+ if ( !getTotalCount.length ) {
1100
+ return res.sendError( { error: 'No Data Found' }, 204 );
1101
+ }
1102
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
1103
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
1104
+ } else {
1105
+ findQuery.push( { $sort: { ['userName']: -1 } } );
1106
+ }
1107
+ let getuserData = await processedchecklistService.aggregate( findQuery );
1108
+ result.userData = getuserData;
1109
+ return res.sendSuccess( result );
1110
+ } catch ( error ) {
1111
+ console.log( 'error =>', error );
1112
+ logger.error( { error: error, message: req.query, function: 'userDropdown' } );
1113
+ return res.sendError( { error: error }, 500 );
1114
+ }
1115
+ };
1116
+
1117
+ export const checklistInfo = async ( req, res ) => {
1118
+ try {
1119
+ let requestData = req.body;
1120
+ let fromDate = new Date( requestData.fromDate );
1121
+ let toDate = new Date( requestData.toDate );
1122
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
1123
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
1124
+ toDate.setUTCHours( 23, 59, 59, 59 );
1125
+ let result = {};
1126
+
1127
+ let findQuery = [];
1128
+ let findAndQuery = [];
1129
+ findAndQuery.push(
1130
+ { client_id: requestData.clientId },
1131
+ { store_id: { $in: requestData.storeId } },
1132
+ { date_iso: { $gte: fromDate } },
1133
+ { date_iso: { $lte: toDate } },
1134
+ { checkListType: { $eq: 'custom' } },
1135
+ );
1136
+
1137
+ if ( requestData.checklistStatus && requestData.checklistStatus != 'All' ) {
1138
+ findAndQuery.push( { checklistStatus: requestData.checklistStatus } );
1139
+ }
1140
+
1141
+ if ( requestData.groupByType == 'Checklist' ) {
1142
+ findAndQuery.push( { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.groupByValue ) } );
1143
+ }
1144
+
1145
+ if ( requestData.groupByType == 'User' ) {
1146
+ findAndQuery.push( { userEmail: requestData.groupByValue } );
1147
+ }
1148
+
1149
+ if ( requestData.groupByType == 'Store' ) {
1150
+ findAndQuery.push( { store_id: requestData.groupByValue } );
1151
+ }
1152
+
1153
+ findQuery.push( { $match: { $and: findAndQuery } } );
1154
+
1155
+ if ( requestData.searchValue && requestData.searchValue != '' ) {
1156
+ if ( requestData.groupByType == 'Checklist' ) {
1157
+ // findQuery.push( { $match: { $or: [ { storeName: { $regex: requestData.searchValue, $options: 'i' } } ] } } );
1158
+ let storeList = requestData.searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
1159
+ let query;
1160
+ if ( storeList.length > 1 ) {
1161
+ findQuery.push( { $addFields: { store: { $toLower: '$storeName' } } } );
1162
+ query = { store: { $in: storeList } };
1163
+ } else {
1164
+ query = { storeName: { $regex: requestData.searchValue.trim(), $options: 'i' } };
1165
+ }
1166
+ findQuery.push( { $match: { $or: [ query ] } } );
1167
+ } else {
1168
+ // findQuery.push( { $match: { $or: [ { checkListName: { $regex: requestData.searchValue, $options: 'i' } } ] } } );
1169
+ let checkListSearch = requestData.searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
1170
+ let query;
1171
+ if ( checkListSearch.length > 1 ) {
1172
+ findQuery.push( { $addFields: { cheklistlowercase: { $toLower: '$checkListName' } } } );
1173
+ query = { cheklistlowercase: { $in: checkListSearch } };
1174
+ } else {
1175
+ query = { checkListName: { $regex: requestData.searchValue.trim(), $options: 'i' } };
1176
+ }
1177
+ findQuery.push( { $match: { $or: [ query ] } } );
1178
+ }
1179
+ }
1180
+
1181
+ findQuery.push( {
1182
+ $project: {
1183
+ checkListName: 1,
1184
+ createdByName: 1,
1185
+ userName: 1,
1186
+ userEmail: 1,
1187
+ checklistStatus: 1,
1188
+ submitTime_string: 1,
1189
+ storeName: 1,
1190
+ checkListType: 1,
1191
+ scheduleRepeatedType: 1,
1192
+ timeFlag: 1,
1193
+ questionFlag: 1,
1194
+ date_iso: 1,
1195
+ store_id: 1,
1196
+ timeFlag: 1,
1197
+ date_string: 1,
1198
+ sourceCheckList_id: 1,
1199
+ reinitiateStatus: 1,
1200
+ },
1201
+ } );
1202
+
1203
+ findQuery.push( {
1204
+ $project: {
1205
+ checkListName: 1,
1206
+ checkListChar: { $substr: [ '$checkListName', 0, 2 ] },
1207
+ createdByName: 1,
1208
+ userName: 1,
1209
+ userEmail: 1,
1210
+ checklistStatus: {
1211
+ $cond: {
1212
+ if: { $eq: [ '$checklistStatus', 'submit' ] },
1213
+ then: 'Submitted',
1214
+ else: {
1215
+ $cond: {
1216
+ if: { $eq: [ '$checklistStatus', 'open' ] },
1217
+ then: 'Open',
1218
+ else: 'In Progress',
1219
+ },
1220
+ },
1221
+ },
1222
+ },
1223
+ // submitTime_string: 1,
1224
+ submitTime_string: {
1225
+ $cond: {
1226
+ if: { $eq: [ '$checklistStatus', 'submit' ] },
1227
+ then: '$submitTime_string',
1228
+ else: '',
1229
+ },
1230
+ },
1231
+ storeName: 1,
1232
+ checkListType: 1,
1233
+ scheduleRepeatedType: 1,
1234
+ flaggedChecklist: { $add: [ '$timeFlag', '$questionFlag' ] },
1235
+ timeFlag: 1,
1236
+ questionFlag: 1,
1237
+ date_iso: 1,
1238
+ store_id: 1,
1239
+ timeFlag: 1,
1240
+ date_string: 1,
1241
+ checklistDate: '$date_string',
1242
+ sourceCheckList_id: 1,
1243
+ reinitiateStatus: 1,
1244
+ },
1245
+ } );
1246
+
1247
+ let getTotalCount = await processedchecklistService.aggregate( findQuery );
1248
+ if ( !getTotalCount.length ) {
1249
+ return res.sendError( { error: 'No Data Found' }, 204 );
1250
+ }
1251
+
1252
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
1253
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
1254
+ } else {
1255
+ findQuery.push( { $sort: { ['date_iso']: -1 } } );
1256
+ }
1257
+
1258
+ let limit = parseInt( requestData?.limit ) || 10;
1259
+ let skip = limit * ( requestData?.offset ) || 0;
1260
+ findQuery.push( { $skip: skip }, { $limit: limit } );
1261
+ let getChecklistPerformanceData = await processedchecklistService.aggregate( findQuery );
1262
+
1263
+ result.totalCount = getTotalCount.length;
1264
+ for ( let i = 0; i < getChecklistPerformanceData.length; i++ ) {
1265
+ getChecklistPerformanceData[i].date_string = dayjs( getChecklistPerformanceData[i].date_string ).format( 'DD MMM YYYY' );
1266
+ }
1267
+ result.checklistInfo = getChecklistPerformanceData;
1268
+ return res.sendSuccess( result );
1269
+ } catch ( error ) {
1270
+ console.log( 'error =>', error );
1271
+ logger.error( { error: error, message: req.query, function: 'checklistInfo' } );
1272
+ return res.sendError( { error: error }, 500 );
1273
+ }
1274
+ };
1275
+
1276
+ export const infoCardsOld = async ( req, res ) => {
1277
+ try {
1278
+ let requestData = req.body;
1279
+ let fromDate = new Date( requestData.fromDate );
1280
+ let toDate = new Date( requestData.toDate );
1281
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
1282
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
1283
+ toDate.setUTCHours( 23, 59, 59, 59 );
1284
+ let activeUnique = 0;
1285
+ let notSubmittedInstances = {
1286
+ 'count': '',
1287
+ 'open': '',
1288
+ 'inprogress': '',
1289
+ };
1290
+ let totalInstances = {
1291
+ 'count': '',
1292
+ 'comparisonData': '',
1293
+ 'ComparisonFlag': '',
1294
+ };
1295
+ let completedInstances = {
1296
+ 'count': '',
1297
+ 'comparisonData': '',
1298
+ 'ComparisonFlag': '',
1299
+ };
1300
+ let flags = {
1301
+ 'count': '',
1302
+ 'comparisonData': '',
1303
+ 'ComparisonFlag': '',
1304
+ };
1305
+ let completionScore = {
1306
+ 'count': '',
1307
+ 'comparisonData': '',
1308
+ 'ComparisonFlag': '',
1309
+ };
1310
+ let complianceRate = {
1311
+ 'count': '',
1312
+ 'comparisonData': '',
1313
+ 'ComparisonFlag': '',
1314
+ };
1315
+ let result = {};
1316
+
1317
+ let findQuery = [];
1318
+ let findUniqueQuery = [];
1319
+ let findAndQuery = [];
1320
+ findAndQuery.push(
1321
+ { client_id: requestData.clientId },
1322
+ { store_id: { $in: requestData.storeId } },
1323
+ { date_iso: { $gte: fromDate } },
1324
+ { date_iso: { $lte: toDate } },
1325
+ );
1326
+
1327
+ if ( requestData.groupByType == 'checklist' ) {
1328
+ findAndQuery.push( { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.groupByValue ) } );
1329
+ }
1330
+
1331
+ if ( requestData.groupByType == 'user' ) {
1332
+ findAndQuery.push( { userEmail: requestData.groupByValue } );
1333
+ }
1334
+
1335
+ if ( requestData.groupByType == 'store' ) {
1336
+ findAndQuery.push( { store_id: requestData.groupByValue } );
1337
+ }
1338
+
1339
+ findQuery.push( { $match: { $and: findAndQuery } } );
1340
+
1341
+ findQuery.push( {
1342
+ $project: {
1343
+ sourceCheckList_id: 1,
1344
+ checkListId: 1,
1345
+ checklistStatus: 1,
1346
+ timeFlag: 1,
1347
+ questionFlag: 1,
1348
+ mobileDetectionFlag: 1,
1349
+ storeOpenCloseFlag: 1,
1350
+ uniformDetectionFlag: 1,
1351
+ markasread: 1,
1352
+ checkListType: 1,
1353
+ },
1354
+ } );
1355
+
1356
+ findQuery.push( {
1357
+ $group: {
1358
+ _id: '',
1359
+ totalChecklist: { $sum: 1 },
1360
+ notSubmittedChecklist: {
1361
+ $sum: {
1362
+ $cond: [
1363
+ {
1364
+ $and: [
1365
+ { $ne: [ '$checklistStatus', 'submit' ] },
1366
+ ],
1367
+ }, 1, 0 ],
1368
+ },
1369
+ },
1370
+ openChecklist: {
1371
+ $sum: {
1372
+ $cond: [
1373
+ {
1374
+ $and: [
1375
+ { $eq: [ '$checklistStatus', 'open' ] },
1376
+ ],
1377
+ }, 1, 0 ],
1378
+ },
1379
+ },
1380
+ inprogressChecklist: {
1381
+ $sum: {
1382
+ $cond: [
1383
+ {
1384
+ $and: [
1385
+ { $eq: [ '$checklistStatus', 'inprogress' ] },
1386
+ ],
1387
+ }, 1, 0 ],
1388
+ },
1389
+ },
1390
+ submittedChecklist: {
1391
+ $sum: {
1392
+ $cond: [
1393
+ {
1394
+ $and: [
1395
+ { $eq: [ '$checklistStatus', 'submit' ] },
1396
+ ],
1397
+ }, 1, 0 ],
1398
+ },
1399
+ },
1400
+ flaggedChecklist: {
1401
+ $sum: {
1402
+ $cond: [ {
1403
+ $or: [
1404
+ { $gt: [ '$timeFlag', 0 ] },
1405
+ { $gt: [ '$mobileDetectionFlag', 0 ] },
1406
+ { $gt: [ '$storeOpenCloseFlag', 0 ] },
1407
+ { $gt: [ '$uniformDetectionFlag', 0 ] },
1408
+ ],
1409
+ }, 1, 0 ],
1410
+ },
1411
+ },
1412
+ },
1413
+ } );
1414
+ let getOverallChecklistData = await processedchecklistService.aggregate( findQuery );
1415
+
1416
+ findUniqueQuery.push( { $match: { $and: findAndQuery } } );
1417
+ findUniqueQuery.push( {
1418
+ $project: {
1419
+ sourceCheckList_id: 1,
1420
+ },
1421
+ } );
1422
+ findUniqueQuery.push( {
1423
+ $group: {
1424
+ _id: '$sourceCheckList_id',
1425
+ },
1426
+ } );
1427
+ findUniqueQuery.push( {
1428
+ $count: 'totalUniqueChecklist',
1429
+ } );
1430
+ let getUniqueChecklistData = await processedchecklistService.aggregate( findUniqueQuery );
1431
+ if ( !getUniqueChecklistData.length && !getOverallChecklistData.length ) {
1432
+ return res.sendError( { error: 'No Data Found' }, 204 );
1433
+ }
1434
+
1435
+ if ( getUniqueChecklistData.length && getUniqueChecklistData.length>0 ) {
1436
+ if ( getUniqueChecklistData[0].totalUniqueChecklist ) {
1437
+ activeUnique = getUniqueChecklistData[0]?.totalUniqueChecklist;
1438
+ }
1439
+ }
1440
+
1441
+ if ( getOverallChecklistData.length && getOverallChecklistData.length>0 ) {
1442
+ if ( getOverallChecklistData[0].totalChecklist ) {
1443
+ totalInstances.count = getOverallChecklistData[0]?.totalChecklist;
1444
+ }
1445
+ if ( getOverallChecklistData[0].notSubmittedChecklist ) {
1446
+ notSubmittedInstances.count = getOverallChecklistData[0]?.notSubmittedChecklist;
1447
+ }
1448
+ if ( getOverallChecklistData[0].openChecklist ) {
1449
+ notSubmittedInstances.open = getOverallChecklistData[0]?.openChecklist;
1450
+ }
1451
+ if ( getOverallChecklistData[0].inprogressChecklist ) {
1452
+ notSubmittedInstances.inprogress = getOverallChecklistData[0]?.inprogressChecklist;
1453
+ }
1454
+ if ( getOverallChecklistData[0].submittedChecklist ) {
1455
+ completedInstances.count = getOverallChecklistData[0]?.submittedChecklist;
1456
+ }
1457
+ if ( getOverallChecklistData[0].flaggedChecklist ) {
1458
+ flags.count = getOverallChecklistData[0]?.flaggedChecklist;
1459
+ }
1460
+ }
1461
+ result.infoCards = {
1462
+ 'activeUnique': activeUnique,
1463
+ 'totalInstances': totalInstances,
1464
+ 'notSubmittedInstances': notSubmittedInstances,
1465
+ 'completedInstances': completedInstances,
1466
+ 'flags': flags,
1467
+ 'completionScore': completionScore,
1468
+ 'complianceRate': complianceRate,
1469
+ };
1470
+ return res.sendSuccess( result );
1471
+ } catch ( error ) {
1472
+ logger.error( { error: error, message: req.query, function: 'infoCards' } );
1473
+ return res.sendError( { error: error }, 500 );
1474
+ }
1475
+ };
1476
+
1477
+ export const monthlyGraph = async ( req, res ) => {
1478
+ try {
1479
+ let requestData = req.body;
1480
+ let resultData = await monthlyChartData( requestData.checklistType );
1481
+ return res.sendSuccess( resultData );
1482
+ } catch ( error ) {
1483
+ console.log( 'error =>', error );
1484
+ logger.error( { error: error, message: req.query, function: 'checklistPerformance' } );
1485
+ return res.sendError( { error: error }, 500 );
1486
+ }
1487
+ };
1488
+
1489
+ export const monthlyGraphOLD = async ( req, res ) => {
1490
+ try {
1491
+ let requestData = req.body;
1492
+ let fromDate = new Date( requestData.fromDate );
1493
+ let toDate = new Date( requestData.toDate );
1494
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
1495
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
1496
+ toDate.setUTCHours( 23, 59, 59, 59 );
1497
+ let result = {};
1498
+
1499
+ let findQuery = [];
1500
+ let findAndQuery = [];
1501
+ findAndQuery.push(
1502
+ { client_id: requestData.clientId },
1503
+ { store_id: { $in: requestData.storeId } },
1504
+ { date_iso: { $gte: fromDate } },
1505
+ { date_iso: { $lte: toDate } },
1506
+ );
1507
+
1508
+ if ( requestData.groupByType == 'checklist' ) {
1509
+ findAndQuery.push( { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.groupByValue ) } );
1510
+ }
1511
+
1512
+ if ( requestData.groupByType == 'user' ) {
1513
+ findAndQuery.push( { userEmail: requestData.groupByValue } );
1514
+ }
1515
+
1516
+ if ( requestData.groupByType == 'store' ) {
1517
+ findAndQuery.push( { store_id: requestData.groupByValue } );
1518
+ }
1519
+
1520
+ findQuery.push( { $match: { $and: findAndQuery } } );
1521
+
1522
+ findQuery.push( {
1523
+ $project: {
1524
+ sourceCheckList_id: 1,
1525
+ checkListId: 1,
1526
+ checkListName: 1,
1527
+ storeCount: 1,
1528
+ createdBy: 1,
1529
+ createdByName: 1,
1530
+ checklistStatus: 1,
1531
+ timeFlag: 1,
1532
+ questionFlag: 1,
1533
+ mobileDetectionFlag: 1,
1534
+ storeOpenCloseFlag: 1,
1535
+ uniformDetectionFlag: 1,
1536
+ checkListType: 1,
1537
+ scheduleRepeatedType: 1,
1538
+ date_string: 1,
1539
+ date_iso: 1,
1540
+ },
1541
+ } );
1542
+
1543
+ findQuery.push( {
1544
+ $group: {
1545
+ _id: '$date_string',
1546
+ date_iso: { $last: '$date_iso' },
1547
+ storeCount: { $max: '$storeCount' },
1548
+ submittedChecklist: {
1549
+ $sum: {
1550
+ $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, 1, 0 ],
1551
+ },
1552
+ },
1553
+ flaggedChecklist: {
1554
+ $sum: {
1555
+ $cond: [ {
1556
+ $or: [
1557
+ { $gt: [ '$timeFlag', 0 ] },
1558
+ { $gt: [ '$questionFlag', 0 ] },
1559
+ { $gt: [ '$mobileDetectionFlag', 0 ] },
1560
+ { $gt: [ '$storeOpenCloseFlag', 0 ] },
1561
+ { $gt: [ '$uniformDetectionFlag', 0 ] },
1562
+ ],
1563
+ }, 1, 0 ],
1564
+ },
1565
+ },
1566
+ checkListType: { $last: '$checkListType' },
1567
+ },
1568
+ } );
1569
+
1570
+ let getTotalCount = await processedchecklistService.aggregate( findQuery );
1571
+ if ( !getTotalCount.length ) {
1572
+ return res.sendError( { error: 'No Data Found' }, 204 );
1573
+ }
1574
+
1575
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
1576
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
1577
+ } else {
1578
+ findQuery.push( { $sort: { ['date_iso']: 1 } } );
1579
+ }
1580
+ let getmonthlyData = await processedchecklistService.aggregate( findQuery );
1581
+
1582
+ result.monthlyData = getmonthlyData;
1583
+ return res.sendSuccess( result );
1584
+ } catch ( error ) {
1585
+ console.log( 'error =>', error );
1586
+ logger.error( { error: error, message: req.query, function: 'checklistPerformance' } );
1587
+ return res.sendError( { error: error }, 500 );
1588
+ }
1589
+ };
1590
+
1591
+ export const flagDetectionCards = async ( req, res ) => {
1592
+ try {
1593
+ let requestData = req.body;
1594
+ let fromDate = new Date( requestData.fromDate );
1595
+ let toDate = new Date( requestData.toDate );
1596
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
1597
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
1598
+ toDate.setUTCHours( 23, 59, 59, 59 );
1599
+ let result = {};
1600
+
1601
+ let findQuery = [];
1602
+ let findAndQuery = [];
1603
+ findAndQuery.push(
1604
+ { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.sourceCheckList_id ) },
1605
+ { client_id: requestData.clientId },
1606
+ { store_id: { $in: requestData.storeId } },
1607
+ { date_iso: { $gte: fromDate } },
1608
+ { date_iso: { $lte: toDate } },
1609
+ );
1610
+ findQuery.push( { $match: { $and: findAndQuery } } );
1611
+
1612
+ if ( requestData.checklistType == 'storeopenandclose' ) {
1613
+ findQuery.push( {
1614
+ $project: {
1615
+ store_id: 1,
1616
+ storeName: 1,
1617
+ sourceCheckList_id: 1,
1618
+ mobileDetectionFlag: 1,
1619
+ storeOpenCloseFlag: 1,
1620
+ uniformDetectionFlag: 1,
1621
+ },
1622
+ } );
1623
+
1624
+ findQuery.push( {
1625
+ $group: {
1626
+ _id: '$store_id',
1627
+ storeName: { $last: '$storeName' },
1628
+ mobileDetectionFlag: { $sum: '$mobileDetectionFlag' },
1629
+ storeOpenCloseFlag: { $sum: '$storeOpenCloseFlag' },
1630
+ uniformDetectionFlag: { $sum: '$uniformDetectionFlag' },
1631
+ },
1632
+ } );
1633
+
1634
+ findQuery.push( {
1635
+ $project: {
1636
+ _id: 1,
1637
+ storeName: 1,
1638
+ detectionFlag: { $sum: [ '$mobileDetectionFlag', '$storeOpenCloseFlag', '$uniformDetectionFlag' ] },
1639
+ },
1640
+ } );
1641
+
1642
+ findQuery.push( {
1643
+ $group: {
1644
+ _id: '',
1645
+ storeCount: { $sum: 1 },
1646
+ detectionSum: { $sum: '$detectionFlag' },
1647
+ },
1648
+ } );
1649
+
1650
+ let getTotalCount = await processedchecklistService.aggregate( findQuery );
1651
+ if ( !getTotalCount.length ) {
1652
+ return res.sendError( { error: 'No Data Found' }, 204 );
1653
+ }
1654
+ result.cards = {};
1655
+ result.cards.totalAssignedStores = {
1656
+ 'count': getTotalCount[0]?.storeCount || 0,
1657
+ 'comparisonData': '',
1658
+ 'ComparisonFlag': '',
1659
+ };
1660
+
1661
+ result.cards.lateOpenStores = {
1662
+ 'count': 0,
1663
+ 'comparisonData': '',
1664
+ 'ComparisonFlag': '',
1665
+ };
1666
+
1667
+ result.cards.earlyClosedStores = {
1668
+ 'count': 0,
1669
+ 'comparisonData': '',
1670
+ 'ComparisonFlag': '',
1671
+ };
1672
+
1673
+ result.cards.Compliance = {
1674
+ 'count': '',
1675
+ 'comparisonData': '',
1676
+ 'ComparisonFlag': '',
1677
+ };
1678
+ } else if ( requestData.checklistType == 'uniformdetection' || requestData.checklistType == 'mobileusagedetection' || requestData.checklistType == 'custom' ) {
1679
+ findQuery.push( {
1680
+ $project: {
1681
+ store_id: 1,
1682
+ storeName: 1,
1683
+ sourceCheckList_id: 1,
1684
+ mobileDetectionFlag: 1,
1685
+ storeOpenCloseFlag: 1,
1686
+ uniformDetectionFlag: 1,
1687
+ },
1688
+ } );
1689
+
1690
+ findQuery.push( {
1691
+ $group: {
1692
+ _id: '$store_id',
1693
+ storeName: { $last: '$storeName' },
1694
+ mobileDetectionFlag: { $sum: '$mobileDetectionFlag' },
1695
+ storeOpenCloseFlag: { $sum: '$storeOpenCloseFlag' },
1696
+ uniformDetectionFlag: { $sum: '$uniformDetectionFlag' },
1697
+ },
1698
+ } );
1699
+
1700
+ findQuery.push( {
1701
+ $project: {
1702
+ _id: 1,
1703
+ storeName: 1,
1704
+ detectionFlag: { $sum: [ '$mobileDetectionFlag', '$storeOpenCloseFlag', '$uniformDetectionFlag' ] },
1705
+ },
1706
+ } );
1707
+
1708
+ findQuery.push( {
1709
+ $group: {
1710
+ _id: '',
1711
+ storeCount: { $sum: 1 },
1712
+ detectionSum: { $sum: '$detectionFlag' },
1713
+ },
1714
+ } );
1715
+
1716
+ let getTotalCount = await processedchecklistService.aggregate( findQuery );
1717
+ if ( !getTotalCount.length ) {
1718
+ return res.sendError( { error: 'No Data Found' }, 204 );
1719
+ }
1720
+ result.cards = {};
1721
+ result.cards.totalAssignedStores = {
1722
+ 'count': getTotalCount[0]?.storeCount || 0,
1723
+ 'comparisonData': '',
1724
+ 'ComparisonFlag': '',
1725
+ };
1726
+
1727
+ result.cards.flagIncidents = {
1728
+ 'count': getTotalCount[0]?.detectionSum || 0,
1729
+ 'comparisonData': '',
1730
+ 'ComparisonFlag': '',
1731
+ };
1732
+
1733
+ result.cards.Compliance = {
1734
+ 'count': '',
1735
+ 'comparisonData': '',
1736
+ 'ComparisonFlag': '',
1737
+ };
1738
+ }
1739
+
1740
+ return res.sendSuccess( result );
1741
+ } catch ( error ) {
1742
+ console.log( 'error =>', error );
1743
+ logger.error( { error: error, message: req.query, function: 'flagDetectionTables' } );
1744
+ return res.sendError( { error: error }, 500 );
1745
+ }
1746
+ };
1747
+
1748
+ export const flagDetectionTables = async ( req, res ) => {
1749
+ try {
1750
+ let requestData = req.body;
1751
+ let fromDate = new Date( requestData.fromDate );
1752
+ let toDate = new Date( requestData.toDate );
1753
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
1754
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
1755
+ toDate.setUTCHours( 23, 59, 59, 59 );
1756
+ let result = {};
1757
+
1758
+ let findQuery = [];
1759
+ let findAndQuery = [];
1760
+ findAndQuery.push(
1761
+ { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.sourceCheckList_id ) },
1762
+ { client_id: requestData.clientId },
1763
+ { store_id: { $in: requestData.storeId } },
1764
+ { date_iso: { $gte: fromDate } },
1765
+ { date_iso: { $lte: toDate } },
1766
+ { checklistStatus: { $eq: 'submit' } },
1767
+ );
1768
+
1769
+ findQuery.push( { $match: { $and: findAndQuery } } );
1770
+
1771
+ if ( requestData.searchValue && requestData.searchValue != '' ) {
1772
+ findQuery.push( { $match: { $or: [ { storeName: { $regex: requestData.searchValue, $options: 'i' } } ] } } );
1773
+ }
1774
+
1775
+ if ( requestData.checklistType == 'storeopenandclose' ) {
1776
+ // console.log( 'check1' );
1777
+ findQuery.push( {
1778
+ $project: {
1779
+ checkListName: 1,
1780
+ createdByName: 1,
1781
+ userName: 1,
1782
+ userEmail: 1,
1783
+ checklistStatus: 1,
1784
+ submitTime_string: 1,
1785
+ date_string: 1,
1786
+ storeName: 1,
1787
+ checkListType: 1,
1788
+ scheduleRepeatedType: 1,
1789
+ detectionFlag: { $sum: [ '$mobileDetectionFlag', '$storeOpenCloseFlag', '$uniformDetectionFlag' ] },
1790
+ date_iso: 1,
1791
+ store_id: 1,
1792
+ openTime: '09:05 AM',
1793
+ lateOpenFlag: '1',
1794
+ CloseTime: '10:00 PM',
1795
+ closeTimeFlag: '0',
1796
+ },
1797
+ } );
1798
+ } else if ( requestData.checklistType == 'uniformdetection' || requestData.checklistType == 'mobileusagedetection' || requestData.checklistType == 'custom' ) {
1799
+ // console.log( 'check2' );
1800
+ findQuery.push( {
1801
+ $project: {
1802
+ checkListName: 1,
1803
+ createdByName: 1,
1804
+ userName: 1,
1805
+ userEmail: 1,
1806
+ checklistStatus: 1,
1807
+ submitTime_string: 1,
1808
+ date_string: 1,
1809
+ storeName: 1,
1810
+ checkListType: 1,
1811
+ scheduleRepeatedType: 1,
1812
+ detectionFlag: { $sum: [ '$mobileDetectionFlag', '$storeOpenCloseFlag', '$uniformDetectionFlag' ] },
1813
+ date_iso: 1,
1814
+ store_id: 1,
1815
+ },
1816
+ } );
1817
+ }
1818
+
1819
+ let getTotalCount = await processedchecklistService.aggregate( findQuery );
1820
+ if ( !getTotalCount.length ) {
1821
+ return res.sendError( { error: 'No Data Found' }, 204 );
1822
+ }
1823
+
1824
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
1825
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
1826
+ } else {
1827
+ findQuery.push( { $sort: { ['detectionFlag']: -1 } } );
1828
+ }
1829
+
1830
+ let limit = parseInt( requestData?.limit ) || 10;
1831
+ let skip = limit * ( requestData?.offset ) || 0;
1832
+ findQuery.push( { $skip: skip }, { $limit: limit } );
1833
+ let getChecklistPerformanceData = await processedchecklistService.aggregate( findQuery );
1834
+
1835
+ result.totalCount = getTotalCount.length;
1836
+ result.checklistInfo = getChecklistPerformanceData;
1837
+ return res.sendSuccess( result );
1838
+ } catch ( error ) {
1839
+ console.log( 'error =>', error );
1840
+ logger.error( { error: error, message: req.query, function: 'flagDetectionTables' } );
1841
+ return res.sendError( { error: error }, 500 );
1842
+ }
1843
+ };
1844
+
1845
+ export const overallCards = async ( req, res ) => {
1846
+ try {
1847
+ let requestData = req.body;
1848
+ let resultData = await overallCardsData( requestData.checklistType );
1849
+ return res.sendSuccess( resultData );
1850
+ } catch ( error ) {
1851
+ console.log( 'error =>', error );
1852
+ logger.error( { error: error, function: 'overallCards' } );
1853
+ return res.sendError( error, 500 );
1854
+ }
1855
+ };
1856
+
1857
+ export const overallComparisonCards = async ( req, res ) => {
1858
+ try {
1859
+ let requestData = req.body;
1860
+ let resultData = await checklistComparisonData( requestData.checklistType );
1861
+ return res.sendSuccess( resultData );
1862
+ } catch ( error ) {
1863
+ console.log( 'error =>', error );
1864
+ logger.error( { error: error, function: 'overallComparisonCards' } );
1865
+ return res.sendError( error, 500 );
1866
+ }
1867
+ };
1868
+
1869
+ async function overallCardsData( checklistType ) {
1870
+ try {
1871
+ let resData = {};
1872
+ let activeUnique = 50;
1873
+ let notSubmittedInstances = {
1874
+ 'count': 300,
1875
+ 'open': 100,
1876
+ 'inprogress': 200,
1877
+ };
1878
+ let totalInstances = {
1879
+ 'count': 500,
1880
+ };
1881
+ let completedInstances = {
1882
+ 'count': 200,
1883
+ };
1884
+ let flags = {
1885
+ 'count': 50,
1886
+ };
1887
+ let completionScore = {
1888
+ 'count': 49,
1889
+ };
1890
+ let complianceRate = {
1891
+ 'count': 66,
1892
+ };
1893
+
1894
+ resData.overallCards = {
1895
+ 'activeUnique': activeUnique,
1896
+ 'totalInstances': totalInstances,
1897
+ 'notSubmittedInstances': notSubmittedInstances,
1898
+ 'completedInstances': completedInstances,
1899
+ 'flags': flags,
1900
+ 'completionScore': completionScore,
1901
+ 'complianceRate': complianceRate,
1902
+ };
1903
+
1904
+ return resData;
1905
+ } catch ( error ) {
1906
+ console.log( 'error =>', error );
1907
+ logger.error( { error: error, message: data, function: 'returnServerError' } );
1908
+ }
1909
+ }
1910
+
1911
+ async function checklistComparisonData( checklistType ) {
1912
+ try {
1913
+ let resData = {};
1914
+ let flags = {
1915
+ 'comparisonData': 40,
1916
+ 'ComparisonFlag': true,
1917
+ };
1918
+ let completionScore = {
1919
+ 'comparisonData': 20,
1920
+ 'ComparisonFlag': false,
1921
+ };
1922
+ let complianceRate = {
1923
+ 'comparisonData': 60,
1924
+ 'ComparisonFlag': true,
1925
+ };
1926
+
1927
+ resData.overallComparisonCards = {
1928
+ 'flags': flags,
1929
+ 'completionScore': completionScore,
1930
+ 'complianceRate': complianceRate,
1931
+ };
1932
+
1933
+ return resData;
1934
+ } catch ( error ) {
1935
+ console.log( 'error =>', error );
1936
+ logger.error( { error: error, message: data, function: 'returnServerError' } );
1937
+ }
1938
+ }
1939
+
1940
+ export const infoCards = async ( req, res ) => {
1941
+ try {
1942
+ let requestData = req.body;
1943
+ let resultData = await infoCardsData( requestData.checklistType );
1944
+ return res.sendSuccess( resultData );
1945
+ } catch ( error ) {
1946
+ console.log( 'error =>', error );
1947
+ logger.error( { error: error, function: 'infoCards' } );
1948
+ return res.sendError( error, 500 );
1949
+ }
1950
+ };
1951
+
1952
+ export const infoComparisonCards = async ( req, res ) => {
1953
+ try {
1954
+ let requestData = req.body;
1955
+ let resultData = await infoCardsComparisonData( requestData.checklistType );
1956
+ return res.sendSuccess( resultData );
1957
+ } catch ( error ) {
1958
+ console.log( 'error =>', error );
1959
+ logger.error( { error: error, function: 'infoComparisonCards' } );
1960
+ return res.sendError( error, 500 );
1961
+ }
1962
+ };
1963
+
1964
+ async function infoCardsData( checklistType ) {
1965
+ try {
1966
+ let resData = {};
1967
+ let activeUnique = 800;
1968
+ let notSubmittedInstances = {
1969
+ 'count': 500,
1970
+ 'open': 300,
1971
+ 'inprogress': 200,
1972
+ };
1973
+ let totalInstances = {
1974
+ 'count': 300,
1975
+ };
1976
+ let completedInstances = {
1977
+ 'count': 70,
1978
+ };
1979
+ let flags = {
1980
+ 'count': 60,
1981
+ };
1982
+ let completionScore = {
1983
+ 'count': 10,
1984
+ };
1985
+ let complianceRate = {
1986
+ 'count': 4,
1987
+ };
1988
+
1989
+ resData.overallCards = {
1990
+ 'activeUnique': activeUnique,
1991
+ 'totalInstances': totalInstances,
1992
+ 'notSubmittedInstances': notSubmittedInstances,
1993
+ 'completedInstances': completedInstances,
1994
+ 'flags': flags,
1995
+ 'completionScore': completionScore,
1996
+ 'complianceRate': complianceRate,
1997
+ };
1998
+
1999
+ return resData;
2000
+ } catch ( error ) {
2001
+ console.log( 'error =>', error );
2002
+ logger.error( { error: error, message: data, function: 'returnServerError' } );
2003
+ }
2004
+ }
2005
+
2006
+ async function infoCardsComparisonData( checklistType ) {
2007
+ try {
2008
+ let resData = {};
2009
+ let totalInstances = {
2010
+ 'comparisonData': 30,
2011
+ 'ComparisonFlag': false,
2012
+ };
2013
+ let completedInstances = {
2014
+ 'comparisonData': 50,
2015
+ 'ComparisonFlag': true,
2016
+ };
2017
+ let flags = {
2018
+ 'comparisonData': 40,
2019
+ 'ComparisonFlag': true,
2020
+ };
2021
+ let completionScore = {
2022
+ 'comparisonData': 20,
2023
+ 'ComparisonFlag': false,
2024
+ };
2025
+ let complianceRate = {
2026
+ 'comparisonData': 60,
2027
+ 'ComparisonFlag': true,
2028
+ };
2029
+
2030
+ resData.overallComparisonCards = {
2031
+ 'totalInstances': totalInstances,
2032
+ 'completedInstances': completedInstances,
2033
+ 'flags': flags,
2034
+ 'completionScore': completionScore,
2035
+ 'complianceRate': complianceRate,
2036
+ };
2037
+
2038
+ return resData;
2039
+ } catch ( error ) {
2040
+ console.log( 'error =>', error );
2041
+ logger.error( { error: error, message: data, function: 'returnServerError' } );
2042
+ }
2043
+ }
2044
+
2045
+ async function monthlyChartData( checklistType ) {
2046
+ try {
2047
+ let resData = {};
2048
+ resData.totalCount = 300;
2049
+ resData.monthlyChartData = {};
2050
+ resData.monthlyChartData = [
2051
+ { 'date': 1, 'colorClass': 'primary900' },
2052
+ { 'date': 3, 'colorClass': 'blue33B5FF' },
2053
+ { 'date': 6, 'colorClass': 'redF04438' },
2054
+ { 'date': 7, 'colorClass': 'lightBlue6BCAFF' },
2055
+ { 'date': 8, 'colorClass': 'blue33B5FF' },
2056
+ { 'date': 9, 'colorClass': 'primary900' },
2057
+ { 'date': 12, 'colorClass': 'primary900' },
2058
+ { 'date': 14, 'colorClass': 'orangeF79009' },
2059
+ { 'date': 15, 'colorClass': 'blue33B5FF' },
2060
+ { 'date': 18, 'colorClass': 'redF04438' },
2061
+ ];
2062
+
2063
+ return resData;
2064
+ } catch ( error ) {
2065
+ console.log( 'error =>', error );
2066
+ logger.error( { error: error, message: data, function: 'returnServerError' } );
2067
+ }
2068
+ }
2069
+
2070
+ export const overallCardsV1 = async ( req, res ) => {
2071
+ try {
2072
+ let requestData = req.body;
2073
+ let fromDate = new Date( requestData.fromDate );
2074
+ let toDate = new Date( requestData.toDate );
2075
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
2076
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
2077
+ toDate.setUTCHours( 23, 59, 59, 59 );
2078
+ let activeUnique = 0;
2079
+ let notSubmittedInstances = {
2080
+ 'count': 0,
2081
+ 'open': 0,
2082
+ 'inprogress': 0,
2083
+ };
2084
+ let totalInstances = {
2085
+ 'count': 0,
2086
+ };
2087
+ let completedInstances = {
2088
+ 'count': 0,
2089
+ };
2090
+ let flags = {
2091
+ 'count': 0,
2092
+ };
2093
+ let completionScore = {
2094
+ 'count': 0,
2095
+ };
2096
+ let complianceRate = {
2097
+ 'count': 0,
2098
+ };
2099
+ let result = {};
2100
+
2101
+ let findQuery = [];
2102
+ let findUniqueQuery = [];
2103
+ let findAndQuery = [];
2104
+ findAndQuery.push(
2105
+ { checkListType: { $eq: 'custom' } },
2106
+ { date_iso: { $gte: fromDate, $lte: toDate } },
2107
+ { client_id: requestData.clientId },
2108
+ { store_id: { $in: requestData.storeId } },
2109
+ );
2110
+
2111
+ findQuery.push( { $match: { $and: findAndQuery } } );
2112
+ findQuery.push( {
2113
+ $group: {
2114
+ _id: '$checklistStatus',
2115
+ count: { $sum: 1 },
2116
+ totalQuestionCount: { $sum: '$questionCount' },
2117
+ totalQuestionFlagCount: { $sum: '$questionFlag' },
2118
+ totalTimeFlag: { $sum: '$timeFlag' },
2119
+ },
2120
+ } );
2121
+ findQuery.push( {
2122
+ $group: {
2123
+ _id: null,
2124
+ totalChecklist: { $sum: '$count' },
2125
+ statusCounts: {
2126
+ $push: { k: '$_id', v: '$count' },
2127
+ },
2128
+ submiitedQuestionCount: {
2129
+ $push: { k: '$_id', v: '$totalQuestionCount' },
2130
+ },
2131
+ flaggedQuestionCount: {
2132
+ $push: { k: '$_id', v: '$totalQuestionFlagCount' },
2133
+ },
2134
+ questionCountSum: { $sum: '$totalQuestionCount' },
2135
+ questionFlagCountSum: { $sum: '$totalQuestionFlagCount' },
2136
+ timeFlagSum: { $sum: '$totalTimeFlag' },
2137
+ },
2138
+ } );
2139
+ findQuery.push( {
2140
+ $addFields: {
2141
+ statusCounts: { $arrayToObject: '$statusCounts' },
2142
+ submittedChecklistQuestionCount: { $arrayToObject: '$submiitedQuestionCount' },
2143
+ submittedChecklistFlagQuestionCount: { $arrayToObject: '$flaggedQuestionCount' },
2144
+ },
2145
+ } );
2146
+ findQuery.push( {
2147
+ $project: {
2148
+ totalChecklist: 1,
2149
+ questionCountSum: 1,
2150
+ questionFlagCountSum: 1,
2151
+ timeFlagSum: 1,
2152
+ statusCounts: 1,
2153
+ submittedChecklistQuestionCount: 1,
2154
+ submittedChecklistFlagQuestionCount: 1,
2155
+ },
2156
+ } );
2157
+ findQuery.push( {
2158
+ $project: {
2159
+ totalChecklist: 1,
2160
+ notSubmittedChecklist: { $add: [ { $ifNull: [ '$statusCounts.open', 0 ] }, { $ifNull: [ '$statusCounts.inprogress', 0 ] } ] },
2161
+ openChecklist: { $ifNull: [ '$statusCounts.open', 0 ] },
2162
+ inprogressChecklist: { $ifNull: [ '$statusCounts.inprogress', 0 ] },
2163
+ submittedChecklist: { $ifNull: [ '$statusCounts.submit', 0 ] },
2164
+ flaggedChecklist: { $add: [ { $ifNull: [ '$timeFlagSum', 0 ] }, { $ifNull: [ '$questionFlagCountSum', 0 ] } ] },
2165
+ completionScore: { $round: [ { $multiply: [ { $divide: [ { $ifNull: [ '$statusCounts.submit', 0 ] }, { $ifNull: [ '$totalChecklist', 0 ] } ] }, 100 ] }, 0 ] },
2166
+ // completionScore: { $multiply: [ { $divide: [ '$statusCounts.submit', '$totalChecklist' ] }, 100 ] },
2167
+ questionFlagCount: { $ifNull: [ '$questionFlagCountSum', 0 ] },
2168
+ questionCount: { $ifNull: [ '$questionCountSum', 0 ] },
2169
+ correctAnswers: { $subtract: [ { $ifNull: [ '$submittedChecklistQuestionCount.submit', 0 ] }, { $ifNull: [ '$submittedChecklistFlagQuestionCount.submit', 0 ] } ] },
2170
+ },
2171
+ } );
2172
+ findQuery.push( {
2173
+ $project: {
2174
+ totalChecklist: 1,
2175
+ notSubmittedChecklist: 1,
2176
+ openChecklist: 1,
2177
+ inprogressChecklist: 1,
2178
+ submittedChecklist: 1,
2179
+ flaggedChecklist: 1,
2180
+ completionScore: 1,
2181
+ questionFlagCount: 1,
2182
+ questionCount: 1,
2183
+ questionCompletionScore: {
2184
+ $cond: {
2185
+ if: { $eq: [ '$questionCount', 0 ] },
2186
+ then: 0,
2187
+ else: {
2188
+ $round: [ { $multiply: [ { $divide: [ '$correctAnswers', '$questionCount' ] }, 100 ] }, 0 ],
2189
+ // $multiply: [ { $divide: [ '$correctAnswers', '$questionCount' ] }, 100 ],
2190
+ },
2191
+ },
2192
+ },
2193
+ },
2194
+ } );
2195
+ console.log( 'overallCardsV1 Query Hit 1=>' );
2196
+ findUniqueQuery.push( { $match: { $and: findAndQuery } } );
2197
+ findUniqueQuery.push( {
2198
+ $project: {
2199
+ sourceCheckList_id: 1,
2200
+ },
2201
+ } );
2202
+ findUniqueQuery.push( {
2203
+ $group: {
2204
+ _id: '$sourceCheckList_id',
2205
+ },
2206
+ } );
2207
+ findUniqueQuery.push( {
2208
+ $count: 'totalUniqueChecklist',
2209
+ } );
2210
+
2211
+ const [ getOverallChecklistData, getUniqueChecklistData ] = await Promise.all( [
2212
+ processedchecklistService.aggregate( findQuery ),
2213
+ processedchecklistService.aggregate( findUniqueQuery ),
2214
+ ] );
2215
+
2216
+
2217
+ if ( getUniqueChecklistData.length && getUniqueChecklistData.length>0 ) {
2218
+ if ( getUniqueChecklistData[0].totalUniqueChecklist ) {
2219
+ activeUnique = getUniqueChecklistData[0]?.totalUniqueChecklist || 0;
2220
+ }
2221
+ }
2222
+
2223
+ if ( getOverallChecklistData.length && getOverallChecklistData.length>0 ) {
2224
+ if ( getOverallChecklistData[0].totalChecklist ) {
2225
+ totalInstances.count = getOverallChecklistData[0]?.totalChecklist || 0;
2226
+ }
2227
+ if ( getOverallChecklistData[0].notSubmittedChecklist ) {
2228
+ notSubmittedInstances.count = getOverallChecklistData[0]?.notSubmittedChecklist || 0;
2229
+ }
2230
+ if ( getOverallChecklistData[0].openChecklist ) {
2231
+ notSubmittedInstances.open = getOverallChecklistData[0]?.openChecklist || 0;
2232
+ }
2233
+ if ( getOverallChecklistData[0].inprogressChecklist ) {
2234
+ notSubmittedInstances.inprogress = getOverallChecklistData[0]?.inprogressChecklist || 0;
2235
+ }
2236
+ if ( getOverallChecklistData[0].submittedChecklist ) {
2237
+ completedInstances.count = getOverallChecklistData[0]?.submittedChecklist || 0;
2238
+ }
2239
+ if ( getOverallChecklistData[0].flaggedChecklist ) {
2240
+ flags.count = getOverallChecklistData[0]?.flaggedChecklist || 0;
2241
+ }
2242
+ if ( getOverallChecklistData[0].completionScore ) {
2243
+ completionScore.count = Math.round( getOverallChecklistData[0]?.completionScore ) || 0; // .toFixed( 2 )
2244
+ }
2245
+ if ( getOverallChecklistData[0].questionCompletionScore ) {
2246
+ complianceRate.count = Math.round( getOverallChecklistData[0]?.questionCompletionScore ) || 0; // .toFixed( 2 )
2247
+ }
2248
+ }
2249
+ result.overallCards = {
2250
+ 'activeUnique': activeUnique,
2251
+ 'totalInstances': totalInstances,
2252
+ 'notSubmittedInstances': notSubmittedInstances,
2253
+ 'completedInstances': completedInstances,
2254
+ 'flags': flags,
2255
+ 'completionScore': completionScore,
2256
+ 'complianceRate': complianceRate,
2257
+ };
2258
+ return res.sendSuccess( result );
2259
+ } catch ( error ) {
2260
+ logger.error( { error: error, message: req.query, function: 'overallCards' } );
2261
+ return res.sendError( { error: error }, 500 );
2262
+ }
2263
+ };
2264
+
2265
+ export const overallComparisonCardsV1 = async ( req, res ) => {
2266
+ try {
2267
+ let requestData = req.body;
2268
+ let toDate = new Date( requestData.toDate );
2269
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
2270
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
2271
+ toDate.setUTCHours( 23, 59, 59, 59 );
2272
+
2273
+ let rangeOneFromDate;
2274
+ let rangeOneToDate;
2275
+ let rangeTwoFromDate;
2276
+ let rangeTwoToDate;
2277
+ if ( requestData.dateType == 'weekly' ) {
2278
+ rangeOneFromDate = new Date( requestData.toDate );
2279
+ rangeOneFromDate.setDate( rangeOneFromDate.getDate() - 6 );
2280
+ rangeOneToDate = new Date( requestData.toDate );
2281
+ rangeOneToDate = new Date( rangeOneToDate.getTime() - userTimezoneOffset );
2282
+ rangeOneToDate.setUTCHours( 23, 59, 59, 59 );
2283
+ rangeTwoToDate = new Date( requestData.toDate );
2284
+ rangeTwoToDate.setDate( rangeTwoToDate.getDate() - 7 );
2285
+ rangeTwoFromDate = new Date( rangeTwoToDate );
2286
+ rangeTwoFromDate.setDate( rangeTwoFromDate.getDate() - 6 );
2287
+ rangeTwoToDate = new Date( rangeTwoToDate.getTime() - userTimezoneOffset );
2288
+ rangeTwoToDate.setUTCHours( 23, 59, 59, 59 );
2289
+ } else if ( requestData.dateType == 'monthly' ) {
2290
+ rangeOneFromDate = new Date( requestData.toDate );
2291
+ rangeOneFromDate.setDate( rangeOneFromDate.getDate() - 30 );
2292
+ rangeOneToDate = new Date( requestData.toDate );
2293
+ rangeOneToDate = new Date( rangeOneToDate.getTime() - userTimezoneOffset );
2294
+ rangeOneToDate.setUTCHours( 23, 59, 59, 59 );
2295
+ rangeTwoToDate = new Date( requestData.toDate );
2296
+ rangeTwoToDate.setDate( rangeTwoToDate.getDate() - 31 );
2297
+ rangeTwoFromDate = new Date( rangeTwoToDate );
2298
+ rangeTwoFromDate.setDate( rangeTwoFromDate.getDate() - 30 );
2299
+ rangeTwoToDate = new Date( rangeTwoToDate.getTime() - userTimezoneOffset );
2300
+ rangeTwoToDate.setUTCHours( 23, 59, 59, 59 );
2301
+ } else {
2302
+ rangeOneFromDate = new Date( requestData.toDate );
2303
+ rangeOneFromDate.setDate( rangeOneFromDate.getDate() - 0 );
2304
+ rangeOneToDate = new Date( requestData.toDate );
2305
+ rangeOneToDate = new Date( rangeOneToDate.getTime() - userTimezoneOffset );
2306
+ rangeOneToDate.setUTCHours( 23, 59, 59, 59 );
2307
+ rangeTwoToDate = new Date( requestData.toDate );
2308
+ rangeTwoToDate.setDate( rangeTwoToDate.getDate() - 1 );
2309
+ rangeTwoFromDate = new Date( rangeTwoToDate );
2310
+ rangeTwoFromDate.setDate( rangeTwoFromDate.getDate() - 0 );
2311
+ rangeTwoToDate = new Date( rangeTwoToDate.getTime() - userTimezoneOffset );
2312
+ rangeTwoToDate.setUTCHours( 23, 59, 59, 59 );
2313
+ }
2314
+
2315
+ // let rangeOneFromDate = new Date( requestData.toDate );
2316
+ // rangeOneFromDate.setDate( rangeOneFromDate.getDate() - 6 );
2317
+ // let rangeOneToDate = new Date( requestData.toDate );
2318
+ // rangeOneToDate = new Date( rangeOneToDate.getTime() - userTimezoneOffset );
2319
+ // rangeOneToDate.setUTCHours( 23, 59, 59, 59 );
2320
+ // let rangeTwoToDate = new Date( requestData.toDate );
2321
+ // rangeTwoToDate.setDate( rangeTwoToDate.getDate() - 7 );
2322
+ // let rangeTwoFromDate = new Date( rangeTwoToDate );
2323
+ // rangeTwoFromDate.setDate( rangeTwoFromDate.getDate() - 6 );
2324
+ // rangeTwoToDate = new Date( rangeTwoToDate.getTime() - userTimezoneOffset );
2325
+ // rangeTwoToDate.setUTCHours( 23, 59, 59, 59 );
2326
+
2327
+ console.log( 'rangeOneFromDate =>', rangeOneFromDate );
2328
+ console.log( 'rangeOneToDate =>', rangeOneToDate );
2329
+ console.log( 'rangeTwoFromDate =>', rangeTwoFromDate );
2330
+ console.log( 'rangeTwoToDate =>', rangeTwoToDate );
2331
+
2332
+ let flags = {
2333
+ 'comparisonData': 0,
2334
+ 'ComparisonFlag': 0,
2335
+ };
2336
+ let completionScore = {
2337
+ 'comparisonData': 0,
2338
+ 'ComparisonFlag': 0,
2339
+ };
2340
+ let complianceRate = {
2341
+ 'comparisonData': 0,
2342
+ 'ComparisonFlag': 0,
2343
+ };
2344
+
2345
+ let overallComparisonCards = {
2346
+ 'flags': flags,
2347
+ 'completionScore': completionScore,
2348
+ 'complianceRate': complianceRate,
2349
+ };
2350
+
2351
+ let rangeOneFindQuery = [];
2352
+ let rangeOneFindAndQuery = [];
2353
+ rangeOneFindAndQuery.push(
2354
+ { checkListType: { $eq: 'custom' } },
2355
+ { date_iso: { $gte: rangeOneFromDate, $lte: rangeOneToDate } },
2356
+ { client_id: requestData.clientId },
2357
+ { store_id: { $in: requestData.storeId } },
2358
+ );
2359
+
2360
+ rangeOneFindQuery.push( { $match: { $and: rangeOneFindAndQuery } } );
2361
+
2362
+ let queryFormat = [
2363
+ {
2364
+ $group: {
2365
+ _id: '$checklistStatus',
2366
+ count: { $sum: 1 },
2367
+ totalQuestionCount: { $sum: '$questionCount' },
2368
+ totalQuestionFlagCount: { $sum: '$questionFlag' },
2369
+ totalTimeFlag: { $sum: '$timeFlag' },
2370
+ },
2371
+ },
2372
+ {
2373
+ $group: {
2374
+ _id: null,
2375
+ totalChecklist: { $sum: '$count' },
2376
+ statusCounts: {
2377
+ $push: { k: '$_id', v: '$count' },
2378
+ },
2379
+ submiitedQuestionCount: {
2380
+ $push: { k: '$_id', v: '$totalQuestionCount' },
2381
+ },
2382
+ flaggedQuestionCount: {
2383
+ $push: { k: '$_id', v: '$totalQuestionFlagCount' },
2384
+ },
2385
+ questionCountSum: { $sum: '$totalQuestionCount' },
2386
+ questionFlagCountSum: { $sum: '$totalQuestionFlagCount' },
2387
+ timeFlagSum: { $sum: '$totalTimeFlag' },
2388
+ },
2389
+ },
2390
+ {
2391
+ $addFields: {
2392
+ statusCounts: { $arrayToObject: '$statusCounts' },
2393
+ submittedChecklistQuestionCount: { $arrayToObject: '$submiitedQuestionCount' },
2394
+ submittedChecklistFlagQuestionCount: { $arrayToObject: '$flaggedQuestionCount' },
2395
+ },
2396
+ },
2397
+ {
2398
+ $project: {
2399
+ totalChecklist: 1,
2400
+ questionCountSum: 1,
2401
+ questionFlagCountSum: 1,
2402
+ timeFlagSum: 1,
2403
+ statusCounts: 1,
2404
+ submittedChecklistQuestionCount: 1,
2405
+ submittedChecklistFlagQuestionCount: 1,
2406
+ },
2407
+ },
2408
+ {
2409
+ $project: {
2410
+ totalChecklist: 1,
2411
+ notSubmittedChecklist: { $add: [ { $ifNull: [ '$statusCounts.open', 0 ] }, { $ifNull: [ '$statusCounts.inprogress', 0 ] } ] },
2412
+ openChecklist: { $ifNull: [ '$statusCounts.open', 0 ] },
2413
+ inprogressChecklist: { $ifNull: [ '$statusCounts.inprogress', 0 ] },
2414
+ submittedChecklist: { $ifNull: [ '$statusCounts.submit', 0 ] },
2415
+ flaggedChecklist: { $add: [ { $ifNull: [ '$timeFlagSum', 0 ] }, { $ifNull: [ '$questionFlagCountSum', 0 ] } ] },
2416
+ completionScore: { $round: [ { $multiply: [ { $divide: [ { $ifNull: [ '$statusCounts.submit', 0 ] }, { $ifNull: [ '$totalChecklist', 0 ] } ] }, 100 ] }, 0 ] },
2417
+ // completionScore: { $multiply: [ { $divide: [ '$statusCounts.submit', '$totalChecklist' ] }, 100 ] },
2418
+ questionFlagCount: { $ifNull: [ '$questionFlagCountSum', 0 ] },
2419
+ questionCount: { $ifNull: [ '$questionCountSum', 0 ] },
2420
+ correctAnswers: { $subtract: [ { $ifNull: [ '$submittedChecklistQuestionCount.submit', 0 ] }, { $ifNull: [ '$submittedChecklistFlagQuestionCount.submit', 0 ] } ] },
2421
+ },
2422
+ },
2423
+ {
2424
+ $project: {
2425
+ totalChecklist: 1,
2426
+ notSubmittedChecklist: 1,
2427
+ openChecklist: 1,
2428
+ inprogressChecklist: 1,
2429
+ submittedChecklist: 1,
2430
+ flaggedChecklist: 1,
2431
+ completionScore: 1,
2432
+ questionFlagCount: 1,
2433
+ questionCount: 1,
2434
+ questionCompletionScore: {
2435
+ $cond: {
2436
+ if: { $eq: [ '$questionCount', 0 ] },
2437
+ then: 0,
2438
+ else: {
2439
+ $round: [ { $multiply: [ { $divide: [ '$correctAnswers', '$questionCount' ] }, 100 ] }, 0 ],
2440
+ // $multiply: [ { $divide: [ '$correctAnswers', '$questionCount' ] }, 100 ],
2441
+ },
2442
+ },
2443
+ },
2444
+ },
2445
+ },
2446
+ ];
2447
+ rangeOneFindQuery.push( ...queryFormat );
2448
+ let rangeTwoFindQuery = [];
2449
+ let rangeTwoFindAndQuery = [];
2450
+ rangeTwoFindAndQuery.push(
2451
+ { checkListType: { $eq: 'custom' } },
2452
+ { date_iso: { $gte: rangeTwoFromDate, $lte: rangeTwoToDate } },
2453
+ { client_id: requestData.clientId },
2454
+ { store_id: { $in: requestData.storeId } },
2455
+ );
2456
+
2457
+ rangeTwoFindQuery.push( { $match: { $and: rangeTwoFindAndQuery } } );
2458
+ rangeTwoFindQuery.push( ...queryFormat );
2459
+ const [ rangeOneData, rangeTwoData ] = await Promise.all( [
2460
+ processedchecklistService.aggregate( rangeOneFindQuery ),
2461
+ processedchecklistService.aggregate( rangeTwoFindQuery ),
2462
+ ] );
2463
+ console.log( 'rangeTwoData =>', rangeTwoData );
2464
+ console.log( 'rangeOneData =>', rangeOneData );
2465
+
2466
+
2467
+ if ( rangeOneData.length >0 && rangeTwoData.length >0 ) {
2468
+ overallComparisonCards.flags.comparisonData = Math.round( ( ( ( rangeOneData[0].flaggedChecklist - rangeTwoData[0].flaggedChecklist )/rangeTwoData[0].flaggedChecklist )*100 ) ); // .toFixed( 2 )
2469
+ if ( overallComparisonCards.flags.comparisonData > 0 ) {
2470
+ overallComparisonCards.flags.ComparisonFlag = true;
2471
+ } else {
2472
+ overallComparisonCards.flags.ComparisonFlag = false;
2473
+ }
2474
+ overallComparisonCards.completionScore.comparisonData = Math.round( ( ( ( rangeOneData[0].completionScore - rangeTwoData[0].completionScore )/rangeTwoData[0].completionScore )*100 ) ); // .toFixed( 2 )
2475
+ if ( overallComparisonCards.completionScore.comparisonData > 0 ) {
2476
+ overallComparisonCards.completionScore.ComparisonFlag = true;
2477
+ } else {
2478
+ overallComparisonCards.completionScore.ComparisonFlag = false;
2479
+ }
2480
+ overallComparisonCards.complianceRate.comparisonData = Math.round( ( ( ( rangeOneData[0].questionCompletionScore - rangeTwoData[0].questionCompletionScore )/rangeTwoData[0].questionCompletionScore )*100 ) ); // .toFixed( 2 )
2481
+ if ( overallComparisonCards.complianceRate.comparisonData > 0 ) {
2482
+ overallComparisonCards.complianceRate.ComparisonFlag = true;
2483
+ } else {
2484
+ overallComparisonCards.complianceRate.ComparisonFlag = false;
2485
+ }
2486
+
2487
+ overallComparisonCards.flags.comparisonData = Math.abs( overallComparisonCards.flags.comparisonData );
2488
+ overallComparisonCards.completionScore.comparisonData = Math.abs( overallComparisonCards.completionScore.comparisonData );
2489
+ overallComparisonCards.complianceRate.comparisonData = Math.abs( overallComparisonCards.complianceRate.comparisonData );
2490
+ }
2491
+
2492
+ let result = {
2493
+ 'overallComparisonCards': overallComparisonCards,
2494
+ };
2495
+ console.log( 'result =>', result );
2496
+ return res.sendSuccess( result );
2497
+ } catch ( error ) {
2498
+ console.log( 'error =>', error );
2499
+ logger.error( { error: error, message: req.query, function: 'overallCards' } );
2500
+ return res.sendError( { error: error }, 500 );
2501
+ }
2502
+ };
2503
+
2504
+ export const reportChecklistTable = async ( req, res ) => {
2505
+ try {
2506
+ let requestData = req.body;
2507
+ let resultData = await reportChecklistData( requestData.checklistType );
2508
+ return res.sendSuccess( resultData );
2509
+ } catch ( error ) {
2510
+ console.log( 'error =>', error );
2511
+ logger.error( { error: error, function: 'subscribedStoreList' } );
2512
+ return res.sendError( error, 500 );
2513
+ }
2514
+ };
2515
+
2516
+ async function reportChecklistData( checklistType ) {
2517
+ try {
2518
+ let resData = {};
2519
+ resData.totalCount = 300;
2520
+ resData.reportChecklistTableData = [
2521
+ {
2522
+ 'submittedDate': '2024-10-20',
2523
+ 'checklistName': 'Field VM- Store Visit Checklist',
2524
+ 'storeName': 'LKST400',
2525
+ 'submittedBy': 'Balaji',
2526
+ 'submittedByChar': 'Ba',
2527
+ 'submittedTime': '11:02 AM, 01 Sep',
2528
+ 'flagCount': 50,
2529
+ },
2530
+ {
2531
+ 'submittedDate': '2024-10-19',
2532
+ 'checklistName': 'Field VM- Store Visit Checklist',
2533
+ 'storeName': 'LKST400',
2534
+ 'submittedBy': 'Balaji',
2535
+ 'submittedByChar': 'Ba',
2536
+ 'submittedTime': '11:02 AM, 01 Sep',
2537
+ 'flagCount': 50,
2538
+ },
2539
+ {
2540
+ 'submittedDate': '2024-10-19',
2541
+ 'checklistName': 'DC Prep Checklist',
2542
+ 'storeName': 'LKST400',
2543
+ 'submittedBy': 'Balaji',
2544
+ 'submittedByChar': 'Ba',
2545
+ 'submittedTime': '11:02 AM, 01 Sep',
2546
+ 'flagCount': 50,
2547
+ },
2548
+ {
2549
+ 'submittedDate': '2024-10-20',
2550
+ 'checklistName': 'DC Prep Checklist',
2551
+ 'storeName': 'LKST400',
2552
+ 'submittedBy': 'Balaji',
2553
+ 'submittedByChar': 'Ba',
2554
+ 'submittedTime': '11:02 AM, 01 Sep',
2555
+ 'flagCount': 50,
2556
+ },
2557
+ ];
2558
+ return resData;
2559
+ } catch ( error ) {
2560
+ console.log( 'error =>', error );
2561
+ logger.error( { error: error, message: data, function: 'returnServerError' } );
2562
+ }
2563
+ }
2564
+
2565
+ export const infoCardsV1 = async ( req, res ) => {
2566
+ try {
2567
+ let requestData = req.body;
2568
+ let fromDate = new Date( requestData.fromDate );
2569
+ let toDate = new Date( requestData.toDate );
2570
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
2571
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
2572
+ toDate.setUTCHours( 23, 59, 59, 59 );
2573
+ let activeUnique = 0;
2574
+ let notSubmittedInstances = {
2575
+ 'count': 0,
2576
+ 'open': 0,
2577
+ 'inprogress': 0,
2578
+ };
2579
+ let totalInstances = {
2580
+ 'count': 0,
2581
+ };
2582
+ let completedInstances = {
2583
+ 'count': 0,
2584
+ };
2585
+ let flags = {
2586
+ 'count': 0,
2587
+ };
2588
+ let completionScore = {
2589
+ 'count': 0,
2590
+ };
2591
+ let complianceRate = {
2592
+ 'count': 0,
2593
+ };
2594
+ let result = {};
2595
+
2596
+ let findQuery = [];
2597
+ let findUniqueQuery = [];
2598
+ let findAndQuery = [];
2599
+ findAndQuery.push(
2600
+ { client_id: requestData.clientId },
2601
+ { store_id: { $in: requestData.storeId } },
2602
+ { date_iso: { $gte: fromDate } },
2603
+ { date_iso: { $lte: toDate } },
2604
+ { checkListType: { $eq: 'custom' } },
2605
+ );
2606
+
2607
+ if ( requestData.groupByType == 'Checklist' ) {
2608
+ findAndQuery.push( { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.groupByValue ) } );
2609
+ }
2610
+
2611
+ if ( requestData.groupByType == 'User' ) {
2612
+ findAndQuery.push( { userEmail: requestData.groupByValue } );
2613
+ }
2614
+
2615
+ if ( requestData.groupByType == 'Store' ) {
2616
+ findAndQuery.push( { store_id: requestData.groupByValue } );
2617
+ }
2618
+
2619
+ findQuery.push( { $match: { $and: findAndQuery } } );
2620
+
2621
+ findQuery.push( {
2622
+ $project: {
2623
+ sourceCheckList_id: 1,
2624
+ checkListId: 1,
2625
+ checklistStatus: 1,
2626
+ timeFlag: 1,
2627
+ questionFlag: 1,
2628
+ mobileDetectionFlag: 1,
2629
+ storeOpenCloseFlag: 1,
2630
+ uniformDetectionFlag: 1,
2631
+ markasread: 1,
2632
+ checkListType: 1,
2633
+ questionCount: 1,
2634
+ },
2635
+ } );
2636
+
2637
+ findQuery.push( {
2638
+ $group: {
2639
+ _id: null,
2640
+ totalChecklist: { $sum: 1 },
2641
+ notSubmittedChecklist: {
2642
+ $sum: { $cond: [ { $ne: [ '$checklistStatus', 'submit' ] }, 1, 0 ] },
2643
+ },
2644
+ openChecklist: {
2645
+ $sum: { $cond: [ { $eq: [ '$checklistStatus', 'open' ] }, 1, 0 ] },
2646
+ },
2647
+ inprogressChecklist: {
2648
+ $sum: { $cond: [ { $eq: [ '$checklistStatus', 'inprogress' ] }, 1, 0 ] },
2649
+ },
2650
+ submittedChecklist: {
2651
+ $sum: { $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, 1, 0 ] },
2652
+ },
2653
+ timeFlag: { $sum: '$timeFlag' },
2654
+ questionFlagCount: { $sum: '$questionFlag' },
2655
+ questionCount: { $sum: '$questionCount' },
2656
+ submittedQuestionCount: {
2657
+ $sum: {
2658
+ $cond: [
2659
+ { $eq: [ '$checklistStatus', 'submit' ] },
2660
+ '$questionCount', // Sum this field if status is 'submit'
2661
+ 0, // Otherwise, add 0
2662
+ ],
2663
+ },
2664
+ },
2665
+ },
2666
+ } );
2667
+ findQuery.push( {
2668
+ $project: {
2669
+ totalChecklist: 1,
2670
+ notSubmittedChecklist: 1,
2671
+ openChecklist: 1,
2672
+ inprogressChecklist: 1,
2673
+ submittedChecklist: 1,
2674
+ questionFlagCount: 1,
2675
+ questionCount: 1,
2676
+ timeFlag: 1,
2677
+ submittedQuestionCount: 1,
2678
+ flaggedChecklist: { $add: [ { $ifNull: [ '$timeFlag', 0 ] }, { $ifNull: [ '$questionFlagCount', 0 ] } ] },
2679
+ completionScore: { $round: [ { $multiply: [ { $divide: [ { $ifNull: [ '$submittedChecklist', 0 ] }, { $ifNull: [ '$totalChecklist', 0 ] } ] }, 100 ] }, 0 ] },
2680
+ // completionScore: { $multiply: [ { $divide: [ '$submittedChecklist', '$totalChecklist' ] }, 100 ] },
2681
+ correctAnswers: { $subtract: [ { $ifNull: [ '$submittedQuestionCount', 0 ] }, { $ifNull: [ '$questionFlagCount', 0 ] } ] },
2682
+ },
2683
+ } );
2684
+ findQuery.push( {
2685
+ $project: {
2686
+ totalChecklist: 1,
2687
+ notSubmittedChecklist: 1,
2688
+ openChecklist: 1,
2689
+ inprogressChecklist: 1,
2690
+ submittedChecklist: 1,
2691
+ flaggedChecklist: 1,
2692
+ completionScore: 1,
2693
+ questionFlagCount: 1,
2694
+ questionCount: 1,
2695
+ timeFlag: 1,
2696
+ correctAnswers: 1,
2697
+ submittedQuestionCount: 1,
2698
+ questionCompletionScore: {
2699
+ $cond: {
2700
+ if: { $eq: [ '$questionCount', 0 ] },
2701
+ then: 0,
2702
+ else: {
2703
+ $round: [ { $multiply: [ { $divide: [ { $ifNull: [ '$correctAnswers', 0 ] }, { $ifNull: [ '$questionCount', 0 ] } ] }, 100 ] }, 0 ],
2704
+ // $multiply: [ { $divide: [ '$correctAnswers', '$questionCount' ] }, 100 ],
2705
+ },
2706
+ },
2707
+ },
2708
+ },
2709
+ } );
2710
+ let getOverallChecklistData = await processedchecklistService.aggregate( findQuery );
2711
+ console.log( 'infoCardsV1 =>', getOverallChecklistData );
2712
+ if ( !getOverallChecklistData.length ) {
2713
+ return res.sendError( { error: 'No Data Found' }, 204 );
2714
+ }
2715
+
2716
+ findUniqueQuery.push( { $match: { $and: findAndQuery } } );
2717
+ findUniqueQuery.push( {
2718
+ $project: {
2719
+ sourceCheckList_id: 1,
2720
+ },
2721
+ } );
2722
+ findUniqueQuery.push( {
2723
+ $group: {
2724
+ _id: '$sourceCheckList_id',
2725
+ },
2726
+ } );
2727
+ findUniqueQuery.push( {
2728
+ $count: 'totalUniqueChecklist',
2729
+ } );
2730
+ let getUniqueChecklistData = await processedchecklistService.aggregate( findUniqueQuery );
2731
+ if ( !getUniqueChecklistData.length && !getOverallChecklistData.length ) {
2732
+ return res.sendError( { error: 'No Data Found' }, 204 );
2733
+ }
2734
+
2735
+ if ( getUniqueChecklistData.length && getUniqueChecklistData.length>0 ) {
2736
+ if ( getUniqueChecklistData[0].totalUniqueChecklist ) {
2737
+ activeUnique = getUniqueChecklistData[0]?.totalUniqueChecklist || 0;
2738
+ }
2739
+ }
2740
+
2741
+ if ( getOverallChecklistData.length && getOverallChecklistData.length>0 ) {
2742
+ if ( getOverallChecklistData[0].totalChecklist ) {
2743
+ totalInstances.count = getOverallChecklistData[0]?.totalChecklist || 0;
2744
+ }
2745
+ if ( getOverallChecklistData[0].notSubmittedChecklist ) {
2746
+ notSubmittedInstances.count = getOverallChecklistData[0]?.notSubmittedChecklist || 0;
2747
+ }
2748
+ if ( getOverallChecklistData[0].openChecklist ) {
2749
+ notSubmittedInstances.open = getOverallChecklistData[0]?.openChecklist || 0;
2750
+ }
2751
+ if ( getOverallChecklistData[0].inprogressChecklist ) {
2752
+ notSubmittedInstances.inprogress = getOverallChecklistData[0]?.inprogressChecklist || 0;
2753
+ }
2754
+ if ( getOverallChecklistData[0].submittedChecklist ) {
2755
+ completedInstances.count = getOverallChecklistData[0]?.submittedChecklist || 0;
2756
+ }
2757
+ if ( getOverallChecklistData[0].flaggedChecklist ) {
2758
+ flags.count = getOverallChecklistData[0]?.flaggedChecklist || 0;
2759
+ }
2760
+ if ( getOverallChecklistData[0].completionScore ) {
2761
+ completionScore.count = Math.round( getOverallChecklistData[0]?.completionScore ) || 0; // .toFixed( 2 )
2762
+ }
2763
+ if ( getOverallChecklistData[0].questionCompletionScore ) {
2764
+ complianceRate.count = Math.round( getOverallChecklistData[0]?.questionCompletionScore ) || 0; // .toFixed( 2 )
2765
+ }
2766
+ }
2767
+ result.overallInfoCards = {
2768
+ 'activeUnique': activeUnique,
2769
+ 'totalInstances': totalInstances,
2770
+ 'notSubmittedInstances': notSubmittedInstances,
2771
+ 'completedInstances': completedInstances,
2772
+ 'flags': flags,
2773
+ 'completionScore': completionScore,
2774
+ 'complianceRate': complianceRate,
2775
+ };
2776
+ return res.sendSuccess( result );
2777
+ } catch ( error ) {
2778
+ console.log( 'error =>', error );
2779
+ logger.error( { error: error, message: req.query, function: 'infoCards' } );
2780
+ return res.sendError( { error: error }, 500 );
2781
+ }
2782
+ };
2783
+
2784
+ export const infoComparisonCardsV1 = async ( req, res ) => {
2785
+ try {
2786
+ let requestData = req.body;
2787
+ let toDate = new Date( requestData.toDate );
2788
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
2789
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
2790
+ toDate.setUTCHours( 23, 59, 59, 59 );
2791
+
2792
+ let rangeOneFromDate;
2793
+ let rangeOneToDate;
2794
+ let rangeTwoFromDate;
2795
+ let rangeTwoToDate;
2796
+ if ( requestData.dateType == 'weekly' ) {
2797
+ rangeOneFromDate = new Date( requestData.toDate );
2798
+ rangeOneFromDate.setDate( rangeOneFromDate.getDate() - 6 );
2799
+ rangeOneToDate = new Date( requestData.toDate );
2800
+ rangeOneToDate = new Date( rangeOneToDate.getTime() - userTimezoneOffset );
2801
+ rangeOneToDate.setUTCHours( 23, 59, 59, 59 );
2802
+ rangeTwoToDate = new Date( requestData.toDate );
2803
+ rangeTwoToDate.setDate( rangeTwoToDate.getDate() - 7 );
2804
+ rangeTwoFromDate = new Date( rangeTwoToDate );
2805
+ rangeTwoFromDate.setDate( rangeTwoFromDate.getDate() - 6 );
2806
+ rangeTwoToDate = new Date( rangeTwoToDate.getTime() - userTimezoneOffset );
2807
+ rangeTwoToDate.setUTCHours( 23, 59, 59, 59 );
2808
+ } else if ( requestData.dateType == 'monthly' ) {
2809
+ rangeOneFromDate = new Date( requestData.toDate );
2810
+ rangeOneFromDate.setDate( rangeOneFromDate.getDate() - 30 );
2811
+ rangeOneToDate = new Date( requestData.toDate );
2812
+ rangeOneToDate = new Date( rangeOneToDate.getTime() - userTimezoneOffset );
2813
+ rangeOneToDate.setUTCHours( 23, 59, 59, 59 );
2814
+ rangeTwoToDate = new Date( requestData.toDate );
2815
+ rangeTwoToDate.setDate( rangeTwoToDate.getDate() - 31 );
2816
+ rangeTwoFromDate = new Date( rangeTwoToDate );
2817
+ rangeTwoFromDate.setDate( rangeTwoFromDate.getDate() - 30 );
2818
+ rangeTwoToDate = new Date( rangeTwoToDate.getTime() - userTimezoneOffset );
2819
+ rangeTwoToDate.setUTCHours( 23, 59, 59, 59 );
2820
+ } else {
2821
+ rangeOneFromDate = new Date( requestData.toDate );
2822
+ rangeOneFromDate.setDate( rangeOneFromDate.getDate() - 0 );
2823
+ rangeOneToDate = new Date( requestData.toDate );
2824
+ rangeOneToDate = new Date( rangeOneToDate.getTime() - userTimezoneOffset );
2825
+ rangeOneToDate.setUTCHours( 23, 59, 59, 59 );
2826
+ rangeTwoToDate = new Date( requestData.toDate );
2827
+ rangeTwoToDate.setDate( rangeTwoToDate.getDate() - 1 );
2828
+ rangeTwoFromDate = new Date( rangeTwoToDate );
2829
+ rangeTwoFromDate.setDate( rangeTwoFromDate.getDate() - 0 );
2830
+ rangeTwoToDate = new Date( rangeTwoToDate.getTime() - userTimezoneOffset );
2831
+ rangeTwoToDate.setUTCHours( 23, 59, 59, 59 );
2832
+ }
2833
+
2834
+ // let rangeOneFromDate = new Date( requestData.toDate );
2835
+ // rangeOneFromDate.setDate( rangeOneFromDate.getDate() - 6 );
2836
+ // let rangeOneToDate = new Date( requestData.toDate );
2837
+ // rangeOneToDate = new Date( rangeOneToDate.getTime() - userTimezoneOffset );
2838
+ // rangeOneToDate.setUTCHours( 23, 59, 59, 59 );
2839
+ // let rangeTwoToDate = new Date( requestData.toDate );
2840
+ // rangeTwoToDate.setDate( rangeTwoToDate.getDate() - 7 );
2841
+ // let rangeTwoFromDate = new Date( rangeTwoToDate );
2842
+ // rangeTwoFromDate.setDate( rangeTwoFromDate.getDate() - 6 );
2843
+ // rangeTwoToDate = new Date( rangeTwoToDate.getTime() - userTimezoneOffset );
2844
+ // rangeTwoToDate.setUTCHours( 23, 59, 59, 59 );
2845
+
2846
+ console.log( 'rangeOneFromDate =>', rangeOneFromDate );
2847
+ console.log( 'rangeOneToDate =>', rangeOneToDate );
2848
+ console.log( 'rangeTwoFromDate =>', rangeTwoFromDate );
2849
+ console.log( 'rangeTwoToDate =>', rangeTwoToDate );
2850
+
2851
+ let totalInstances = {
2852
+ 'comparisonData': 0,
2853
+ 'ComparisonFlag': 0,
2854
+ };
2855
+ let completedInstances = {
2856
+ 'comparisonData': 0,
2857
+ 'ComparisonFlag': 0,
2858
+ };
2859
+ let flags = {
2860
+ 'comparisonData': 0,
2861
+ 'ComparisonFlag': 0,
2862
+ };
2863
+ let completionScore = {
2864
+ 'comparisonData': 0,
2865
+ 'ComparisonFlag': 0,
2866
+ };
2867
+ let complianceRate = {
2868
+ 'comparisonData': 0,
2869
+ 'ComparisonFlag': 0,
2870
+ };
2871
+
2872
+ let infoComparisonCards = {
2873
+ 'totalInstances': totalInstances,
2874
+ 'completedInstances': completedInstances,
2875
+ 'flags': flags,
2876
+ 'completionScore': completionScore,
2877
+ 'complianceRate': complianceRate,
2878
+ };
2879
+
2880
+ let rangeOneFindQuery = [];
2881
+ let rangeOneFindAndQuery = [];
2882
+ rangeOneFindAndQuery.push(
2883
+ { client_id: requestData.clientId },
2884
+ { store_id: { $in: requestData.storeId } },
2885
+ { date_iso: { $gte: rangeOneFromDate } },
2886
+ { date_iso: { $lte: rangeOneToDate } },
2887
+ { checkListType: { $eq: 'custom' } },
2888
+ );
2889
+
2890
+ if ( requestData.groupByType == 'Checklist' ) {
2891
+ rangeOneFindAndQuery.push( { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.groupByValue ) } );
2892
+ }
2893
+
2894
+ if ( requestData.groupByType == 'User' ) {
2895
+ rangeOneFindAndQuery.push( { userEmail: requestData.groupByValue } );
2896
+ }
2897
+
2898
+ if ( requestData.groupByType == 'Store' ) {
2899
+ rangeOneFindAndQuery.push( { store_id: requestData.groupByValue } );
2900
+ }
2901
+
2902
+ rangeOneFindQuery.push( { $match: { $and: rangeOneFindAndQuery } } );
2903
+
2904
+
2905
+ let queryFormat = [
2906
+ {
2907
+ $project: {
2908
+ sourceCheckList_id: 1,
2909
+ checkListId: 1,
2910
+ checklistStatus: 1,
2911
+ timeFlag: 1,
2912
+ questionFlag: 1,
2913
+ mobileDetectionFlag: 1,
2914
+ storeOpenCloseFlag: 1,
2915
+ uniformDetectionFlag: 1,
2916
+ markasread: 1,
2917
+ checkListType: 1,
2918
+ questionCount: 1,
2919
+ },
2920
+ },
2921
+ {
2922
+ $group: {
2923
+ _id: null,
2924
+ totalChecklist: { $sum: 1 },
2925
+ notSubmittedChecklist: {
2926
+ $sum: { $cond: [ { $ne: [ '$checklistStatus', 'submit' ] }, 1, 0 ] },
2927
+ },
2928
+ openChecklist: {
2929
+ $sum: { $cond: [ { $eq: [ '$checklistStatus', 'open' ] }, 1, 0 ] },
2930
+ },
2931
+ inprogressChecklist: {
2932
+ $sum: { $cond: [ { $eq: [ '$checklistStatus', 'inprogress' ] }, 1, 0 ] },
2933
+ },
2934
+ submittedChecklist: {
2935
+ $sum: { $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, 1, 0 ] },
2936
+ },
2937
+ timeFlag: { $sum: '$timeFlag' },
2938
+ questionFlagCount: { $sum: '$questionFlag' },
2939
+ questionCount: { $sum: '$questionCount' },
2940
+ submittedQuestionCount: {
2941
+ $sum: {
2942
+ $cond: [
2943
+ { $eq: [ '$checklistStatus', 'submit' ] },
2944
+ '$questionCount', // Sum this field if status is 'submit'
2945
+ 0, // Otherwise, add 0
2946
+ ],
2947
+ },
2948
+ },
2949
+ },
2950
+ },
2951
+ {
2952
+ $project: {
2953
+ totalChecklist: 1,
2954
+ notSubmittedChecklist: 1,
2955
+ openChecklist: 1,
2956
+ inprogressChecklist: 1,
2957
+ submittedChecklist: 1,
2958
+ questionFlagCount: 1,
2959
+ questionCount: 1,
2960
+ timeFlag: 1,
2961
+ submittedQuestionCount: 1,
2962
+ flaggedChecklist: { $add: [ { $ifNull: [ '$timeFlag', 0 ] }, { $ifNull: [ '$questionFlagCount', 0 ] } ] },
2963
+ completionScore: { $round: [ { $multiply: [ { $divide: [ { $ifNull: [ '$submittedChecklist', 0 ] }, { $ifNull: [ '$totalChecklist', 0 ] } ] }, 100 ] }, 0 ] },
2964
+ // completionScore: { $multiply: [ { $divide: [ '$submittedChecklist', '$totalChecklist' ] }, 100 ] },
2965
+ correctAnswers: { $subtract: [ { $ifNull: [ '$submittedQuestionCount', 0 ] }, { $ifNull: [ '$questionFlagCount', 0 ] } ] },
2966
+ },
2967
+ },
2968
+ {
2969
+ $project: {
2970
+ totalChecklist: 1,
2971
+ notSubmittedChecklist: 1,
2972
+ openChecklist: 1,
2973
+ inprogressChecklist: 1,
2974
+ submittedChecklist: 1,
2975
+ flaggedChecklist: 1,
2976
+ completionScore: 1,
2977
+ questionFlagCount: 1,
2978
+ questionCount: 1,
2979
+ timeFlag: 1,
2980
+ correctAnswers: 1,
2981
+ submittedQuestionCount: 1,
2982
+ questionCompletionScore: {
2983
+ $cond: {
2984
+ if: { $eq: [ '$questionCount', 0 ] },
2985
+ then: 0,
2986
+ else: {
2987
+ $round: [ { $multiply: [ { $divide: [ { $ifNull: [ '$correctAnswers', 0 ] }, { $ifNull: [ '$questionCount', 0 ] } ] }, 100 ] }, 0 ],
2988
+ // $multiply: [ { $divide: [ '$correctAnswers', '$questionCount' ] }, 100 ],
2989
+ },
2990
+ },
2991
+ },
2992
+ },
2993
+ },
2994
+ ];
2995
+ rangeOneFindQuery.push( ...queryFormat );
2996
+ let rangeOneData = await processedchecklistService.aggregate( rangeOneFindQuery );
2997
+ console.log( 'rangeOneData =>', rangeOneData );
2998
+
2999
+ let rangeTwoFindQuery = [];
3000
+ let rangeTwoFindAndQuery = [];
3001
+ rangeTwoFindAndQuery.push(
3002
+ { client_id: requestData.clientId },
3003
+ { store_id: { $in: requestData.storeId } },
3004
+ { date_iso: { $gte: rangeTwoFromDate } },
3005
+ { date_iso: { $lte: rangeTwoToDate } },
3006
+ { checkListType: { $eq: 'custom' } },
3007
+ );
3008
+ if ( requestData.groupByType == 'Checklist' ) {
3009
+ rangeTwoFindAndQuery.push( { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.groupByValue ) } );
3010
+ }
3011
+
3012
+ if ( requestData.groupByType == 'user' ) {
3013
+ rangeTwoFindAndQuery.push( { userEmail: requestData.groupByValue } );
3014
+ }
3015
+
3016
+ if ( requestData.groupByType == 'store' ) {
3017
+ rangeTwoFindAndQuery.push( { store_id: requestData.groupByValue } );
3018
+ }
3019
+
3020
+ rangeTwoFindQuery.push( { $match: { $and: rangeTwoFindAndQuery } } );
3021
+
3022
+ rangeTwoFindQuery.push( ...queryFormat );
3023
+ let rangeTwoData = await processedchecklistService.aggregate( rangeTwoFindQuery );
3024
+ console.log( 'rangeTwoData =>', rangeTwoData );
3025
+
3026
+ if ( !rangeOneData.length ) {
3027
+ return res.sendError( { error: 'No Data Found' }, 204 );
3028
+ }
3029
+
3030
+ if ( rangeOneData.length >0 && rangeTwoData.length >0 ) {
3031
+ infoComparisonCards.totalInstances.comparisonData = Math.round( ( ( ( rangeOneData[0].totalChecklist - rangeTwoData[0].totalChecklist )/rangeTwoData[0].totalChecklist )*100 ) ); // .toFixed( 2 )
3032
+ if ( infoComparisonCards.totalInstances.comparisonData > 0 ) {
3033
+ infoComparisonCards.totalInstances.ComparisonFlag = true;
3034
+ } else {
3035
+ infoComparisonCards.totalInstances.ComparisonFlag = false;
3036
+ }
3037
+ infoComparisonCards.completedInstances.comparisonData = Math.round( ( ( ( rangeOneData[0].submittedChecklist - rangeTwoData[0].submittedChecklist )/rangeTwoData[0].submittedChecklist )*100 ) ); // .toFixed( 2 )
3038
+ if ( infoComparisonCards.completedInstances.comparisonData > 0 ) {
3039
+ infoComparisonCards.completedInstances.ComparisonFlag = true;
3040
+ } else {
3041
+ infoComparisonCards.completedInstances.ComparisonFlag = false;
3042
+ }
3043
+ infoComparisonCards.flags.comparisonData = Math.round( ( ( ( rangeOneData[0].flaggedChecklist - rangeTwoData[0].flaggedChecklist )/rangeTwoData[0].flaggedChecklist )*100 ) ); // .toFixed( 2 )
3044
+ if ( infoComparisonCards.flags.comparisonData > 0 ) {
3045
+ infoComparisonCards.flags.ComparisonFlag = true;
3046
+ } else {
3047
+ infoComparisonCards.flags.ComparisonFlag = false;
3048
+ }
3049
+ infoComparisonCards.completionScore.comparisonData = Math.round( ( ( ( rangeOneData[0].questionCompletionScore - rangeTwoData[0].questionCompletionScore )/rangeTwoData[0].questionCompletionScore )*100 ) ); // .toFixed( 2 )
3050
+ if ( infoComparisonCards.completionScore.comparisonData > 0 ) {
3051
+ infoComparisonCards.completionScore.ComparisonFlag = true;
3052
+ } else {
3053
+ infoComparisonCards.completionScore.ComparisonFlag = false;
3054
+ }
3055
+ infoComparisonCards.complianceRate.comparisonData = Math.round( ( ( ( rangeOneData[0].completionScore - rangeTwoData[0].completionScore )/rangeTwoData[0].completionScore )*100 ) ); // .toFixed( 2 )
3056
+ if ( infoComparisonCards.complianceRate.comparisonData > 0 ) {
3057
+ infoComparisonCards.complianceRate.ComparisonFlag = true;
3058
+ } else {
3059
+ infoComparisonCards.complianceRate.ComparisonFlag = false;
3060
+ }
3061
+
3062
+ infoComparisonCards.totalInstances.comparisonData = Math.abs( infoComparisonCards.totalInstances.comparisonData ) || 0;
3063
+ infoComparisonCards.completedInstances.comparisonData = Math.abs( infoComparisonCards.completedInstances.comparisonData ) || 0;
3064
+ infoComparisonCards.flags.comparisonData = Math.abs( infoComparisonCards.flags.comparisonData ) || 0;
3065
+ infoComparisonCards.completionScore.comparisonData = Math.abs( infoComparisonCards.completionScore.comparisonData ) || 0;
3066
+ infoComparisonCards.complianceRate.comparisonData = Math.abs( infoComparisonCards.complianceRate.comparisonData ) || 0;
3067
+ }
3068
+ console.log( 'infoComparisonCards =>', infoComparisonCards );
3069
+ let result = {
3070
+ 'infoComparisonCards': infoComparisonCards,
3071
+ };
3072
+ return res.sendSuccess( result );
3073
+ } catch ( error ) {
3074
+ console.log( 'error =>', error );
3075
+ logger.error( { error: error, message: req.query, function: 'infoCards' } );
3076
+ return res.sendError( { error: error }, 500 );
3077
+ }
3078
+ };
3079
+
3080
+ export const reportChecklistTableV1 = async ( req, res ) => {
3081
+ try {
3082
+ let requestData = req.body;
3083
+ let fromDate = new Date( requestData.fromDate );
3084
+ let toDate = new Date( requestData.toDate );
3085
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
3086
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
3087
+ toDate.setUTCHours( 23, 59, 59, 59 );
3088
+ let result = {};
3089
+
3090
+ let findQuery = [];
3091
+ let findAndQuery = [];
3092
+ findAndQuery.push(
3093
+ { client_id: requestData.clientId },
3094
+ { store_id: { $in: requestData.storeId } },
3095
+ { date_iso: { $gte: fromDate } },
3096
+ { date_iso: { $lte: toDate } },
3097
+ { checklistStatus: { $eq: 'submit' } },
3098
+ { sourceCheckList_id: { $eq: new mongoose.Types.ObjectId( requestData.sourceCheckList_id ) } },
3099
+ );
3100
+
3101
+ findQuery.push( { $match: { $and: findAndQuery } } );
3102
+
3103
+ if ( requestData.searchValue && requestData.searchValue != '' ) {
3104
+ let storeList = requestData.searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
3105
+ let query;
3106
+ if ( storeList.length > 1 ) {
3107
+ findQuery.push( { $addFields: { store: { $toLower: '$storeName' } } } );
3108
+ query = { store: { $in: storeList } };
3109
+ } else {
3110
+ query = { storeName: { $regex: requestData.searchValue.trim(), $options: 'i' } };
3111
+ }
3112
+ findQuery.push( { $match: { $or: [ query ] } } );
3113
+ }
3114
+
3115
+ findQuery.push( {
3116
+ $project: {
3117
+ submittedDate: '$submitTime_string',
3118
+ submittedTime: '$submitTime_string',
3119
+ checkListName: 1,
3120
+ storeName: 1,
3121
+ store_id: 1,
3122
+ submittedBy: '$userName',
3123
+ submittedByChar: { $substr: [ '$userName', 0, 2 ] },
3124
+ flagCount: {
3125
+ $sum: {
3126
+ $cond: [ {
3127
+ $or: [
3128
+ { $gt: [ '$timeFlag', 0 ] },
3129
+ { $gt: [ '$questionFlag', 0 ] },
3130
+ { $gt: [ '$mobileDetectionFlag', 0 ] },
3131
+ { $gt: [ '$storeOpenCloseFlag', 0 ] },
3132
+ { $gt: [ '$uniformDetectionFlag', 0 ] },
3133
+ ],
3134
+ }, 1, 0 ],
3135
+ },
3136
+ },
3137
+ },
3138
+ } );
3139
+
3140
+ let getTotalCount = await processedchecklistService.aggregate( findQuery );
3141
+ if ( !getTotalCount.length ) {
3142
+ return res.sendError( { error: 'No Data Found' }, 204 );
3143
+ }
3144
+
3145
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
3146
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
3147
+ } else {
3148
+ findQuery.push( { $sort: { ['date_iso']: -1 } } );
3149
+ }
3150
+
3151
+ let limit = parseInt( requestData?.limit ) || 10;
3152
+ let skip = limit * ( requestData?.offset ) || 0;
3153
+ findQuery.push( { $skip: skip }, { $limit: limit } );
3154
+ let getChecklistPerformanceData = await processedchecklistService.aggregate( findQuery );
3155
+
3156
+ result.totalCount = getTotalCount.length;
3157
+ result.reportChecklistTableData = getChecklistPerformanceData;
3158
+ return res.sendSuccess( result );
3159
+ } catch ( error ) {
3160
+ console.log( 'error =>', error );
3161
+ logger.error( { error: error, message: req.query, function: 'checklistInfo' } );
3162
+ return res.sendError( { error: error }, 500 );
3163
+ }
3164
+ };
3165
+
3166
+ export const monthlyGraphV1 = async ( req, res ) => {
3167
+ try {
3168
+ let requestData = req.body;
3169
+ let currentDate = dayjs().set( 'hour', 0 ).set( 'minute', 0 ).set( 'second', 0 ).set( 'millisecond', 0 );
3170
+ let targetDate = dayjs( requestData.toDate ).set( 'hour', 0 ).set( 'minute', 0 ).set( 'second', 0 ).set( 'millisecond', 0 );
3171
+ let startOfMonth;
3172
+ let endOfMonth;
3173
+ let monthDate = requestData.monthDate || requestData.toDate;
3174
+ if ( currentDate.format( 'YYYY-MM-DDTHH:mm:ss.SSS[+00:00]' ) > targetDate.format( 'YYYY-MM-DDTHH:mm:ss.SSS[+00:00]' ) ) {
3175
+ if ( currentDate.format( 'YYYY-MM' ) == targetDate.format( 'YYYY-MM' ) ) {
3176
+ startOfMonth = dayjs( monthDate ).startOf( 'month' ).format( 'YYYY-MM-DD' );
3177
+ endOfMonth = currentDate.format( 'YYYY-MM-DD' );
3178
+ } else {
3179
+ startOfMonth = dayjs( monthDate ).startOf( 'month' ).format( 'YYYY-MM-DD' );
3180
+ endOfMonth = dayjs( monthDate ).endOf( 'month' ).format( 'YYYY-MM-DD' );
3181
+ }
3182
+ } else {
3183
+ startOfMonth = dayjs( monthDate ).startOf( 'month' ).format( 'YYYY-MM-DD' );
3184
+ endOfMonth = currentDate.format( 'YYYY-MM-DD' );
3185
+ }
3186
+
3187
+ let fromDate = new Date( startOfMonth );
3188
+ let toDate = new Date( endOfMonth );
3189
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
3190
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
3191
+ toDate.setUTCHours( 23, 59, 59, 59 );
3192
+ let result = {};
3193
+ let findQuery = [];
3194
+ let findAndQuery = [];
3195
+ findAndQuery.push(
3196
+ { client_id: requestData.clientId },
3197
+ { store_id: { $in: requestData.storeId } },
3198
+ { date_iso: { $gte: fromDate } },
3199
+ { date_iso: { $lte: toDate } },
3200
+ { checkListType: { $eq: 'custom' } },
3201
+ );
3202
+
3203
+ if ( requestData.groupByType == 'Checklist' ) {
3204
+ findAndQuery.push( { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.groupByValue ) } );
3205
+ }
3206
+
3207
+ if ( requestData.groupByType == 'User' ) {
3208
+ findAndQuery.push( { userEmail: requestData.groupByValue } );
3209
+ }
3210
+
3211
+ findQuery.push( { $match: { $and: findAndQuery } } );
3212
+
3213
+ if ( requestData.searchValue && requestData.searchValue != '' ) {
3214
+ findQuery.push( { $match: { $or: [ { checkListName: { $regex: requestData.searchValue, $options: 'i' } } ] } } );
3215
+ }
3216
+
3217
+ findQuery.push( {
3218
+ $project: {
3219
+ sourceCheckList_id: 1,
3220
+ checkListId: 1,
3221
+ checklistStatus: 1,
3222
+ date_iso: 1,
3223
+ date_string: 1,
3224
+ _id: 1,
3225
+ },
3226
+ } );
3227
+
3228
+ findQuery.push( {
3229
+ $group: {
3230
+ _id: '$date_iso',
3231
+ totalChecklist: { $sum: 1 },
3232
+ submittedChecklist: {
3233
+ $sum: {
3234
+ $cond: [
3235
+ {
3236
+ $and: [
3237
+ { $eq: [ '$checklistStatus', 'submit' ] },
3238
+ ],
3239
+ }, 1, 0 ],
3240
+ },
3241
+ },
3242
+ },
3243
+ } );
3244
+
3245
+ findQuery.push( {
3246
+ $project: {
3247
+ date: { $dayOfMonth: '$_id' },
3248
+ totalChecklist: 1,
3249
+ submittedChecklist: 1,
3250
+ completionScore: {
3251
+ $multiply: [
3252
+ { $divide: [ '$submittedChecklist', '$totalChecklist' ] },
3253
+ 100,
3254
+ ],
3255
+ },
3256
+ colorClass: '',
3257
+ },
3258
+ } );
3259
+
3260
+ let getTotalCount = await processedchecklistService.aggregate( findQuery );
3261
+ if ( !getTotalCount.length ) {
3262
+ return res.sendError( { error: 'No Data Found' }, 204 );
3263
+ }
3264
+
3265
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
3266
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
3267
+ } else {
3268
+ findQuery.push( { $sort: { ['date_iso']: -1 } } );
3269
+ }
3270
+
3271
+ // let limit = parseInt( requestData?.limit ) || 10;
3272
+ // let skip = limit * ( requestData?.offset ) || 0;
3273
+ // findQuery.push( { $skip: skip }, { $limit: limit } );
3274
+ let getChecklistPerformanceData = await processedchecklistService.aggregate( findQuery );
3275
+
3276
+ let colorcode ={
3277
+ darkBlue: 'primary900',
3278
+ blue: 'blue33B5FF',
3279
+ liteBlue: 'lightBlue6BCAFF',
3280
+ red: 'redF04438',
3281
+ organge: 'orangeF79009',
3282
+ };
3283
+ for ( let i=0; i<getChecklistPerformanceData.length; i++ ) {
3284
+ if ( getChecklistPerformanceData[i].completionScore > 40 && getChecklistPerformanceData[i].completionScore <= 55 ) {
3285
+ getChecklistPerformanceData[i].colorClass = colorcode.organge;
3286
+ } else if ( getChecklistPerformanceData[i].completionScore >= 56 && getChecklistPerformanceData[i].completionScore <= 70 ) {
3287
+ getChecklistPerformanceData[i].colorClass = colorcode.liteBlue;
3288
+ } else if ( getChecklistPerformanceData[i].completionScore >= 71 && getChecklistPerformanceData[i].completionScore <= 85 ) {
3289
+ getChecklistPerformanceData[i].colorClass = colorcode.blue;
3290
+ } else if ( getChecklistPerformanceData[i].completionScore >= 86 && getChecklistPerformanceData[i].completionScore <= 100 ) {
3291
+ getChecklistPerformanceData[i].colorClass = colorcode.darkBlue;
3292
+ } else {
3293
+ getChecklistPerformanceData[i].colorClass = colorcode.red;
3294
+ }
3295
+ }
3296
+
3297
+ result.totalCount = getTotalCount.length;
3298
+ result.monthlyChartData = getChecklistPerformanceData;
3299
+ return res.sendSuccess( result );
3300
+ } catch ( error ) {
3301
+ console.log( 'error =>', error );
3302
+ logger.error( { error: error, message: req.query, function: 'checklistInfo' } );
3303
+ return res.sendError( { error: error }, 500 );
3304
+ }
3305
+ };
3306
+
3307
+ export const reinitiatechecklist = async ( req, res ) => {
3308
+ try {
3309
+ let requestData = req.body;
3310
+ let findQuery = [];
3311
+ let findAndQuery = [];
3312
+
3313
+ if ( requestData._id ) {
3314
+ findAndQuery.push(
3315
+ { _id: new mongoose.Types.ObjectId( requestData._id ) },
3316
+ );
3317
+ findQuery.push( { $match: { $and: findAndQuery } } );
3318
+ findQuery.push( {
3319
+ $project: {
3320
+ _id: 1,
3321
+ reinitiateStatus: 1,
3322
+ },
3323
+ } );
3324
+ let getChecklist = await processedchecklistService.aggregate( findQuery );
3325
+ console.log( 'getChecklist by _id=>', getChecklist );
3326
+ if ( getChecklist.length>0 ) {
3327
+ let updateChecklist = await processedchecklistService.updateOne( { _id: new mongoose.Types.ObjectId( requestData._id ) }, { reinitiateStatus: true } );
3328
+ if ( updateChecklist ) {
3329
+ // Delete Time Flag Detection //
3330
+
3331
+ return res.sendSuccess( 'Reinitiate Successfully' );
3332
+ } else {
3333
+ return res.sendError( { error: 'Something Went wrong Please Try Again' }, 400 );
3334
+ }
3335
+ } else {
3336
+ return res.sendError( { error: 'Invalid Checklist Id' }, 400 );
3337
+ }
3338
+ } else if ( requestData.sourceCheckList_id ) {
3339
+ findAndQuery.push(
3340
+ { _id: new mongoose.Types.ObjectId( requestData.sourceCheckList_id ) },
3341
+ );
3342
+ findQuery.push( { $match: { $and: findAndQuery } } );
3343
+ findQuery.push( {
3344
+ $project: {
3345
+ _id: 1,
3346
+ reinitiateStatus: 1,
3347
+ },
3348
+ } );
3349
+ let getChecklist = await processedchecklistService.aggregate( findQuery );
3350
+ console.log( 'getChecklist source checklist=>', getChecklist );
3351
+ if ( getChecklist.length>0 ) {
3352
+ let updateQuery = {
3353
+ _id: new mongoose.Types.ObjectId( requestData.sourceCheckList_id ),
3354
+ timeFlag: { $gt: 0 },
3355
+ };
3356
+ let updateChecklist = await processedchecklistService.updateMany( updateQuery, { reinitiateStatus: true } );
3357
+ if ( updateChecklist ) {
3358
+ // Delete Time Flag Detection //
3359
+
3360
+ return res.sendSuccess( 'Reinitiate Successfully' );
3361
+ } else {
3362
+ return res.sendError( { error: 'Something Went wrong Please Try Again' }, 400 );
3363
+ }
3364
+ } else {
3365
+ return res.sendError( { error: 'Invalid Checklist Id' }, 400 );
3366
+ }
3367
+ } else {
3368
+ return res.sendError( { error: '_id or sourceCheckList_id is required' }, 400 );
3369
+ }
3370
+ } catch ( error ) {
3371
+ console.log( 'error =>', error );
3372
+ logger.error( { error: error, message: req.query, function: 'checklistInfo' } );
3373
+ return res.sendError( { error: error }, 500 );
3374
+ }
3375
+ };
3376
+
3377
+ export const checklistDropdownOld = async ( req, res ) => {
3378
+ try {
3379
+ let requestData = req.body;
3380
+ let result = {};
3381
+ let findQuery = [];
3382
+ let findAndQuery = [];
3383
+ findAndQuery.push(
3384
+ { client_id: requestData.clientId },
3385
+ { checkListType: 'custom' },
3386
+ );
3387
+ findQuery.push( { $match: { $and: findAndQuery } } );
3388
+ findQuery.push( {
3389
+ $project: {
3390
+ sourceCheckList_id: '$_id',
3391
+ checkListName: 1,
3392
+ checkListNameLowercase: { $toLower: '$checkListName' },
3393
+ checkListType: 1,
3394
+ createdByName: 1,
3395
+ storeCount: 1,
3396
+ scheduleRepeatedType: 1,
3397
+ scheduleStartTime: 1,
3398
+ scheduleEndTime: 1,
3399
+ publish: 1,
3400
+ },
3401
+ } );
3402
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
3403
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
3404
+ } else {
3405
+ findQuery.push( { $sort: { ['checkListNameLowercase']: 1 } } );
3406
+ }
3407
+ let getChecklistData = await checklistconfigService.aggregate( findQuery );
3408
+ if ( !getChecklistData.length ) {
3409
+ return res.sendError( { error: 'No Data Found' }, 204 );
3410
+ }
3411
+ result.totalCount = getChecklistData.length;
3412
+ result.checklistData = getChecklistData;
3413
+ return res.sendSuccess( result );
3414
+ } catch ( error ) {
3415
+ console.log( 'error =>', error );
3416
+ logger.error( { error: error, message: req.query, function: 'checklistDropdown' } );
3417
+ return res.sendError( { error: error }, 500 );
3418
+ }
3419
+ };
3420
+
3421
+ export const checklistDropdown = async ( req, res ) => {
3422
+ try {
3423
+ let requestData = req.body;
3424
+ let fromDate = new Date( requestData.fromDate );
3425
+ let toDate = new Date( requestData.toDate );
3426
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
3427
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
3428
+ toDate.setUTCHours( 23, 59, 59, 59 );
3429
+ let result = {};
3430
+
3431
+ let findQuery = [
3432
+ { $match: { $and: [
3433
+ { client_id: requestData.clientId },
3434
+ { store_id: { $in: requestData.storeId } },
3435
+ { date_iso: { $gte: fromDate, $lte: toDate } },
3436
+ { checkListType: 'custom' },
3437
+ ] } },
3438
+ {
3439
+ $project: {
3440
+ sourceCheckList_id: 1,
3441
+ checkListName: 1,
3442
+ },
3443
+ },
3444
+ {
3445
+ $group: {
3446
+ _id: '$sourceCheckList_id',
3447
+ checkListName: { $last: '$checkListName' },
3448
+ },
3449
+ },
3450
+ {
3451
+ $project: {
3452
+ _id: 0,
3453
+ sourceCheckList_id: '$_id',
3454
+ checkListName: 1,
3455
+ },
3456
+ },
3457
+ {
3458
+ $lookup: {
3459
+ from: 'checklistconfigs',
3460
+ let: { sourceId: '$sourceCheckList_id' },
3461
+ pipeline: [
3462
+ {
3463
+ $match: {
3464
+ $expr: {
3465
+ $and: [
3466
+ { $eq: [ '$_id', '$$sourceId' ] },
3467
+ ],
3468
+ },
3469
+ },
3470
+ },
3471
+ {
3472
+ $project: {
3473
+ _id: 1,
3474
+ checkListName: 1,
3475
+ checkListNameLowercase: { $toLower: '$checkListName' },
3476
+ checkListType: 1,
3477
+ createdByName: 1,
3478
+ publish: 1,
3479
+ scheduleEndTime: 1,
3480
+ scheduleRepeatedType: 1,
3481
+ scheduleStartTime: 1,
3482
+ sourceCheckList_id: '$_id',
3483
+ storeCount: 1,
3484
+ },
3485
+ },
3486
+ ], as: 'checklistData',
3487
+ },
3488
+ },
3489
+ { $unwind: { path: '$checklistData', preserveNullAndEmptyArrays: true } },
3490
+ {
3491
+ $project: {
3492
+ _id: '$checklistData._id',
3493
+ checkListName: '$checklistData.checkListName',
3494
+ checkListNameLowercase: { $toLower: '$checkListName' },
3495
+ checkListType: '$checklistData.checkListType',
3496
+ createdByName: '$checklistData.createdByName',
3497
+ publish: '$checklistData.publish',
3498
+ scheduleEndTime: '$checklistData.scheduleEndTime',
3499
+ scheduleRepeatedType: '$checklistData.scheduleRepeatedType',
3500
+ scheduleStartTime: '$checklistData.scheduleStartTime',
3501
+ sourceCheckList_id: '$checklistData._id',
3502
+ storeCount: '$checklistData.storeCount',
3503
+ },
3504
+ },
3505
+ ];
3506
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
3507
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
3508
+ } else {
3509
+ findQuery.push( { $sort: { ['checkListNameLowercase']: -1 } } );
3510
+ }
3511
+
3512
+ let getChecklistData = await processedchecklistService.aggregate( findQuery );
3513
+ if ( !getChecklistData.length ) {
3514
+ return res.sendError( { error: 'No Data Found' }, 204 );
3515
+ }
3516
+ result.totalCount = getChecklistData.length;
3517
+ result.checklistData = getChecklistData;
3518
+ return res.sendSuccess( result );
3519
+ } catch ( error ) {
3520
+ logger.error( { error: error, message: req.query, function: 'checklistDropdownV1' } );
3521
+ return res.sendError( { error: error }, 500 );
3522
+ }
3523
+ };