tango-app-api-trax 3.0.5 → 3.2.0-beta.0

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.
@@ -0,0 +1,13 @@
1
+ {
2
+ "type": "service_account",
3
+ "project_id": "tango-trax",
4
+ "private_key_id": "ab5f0323dc6b9ea8aa912aadd631d03305cb194e",
5
+ "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDFxYgl89aDMT6O\nwOeUTgzY1lYYwmg3GOLhNYTB5hGrkGobn8sEtr8YPV5ADTZCWbhQbF2f9AC6xUtu\nXkWvx2zGtKKqH9kMd4ek3rALVgOI0VtAlFKa8wNqiWISXx6YR9Ff9+z47Gb8H3fP\nrWTTCIgndR/aM5/wL66FNx4MnlEUUn0K0GwQJFAa0ts+N4FIF584VsKjoPDUxvFh\nx29F6HoizlD585pycyfx1J8mVSBPNSwns2b0SqNwY9vkmzpGbeuz2pS6NWB+tA76\nYw8cMujRBUEu2Q6vgsZsDeB9SP08U190C6NpInrJdh9XjTXAcxSxfj6r0BDP24sn\n9UNCFjAnAgMBAAECggEAHcGyJykLC5jrNVtnp0l0ZrkedyyOYEve8c0K+TRT+vZd\noLkS44CuO/LFLZSkp1MMh707plzLh06MIkJjmyZsWBwcUBs+ncDrqIOBBWj0Wy+f\nhUChNS3UmRZWqouj9NGez8u5Mgg6bpDlhq/WIxJ+OexGK0nn30xX+9870YuN3iVP\nVkhDk11BjYmVAGRqVQN42Z7hIrot7ilgEDvP2/o569b+aXQWT3tzuJjpMKGO6lDn\nSHFfoUkLBo0Ag2er9CXrCwY4t1+1LoUoGqnzAfT6cubJL1so376XjUME4fSOav1X\ns9u8l9tOmlrkI7gCFh26yoy8EcYIEu6sJY20eHefIQKBgQDuhrvaGKbUclprZpKN\nhrCa4BIt4oNHRL10GQGf3rbqC4xznLuKdtFmhi7cehfO6A6IZ6MstVikDl62RWda\nK0woa3M+QUN8VF5nSfchCIp8SaR1xspIKDqTUFs2Zxd5f2ZPvIiOSAOZGXSR4IOZ\nogjHW/Y5+V5Of1kH4zJHxcBZGQKBgQDUQn56/drr5WG/6LUrCMAWKSm7++2mk6wk\neH5cbsWvknKqjddM71l20o+K76suuKh+S7hwuynTFd1uuDUn0STvEIBNaeS0odYY\n9Mk+t2Poty9TmRlhFMUg4wjahfbva/db3+xNaFy6Ui0aggYwHWGtMeO3Z4iTICrA\nW/4a8mO7PwKBgBhP82XPjXGqFj1MPzPdvrKKlE/I/siM4SKsSdYZxVslwjMmjpcW\ndVGV68DdUP/t901AAwnTki1mqZC6kkZGk4SrLxvxWweZMeYlS+M6E6OHfhPjJ0sj\njpTlY+avq1ze93a5KNIX1QYN6ivUTpWiRnLLlNVj7ZDiuhhrt/xI08pBAoGARmNq\nmBS41YNyv/ZGpee382FhIqKvs47DBjEjDjn5unhhKCfzXuL8kwfObC+AX/UCgydP\nGasneoWEsePKVNLpYZHiSkm0eyYz3oDGf9LHAfRFcH6TyPrfSEGhvP2x3sG5/LCs\nJxtVrL/1jxXRQYFs08d4kWh3BPsk0jdvc7Yv1ncCgYBkpm2Aet2Kx+vvUcqoGTuc\nWXFEksRxgW07iohnpvYJnsUZRXYNrA32iMpcUB0Z/rmS7Zn5iyuK9vpELCRKVQLh\n29rHmEa4UDP4T2/uqP97HpjWQJW/FfMO8eoQdonsPAHh0UnNdM7jMXj9LXLByxu0\nf9ctSBzSDocwkFWbVb30wQ==\n-----END PRIVATE KEY-----\n",
6
+ "client_email": "firebase-adminsdk-k7lom@tango-trax.iam.gserviceaccount.com",
7
+ "client_id": "112026772051782321525",
8
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
9
+ "token_uri": "https://oauth2.googleapis.com/token",
10
+ "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11
+ "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-k7lom%40tango-trax.iam.gserviceaccount.com",
12
+ "universe_domain": "googleapis.com"
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-trax",
3
- "version": "3.0.5",
3
+ "version": "3.2.0-beta.0",
4
4
  "description": "Trax",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -19,12 +19,16 @@
19
19
  "dotenv": "^16.4.5",
20
20
  "express": "^4.21.1",
21
21
  "express-fileupload": "^1.5.1",
22
+ "firebase-admin": "^13.0.0",
23
+ "fs": "^0.0.1-security",
22
24
  "handlebars": "^4.7.8",
23
25
  "lodash": "^4.17.21",
24
26
  "mongodb": "^6.8.0",
25
27
  "nodemon": "^3.1.4",
26
- "tango-api-schema": "^2.1.66",
27
- "tango-app-api-middleware": "^3.1.43-alpha.10",
28
+ "path": "^0.12.7",
29
+ "tango-api-schema": "^2.2.16",
30
+ "tango-app-api-middleware": "^3.1.48",
31
+ "url": "^0.11.4",
28
32
  "winston": "^3.13.1",
29
33
  "winston-daily-rotate-file": "^5.0.0"
30
34
  },
