tango-app-api-trax 3.3.1-beta-8 → 3.3.1-beta-10

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-trax",
3
- "version": "3.3.1-beta-8",
3
+ "version": "3.3.1-beta-10",
4
4
  "description": "Trax",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -26,7 +26,7 @@
26
26
  "mongodb": "^6.8.0",
27
27
  "nodemon": "^3.1.4",
28
28
  "path": "^0.12.7",
29
- "tango-api-schema": "^2.2.35",
29
+ "tango-api-schema": "^2.2.41",
30
30
  "tango-app-api-middleware": "^3.1.50",
31
31
  "url": "^0.11.4",
32
32
  "winston": "^3.13.1",
@@ -354,14 +354,14 @@ export async function redoChecklist( req, res ) {
354
354
  return res.sendError( 'section is not found', 400 );
355
355
  }
356
356
 
357
- let findQuestion = question[sectionIndex].questions.findIndex( ( ele ) => ele.qno == req.body.payload.qno );
357
+ let findQuestion = question[sectionIndex].questions.findIndex( ( ele ) => ele?.parentQuestion ? ele.qno == req.body.payload.qno && ele.parentQuestion == req.body.payload.parentQuestion : ele.qno == req.body.payload.qno );
358
358
 
359
359
  let data = { ...question[sectionIndex].questions[findQuestion], redo: true, redoComment: req.body.payload?.checklistDescription || '' };
360
360
  // if ( checklistDetails.client_id == '458' ) {
