tango-app-api-trax 3.4.1-beta-1 → 3.5.0-ai-2

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.
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- import { logger } from 'tango-app-api-middleware';
3
+ import { logger, insertOpenSearchData } from 'tango-app-api-middleware';
4
4
  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';
@@ -14,6 +14,12 @@ import dayjs from 'dayjs';
14
14
  import { aggregateTeams, findteams } from '../services/teams.service.js';
15
15
  import { aggregate } from '../services/user.service.js';
16
16
  // import axios from 'axios';
17
+ import timeZone from 'dayjs/plugin/timezone.js';
18
+ import customParseFormat from 'dayjs/plugin/customParseFormat.js';
19
+
20
+ dayjs.extend( customParseFormat );
21
+ dayjs.extend( timeZone );
22
+
17
23
  async function LamdaServiceCall( url, data ) {
18
24
  try {
19
25
  const requestOptions = {
@@ -282,15 +288,24 @@ export async function approveChecklist( req, res ) {
282
288
  if ( req.body?.storeId?.length ) {
283
289
  query['store_id'] = { $in: req.body.storeId };
284
290
  }
285
- let checklistDetails = await processedChecklist.find( query, { _id: 1, checklistStatus: 1, storeName: 1, store_id: 1, userEmail: 1 } );
291
+ let checklistDetails = await processedChecklist.find( query, { _id: 1, checklistStatus: 1, storeName: 1, store_id: 1, userEmail: 1, client_id: 1, coverage: 1, checkListName: 1 } );
286
292
  if ( !checklistDetails.length ) {
287
293
  return res.sendError( 'No data found', 204 );
288
294
  }
289
295
  let idList = checklistDetails.filter( ( ele ) => ele.checklistStatus == 'submit' ).map( ( item ) => item._id );
290
296
  if ( idList.length ) {
291
- let updateResponse = await processedChecklist.updateMany( { _id: { $in: idList } }, { approvalStatus: true } );
297
+ let approvalUpdateData = {
298
+ approvalStatus: true,
299
+ approvalTime: new Date(),
300
+ // approvalTime_string: dayjs().format( 'hh:mm A, DD MMM YYYY' ),
301
+ approvalTime_string: dayjs().tz( 'Asia/Kolkata' ).format( 'hh:mm A, DD MMM YYYY' ),
302
+ approvalByName: req.user.userName,
303
+ approvalByEmail: req.user.email,
304
+ };
305
+ let updateResponse = await processedChecklist.updateMany( { _id: { $in: idList }, approvalStatus: false }, approvalUpdateData );
292
306
  if ( updateResponse.modifiedCount || updateResponse.matchedCount ) {
293
307
  let inputstores = checklistDetails.filter( ( data ) => data.checklistStatus == 'submit' );
308
+ let storeNames = inputstores.map( ( data ) => data.storeName );
294
309
 
295
310
 
296
311
  let params = {
@@ -303,6 +318,10 @@ export async function approveChecklist( req, res ) {
303
318
  },
304
319
  'upsert': {
305
320
  approvalStatus: true,
321
+ approvalTime: new Date(),
322
+ approvalTime_string: dayjs().tz( 'Asia/Kolkata' ).format( 'hh:mm A, DD MMM YYYY' ),
323
+ approvalByName: req.user.userName,
324
+ approvalByEmail: req.user.email,
306
325
  },
307
326
  };
308
327
 
@@ -315,8 +334,28 @@ export async function approveChecklist( req, res ) {
315
334
  };
316
335
  let url = JSON.parse( process.env.LAMBDAURL );
317
336
  let searchResponse = await fetch( url.approveChecklist, requestOptions );
318
-
319
- console.log( searchResponse.ok );
337
+ let logData= {
338
+ 'client_id': checklistDetails[0].client_id,
339
+ 'createAt': new Date(),
340
+ 'sourceCheckList_id': req.body.sourceCheckList_id,
341
+ 'checkListName': checklistDetails[0].checkListName,
342
+ 'fromCheckListName': '',
343
+ 'type': 'checklist',
344
+ 'action': 'approved',
345
+ 'storeName': '',
346
+ 'store_id': '',
347
+ 'createdByEmail': req.user.email,
348
+ 'createdBy': req.user._id,
349
+ 'coverage': checklistDetails[0].coverage,
350
+ 'logDetails': {
351
+ 'approved': storeNames,
352
+ },
353
+ 'userType': req.user.userType,
354
+ };
355
+ console.log( 'logData', logData );
356
+ let urlopensearch = JSON.parse( process.env.OPENSEARCH );
357
+ insertOpenSearchData( urlopensearch.traxActivityLog, logData );
358
+ // console.log( searchResponse.ok );
320
359
  if ( searchResponse.ok ) {
321
360
  let pendingstores = checklistDetails.filter( ( data ) => data.checklistStatus != 'submit' );
322
361
  if ( pendingstores && pendingstores.length > 0 ) {
@@ -332,6 +371,7 @@ export async function approveChecklist( req, res ) {
332
371
  return res.sendError( `${redoList.length} stores has not been approved since those stores are not submitted`, 400 );
333
372
  }
334
373
  } catch ( e ) {
374
+ // console.log( 'e', e );
335
375
  logger.error( { function: 'approveChecklist', error: e } );
336
376
  return res.sendError( e, 500 );
337
377
  }
@@ -350,7 +390,7 @@ export async function redoChecklist( req, res ) {
350
390
  }
351
391
 
352
392
 
353
- 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 } );
393
+ 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, coverage: 1, userEmail: 1 } );
354
394
  if ( !checklistDetails ) {
355
395
  return res.sendError( 'No data found', 204 );
356
396
  }
@@ -399,7 +439,7 @@ export async function redoChecklist( req, res ) {
399
439
  };
400
440
 
401
441
  let response = await processedChecklist.updateOne( { _id: req.body.payload._id }, updateData );
402
- console.log( response );
442
+ // console.log( response );
403
443
  if ( response.modifiedCount || response.matchedCount ) {
404
444
  let storeTimeZone = await storeService.findOne( { storeName: checklistDetails.storeName }, { 'storeProfile.timeZone': 1 } );
405
445
  let currentDateTime;
@@ -439,8 +479,28 @@ export async function redoChecklist( req, res ) {
439
479
  };
440
480
  let url = JSON.parse( process.env.LAMBDAURL );
441
481
  let searchResponse = await fetch( url.redoChecklist, requestOptions );
442
- console.log( searchResponse.ok );
443
-
482
+ // console.log( searchResponse.ok );
483
+ let logData= {
484
+ 'client_id': checklistDetails.client_id,
485
+ 'createAt': new Date(),
486
+ 'sourceCheckList_id': checklistDetails.sourceCheckList_id,
487
+ 'checkListName': checklistDetails.checkListName,
488
+ 'fromCheckListName': checklistDetails.checkListName,
489
+ 'type': 'checklist',
490
+ 'action': checklistDetails.redoStatus?'multiredo': 'redo',
491
+ 'storeName': checklistDetails?.storeName?checklistDetails?.storeName:'',
492
+ 'store_id': checklistDetails?.store_id?checklistDetails?.store_id:'',
493
+ 'userName': checklistDetails.userName,
494
+ 'userEmail': checklistDetails.userEmail,
495
+ 'createdByEmail': req.user.email,
496
+ 'createdBy': req.user.userName,
497
+ 'coverage': checklistDetails.coverage,
498
+ 'logDetails': {},
499
+ 'userType': req.user.userType,
500
+ };
501
+ let urlopensearch = JSON.parse( process.env.OPENSEARCH );
502
+ console.log( 'logData', logData );
503
+ insertOpenSearchData( urlopensearch.traxActivityLog, logData );
444
504
  if ( searchResponse.ok ) {
445
505
  return res.sendSuccess( 'Question redo successfully' );
446
506
  } else {
@@ -455,6 +515,150 @@ export async function redoChecklist( req, res ) {
455
515
  }
456
516
  }
457
517
 
518
+ export async function redomultiChecklist( req, res ) {
519
+ try {
520
+ let inputData = req.body.payload.coverage==='user'?req.body.payload.userEmail:req.body.payload.storeName;
521
+
522
+
523
+ for ( let originalData of inputData ) {
524
+ if ( !originalData.checklistId ) {
525
+ return res.sendError( 'Id is Required', 400 );
526
+ }
527
+ if ( !originalData.section_id ) {
528
+ return res.sendError( 'Section id is Required', 400 );
529
+ }
530
+ if ( !req.body.payload.qno ) {
531
+ return res.sendError( 'Question number is Required', 400 );
532
+ }
533
+
534
+
535
+ let checklistDetails = await processedChecklist.findOne( { _id: originalData.checklistId }, { 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 } );
536
+ if ( !checklistDetails ) {
537
+ return res.sendError( 'No data found', 204 );
538
+ }
539
+ let question = checklistDetails.questionAnswers;
540
+
541
+
542
+ let sectionIndex = question.findIndex( ( sec ) => sec.sectionName == req.body.payload.sectionName );
543
+ if ( sectionIndex == -1 ) {
544
+ return res.sendError( 'section is not found', 400 );
545
+ }
546
+
547
+ let findQuestion = question[sectionIndex].questions.findIndex( ( ele ) => ele.qno == req.body.payload.qno );
548
+
549
+ let data = { ...question[sectionIndex].questions[findQuestion], redo: true, redoComment: req.body.payload?.checklistDescription || '' };
550
+ // if ( checklistDetails.client_id == '458' ) {
551
+ data.answers.forEach( ( item ) => {
552
+ if ( item.showLinked ) {
553
+ item.nestedQuestion.forEach( ( ele ) => {
554
+ let eleIndex = question[sectionIndex].questions.findIndex( ( qn ) => qn.qno == parseInt( ele ) );
555
+ let element = { ...question[sectionIndex].questions[eleIndex], redo: true, redoComment: '', linkquestionenabled: false };
556
+ question[sectionIndex].questions[eleIndex] = element;
557
+ question[sectionIndex].questions[eleIndex].userAnswer = [];
558
+ question[sectionIndex].questions[eleIndex].remarks = '';
559
+ } );
560
+ }
561
+ } );
562
+ // data.answers.forEach( ( item ) => {
563
+ // if ( item.showLinked ) {
564
+ // item.nestedQuestion = [];
565
+ // item.showLinked = false;
566
+ // item.linkedQuestion = 0;
567
+ // }
568
+ // } );
569
+ // }
570
+ let userAnswer = data.userAnswer;
571
+
572
+ question[sectionIndex].questions[findQuestion] = data;
573
+ question[sectionIndex].questions[findQuestion].userAnswer = [];
574
+ question[sectionIndex].questions[findQuestion].remarks = '';
575
+ checklistDetails.questionAnswers = question;
576
+ let updateData = {
577
+ checklistStatus: 'open',
578
+ redoStatus: true,
579
+ reinitiateStatus: true,
580
+ questionAnswers: question,
581
+ };
582
+
583
+ let response = await processedChecklist.updateOne( { _id: originalData.checklistId }, updateData );
584
+ // console.log( response );
585
+ if ( response.modifiedCount || response.matchedCount ) {
586
+ let storeTimeZone = await storeService.findOne( { storeName: checklistDetails.storeName }, { 'storeProfile.timeZone': 1 } );
587
+ let currentDateTime;
588
+ if ( storeTimeZone?.storeProfile?.timeZone ) {
589
+ currentDateTime = dayjs().tz( storeTimeZone?.storeProfile?.timeZone );
590
+ } else {
591
+ currentDateTime = dayjs();
592
+ }
593
+ data = {
594
+ checklistId: checklistDetails.sourceCheckList_id,
595
+ checkListName: checklistDetails.checkListName,
596
+ checklistDescription: req.body.payload.checklistDescription,
597
+ sectionId: originalData.section_id,
598
+ sectionName: question[sectionIndex].sectionName,
599
+ questionName: question[sectionIndex].questions[findQuestion].qname,
600
+ action: 'redo',
601
+ store_id: checklistDetails?.store_id?checklistDetails?.store_id:'',
602
+ storeName: checklistDetails?.storeName?checklistDetails?.storeName:'',
603
+ client_id: checklistDetails.client_id,
604
+ processedChecklistId: checklistDetails._id,
605
+ type: checklistDetails.checkListType,
606
+ userAnswer: userAnswer,
607
+ initiatedBy: req.user.userName,
608
+ initiatedTime: dayjs.utc( currentDateTime.format( 'hh:mm:ss A, DD MMM YYYY' ), 'hh:mm:ss A, DD MMM YYYY' ).format(),
609
+ answerType: question[sectionIndex].questions[findQuestion].answerType,
610
+ submitedBy: checklistDetails.userName,
611
+ submitTime: checklistDetails.submitTime,
612
+ };
613
+ await checklistLogs.create( data );
614
+
615
+
616
+ req.body.payload._id=originalData.checklistId;
617
+ req.body.payload.section_id=originalData.section_id;
618
+ req.body.payload.uniqueNo=originalData.uniqueNo;
619
+ // console.log( req.body );
620
+
621
+
622
+ const requestOptions = {
623
+ method: 'POST',
624
+ headers: {
625
+ 'Content-Type': 'application/json',
626
+ },
627
+ body: JSON.stringify( req.body ),
628
+ };
629
+ let url = JSON.parse( process.env.LAMBDAURL );
630
+ let searchResponse = await fetch( url.redoChecklist, requestOptions );
631
+ console.log( searchResponse.ok );
632
+ let logData= {
633
+ 'client_id': checklistDetails.client_id,
634
+ 'createAt': new Date(),
635
+ 'sourceCheckList_id': checklistDetails.sourceCheckList_id,
636
+ 'checkListName': checklistDetails.checkListName,
637
+ 'fromCheckListName': checklistDetails.checkListName,
638
+ 'type': 'checklist',
639
+ 'action': checklistDetails.redoStatus?'multiredo': 'redo',
640
+ 'storeName': checklistDetails?.storeName?checklistDetails?.storeName:'',
641
+ 'store_id': checklistDetails?.store_id?checklistDetails?.store_id:'',
642
+ 'userName': checklistDetails.userName,
643
+ 'userEmail': checklistDetails.userEmail,
644
+ 'createdByEmail': req.user.email,
645
+ 'createdBy': req.user.userName,
646
+ 'coverage': checklistDetails.coverage,
647
+ 'logDetails': {},
648
+ 'userType': req.user.userType,
649
+ };
650
+ let urlopensearch = JSON.parse( process.env.OPENSEARCH );
651
+ console.log( 'logData', logData );
652
+ insertOpenSearchData( urlopensearch.traxActivityLog, logData );
653
+ }
654
+ }
655
+ return res.sendSuccess( 'Question redo successfully' );
656
+ } catch ( e ) {
657
+ logger.error( { function: 'redomultiChecklist', error: e } );
658
+ return res.sendError( e, 500 );
659
+ }
660
+ }
661
+
458
662
 
459
663
  export async function getLogs( req, res ) {
460
664
  try {