@@ -2,7 +2,10 @@ import { logger, signedUrl, sendMessageToQueue } from 'tango-app-api-middleware'
2
2
  import dayjs from 'dayjs';
3
3
  import * as downloadService from '../services/download.services.js';
4
4
  import * as processedchecklistService from '../services/processedchecklist.services.js';
5
+ import * as processedTaskService from '../services/processedTaskList.service.js';
6
+ import * as taskConfigService from '../services/taskConfig.service.js';
5
7
  import * as checklistService from '../services/checklist.service.js';
8
+ // import * as processedTaskconfigService from '../services/processedTaskConfig.service.js';
6
9
  import * as clientService from '../services/clients.services.js';
7
10
  import * as storeService from '../services/store.service.js';
8
11
 
@@ -40,23 +43,29 @@ export const downloadInsert = async ( req, res ) => {
40
43
  }
41
44
  }
42
45
 
43
- if ( requestData.sourceCheckList_id && requestData.sourceCheckList_id !='' ) {
46
+ if ( requestData.sourceCheckList_id && requestData.sourceCheckList_id != '' ) {
44
47
  let getChecklistQuery = [];
45
- getChecklistQuery.push( { $project: { sourceCheckList_id: 1, date_iso: 1, store_id: 1, checklistStatus: 1 } } );
48
+ getChecklistQuery.push( { $project: { sourceCheckList_id: 1, date_iso: 1, store_id: 1, checklistStatus: 1, redoStatus: 1 } } );
46
49
  getChecklistQuery.push( {
47
50
  $match: {
48
51
  $and: [
49
52
  { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.sourceCheckList_id ) },
50
53
  { date_iso: { $gte: fromDate, $lte: toDate } },
51
54
  { store_id: { $in: requestData.storeIds } },
55
+ ],
56
+ $or: [
52
57
  { checklistStatus: 'submit' },
58
+ { redoStatus: true },
53
59
  ],
54
60
  },
55
61
  } );
56
62
  getChecklistQuery.push( { $count: 'totalCount' } );
57
- let getChecklistCount = await processedchecklistService.aggregate( getChecklistQuery );
58
63
 
59
- if ( getChecklistCount && getChecklistCount[0]?.totalCount && getChecklistCount[0].totalCount >0 ) {
64
+ let getChecklistCount = await processedchecklistService.aggregate( getChecklistQuery );
65
+ if ( requestData.insertType === 'task' ) {
66
+ getChecklistCount = await processedTaskService.aggregate( getChecklistQuery );
67
+ }
68
+ if ( getChecklistCount && getChecklistCount[0]?.totalCount && getChecklistCount[0].totalCount > 0 ) {
60
69
  // console.log( 'if' );
61
70
  // console.log( 'getChecklistCountgetChecklistCount[0].totalCount =>', getChecklistCount[0]?.totalCount );
62
71
  } else {
@@ -65,7 +74,7 @@ export const downloadInsert = async ( req, res ) => {
65
74
  }
66
75
 
67
76
  if ( requestData.fileType == 'pdf' ) {
68
- if ( requestData?.sourceCheckList_id && requestData?.sourceCheckList_id !='' ) {
77
+ if ( requestData?.sourceCheckList_id && requestData?.sourceCheckList_id != '' ) {
69
78
  fileType = 'pdfzip';
70
79
  }
71
80
  if ( requestData?.checklistId && requestData?.checklistId.length > 1 ) {
@@ -76,7 +85,7 @@ export const downloadInsert = async ( req, res ) => {
76
85
  if ( requestData.checklistName != '' ) {
77
86
  name = requestData.checklistName + '_' + dayjs( requestData.fromDate ).format( 'DD/MM/YYYY' );
78
87
  if ( requestData?.toDate ) {
79
- name = name + '-'+ dayjs( requestData?.toDate ).format( 'DD/MM/YYYY' );
88
+ name = name + '-' + dayjs( requestData?.toDate ).format( 'DD/MM/YYYY' );
80
89
  }
81
90
  let regexName = new RegExp( `^${name.split( '(' )[0]} \\(.*\\)$`, 'i' );
82
91
  let type = requestData.fileType || 'pdfzip';
@@ -88,7 +97,7 @@ export const downloadInsert = async ( req, res ) => {
88
97
  name = requestData?.checklistName;
89
98
  }
90
99
 
91
- name = name+'-'+requestData.fileType;
100
+ name = name + '-' + requestData.fileType;
92
101
  let insertData = {
93
102
  'date_string': requestData.fromDate || '',
94
103
  'sourceCheckList_id': requestData?.sourceCheckList_id || '',
@@ -110,6 +119,9 @@ export const downloadInsert = async ( req, res ) => {
110
119
  'downloadInsertFrom': requestData.downloadInsertFrom || '',
111
120
  'answerType': requestData.answerType || '',
112
121
  'searchValue': requestData.searchValue || '',
122
+ 'insertType': requestData.insertType || '',
123
+ 'viewRedo': requestData.viewRedo || false,
124
+
113
125
  };
114
126
  let resultData = await downloadService.insert( insertData );
115
127
  if ( resultData ) {
@@ -151,10 +163,10 @@ export const downloadInsertOld = async ( req, res ) => {
151
163
  { date_iso: { $lte: todate } },
152
164
  { checklistStatus: 'submit' },
153
165
  );
154
- if ( requestData.storeIds && requestData.storeIds.length >0 ) {
166
+ if ( requestData.storeIds && requestData.storeIds.length > 0 ) {
155
167
  findAndQuery.push( { store_id: { $in: requestData.storeIds } } );
156
168
  }
157
- if ( requestData.sourceCheckList_id && requestData.sourceCheckList_id !='' ) {
169
+ if ( requestData.sourceCheckList_id && requestData.sourceCheckList_id != '' ) {
158
170
  findAndQuery.push( { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.sourceCheckList_id ) } );
159
171
  }
160
172
  if ( requestData.checklistId && requestData.checklistId.length > 0 ) {
@@ -175,11 +187,11 @@ export const downloadInsertOld = async ( req, res ) => {
175
187
  },
176
188
  } );
177
189
  let checklistDetails = await processedchecklistService.aggregate( findQuery );
178
- if ( checklistDetails && checklistDetails.length >0 ) {
190
+ if ( checklistDetails && checklistDetails.length > 0 ) {
179
191
  let name;
180
192
  let fileType = requestData?.fileType || 'pdfzip';
181
193
  if ( requestData.fileType == 'pdf' ) {
182
- if ( requestData?.sourceCheckList_id && requestData?.sourceCheckList_id !='' ) {
194
+ if ( requestData?.sourceCheckList_id && requestData?.sourceCheckList_id != '' ) {
183
195
  fileType = 'pdfzip';
184
196
  }
185
197
  if ( requestData?.checklistId && requestData?.checklistId.length > 1 ) {
@@ -191,7 +203,7 @@ export const downloadInsertOld = async ( req, res ) => {
191
203
  if ( requestData.checklistName != '' ) {
192
204
  name = requestData.checklistName + '_' + dayjs( requestData.fromDate ).format( 'DD/MM/YYYY' );
193
205
  if ( requestData?.toDate ) {
194
- name = name + '-'+ dayjs( requestData?.toDate ).format( 'DD/MM/YYYY' );
206
+ name = name + '-' + dayjs( requestData?.toDate ).format( 'DD/MM/YYYY' );
195
207
  }
196
208
  let regexName = new RegExp( `^${name.split( '(' )[0]} \\(.*\\)$`, 'i' );
197
209
  let type = requestData.fileType || 'pdfzip';
@@ -203,7 +215,7 @@ export const downloadInsertOld = async ( req, res ) => {
203
215
  name = requestData?.checklistName;
204
216
  }
205
217
 
206
- name = name+'-'+requestData.fileType;
218
+ name = name + '-' + requestData.fileType;
207
219
  let insertData = {
208
220
  'date_string': requestData.fromDate || '',
209
221
  'sourceCheckList_id': requestData?.sourceCheckList_id || '',
@@ -270,6 +282,7 @@ export const downloadList = async ( req, res ) => {
270
282
  findAndQuery.push(
271
283
  { client_id: requestData.clientId },
272
284
  { createdBy: new mongoose.Types.ObjectId( requestData.createdBy ) },
285
+ { status: { $ne: 'canceled' } },
273
286
  );
274
287
 
275
288
  findQuery.push( { $match: { $and: findAndQuery } } );
@@ -296,7 +309,7 @@ export const downloadList = async ( req, res ) => {
296
309
  return res.sendError( { error: 'No Data Found' }, 204 );
297
310
  }
298
311
 
299
- if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
312
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy != '' ) {
300
313
  findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
301
314
  } else {
302
315
  findQuery.push( { $sort: { ['_id']: -1 } } );
@@ -307,8 +320,8 @@ export const downloadList = async ( req, res ) => {
307
320
  findQuery.push( { $skip: skip }, { $limit: limit } );
308
321
  let getDownlaodData = await downloadService.aggregate( findQuery );
309
322
  let bucket = JSON.parse( process.env.BUCKET );
310
- for ( let i=0; i<getDownlaodData.length; i++ ) {
311
- if ( getDownlaodData[i].url && getDownlaodData[i].url !='' ) {
323
+ for ( let i = 0; i < getDownlaodData.length; i++ ) {
324
+ if ( getDownlaodData[i].url && getDownlaodData[i].url != '' ) {
312
325
  let inputData1 = {
313
326
  Bucket: bucket.sop,
314
327
  file_path: getDownlaodData[i].url,
@@ -345,10 +358,10 @@ export const downloadUpdate = async ( req, res ) => {
345
358
  if ( resultData ) {
346
359
  return res.sendSuccess( 'download updated successfully' );
347
360
  } else {
348
- return res.sendServerError( 'something went wrong, please try again' );
361
+ return res.sendError( { error: 'something went wrong, please try again' }, 500 );
349
362
  }
350
363
  } else {
351
- return res.sendBadRequest( 'Invalid Zip Id' );
364
+ return res.sendError( { error: 'Invalid Zip Id' }, 400 );
352
365
  }
353
366
  } catch ( error ) {
354
367
  console.log( 'error =>', error );
@@ -367,12 +380,11 @@ export const getChecklistFromZipId = async ( req, res ) => {
367
380
  brandLogo: '',
368
381
  checkListDescription: '',
369
382
  };
370
-
371
383
  if ( getzipdata ) {
372
384
  let getClientData = await clientService.findOne( { clientId: getzipdata.client_id } );
373
385
  if ( getClientData ) {
374
386
  brandInfo.clientName = getClientData.clientName;
375
- brandInfo.brandLogo = getzipdata.client_id+'/logo/'+getClientData.profileDetails.logo;
387
+ brandInfo.brandLogo = getzipdata.client_id + '/logo/' + getClientData.profileDetails.logo;
376
388
  }
377
389
 
378
390
  // if ( getzipdata.fileType == 'zipfiles' ) {
@@ -387,7 +399,12 @@ export const getChecklistFromZipId = async ( req, res ) => {
387
399
 
388
400
  if ( getzipdata.sourceCheckList_id && getzipdata.sourceCheckList_id != '' ) {
389
401
  let getChecklistDescription = await checklistService.findOne( { _id: getzipdata.sourceCheckList_id }, { checkListName: 1, checkListDescription: 1 } );
390
- brandInfo.checkListDescription = getChecklistDescription.checkListDescription || '';
402
+ if ( getzipdata.insertType == 'task' ) {
403
+ let getChecklistDescription = await taskConfigService.findOne( { _id: getzipdata.sourceCheckList_id }, { checkListName: 1, checkListDescription: 1 } );
404
+ brandInfo.checkListDescription = getChecklistDescription.checkListDescription || '';
405
+ } else {
406
+ brandInfo.checkListDescription = getChecklistDescription.checkListDescription || '';
407
+ }
391
408
 
392
409
  resultData.brandInfo = brandInfo;
393
410
  if ( getzipdata.sourceCheckList_id && getzipdata.sourceCheckList_id != '' ) {
@@ -404,13 +421,23 @@ export const getChecklistFromZipId = async ( req, res ) => {
404
421
  resultData.viewFlag = getzipdata.viewFlag;
405
422
  resultData.previewType = getzipdata.previewType || '';
406
423
  resultData.answerType = getzipdata.answerType || '';
424
+ resultData.insertType = getzipdata.insertType;
425
+ resultData.status = getzipdata.status;
426
+
407
427
  return res.sendSuccess( resultData );
408
428
  } else {
409
429
  let getchecklistsQuery = {};
410
430
  getchecklistsQuery._id = new mongoose.Types.ObjectId( getzipdata.checklistIdList[0] );
411
431
  let getchecklistsData = await processedchecklistService.findOne( getchecklistsQuery, { _id: 1, sourceCheckList_id: 1 } );
432
+ if ( getzipdata.insertType == 'task' ) {
433
+ getchecklistsData = await processedTaskService.findOne( getchecklistsQuery, { _id: 1, sourceCheckList_id: 1 } );
434
+ }
435
+
412
436
  if ( getchecklistsData ) {
413
437
  let getChecklistDescription = await checklistService.findOne( { _id: getchecklistsData.sourceCheckList_id }, { checkListName: 1, checkListDescription: 1 } );
438
+ if ( getzipdata.insertType == 'task' ) {
439
+ getChecklistDescription = await taskConfigService.findOne( { _id: getzipdata.sourceCheckList_id }, { checkListName: 1, checkListDescription: 1 } );
440
+ }
414
441
  brandInfo.checkListDescription = getChecklistDescription.checkListDescription || '';
415
442
  }
416
443
 
@@ -418,6 +445,8 @@ export const getChecklistFromZipId = async ( req, res ) => {
418
445
  resultData.checklistIds = getzipdata.checklistIdList;
419
446
  resultData.fileType = getzipdata.fileType;
420
447
  resultData.questions = getzipdata.questions;
448
+ resultData.insertType = getzipdata.insertType;
449
+ resultData.status = getzipdata.status;
421
450
  return res.sendSuccess( resultData );
422
451
  }
423
452
  } else {
@@ -459,12 +488,14 @@ export const getPDFCSVChecklistDetails = async ( req, res ) => {
459
488
  ];
460
489
 
461
490
  let getchecklistsData = await processedchecklistService.aggregate( query );
462
- if ( getchecklistsData && getchecklistsData.length >0 ) {
491
+
492
+
493
+ if ( getchecklistsData && getchecklistsData.length > 0 ) {
463
494
  let getbrand = await clientService.findOne( { clientId: getchecklistsData[0].client_id }, { clientName: 1 } );
464
495
  let brandInfo = {};
465
496
  if ( getbrand ) {
466
497
  brandInfo.clientName = getbrand.clientName;
467
- brandInfo.brandLogo = getchecklistsData[0].client_id+'/logo/brandLogo.png';
498
+ brandInfo.brandLogo = getchecklistsData[0].client_id + '/logo/brandLogo.png';
468
499
  }
469
500
 
470
501
  let getstore = await storeService.findOne( { id: getchecklistsData[0].store_id }, { storeProfile: 1 } );
@@ -472,7 +503,7 @@ export const getPDFCSVChecklistDetails = async ( req, res ) => {
472
503
  getchecklistsData[0].city = getstore?.storeProfile?.city || '--';
473
504
  getchecklistsData[0].state = getstore?.storeProfile?.state || '--';
474
505
  let questions = [];
475
- function processQuestion( question, section, questions, nested=false ) {
506
+ function processQuestion( question, section, questions, nested = false ) {
476
507
  let findExists = questions.find( ( item ) => item?.qno && item.qno == question.qno );
477
508
  if ( findExists && nested ) {
478
509
  let findIndex = questions.findIndex( ( item ) => item?.qno && item.qno == question.qno );
@@ -542,3 +573,19 @@ export const getPDFCSVChecklistDetails = async ( req, res ) => {
542
573
  };
543
574
 
544
575
 
576
+ export async function cancelDownload( req, res ) {
577
+ try {
578
+ let finddata = await downloadService.findOne( { _id: req.params.id, status: 'completed' } );
579
+ if ( finddata ) {
580
+ return res.sendError( 'Download Already Completed', 500 );
581
+ }
582
+
583
+ let update = await downloadService.updateOne( { _id: req.params.id }, { status: 'canceled' } );
584
+ if ( update ) {
585
+ return res.sendSuccess( 'Canceled Successfully' );
586
+ }
587
+ } catch ( error ) {
588
+ logger.error( { error: error, function: 'cancelDownload' } );
589
+ return res.sendError( error, 500 );
590
+ }
591
+ }
@@ -5,6 +5,12 @@ import * as storeService from '../services/store.service.js';
5
5
  import * as groupService from '../services/group.service.js';
6
6
  import * as clusterService from '../services/cluster.service.js';
7
7
  import * as processedchecklistconfigService from '../services/processedchecklistconfig.services.js';
8
+ import * as processedChecklist from '../services/processedchecklist.services.js';
9
+ import * as checklistLogs from '../services/checklistlog.service.js';
10
+ import * as ApproverModel from '../services/approver.service.js';
11
+
12
+ import mongoose from 'mongoose';
13
+ import dayjs from 'dayjs';
8
14
  // import axios from 'axios';
9
15
  async function LamdaServiceCall( url, data ) {
10
16
  try {
@@ -30,10 +36,10 @@ async function LamdaServiceCall( url, data ) {
30
36
  export async function getchecklist( req, res ) {
31
37
  try {
32
38
  let url = JSON.parse( process.env.LAMBDAURL );
33
- console.log( url.checklistQuestion );
39
+
34
40
 
35
41
  let resultData = await LamdaServiceCall( url.checklistQuestion, req.body );
36
- console.log( resultData );
42
+
37
43
 
38
44
  if ( resultData ) {
39
45
  if ( resultData.status_code == '200' ) {
@@ -125,16 +131,12 @@ export async function getcustomerunattendedlist( req, res ) {
125
131
 
126
132
  export async function storesList( req, res ) {
127
133
  try {
128
- console.log( req.body );
129
-
130
134
  let storeslist = await storeService.find( { clientId: req.body.clientId }, { storeId: 1, storeName: 1 } );
131
135
  if ( req.body.groups&&req.body.groups.length>0 ) {
132
136
  let stores = [];
133
137
  for ( let group of req.body.groups ) {
134
138
  let findGroup = await groupService.findOneGroup( { groupName: group.groupName }, { storeList: 1 } );
135
- console.log( findGroup );
136
139
  stores = [ ...stores, ...findGroup.storeList ];
137
- console.log( stores );
138
140
  }
139
141
  let storeslist = await storeService.find( { storeId: { $in: stores } }, { storeId: 1, storeName: 1 } );
140
142
  return res.sendSuccess( { result: storeslist } );
@@ -161,7 +163,7 @@ export const checklistDropdown = async ( req, res ) => {
161
163
  { client_id: requestData.clientId },
162
164
  // { date_iso: { $gte: fromDate } },
163
165
  // { date_iso: { $lte: toDate } },
164
- { checkListType: { $eq: 'custom' } },
166
+ // { checkListType: { $eq: 'custom' } },
165
167
  );
166
168
 
167
169
  findQuery.push( { $match: { $and: findAndQuery } } );
@@ -245,7 +247,17 @@ export const checklistDropdown = async ( req, res ) => {
245
247
  }
246
248
 
247
249
  let getChecklistData = await processedchecklistconfigService.aggregate( findQuery );
248
- result.checklistData = getChecklistData;
250
+
251
+ let AiCheklist = getChecklistData.filter( ( data ) => data.checkListType!='custom'&& data.publish== true );
252
+ let CustomCheklist = getChecklistData.filter( ( data ) => data.checkListType=='custom' );
253
+
254
+
255
+ AiCheklist.forEach( ( data ) => {
256
+ data.sourceCheckList_id = data.checkListType;
257
+ data.checkListType = 'AI';
258
+ } );
259
+
260
+ result.checklistData = [ ...CustomCheklist, ...AiCheklist ];
249
261
  return res.sendSuccess( result );
250
262
  } catch ( error ) {
251
263
  logger.error( { error: error, message: req.query, function: 'checklistDropdown' } );
@@ -253,6 +265,241 @@ export const checklistDropdown = async ( req, res ) => {
253
265
  }
254
266
  };
255
267
 
268
+
269
+ export async function approveChecklist( req, res ) {
270
+ try {
271
+ let toDate = new Date( req.body.toDate );
272
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
273
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
274
+ toDate.setUTCHours( 23, 59, 59, 59 );
275
+ let query = { sourceCheckList_id: req.body.sourceCheckList_id, date_iso: { $gte: req.body.fromDate, $lte: toDate }, $or: [ { checklistStatus: 'submit' }, { redoStatus: true } ], approvalEnable: true };
276
+ if ( req.body?.storeId?.length ) {
277
+ query['store_id'] = { $in: req.body.storeId };
278
+ }
279
+ let checklistDetails = await processedChecklist.find( query, { _id: 1, checklistStatus: 1, storeName: 1, store_id: 1, userEmail: 1 } );
280
+ if ( !checklistDetails.length ) {
281
+ return res.sendError( 'No data found', 204 );
282
+ }
283
+ let idList = checklistDetails.filter( ( ele ) => ele.checklistStatus == 'submit' ).map( ( item ) => item._id );
284
+ if ( idList.length ) {
285
+ let updateResponse = await processedChecklist.updateMany( { _id: { $in: idList } }, { approvalStatus: true } );
286
+ if ( updateResponse.modifiedCount || updateResponse.matchedCount ) {
287
+ let inputstores = checklistDetails.filter( ( data ) => data.checklistStatus == 'submit' );
288
+ console.log( inputstores );
289
+ let params = {
290
+ 'payload': {
291
+ sourceCheckList_id: req.body.sourceCheckList_id,
292
+ fromDate: req.body.fromDate,
293
+ toDate: req.body.toDate,
294
+ store_id: inputstores,
295
+ },
296
+ 'upsert': {
297
+ approvalStatus: true,
298
+ },
299
+ };
300
+
301
+ const requestOptions = {
302
+ method: 'POST',
303
+ headers: {
304
+ 'Content-Type': 'application/json',
305
+ },
306
+ body: JSON.stringify( params ),
307
+ };
308
+ let url = JSON.parse( process.env.LAMBDAURL );
309
+ let searchResponse = await fetch( url.approveChecklist, requestOptions );
310
+
311
+
312
+ if ( searchResponse.ok ) {
313
+ let pendingstores = checklistDetails.filter( ( data ) => data.checklistStatus != 'submit' );
314
+ if ( pendingstores&&pendingstores.length>0 ) {
315
+ return res.sendSuccess( `${pendingstores.length} remain pending approval due to being marked for redo.` );
316
+ }
317
+ return res.sendSuccess( 'Checklist has been approved the submitted stores' );
318
+ } else {
319
+ return res.sendError( 'Something went wrong', 500 );
320
+ }
321
+ }
322
+ } else {
323
+ let redoList = checklistDetails.filter( ( ele ) => ele.checklistStatus == 'open' );
324
+ return res.sendError( `${redoList.length} stores has not been approved since those stores are not submitted`, 400 );
325
+ }
326
+ } catch ( e ) {
327
+ logger.error( { function: 'approveChecklist', error: e } );
328
+ return res.sendError( e, 500 );
329
+ }
330
+ }
331
+
332
+ export async function redoChecklist( req, res ) {
333
+ try {
334
+ if ( !req.body.payload._id ) {
335
+ return res.sendError( 'Id is Required', 400 );
336
+ }
337
+ if ( !req.body.payload.section_id ) {
338
+ return res.sendError( 'Section id is Required', 400 );
339
+ }
340
+ if ( !req.body.payload.qno ) {
341
+ return res.sendError( 'Question number is Required', 400 );
342
+ }
343
+
344
+
345
+ let checklistDetails = await processedChecklist.findOne( { _id: req.body.payload._id }, { questionAnswers: 1, redoStatus: 1, checklistStatus: 1, client_id: 1, store_id: 1, storeName: 1, checkListType: 1, sourceCheckList_id: 1, checkListName: 1, submitTime: 1, userName: 1, answerType: 1 } );
346
+ if ( !checklistDetails ) {
347
+ return res.sendError( 'No data found', 204 );
348
+ }
349
+ let question = checklistDetails.questionAnswers;
350
+
351
+
352
+ let sectionIndex = question.findIndex( ( sec ) => sec.section_id == req.body.payload.section_id );
353
+ if ( sectionIndex == -1 ) {
354
+ return res.sendError( 'section is not found', 400 );
355
+ }
356
+
357
+ let data = { ...question[sectionIndex].questions[req.body.payload.qno - 1], redo: true, redoComment: req.body.payload?.checklistDescription || '' };
358
+ let userAnswer = data.userAnswer;
359
+
360
+
361
+ question[sectionIndex].questions[req.body.payload.qno - 1] = data;
362
+ question[sectionIndex].questions[req.body.payload.qno - 1].userAnswer = [];
363
+ question[sectionIndex].questions[req.body.payload.qno - 1].remarks = '';
364
+ checklistDetails.questionAnswers = question;
365
+ let updateData = {
366
+ checklistStatus: 'open',
367
+ redoStatus: true,
368
+ reinitiateStatus: true,
369
+ questionAnswers: question,
370
+ };
371
+
372
+ let response = await processedChecklist.updateOne( { _id: req.body.payload._id }, updateData );
373
+ if ( response.modifiedCount || response.matchedCount ) {
374
+ data = {
375
+ checklistId: checklistDetails.sourceCheckList_id,
376
+ checkListName: checklistDetails.checkListName,
377
+ checklistDescription: req.body.payload.checklistDescription,
378
+ sectionId: req.body.payload.section_id,
379
+ sectionName: question[sectionIndex].sectionName,
380
+ questionName: question[sectionIndex].questions[req.body.payload.qno - 1].qname,
381
+ action: 'redo',
382
+ store_id: checklistDetails.store_id,
383
+ storeName: checklistDetails.storeName,
384
+ client_id: checklistDetails.client_id,
385
+ processedChecklistId: checklistDetails._id,
386
+ type: checklistDetails.checkListType,
387
+ userAnswer: userAnswer,
388
+ initiatedBy: req.user.userName,
389
+ initiatedTime: dayjs().format(),
390
+ answerType: question[sectionIndex].questions[req.body.payload.qno - 1].answerType,
391
+ submitedBy: checklistDetails.userName,
392
+ submitTime: checklistDetails.submitTime,
393
+ };
394
+
395
+ // return;
396
+ await checklistLogs.create( data );
397
+
398
+ const requestOptions = {
399
+ method: 'POST',
400
+ headers: {
401
+ 'Content-Type': 'application/json',
402
+ },
403
+ body: JSON.stringify( req.body ),
404
+ };
405
+ let url = JSON.parse( process.env.LAMBDAURL );
406
+ let searchResponse = await fetch( url.redoChecklist, requestOptions );
407
+
408
+
409
+ if ( searchResponse.ok ) {
410
+ return res.sendSuccess( 'Question redo successfully' );
411
+ } else {
412
+ return res.sendError( 'Something went wrong', 500 );
413
+ }
414
+ } else {
415
+ return res.sendError( 'Something went wrong', 500 );
416
+ }
417
+ } catch ( e ) {
418
+ logger.error( { function: 'redoChecklist', error: e } );
419
+ return res.sendError( e, 500 );
420
+ }
421
+ }
422
+
423
+
424
+ export async function getLogs( req, res ) {
425
+ try {
426
+ let result = await checklistLogs.find( {
427
+ checklistId: new mongoose.Types.ObjectId( req.body.checklistId ),
428
+ processedChecklistId: new mongoose.Types.ObjectId( req.body.processedChecklistId ),
429
+ sectionId: new mongoose.Types.ObjectId( req.body.section_id ),
430
+ questionName: req.body.qname,
431
+ action: 'redo',
432
+ } );
433
+
434
+ let response =[];
435
+ for ( let data of result ) {
436
+ let userAnswer = {
437
+ actionType: 'answer',
438
+ userName: data.submitedBy,
439
+ Timestamp: data.submitTime,
440
+ userAnswer: data.userAnswer,
441
+ answerType: data.answerType,
442
+ };
443
+ let initiatedstatus = {
444
+ actionType: 'initation',
445
+ userName: data.initiatedBy,
446
+ Timestamp: data.initiatedTime,
447
+ description: data.checklistDescription,
448
+
449
+ };
450
+ response.push( initiatedstatus );
451
+ response.push( userAnswer );
452
+ }
453
+ response = response.sort( ( a, b ) => b.Timestamp - a.Timestamp );
454
+
455
+ res.sendSuccess( response );
456
+ } catch ( e ) {
457
+ logger.error( { function: 'getLogs', error: e } );
458
+ return res.sendError( e, 500 );
459
+ }
460
+ }
461
+
462
+
463
+ export async function approvalstatus( req, res ) {
464
+ try {
465
+ let Approver = await ApproverModel.find( { checkListId: req.body.checklistId } );
466
+ if ( Approver.length === 0 ) {
467
+ return res.sendSuccess( 'suceess' );
468
+ }
469
+ let filterApprover = Approver.filter( ( data ) => data.userEmail=== req.user.email );
470
+ if ( filterApprover.length === 0 ) {
471
+ if ( ( req?.user?.userType == 'client' && req.user.role == 'superadmin' ) ) {
472
+ let url = JSON.parse( process.env.LAMBDAURL );
473
+ let resultData = await LamdaServiceCall( url.approvalstatus, req.body );
474
+ if ( resultData ) {
475
+ if ( resultData.status_code == '200' ) {
476
+ return res.sendSuccess( resultData );
477
+ }
478
+ }
479
+ return res.sendError( 'No Content', 204 );
480
+ }
481
+ return res.sendSuccess( 'suceess' );
482
+ }
483
+
484
+
485
+ let url = JSON.parse( process.env.LAMBDAURL );
486
+ let resultData = await LamdaServiceCall( url.approvalstatus, req.body );
487
+
488
+ if ( resultData ) {
489
+ if ( resultData.status_code == '200' ) {
490
+ return res.sendSuccess( resultData );
491
+ } else {
492
+ return res.sendError( 'No Content', 204 );
493
+ }
494
+ } else {
495
+ return res.sendError( 'No Content', 204 );
496
+ }
497
+ } catch ( error ) {
498
+ logger.error( error, 'approvalstatus' );
499
+ return res.sendError( error, 500 );
500
+ }
501
+ }
502
+
256
503
  export const headerStoresV2 = async ( req, res ) => {
257
504
  try {
258
505
  let reqestData = req.body;
@@ -261,7 +508,7 @@ export const headerStoresV2 = async ( req, res ) => {
261
508
  let getRole = req.user.role;
262
509
  let getClientId = reqestData.clientId;
263
510
  let totalStores = await getStoresList( getUserEmail, getClientId, getUserType, getRole, req );
264
- console.log( totalStores.length );
511
+
265
512
 
266
513
  if ( totalStores && totalStores.length>0 ) {
267
514
  let storeQuery = [];