361
361
  data.answers.forEach( ( item ) => {
362
362
  if ( item.showLinked ) {
363
363
  item.nestedQuestion.forEach( ( ele ) => {
364
- let eleIndex = question[sectionIndex].questions.findIndex( ( qn ) => qn.qno == parseInt( ele ) );
364
+ let eleIndex = question[sectionIndex].questions.findIndex( ( qn ) => qn?.parentQuestion ? qn.qno == parseInt( ele ) && qn?.parentQuestion == req.body.payload?.parentQuestion : qn.qno == parseInt( ele ) );
365
365
  let element = { ...question[sectionIndex].questions[eleIndex], redo: true, redoComment: '', linkquestionenabled: false };
366
366
  question[sectionIndex].questions[eleIndex] = element;
367
367
  question[sectionIndex].questions[eleIndex].userAnswer = [];
@@ -149,7 +149,7 @@ export async function startChecklist( req, res ) {
149
149
  }
150
150
 
151
151
  let updateData = {};
152
- let storeTimeZone = await storeService.findOne( { storeName: getBeforeChecklist[0].storeName }, { 'storeProfile.timeZone': 1 } );
152
+ let storeTimeZone = await storeService.findOne( { storeName: { $regex: getBeforeChecklist[0].storeName, $options: 'i' }, clientId: getBeforeChecklist[0].client_id }, { 'storeProfile.timeZone': 1 } );
153
153
  let currentDateTime;
154
154
  if ( storeTimeZone?.storeProfile?.timeZone ) {
155
155
  currentDateTime = dayjs().tz( storeTimeZone?.storeProfile?.timeZone );
@@ -244,6 +244,7 @@ export async function startChecklist( req, res ) {
244
244
  questions = [];
245
245
  for ( let question of section.questions ) {
246
246
  if ( !question.linkType ) {
247
+ question.parentQuestion = question.qno;
247
248
  questions.push( question );
248
249
  let linkedAnswer = new Set( question.answers.filter( ( ele ) => ele.showLinked ).flatMap( ( ele ) => ele.nestedQuestion ) );
249
250
  let linkedQuestion = section.questions.filter( ( qn ) => linkedAnswer.has( qn.qno ) );
@@ -1725,12 +1726,12 @@ export async function submitChecklist( req, res ) {
1725
1726
  updateQuery._id = new ObjectId( requestData.processedcheckListId );
1726
1727
  updateQuery.userId = req.user._id;
1727
1728
  updateQuery.date_string = requestData.date;
1728
- let storeTimeZone = await storeService.findOne( { storeName: getchecklist[0].storeName }, { 'storeProfile.timeZone': 1 } );
1729
+ let storeTimeZone = await storeService.findOne( { storeName: { $regex: getchecklist[0].storeName, $options: 'i' }, clientId: getchecklist[0].client_id }, { 'storeProfile.timeZone': 1 } );
1729
1730
  let currentDateTime;
1730
1731
  if ( storeTimeZone?.storeProfile?.timeZone ) {
1731
1732
  currentDateTime = dayjs().tz( storeTimeZone?.storeProfile?.timeZone );
1732
1733
  } else {
1733
- currentDateTime = dayjs();
1734
+ currentDateTime = requestData?.currentTime ? dayjs( requestData.currentTime, 'HH:mm:ss' ) : dayjs();
1734
1735
  }
1735
1736
  let updateData = {};
1736
1737
  let flagCount = QuestionFlag( req, res );
@@ -2605,6 +2606,7 @@ export async function questionList( req, res ) {
2605
2606
  questions = [];
2606
2607
  for ( let question of section.questions ) {
2607
2608
  if ( !question.linkType ) {
2609
+ question.parentQuestion = question.qno;
2608
2610
  questions.push( question );
2609
2611
  let linkedAnswer = new Set( question.answers.filter( ( ele ) => ele.showLinked ).flatMap( ( ele ) => ele.nestedQuestion ) );
2610
2612
  let linkedQuestion = section.questions.filter( ( qn ) => qn?.parentQuestion ? qn?.parentQuestion == question.qno && linkedAnswer.has( qn.qno ) : linkedAnswer.has( qn.qno ) );
@@ -564,7 +564,10 @@ export const flagCardsV1 = async ( req, res ) => {
564
564
  {
565
565
  $match: {
566
566
  client_id: clientId,
567
- store_id: { $in: storeId },
567
+ $or: [
568
+ { store_id: { $in: storeId } },
569
+ { aiStoreList: { $in: storeId } },
570
+ ],
568
571
  date_iso: { $gte: adjustedFromDate, $lte: adjustedToDate },
569
572
  },
570
573
  },
@@ -634,7 +637,7 @@ export const flagCardsV1 = async ( req, res ) => {
634
637
  flagCards.detectionFlag.count += resultData[item];
635
638
  }
636
639
  } );
637
- flagCards.totalFlag = flagCards.detectionFlag.count;
640
+ flagCards.totalFlag += flagCards.detectionFlag.count;
638
641
  }
639
642
 
640
643
  return res.sendSuccess( { flagCards } );
@@ -697,7 +700,12 @@ export const flagComparisonCardsV1 = async ( req, res ) => {
697
700
  }
698
701
 
699
702
  const createFindQuery = ( fromDate, toDate ) => [
700
- { $match: { client_id: requestData.clientId, store_id: { $in: requestData.storeId }, date_iso: { $gte: fromDate, $lte: toDate } } },
703
+ { $match: { client_id: requestData.clientId,
704
+ $or: [
705
+ { store_id: { $in: requestData.storeId } },
706
+ { aiStoreList: { $in: requestData.storeId } },
707
+ ],
708
+ date_iso: { $gte: fromDate, $lte: toDate } } },
701
709
  {
702
710
  $project: {
703
711
  timeFlag: 1,
@@ -859,11 +859,11 @@ export const assignedUserDetails = async ( req, res ) => {
859
859
  if ( !req.params.checklistId ) {
860
860
  return res.sendError( { message: 'checklist Id is required' }, 400 );
861
861
  }
862
- let limit = parseInt( req.query.limit ) || 10;
862
+ let limit = parseInt( req.query.limit ) || 5;
863
863
  let page = parseInt( req.query.offset ) || 0;
864
864
  let skip = limit * page;
865
865
 
866
- let query = [ { $match: { checkListId: new ObjectId( req.params.checklistId ) } } ];
866
+ let query = [ { $match: { checkListId: new ObjectId( req.params.checklistId ), coverage: req.body.coverage } } ];
867
867
  if ( req.query.search.trim() && req.query.search.trim() != '' ) {
868
868
  let searchValue = req.query.search;
869
869
  searchValue = searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
@@ -875,37 +875,81 @@ export const assignedUserDetails = async ( req, res ) => {
875
875
  }
876
876
  }
877
877
 
878
- query.push( {
879
- $facet: {
880
- data: [
881
- { $skip: skip }, { $limit: limit },
882
- ],
883
- count: [
884
- { $count: 'total' },
885
- ],
886
- },
887
- } );
878
+ query.push( { $sort: { _id: -1 } } );
888
879
 
889
880
  let checklistDetails = await assignedService.aggregate( query );
890
-
891
- if ( !checklistDetails[0].data.length ) {
881
+ if ( !checklistDetails.length ) {
892
882
  return res.sendError( 'no data found', 204 );
893
883
  }
894
884
 
895
885
  let userDetails = [];
896
- checklistDetails[0].data.forEach( ( item ) => {
897
- userDetails.push( {
898
- id: item._id,
899
- userName: item.userName,
900
- userEmail: item.userEmail,
901
- store_id: item.store_id,
902
- storeName: item.storeName,
903
- userPhone: item.userPhone,
904
- city: item.city,
905
- checkFlag: item.checkFlag,
906
- } );
907
- } );
908
- return res.sendSuccess( { users: userDetails, count: checklistDetails[0].count[0].total } );
886
+ let storeList = [];
887
+ let userList = [];
888
+ await Promise.all( checklistDetails.map( async ( ele ) => {
889
+ if ( ele?.store_id || ele?.clusterName ) {
890
+ storeList.push( ele.assignId );
891
+ } else {
892
+ userList.push( ele.assignId );
893
+ }
894
+
895
+ if ( ele?.clusterName ) {
896
+ let clusterDetails = await clusterServices.findOneCluster( { _id: ele.assignId }, { stores: 1 } );
897
+ if ( clusterDetails ) {
898
+ let storeDetails = await storeService.find( { _id: { $in: clusterDetails.stores.map( ( item ) => item.store ) } } );
899
+ storeDetails.forEach( ( item ) => {
900
+ userDetails.push( {
901
+ id: ele._id,
902
+ userName: item.spocDetails?.[0]?.name,
903
+ userEmail: item.spocDetails[0].email,
904
+ store_id: item?.storeId,
905
+ storeName: item?.storeName,
906
+ userPhone: item.spocDetails?.[0]?.phone,
907
+ city: ele?.city,
908
+ checkFlag: ele.checkFlag,
909
+ clusterName: ele?.clusterName || '',
910
+ teamName: ele?.teamName || '',
911
+ } );
912
+ } );
913
+ }
914
+ } else if ( ele?.teamName ) {
915
+ let teamDetails = await teamsServices.findOneTeams( { _id: ele.assignId }, { users: 1, teamName: 1 } );
916
+ if ( teamDetails ) {
917
+ let teamUserDetails = await userService.find( { _id: { $in: teamDetails.users.map( ( item ) => item.userId ) } } );
918
+ teamUserDetails.forEach( ( item ) => {
919
+ userDetails.push( {
920
+ id: ele._id,
921
+ userName: item.userName,
922
+ userEmail: item.email,
923
+ store_id: ele?.storeId,
924
+ storeName: ele?.storeName,
925
+ userPhone: item?.mobileNumber,
926
+ city: ele?.city,
927
+ checkFlag: ele.checkFlag,
928
+ clusterName: ele?.clusterName || '',
929
+ teamName: ele?.teamName || '',
930
+ } );
931
+ } );
932
+ }
933
+ } else {
934
+ userDetails.push( {
935
+ id: ele._id,
936
+ userName: ele.userName,
937
+ userEmail: ele.userEmail,
938
+ store_id: ele?.store_id,
939
+ storeName: ele?.storeName,
940
+ userPhone: ele.userPhone,
941
+ city: ele.city,
942
+ checkFlag: ele.checkFlag,
943
+ clusterName: ele?.clusterName || '',
944
+ teamName: ele?.teamName || '',
945
+ } );
946
+ }
947
+ } ) );
948
+
949
+ userDetails.sort( ( a, b ) => b.teamName - a.teamName );
950
+ console.log( userDetails );
951
+ let limitedData = userDetails.slice( skip, limit + skip );
952
+ return res.sendSuccess( { count: userDetails.length, storeList, userList, users: limitedData } );
909
953
  } catch ( e ) {
910
954
  logger.error( 'assignedUserDetails =>', e );
911
955
  return res.sendError( e, 500 );
@@ -1252,6 +1296,226 @@ export const updateConfigure =async ( req, res ) => {
1252
1296
  }
1253
1297
  };
1254
1298
 
1299
+ export const updateConfigurev1 =async ( req, res ) => {
1300
+ try {
1301
+ let inputBody = req.body;
1302
+ let id;
1303
+ let checklistDetails;
1304
+ if ( !inputBody.checkListDetails._id ) {
1305
+ return res.sendError( 'checkListId is Required', 400 );
1306
+ }
1307
+
1308
+ if ( inputBody.checkListDetails.checkListType == 'mobileusagedetection' && inputBody.submitType == 'publish' ) {
1309
+ if ( !inputBody?.checkListDetails?.alert?.usageExceeds ) {
1310
+ return res.sendError( 'Please Enter usage exceeds', 400 );
1311
+ }
1312
+
1313
+ if ( !inputBody?.checkListDetails?.alert?.alertsTo.length ) {
1314
+ return res.sendError( 'Please select users to send alert', 400 );
1315
+ }
1316
+
1317
+ if ( !inputBody?.checkListDetails?.detectionArea.length ) {
1318
+ return res.sendError( 'Please select detection area', 400 );
1319
+ }
1320
+ }
1321
+
1322
+ if ( !inputBody.checkListDetails.assignedUsers.length && inputBody.submitType == 'publish' ) {
1323
+ return res.sendError( 'Please Assigned a user', 400 );
1324
+ }
1325
+
1326
+ if ( !inputBody?.checkListDetails?.approver.length && inputBody.submitType == 'publish' ) {
1327
+ return res.sendError( 'Please assign approver', 400 );
1328
+ }
1329
+
1330
+
1331
+ let logInsertData = {
1332
+ action: inputBody.submitType == 'publish' ? 'checklistPublishUsingConfigPage' : 'checklistConfigDraft',
1333
+ checklistId: inputBody.checkListDetails._id,
1334
+ checkListName: inputBody?.checkListDetails.checkListName,
1335
+ createdBy: req.user._id,
1336
+ createdByName: req.user.userName,
1337
+ client_id: req.body.clientId,
1338
+ createdByEmail: req.user.email,
1339
+ approver: inputBody?.checkListDetails?.approver || [],
1340
+ };
1341
+ await checklistLogs.create( logInsertData );
1342
+
1343
+ checklistDetails = await checklistService.findOne( { _id: inputBody.checkListDetails._id, type: 'checklist', isdeleted: false } );
1344
+
1345
+ if ( [ 'mobileusagedetection', 'storeopenandclose', 'uniformdetection' ].includes( inputBody.checkListDetails.checkListType ) && inputBody.uploadUser ) {
1346
+ checklistDetails = await checklistService.findOne( { _id: inputBody.checkListDetails._id, type: 'checklist' } );
1347
+ }
1348
+
1349
+ if ( !checklistDetails ) {
1350
+ return res.sendError( 'no data found', 204 );
1351
+ }
1352
+
1353
+ let currentDate = dayjs.utc().format();
1354
+ let updatedscheduleEndTimeISO = dayjs.utc( inputBody?.checkListDetails?.scheduleEndTime, 'hh:mm A' ).format( 'HH:mm:ss' );
1355
+ let newUpdatedDate = dayjs.utc( updatedscheduleEndTimeISO, 'HH:mm:ss' ).format();
1356
+
1357
+ if ( inputBody.submitType == 'publish' && inputBody?.showEdit && typeof inputBody?.editSubmit == 'undefined' && newUpdatedDate > currentDate ) {
1358
+ let checkSubmitDetails = await processedchecklist.findOne( { sourceCheckList_id: inputBody.checkListDetails._id, date_string: dayjs().format( 'YYYY-MM-DD' ), checklistStatus: 'submit' } );
1359
+ if ( checkSubmitDetails ) {
1360
+ return res.sendError( 'Checklist got submitted', 400 );
1361
+ }
1362
+ }
1363
+
1364
+ if ( checklistDetails?.publishDate && inputBody.submitType == 'publish' ) {
1365
+ let date = dayjs();
1366
+ let diff = date.diff( dayjs.utc( checklistDetails?.publishDate ), 'minutes' );
1367
+ if ( diff < 5 ) {
1368
+ let mins = ( 5 - diff ) > 1 ? 'minutes' : 'minute';
1369
+ return res.sendError( `Please try after ${5 - diff} ${mins}`, 400 );
1370
+ }
1371
+ }
1372
+
1373
+ if ( inputBody?.checkListDetails?.scheduleRepeatedMonthSetup == 'date' ) {
1374
+ inputBody.checkListDetails.scheduleRepeatedDay = inputBody?.checkListDetails?.scheduleRepeatedDay;
1375
+ }
1376
+
1377
+ if ( inputBody?.checkListDetails?.scheduleDate ) {
1378
+ inputBody.checkListDetails.scheduleDate = new Date( inputBody?.checkListDetails?.scheduleDate );
1379
+ }
1380
+ let configDetails = {
1381
+ ...inputBody?.checkListDetails,
1382
+ publishDate: inputBody.checkListDetails.publish ? new Date() : '',
1383
+ scheduleStartTimeISO: dayjs.utc( inputBody?.checkListDetails?.scheduleStartTime, 'hh:mm A' ).format(),
1384
+ scheduleEndTimeISO: dayjs.utc( inputBody?.checkListDetails?.scheduleEndTime, 'hh:mm A' ).format(),
1385
+ scheduleDate: inputBody?.checkListDetails?.scheduleDate ? dayjs( inputBody.checkListDetails.scheduleDate ).format() : '',
1386
+ approver: inputBody?.checkListDetails?.approver.length ? inputBody?.checkListDetails?.approver.map( ( item ) => {
1387
+ return { name: item.name, value: item.value };
1388
+ } ) : [],
1389
+ };
1390
+
1391
+ if ( inputBody?.checkListDetails?.approver.length ) {
1392
+ let data = [];
1393
+ let existEmail = await traxApprover.find( { checkListId: inputBody.checkListDetails._id, isDeleted: false } );
1394
+ let mailList = existEmail.map( ( item ) => item.userEmail );
1395
+ existEmail = inputBody?.checkListDetails?.approver.filter( ( item ) => mailList.includes( item.value ) ).map( ( item ) => item.value );
1396
+ let userMailList = inputBody?.checkListDetails?.approver.map( ( ele ) => ele.value );
1397
+ inputBody?.checkListDetails?.approver.forEach( ( ele ) => {
1398
+ if ( !existEmail.includes( ele.value ) ) {
1399
+ data.push( {
1400
+ userEmail: ele.value,
1401
+ checkListId: inputBody.checkListDetails._id,
1402
+ type: 'checklist',
1403
+ checkListName: inputBody?.checkListDetails.checkListName,
1404
+ client_id: inputBody?.clientId,
1405
+ } );
1406
+ }
1407
+ } );
1408
+
1409
+ await traxApprover.updateMany( { checkListId: inputBody.checkListDetails._id, userEmail: { $nin: userMailList } }, { isDeleted: true } );
1410
+ if ( data.length ) {
1411
+ await traxApprover.insertMany( data );
1412
+ }
1413
+ }
1414
+
1415
+ if ( checklistDetails.checkListType != 'custom' && !checklistDetails.client_id ) {
1416
+ let getchecklistNumber = await checklistService.findOne( { client_id: req.body.clientId, type: 'checklist' }, { checkListNumber: 1 } );
1417
+ let name = checklistDetails.checkListName.split( '(' )[0];
1418
+ let checkListNameDetails = await checklistService.find( { checkListName: { $regex: name }, client_id: req.body.clientId, type: 'checklist', isdeleted: false } );
1419
+ if ( checkListNameDetails.length ) {
1420
+ let nameLength = ( checkListNameDetails.length-1 ) + 1;
1421
+ checklistDetails.checkListName = checklistDetails.checkListName.split( '(' )[0] + '(' + nameLength + ')';
1422
+ } else {
1423
+ checklistDetails.checkListName = checklistDetails.checkListName;
1424
+ }
1425
+ configDetails.type = 'checklist';
1426
+ configDetails.checkListName = checklistDetails.checkListName;
1427
+ configDetails.checkListDescription = checklistDetails.checkListDescription;
1428
+ if ( getchecklistNumber ) {
1429
+ configDetails.checkListNumber = getchecklistNumber.checkListNumber + 1;
1430
+ } else {
1431
+ configDetails.checkListNumber = 1;
1432
+ }
1433
+ configDetails.createdBy= req.user._id;
1434
+ configDetails.createdByName=req.user.userName;
1435
+ delete inputBody.checkListDetails['_id'];
1436
+ if ( [ 'storeopenandclose', 'uniformdetection' ].includes( checklistDetails.checkListType ) ) {
1437
+ let query = { client_id: req.body.clientId, type: 'checklist', isdeleted: false };
1438
+ if ( checklistDetails.checkListType == 'storeopenandclose' ) {
1439
+ query.checkListType= 'storeopenandclose';
1440
+ } else {
1441
+ query.checkListType= 'uniformdetection';
1442
+ }
1443
+ let storeChecklisDetails = await checklistService.findOne( query );
1444
+ if ( storeChecklisDetails ) {
1445
+ inputBody.checkListDetails._id = storeChecklisDetails._id;
1446
+ }
1447
+ }
1448
+ }
1449
+ let query = {
1450
+ client_id: req.body.clientId,
1451
+ checkListType: checklistDetails.checkListType,
1452
+ _id: inputBody.checkListDetails._id,
1453
+ };
1454
+ let response;
1455
+ if ( inputBody.checkListDetails._id ) {
1456
+ if ( [ 'mobileusagedetection', 'storeopenandclose', 'uniformdetection' ].includes( inputBody.checkListDetails.checkListType ) && inputBody.uploadUser ) {
1457
+ configDetails.isdeleted = false;
1458
+ }
1459
+ id = inputBody.checkListDetails._id;
1460
+ response = await checklistService.updateOne( query, configDetails );
1461
+ } else {
1462
+ configDetails.client_id = req.body.clientId;
1463
+ configDetails.checkListType = checklistDetails.checkListType;
1464
+ response = await checklistService.create( configDetails );
1465
+ id = response._id;
1466
+ }
1467
+ // if ( inputBody.checkListDetails.assignedUsers.length ) {
1468
+ // await assignedService.deleteMany( { checkListId: inputBody.checkListDetails._id } );
1469
+ // await Promise.all( inputBody.checkListDetails.assignedUsers.map( async ( user ) => {
1470
+ // let data = {
1471
+ // ...user,
1472
+ // clientId: req.body.clientId,
1473
+ // checkListName: checklistDetails.checkListName,
1474
+ // checklistId: checklistDetails._id,
1475
+ // };
1476
+ // await assignUsers( data );
1477
+ // } ) );
1478
+ // }
1479
+ if ( inputBody.submitType == 'publish' ) {
1480
+ if ( inputBody.checkListDetails.checkListType == 'custom' ) {
1481
+ let currentDate = dayjs.utc().format();
1482
+ let updatedscheduleEndTimeISO = dayjs.utc( configDetails.scheduleEndTimeISO ).format( 'HH:mm:ss' );
1483
+ let newUpdatedDate = dayjs.utc( updatedscheduleEndTimeISO, 'HH:mm:ss' ).format();
1484
+ if ( newUpdatedDate > currentDate ) {
1485
+ let deleteQuery = {
1486
+ $and: [
1487
+ { date_string: dayjs().format( 'YYYY-MM-DD' ) },
1488
+ { sourceCheckList_id: new ObjectId( req.params.checklistId ) },
1489
+ { scheduleEndTime_iso: { $gt: currentDate } },
1490
+ ],
1491
+ };
1492
+ deleteQuery.$and.push( { checklistStatus: { $ne: 'submit' } } );
1493
+ let PClicklist = await processedchecklist.deleteMany( deleteQuery );
1494
+ logger.info( { function: 'updateConfigure', query: deleteQuery } );
1495
+ if ( PClicklist.acknowledged || inputBody.updateConfigure ) {
1496
+ await insertSingleProcessData( inputBody.checkListDetails._id, 0, inputBody.updateConfigure, inputBody?.editSubmit, inputBody?.showEdit );
1497
+ } else {
1498
+ res.sendError( 'something went wrong, please try again', 500 );
1499
+ }
1500
+ } else {
1501
+ logger.info( `Schudled End Time Breached Checklist publish true => Checklist Name: ${checklistDetails.checkListName}` );
1502
+ }
1503
+
1504
+ futureDaysDataRemove( currentDate, req.params.checklistId, checklistDetails.checkListName, '333' );
1505
+ }
1506
+ }
1507
+ if ( response?.modifiedCount || response?.matchedCount || response?.upsertedCount ) {
1508
+ return res.sendSuccess( { id, message: 'Configured Updated Successfully' } );
1509
+ }
1510
+ if ( response._id ) {
1511
+ return res.sendSuccess( { id, message: 'Configured Added Successfully' } );
1512
+ }
1513
+ } catch ( e ) {
1514
+ logger.error( 'updateConfigure =>', e );
1515
+ return res.sendError( e, 500 );
1516
+ }
1517
+ };
1518
+
1255
1519
  export const updatePublish = async ( req, res ) => {
1256
1520
  try {
1257
1521
  if ( typeof req?.body?.checklistId == 'undefined' && typeof req.body.type == 'undefined' ) {
@@ -1580,6 +1844,127 @@ export const validateUser = async ( req, res ) => {
1580
1844
  }
1581
1845
  };
1582
1846
 
1847
+ export const validateUserv1 = async ( req, res ) => {
1848
+ try {
1849
+ if ( !req.body.assignedUsers.length ) {
1850
+ return res.sendError( 'Please Enter user Details', 400 );
1851
+ }
1852
+
1853
+ let assignDetails = req.body.assignedUsers;
1854
+
1855
+ const duplicateStore = assignDetails.reduce( ( acc, obj ) => {
1856
+ if ( !acc[obj.storeName.toLowerCase()] ) {
1857
+ acc[obj.storeName.toLowerCase()] = {
1858
+ email: [ obj.userEmail.toLowerCase() ],
1859
+ count: 1,
1860
+ };
1861
+ } else {
1862
+ if ( acc[obj.storeName.toLowerCase()] ) {
1863
+ if ( acc[obj.storeName.toLowerCase()].email.includes( obj.userEmail.toLowerCase() ) ) {
1864
+ acc[obj.storeName.toLowerCase()].count++;
1865
+ } else {
1866
+ acc[obj.storeName.toLowerCase()].email.push( obj.userEmail.toLowerCase() );
1867
+ }
1868
+ }
1869
+ }
1870
+ return acc;
1871
+ }, {} );
1872
+
1873
+ const duplicateStores = Object.keys( duplicateStore ).filter( ( storeName ) => duplicateStore[storeName].count > 1 );
1874
+ if ( duplicateStores.length ) {
1875
+ return res.sendSuccess( { validate: false, ExistsEmail: duplicateStores, message: 'store and email is Duplicated' } );
1876
+ }
1877
+
1878
+ if ( req.body.hasOwnProperty( 'delete' ) ) {
1879
+ let checkExists = await assignedService.findOne( { userEmail: assignDetails[0].userEmail, storeName: assignDetails[0].storeName, checkListId: req.body.id } );
1880
+ if ( checkExists ) {
1881
+ return res.sendError( 'User already Exists', 400 );
1882
+ }
1883
+ }
1884
+ // let userChunk =await chunkArray( users, 10 );
1885
+
1886
+ await Promise.all( users.map( async ( chunk ) => {
1887
+ await processUser( [ chunk ] );
1888
+ } ) );
1889
+
1890
+ let userEmailList = assignDetails.map( ( item ) => item.userEmail.toLowerCase() );
1891
+ let storeList = assignDetails.map( ( item ) => item.storeName.toLowerCase() );
1892
+
1893
+ let userQuery = [
1894
+ {
1895
+ $project: {
1896
+ newEmail: { $toLower: '$email' },
1897
+ isActive: 1,
1898
+ clientId: 1,
1899
+ },
1900
+ },
1901
+ {
1902
+ $match: {
1903
+ newEmail: { $in: userEmailList },
1904
+ isActive: true,
1905
+ clientId: req.body.clientId,
1906
+ },
1907
+ },
1908
+ ];
1909
+
1910
+ let storeQuery = [
1911
+ {
1912
+ $project: {
1913
+ 'storeName': { $toLower: '$storeName' },
1914
+ 'clientId': 1,
1915
+ 'storeProfile.city': 1,
1916
+ 'status': 1,
1917
+ },
1918
+ },
1919
+ {
1920
+ $match: {
1921
+ clientId: req.body.clientId,
1922
+ storeName: { $in: storeList },
1923
+ },
1924
+ },
1925
+ ];
1926
+
1927
+ let [ userDetails, storeDetails ] = await Promise.all[
1928
+ await userService.aggregate( userQuery ),
1929
+ await storeService.aggregate( storeQuery )
1930
+ ];
1931
+
1932
+ let existUSerEmail = userDetails.map( ( ele ) => ele.email );
1933
+
1934
+ userDetails = userEmailList.filter( ( ele ) => !existUSerEmail.includes( ele ) );
1935
+
1936
+ let existsStore = storeDetails.map( ( ele ) => ele.storeName );
1937
+ storeDetails = storeList.filter( ( ele ) => !existsStore.includes( ele ) );
1938
+
1939
+ if ( userDetails.length || storeDetails.length ) {
1940
+ return res.sendError( { validate: false, user: userDetails, store: storeDetails }, 400 );
1941
+ }
1942
+
1943
+ return res.sendSuccess( { validate: true } );
1944
+ } catch ( e ) {
1945
+ logger.error( 'validateUser 2=>', e );
1946
+ return res.sendError( e, 500 );
1947
+ }
1948
+ };
1949
+
1950
+ export async function assignUser( req, res ) {
1951
+ try {
1952
+ let inputBody = req.body;
1953
+ let assignDetails = inputBody.assignUsers;
1954
+ if ( inputBody.type == 'upload' ) {
1955
+ await assignedService.deleteMany( { checkListId: inputBody.checklistId } );
1956
+ }
1957
+ await Promise.all( assignDetails.map( ( assign ) => {
1958
+ assignUsers( assign );
1959
+ } ) );
1960
+ return res.sendSuccess( 'Details updated successfully' );
1961
+ } catch ( e ) {
1962
+ logger.error( { functionName: 'assignUser', error: e } );
1963
+ return res.sendError( e, 500 );
1964
+ }
1965
+ }
1966
+
1967
+
1583
1968
  async function uploadUser( req, res ) {
1584
1969
  try {
1585
1970
  let inputBody = req.body;
@@ -2711,3 +3096,148 @@ export const checklistV2 = async ( req, res ) => {
2711
3096
  return res.sendError( e, 500 );
2712
3097
  }
2713
3098
  };
3099
+
3100
+ async function assignUsers( data ) {
3101
+ let assignedData;
3102
+ if ( data.coverage == 'store' ) {
3103
+ if ( data.type == 'cluster' ) {
3104
+ let clusterDetails = await clusterServices.findOneCluster( { _id: data.id }, { stores: 1, clusterName: 1 } );
3105
+ if ( clusterDetails ) {
3106
+ assignedData = {
3107
+ checkFlag: true,
3108
+ checkListId: data.checklistId,
3109
+ checkListName: data.checkListName,
3110
+ client_id: data?.clientId,
3111
+ clusterName: clusterDetails?.clusterName,
3112
+ assignId: data.id,
3113
+ };
3114
+ }
3115
+ } else {
3116
+ let storeDetails = await storeService.findOne( { _id: data.id } );
3117
+ if ( storeDetails ) {
3118
+ let userDetails = await userService.findOne( { userEmail: storeDetails?.spocDetails?.email, clientId: data.clientId } );
3119
+ if ( !userDetails ) {
3120
+ let userData = {
3121
+ userName: storeDetails.spocDetails?.[0]?.name,
3122
+ email: storeDetails.spocDetails[0].email,
3123
+ mobileNumber: storeDetails.spocDetails?.[0]?.phone,
3124
+ clientId: data.clientId,
3125
+ };
3126
+ createUser( userData );
3127
+ }
3128
+ assignedData = {
3129
+ store_id: storeDetails.storeId,
3130
+ storeName: storeDetails.storeName,
3131
+ userId: userDetails._id,
3132
+ userName: userDetails.userName,
3133
+ userEmail: userDetails.email,
3134
+ userPhone: userDetails?.mobileNumber,
3135
+ city: storeDetails?.storeProfile?.city,
3136
+ country: storeDetails?.storeprofile?.country,
3137
+ checkFlag: true,
3138
+ checkListId: data.checklistId,
3139
+ checkListName: data.checkListName,
3140
+ client_id: data.clientId,
3141
+ assignId: data.id,
3142
+ };
3143
+ }
3144
+ }
3145
+ } else {
3146
+ if ( data.type == 'teams' ) {
3147
+ let teamDetails = await teamsServices.findOneTeams( { _id: data.id }, { users: 1, teamName: 1 } );
3148
+ if ( teamDetails ) {
3149
+ assignedData = {
3150
+ checkFlag: true,
3151
+ checkListId: data.checklistId,
3152
+ checkListName: data.checkListName,
3153
+ client_id: data.clientId,
3154
+ teamName: teamDetails?.teamName,
3155
+ assignId: data.id,
3156
+ };
3157
+ }
3158
+ } else {
3159
+ let userDetails = await userService.findOne( { _id: { $in: data.id } } );
3160
+ if ( userDetails ) {
3161
+ assignedData = {
3162
+ userId: userDetails._id,
3163
+ userName: userDetails.userName,
3164
+ userEmail: userDetails.email,
3165
+ userPhone: userDetails?.mobileNumber,
3166
+ checkFlag: true,
3167
+ checkListId: data.checklistId,
3168
+ checkListName: data.checkListName,
3169
+ client_id: data.clientId,
3170
+ assignId: data.id,
3171
+ };
3172
+ }
3173
+ }
3174
+ }
3175
+ await assignedService.create( assignedData );
3176
+ }
3177
+
3178
+ export async function checklistAssign( req, res ) {
3179
+ try {
3180
+ if ( !req.body.type ) {
3181
+ return res.sendError( 'Type is required', 400 );
3182
+ }
3183
+ if ( !req.body.coverage ) {
3184
+ return res.sendError( 'coverage is required', 400 );
3185
+ }
3186
+ if ( !req.body.id ) {
3187
+ return res.sendError( 'coverage is required', 400 );
3188
+ }
3189
+ if ( !req.body.checklistId ) {
3190
+ return res.sendError( 'Checklist id is required', 400 );
3191
+ }
3192
+ if ( !req.body.clientId ) {
3193
+ return res.sendError( 'Client id is required', 400 );
3194
+ }
3195
+ let checklistDetails = await checklistService.findOne( { _id: req.body.checklistId } );
3196
+ if ( !checklistDetails ) {
3197
+ return res.sendError( 'No data found', 204 );
3198
+ }
3199
+ req.body.checkListName = checklistDetails.checkListName;
3200
+ await assignUsers( req.body );
3201
+ return res.sendSuccess( 'Details updated successfully' );
3202
+ } catch ( e ) {
3203
+ logger.error( { functionName: 'checklistAssign', error: e } );
3204
+ return res.sendError( e, 500 );
3205
+ }
3206
+ }
3207
+
3208
+ export async function removeAssign( req, res ) {
3209
+ try {
3210
+ if ( !req.body.id ) {
3211
+ return res.sendError( 'Id is required', 400 );
3212
+ }
3213
+ if ( !req.body.checkListId ) {
3214
+ return res.sendError( 'Checklist id is required', 400 );
3215
+ }
3216
+ let checklistDetails = await checklistService.findOne( { _id: req.body.checkListId } );
3217
+ if ( !checklistDetails ) {
3218
+ return res.sendError( 'No data found', 204 );
3219
+ }
3220
+ await assignedService.deleteMany( { assignId: req.body.id } );
3221
+ return res.sendSuccess( 'Details removed successfully' );
3222
+ } catch ( e ) {
3223
+ logger.error( { functionName: 'removeAssign', error: e } );
3224
+ return res.sendError( e, 500 );
3225
+ }
3226
+ }
3227
+
3228
+ export async function deleteAssignList( req, res ) {
3229
+ try {
3230
+ if ( !req.body.checkListId ) {
3231
+ return res.sendError( 'Checklist id is required', 400 );
3232
+ }
3233
+ let checklistDetails = await checklistService.findOne( { _id: req.body.checkListId, isdeleted: false } );
3234
+ if ( !checklistDetails ) {
3235
+ return res.sendError( 'No data found', 204 );
3236
+ }
3237
+ await assignedService.updateMany( { checkListId: req.body.checkListId }, { isdeleted: true } );
3238
+ return res.sendSuccess( 'Assign details removed successfully' );
3239
+ } catch ( e ) {
3240
+ logger.error( { functionName: 'deleteAssignList', error: e } );
3241
+ return res.sendError( e, 500 );
3242
+ }
3243
+ }
@@ -16,6 +16,7 @@ traxRouter
16
16
  .post( '/validateUser', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), validate( uploadUserValidation ), traxController.validateUser )
17
17
  .get( '/userDetails/:checklistId', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ), validate( duplicateValidation ), traxController.assignedUserDetails )
18
18
  .post( '/checklistConfigure', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), traxController.updateConfigure )
19
+ .post( '/checklistConfigurev1', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), traxController.updateConfigurev1 )
19
20
  .delete( '/deleteChecklist/:checklistId', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), validate( duplicateValidation ), traxController.deleteChecklist )
20
21
  .put( '/publish', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), validate( publishValidation ), traxController.updatePublish )
21
22
  .get( '/userList', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ), traxController.userlist )
@@ -23,6 +24,9 @@ traxRouter
23
24
  .get( '/aichecklist', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ), validate( aichecklistValidation ), traxController.aiChecklist )
24
25
  .get( '/predefinedChecklist', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ), validate( aichecklistValidation ), traxController.preDefinedChecklist )
25
26
  .post( '/selectAssign', validate( selectAssign ), traxController.selectAssign )
26
- .get( '/checklistV2', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ), validate( checklistPageSchema ), traxController.checklistV2 );
27
+ .get( '/checklistV2', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ), validate( checklistPageSchema ), traxController.checklistV2 )
28
+ .post( '/assign', isAllowedSessionHandler, traxController.checklistAssign )
29
+ .post( '/remove', isAllowedSessionHandler, traxController.removeAssign )
30
+ .post( '/removeAssignList', isAllowedSessionHandler, traxController.deleteAssignList );
27
31
 
28
32
  // isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ),
@@ -28,4 +28,8 @@ export const aggregate = async ( query = {} ) => {
28
28
  return model.checklistassignconfigModel.aggregate( query );
29
29
  };
30
30
 
31
+ export const count = async ( query = {} ) => {
32
+ return model.checklistassignconfigModel.countDocuments( query );
33
+ };
34
+
31
35