tango-app-api-trax 3.3.1-beta-18 → 3.3.1-beta-19

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.
@@ -863,7 +863,7 @@ export const assignedUserDetails = async ( req, res ) => {
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 ), isdeleted: false } } ];
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() );
@@ -963,6 +963,105 @@ export const assignedUserDetails = async ( req, res ) => {
963
963
  }
964
964
  };
965
965
 
966
+ export const assignedUserDetailsv1 = async ( req, res ) => {
967
+ try {
968
+ if ( !req.params.checklistId ) {
969
+ return res.sendError( { message: 'checklist Id is required' }, 400 );
970
+ }
971
+ // let limit = parseInt( req.query.limit ) || 5;
972
+ // let page = parseInt( req.query.offset ) || 0;
973
+ // let skip = limit * page;
974
+
975
+ let query = [ { $match: { checkListId: new ObjectId( req.params.checklistId ), isdeleted: false } } ];
976
+ if ( req.query.search.trim() && req.query.search.trim() != '' ) {
977
+ let searchValue = req.query.search;
978
+ searchValue = searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
979
+ if ( searchValue.length > 1 ) {
980
+ query.push( { $addFields: { storeLower: { $toLower: '$storeName' } } } );
981
+ query.push( { $match: { storeLower: { $in: searchValue } } } );
982
+ } else {
983
+ query.push( { $match: { storeName: { $regex: req.query.search.trim(), $options: 'i' } } } );
984
+ }
985
+ }
986
+
987
+ query.push( { $sort: { _id: -1 } } );
988
+
989
+ let checklistDetails = await assignedService.aggregate( query );
990
+ if ( !checklistDetails.length ) {
991
+ return res.sendError( 'no data found', 204 );
992
+ }
993
+
994
+ let userDetails = [];
995
+ let storeList = [];
996
+ let userList = [];
997
+ await Promise.all( checklistDetails.map( async ( ele ) => {
998
+ if ( ele?.store_id || ele?.clusterName ) {
999
+ storeList.push( ele.assignId );
1000
+ } else {
1001
+ userList.push( ele.assignId );
1002
+ }
1003
+
1004
+ if ( ele?.clusterName ) {
1005
+ let clusterDetails = await clusterServices.findOneCluster( { _id: ele.assignId }, { stores: 1 } );
1006
+ if ( clusterDetails ) {
1007
+ let storeDetails = await storeService.find( { _id: { $in: clusterDetails.stores.map( ( item ) => item.store ) } } );
1008
+ storeDetails.forEach( ( item ) => {
1009
+ userDetails.push( {
1010
+ id: ele._id,
1011
+ userName: item.spocDetails?.[0]?.name,
1012
+ userEmail: item.spocDetails?.[0]?.email,
1013
+ store_id: item?.storeId,
1014
+ storeName: item?.storeName,
1015
+ userPhone: item.spocDetails?.[0]?.phone,
1016
+ city: ele?.city,
1017
+ checkFlag: ele.checkFlag,
1018
+ clusterName: ele?.clusterName || '',
1019
+ teamName: ele?.teamName || '',
1020
+ } );
1021
+ } );
1022
+ }
1023
+ } else if ( ele?.teamName ) {
1024
+ let teamDetails = await teamsServices.findOneTeams( { _id: ele.assignId }, { users: 1, teamName: 1 } );
1025
+ if ( teamDetails ) {
1026
+ let teamUserDetails = await userService.find( { _id: { $in: teamDetails.users.map( ( item ) => item.userId ) } } );
1027
+ teamUserDetails.forEach( ( item ) => {
1028
+ userDetails.push( {
1029
+ id: ele._id,
1030
+ userName: item.userName,
1031
+ userEmail: item.email,
1032
+ store_id: ele?.storeId,
1033
+ storeName: ele?.storeName,
1034
+ userPhone: item?.mobileNumber,
1035
+ city: ele?.city,
1036
+ checkFlag: ele.checkFlag,
1037
+ clusterName: ele?.clusterName || '',
1038
+ teamName: ele?.teamName || '',
1039
+ } );
1040
+ } );
1041
+ }
1042
+ } else {
1043
+ userDetails.push( {
1044
+ id: ele._id,
1045
+ userName: ele.userName,
1046
+ userEmail: ele.userEmail,
1047
+ store_id: ele?.store_id,
1048
+ storeName: ele?.storeName,
1049
+ userPhone: ele.userPhone,
1050
+ city: ele.city,
1051
+ checkFlag: ele.checkFlag,
1052
+ clusterName: ele?.clusterName || '',
1053
+ teamName: ele?.teamName || '',
1054
+ } );
1055
+ }
1056
+ } ) );
1057
+
1058
+ return res.sendSuccess( { count: userDetails.length, storeList, userList, users: limitedData } );
1059
+ } catch ( e ) {
1060
+ logger.error( 'assignedUserDetails =>', e );
1061
+ return res.sendError( e, 500 );
1062
+ }
1063
+ };
1064
+
966
1065
  export const runAIInsert = async ( req, res ) => {
967
1066
  try {
968
1067
  let requestData = req.body;
@@ -1471,18 +1570,19 @@ export const updateConfigurev1 =async ( req, res ) => {
1471
1570
  response = await checklistService.create( configDetails );
1472
1571
  id = response._id;
1473
1572
  }
1474
- // if ( inputBody.checkListDetails.assignedUsers.length ) {
1475
- // await assignedService.deleteMany( { checkListId: inputBody.checkListDetails._id } );
1476
- // await Promise.all( inputBody.checkListDetails.assignedUsers.map( async ( user ) => {
1477
- // let data = {
1478
- // ...user,
1479
- // clientId: req.body.clientId,
1480
- // checkListName: checklistDetails.checkListName,
1481
- // checklistId: checklistDetails._id,
1482
- // };
1483
- // await assignUsers( data );
1484
- // } ) );
1485
- // }
1573
+ if ( inputBody.checkListDetails.assignedUsers.length ) {
1574
+ await assignedService.deleteMany( { checkListId: inputBody.checkListDetails._id } );
1575
+ await Promise.all( inputBody.checkListDetails.assignedUsers.map( async ( user ) => {
1576
+ let data = {
1577
+ ...user,
1578
+ clientId: req.body.clientId,
1579
+ checkListName: checklistDetails.checkListName,
1580
+ checklistId: checklistDetails._id,
1581
+ coverage: checklistDetails.coverage,
1582
+ };
1583
+ await assignUsers( data );
1584
+ } ) );
1585
+ }
1486
1586
  if ( inputBody.coverage ) {
1487
1587
  await assignedService.deleteMany( { coverage: { $ne: inputBody.coverage } } );
1488
1588
  }
@@ -1854,7 +1954,7 @@ export const validateUser = async ( req, res ) => {
1854
1954
  }
1855
1955
  };
1856
1956
 
1857
- export const validateUserv1 = async ( req, res ) => {
1957
+ export const validateUserv1 = async ( req, res, next ) => {
1858
1958
  try {
1859
1959
  if ( !req.body.assignedUsers.length ) {
1860
1960
  return res.sendError( 'Please Enter user Details', 400 );
@@ -1950,7 +2050,7 @@ export const validateUserv1 = async ( req, res ) => {
1950
2050
  return res.sendError( { validate: false, user: userDetails, store: storeDetails }, 400 );
1951
2051
  }
1952
2052
 
1953
- return res.sendSuccess( { validate: true } );
2053
+ next();
1954
2054
  } catch ( e ) {
1955
2055
  logger.error( 'validateUser 2=>', e );
1956
2056
  return res.sendError( e, 500 );
@@ -2406,7 +2506,7 @@ export async function insertSingleProcessData( checklistId, processId = 0, oldDa
2406
2506
  updatedchecklist = checklistDetails;
2407
2507
  }
2408
2508
  if ( updatedchecklist ) {
2409
- insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedchecklist, date, startTimeIso, endTimeIso, insertdata, processId, oldData, editSubmit, showEdit );
2509
+ insertPCBulkV4( getCLconfig, checklistId, currentdate, updatedchecklist, date, startTimeIso, endTimeIso, insertdata, processId, oldData, editSubmit, showEdit );
2410
2510
  }
2411
2511
  }
2412
2512
  }
@@ -2417,7 +2517,322 @@ export async function insertSingleProcessData( checklistId, processId = 0, oldDa
2417
2517
  }
2418
2518
  };
2419
2519
 
2420
- async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedchecklist, date, startTimeIso, endTimeIso, insertdata, processId, oldData, editSubmit, showEdit ) {
2520
+ // async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedchecklist, date, startTimeIso, endTimeIso, insertdata, processId, oldData, editSubmit, showEdit ) {
2521
+ // let getquestionQuery = [];
2522
+ // if ( [ 'storeopenandclose', 'mobileusagedetection', 'uniformdetection' ].includes( getCLconfig.checkListType ) ) {
2523
+ // getquestionQuery.push(
2524
+ // {
2525
+ // $match: {
2526
+ // checkListId: new ObjectId( checklistId ),
2527
+ // checkFlag: true,
2528
+ // },
2529
+ // },
2530
+ // {
2531
+ // $group: {
2532
+ // '_id': '$store_id',
2533
+ // 'store_id': { $first: '$store_id' },
2534
+ // 'client_id': { $first: '$client_id' },
2535
+ // 'storeName': { $first: '$storeName' },
2536
+ // 'userId': { $first: '$userId' },
2537
+ // 'userEmail': { $first: '$userEmail' },
2538
+ // 'userName': { $first: '$userName' },
2539
+ // 'checkFlag': { $first: '$checkFlag' },
2540
+ // 'checkListId': { $first: '$checkListId' },
2541
+ // 'checkListName': { $first: '$checkListName' },
2542
+ // 'country': { $first: '$country' },
2543
+ // 'createdAt': { $first: '$createdAt' },
2544
+ // 'updatedAt': { $first: '$updatedAt' },
2545
+ // },
2546
+ // },
2547
+ // );
2548
+ // } else {
2549
+ // getquestionQuery.push( {
2550
+ // $match: {
2551
+ // checkListId: new ObjectId( checklistId ),
2552
+ // checkFlag: true,
2553
+ // isdeleted: false,
2554
+ // },
2555
+ // } );
2556
+ // }
2557
+ // let allQuestion = await assignedService.aggregate( getquestionQuery );
2558
+ // if ( allQuestion ) {
2559
+ // let userIdList = [];
2560
+ // let tokenList = [];
2561
+ // let notifyUserList = [];
2562
+ // let status = [ { checklistStatus: { $ne: 'open' } } ];
2563
+
2564
+ // for ( let element4 of allQuestion ) {
2565
+ // let getToken = await userService.findOne( { _id: element4.userId }, { fcmToken: 1 } );
2566
+ // if ( getToken && getToken?.fcmToken && element4?.sendNotification && showEdit ) {
2567
+ // tokenList.push( getToken.fcmToken );
2568
+ // }
2569
+ // if ( !element4?.sendNotification ) {
2570
+ // notifyUserList.push( { token: getToken?.fcmToken, storeName: element4.storeName, id: element4._id } );
2571
+ // }
2572
+ // if ( !getCLconfig?.allowedMultiSubmit ) {
2573
+ // let query;
2574
+ // if ( getCLconfig.allowOnce && ![ 'onetime', 'daily' ].includes( getCLconfig.schedule ) ) {
2575
+ // if ( [ 'weekday', 'weekly', 'monthly' ].includes( getCLconfig.schedule ) ) {
2576
+ // let startDate; let endDate;
2577
+ // if ( [ 'weekday', 'weekly' ].includes( getCLconfig.schedule ) ) {
2578
+ // startDate = dayjs.utc( date ).clone().startOf( 'week' );
2579
+ // endDate = dayjs.utc( date ).clone().endOf( 'week' );
2580
+ // } else {
2581
+ // startDate = dayjs.utc( date ).clone().startOf( 'month' );
2582
+ // endDate = dayjs.utc( date ).clone().endOf( 'month' );
2583
+ // }
2584
+ // query = {
2585
+ // sourceCheckList_id: getCLconfig._id,
2586
+ // $or: [
2587
+ // { submitTime: { $exists: true } },
2588
+ // ...status,
2589
+ // ],
2590
+ // userId: element4.userId,
2591
+ // store_id: element4.store_id,
2592
+ // $and: [ {
2593
+ // date_iso: {
2594
+ // $gte: new Date( startDate.format( 'YYYY-MM-DD' ) ),
2595
+ // $lte: new Date( endDate.format( 'YYYY-MM-DD' ) ) },
2596
+ // } ],
2597
+ // };
2598
+ // } else {
2599
+ // if ( getCLconfig.schedule == 'range' ) {
2600
+ // query = {
2601
+ // sourceCheckList_id: getCLconfig._id,
2602
+ // $or: [
2603
+ // { submitTime: { $exists: true } },
2604
+ // ...status,
2605
+ // ],
2606
+ // userId: element4.userId,
2607
+ // store_id: element4.store_id,
2608
+ // $and: [
2609
+ // {
2610
+ // date_iso: {
2611
+ // $gte: new Date( dayjs( getCLconfig.configStartDate ).format( 'YYYY-MM-DD' ) ),
2612
+ // $lte: new Date( dayjs( getCLconfig.configEndDate ).format( 'YYYY-MM-DD' ) ),
2613
+ // },
2614
+ // },
2615
+ // ],
2616
+ // };
2617
+ // }
2618
+ // }
2619
+ // } else {
2620
+ // query = {
2621
+ // date_string: date,
2622
+ // sourceCheckList_id: getCLconfig._id,
2623
+ // $or: [
2624
+ // ...status,
2625
+ // { submitTime: { $exists: true } },
2626
+ // ],
2627
+ // userId: element4.userId,
2628
+ // store_id: element4.store_id,
2629
+ // };
2630
+ // }
2631
+ // let getsubmitDetails = await processedchecklist.find( query );
2632
+ // function findDifferences( obj1, obj2 ) {
2633
+ // return Object.keys( obj1 ).reduce( ( diff, key ) => {
2634
+ // if ( !isEqual( obj1[key], obj2[key] ) ) {
2635
+ // diff[key] = { value1: obj1[key], value2: obj2[key] };
2636
+ // }
2637
+ // return diff;
2638
+ // }, {} );
2639
+ // }
2640
+ // if ( getsubmitDetails.length ) {
2641
+ // if ( showEdit && ( ( editSubmit && getsubmitDetails[0].checklistStatus == 'submit' ) || getsubmitDetails[0].checklistStatus == 'inprogress' ) ) {
2642
+ // let modifiedCount = 0;
2643
+ // let questionList = insertdata.questionAnswers;
2644
+ // if ( getsubmitDetails[0].checkListName != getCLconfig.checkListName ) {
2645
+ // getsubmitDetails[0].checkListName = getCLconfig.checkListName;
2646
+ // }
2647
+ // if ( getsubmitDetails[0].checkListDescription != getCLconfig.checkListDescription ) {
2648
+ // getsubmitDetails[0].checkListDescription = getCLconfig.checkListDescription;
2649
+ // }
2650
+ // let sectionList = [];
2651
+ // for ( let [ index, section ] of questionList.entries() ) {
2652
+ // let checkExists = getsubmitDetails[0].questionAnswers.findIndex( ( sec ) => sec.sectionName == section?.sectionOldName || sec.sectionName == section.sectionName );
2653
+ // if ( checkExists != -1 ) {
2654
+ // getsubmitDetails[0].questionAnswers[index].section_id = section.section_id;
2655
+ // getsubmitDetails[0].questionAnswers[index].sectionName = section.sectionName;
2656
+ // let question = [];
2657
+ // section.questions.forEach( ( qns ) => {
2658
+ // let findQuestion = getsubmitDetails[0].questionAnswers[index].questions.findIndex( ( ele ) => ele.qname.trim() == qns?.oldQname?.trim() || ele.qname.trim() == qns.qname.trim() );
2659
+ // if ( findQuestion != -1 ) {
2660
+ // let data = JSON.parse( JSON.stringify( getsubmitDetails[0].questionAnswers[index].questions[findQuestion] ) );
2661
+ // getsubmitDetails[0].questionAnswers[index].questions[findQuestion].qno = qns.qno;
2662
+ // delete data.userAnswer;
2663
+ // delete data.parentanswer;
2664
+ // delete data.remarks;
2665
+ // delete data.linkquestionenabled;
2666
+ // if ( data.descriptivetype == null ) {
2667
+ // delete data.descriptivetype;
2668
+ // }
2669
+ // qns.answers.forEach( ( ans ) => {
2670
+ // delete ans.validationAnswer;
2671
+ // delete ans.answeroptionNumber;
2672
+ // } );
2673
+ // data.answers.forEach( ( ans ) => {
2674
+ // delete ans.index;
2675
+ // delete ans.validationAnswer;
2676
+ // delete ans.answeroptionNumber;
2677
+ // } );
2678
+ // const compare = findDifferences( data, qns );
2679
+ // if ( compare?.answerType || compare?.answers || compare?.linkType ) {
2680
+ // logger.info( 'compare =>', compare );
2681
+ // modifiedCount++;
2682
+ // question.push( qns );
2683
+ // } else {
2684
+ // getsubmitDetails[0].questionAnswers[index].questions[findQuestion].qname = qns.qname;
2685
+ // question.push( getsubmitDetails[0].questionAnswers[index].questions[findQuestion] );
2686
+ // }
2687
+ // } else {
2688
+ // modifiedCount++;
2689
+ // question.push( qns );
2690
+ // }
2691
+ // } );
2692
+ // getsubmitDetails[0].questionAnswers[index].questions = question;
2693
+ // sectionList.push( getsubmitDetails[0].questionAnswers[index] );
2694
+ // } else {
2695
+ // modifiedCount++;
2696
+ // sectionList.push( section );
2697
+ // }
2698
+ // }
2699
+ // getsubmitDetails[0].questionAnswers = sectionList;
2700
+ // if ( modifiedCount ) {
2701
+ // getsubmitDetails[0].checklistStatus = 'inprogress';
2702
+ // getsubmitDetails[0].date_string = dayjs( currentdate ).format( 'YYYY-MM-DD' );
2703
+ // getsubmitDetails[0].date_iso = new Date( date );
2704
+ // getsubmitDetails[0].redoStatus = false;
2705
+ // getsubmitDetails[0].approvalStatus = false;
2706
+ // }
2707
+ // let data = { ...getsubmitDetails[0]._doc };
2708
+ // await processedchecklist.updateOne( { _id: getsubmitDetails[0]._id }, data );
2709
+ // if ( editSubmit && getsubmitDetails[0].checklistStatus == 'submit' ) {
2710
+ // console.log( editSubmit );
2711
+ // let user = {
2712
+ // _id: getsubmitDetails[0].userId,
2713
+ // clientId: getsubmitDetails[0].client_id,
2714
+ // };
2715
+ // updateOpenSearch( user, { processedcheckListId: getsubmitDetails[0]._id, date: getsubmitDetails[0].date_string } );
2716
+ // }
2717
+ // }
2718
+ // if ( getsubmitDetails[0]?.checklistStatus == 'submit' ) {
2719
+ // userIdList.push( element4._id );
2720
+ // continue;
2721
+ // }
2722
+ // }
2723
+ // }
2724
+ // delete element4._id;
2725
+ // delete element4.checkFlag;
2726
+ // delete element4.isdeleted;
2727
+ // delete element4.createdAt;
2728
+ // delete element4.updatedAt;
2729
+ // element4.checkListId = updatedchecklist._id;
2730
+ // element4.checkListName = getCLconfig.checkListName;
2731
+ // element4.checkListDescription = getCLconfig.checkListDescription;
2732
+ // element4.date_iso = new Date( date );
2733
+ // element4.date_string = dayjs( currentdate ).format( 'YYYY-MM-DD' );
2734
+ // element4.allowedOverTime = getCLconfig.allowedOverTime;
2735
+ // element4.allowedStoreLocation = getCLconfig.allowedStoreLocation;
2736
+ // element4.scheduleStartTime = getCLconfig.scheduleStartTime;
2737
+ // element4.scheduleStartTime_iso = startTimeIso.format();
2738
+ // element4.scheduleEndTime = getCLconfig.scheduleEndTime;
2739
+ // element4.scheduleEndTime_iso = endTimeIso.format();
2740
+ // element4.createdBy = new ObjectId( getCLconfig.createdBy );
2741
+ // element4.createdByName = getCLconfig.createdByName;
2742
+ // element4.sourceCheckList_id = getCLconfig._id;
2743
+ // element4.checkListType = getCLconfig.checkListType;
2744
+ // element4.storeCount = getCLconfig.storeCount;
2745
+ // element4.questionCount = getCLconfig.questionCount;
2746
+ // element4.publishDate = getCLconfig.publishDate;
2747
+ // element4.locationCount = getCLconfig.locationCount;
2748
+ // element4.scheduleRepeatedType = getCLconfig.scheduleRepeatedType;
2749
+ // element4.approvalEnable = getCLconfig.approver.length ? true : false;
2750
+ // element4.remainder = getCLconfig?.remainder || [];
2751
+ // element4.restrictAttendance = getCLconfig?.restrictAttendance;
2752
+ // }
2753
+ // if ( userIdList.length ) {
2754
+ // allQuestion = allQuestion.filter( ( item ) => typeof item._id == 'undefined' );
2755
+ // }
2756
+ // if ( allQuestion ) {
2757
+ // if ( getCLconfig?.allowedMultiSubmit || processId ) {
2758
+ // if ( processId ) {
2759
+ // let processedDetails = await processedchecklist.findOne( { _id: processId } );
2760
+ // if ( processedDetails ) {
2761
+ // let submitUser = allQuestion.find( ( item ) => item.userId.toString() == processedDetails.userId.toString() && item.store_id == processedDetails.store_id );
2762
+ // await processedchecklist.insertMany( submitUser );
2763
+ // }
2764
+ // } else {
2765
+ // let deleteInprogressQuery = {
2766
+ // date_string: insertdata.date_string,
2767
+ // date_iso: insertdata.date_iso,
2768
+ // client_id: insertdata.client_id,
2769
+ // checkListId: updatedchecklist._id,
2770
+ // checklistStatus: { $nin: [ 'submit', 'inprogress' ] },
2771
+ // redoStatus: false,
2772
+ // };
2773
+ // await processedchecklist.deleteMany( deleteInprogressQuery );
2774
+ // deleteInprogressQuery.checklistStatus = 'inprogress';
2775
+ // deleteInprogressQuery.redoStatus = false;
2776
+ // let getInprogressData = await processedchecklist.find( deleteInprogressQuery, { userId: 1, store_id: 1 } );
2777
+ // if ( getInprogressData ) {
2778
+ // allQuestion = allQuestion.filter( ( item ) => {
2779
+ // let inprogressData = getInprogressData.find( ( ele ) => ele.userId.toString() == item.userId.toString() && ele.store_id == item.store_id );
2780
+ // if ( !inprogressData ) {
2781
+ // return item;
2782
+ // }
2783
+ // } );
2784
+ // }
2785
+ // if ( allQuestion.length ) {
2786
+ // await processedchecklist.insertMany( allQuestion );
2787
+ // }
2788
+ // }
2789
+ // } else {
2790
+ // let unAssignedList = allQuestion.reduce( ( acc, item ) => {
2791
+ // if ( !acc[item.userEmail] ) {
2792
+ // acc[item.userEmail]=[ item.store_id ];
2793
+ // } else {
2794
+ // acc[item.userEmail].push( item.store_id );
2795
+ // }
2796
+ // return acc;
2797
+ // }, {} );
2798
+
2799
+ // let userList = Object.keys( unAssignedList );
2800
+
2801
+ // await processedchecklist.deleteMany( { userEmail: { $nin: userList }, date_string: insertdata.date_string, date_iso: insertdata.date_iso, client_id: insertdata.client_id, checkListId: updatedchecklist._id, checklistStatus: { $nin: [ 'submit' ] } } );
2802
+
2803
+ // for ( let key in unAssignedList ) {
2804
+ // if ( unAssignedList.hasOwnProperty( key ) ) {
2805
+ // await processedchecklist.deleteMany( { date_string: insertdata.date_string, date_iso: insertdata.date_iso, client_id: insertdata.client_id, checkListId: updatedchecklist._id, $and: [ { userEmail: key }, { store_id: { $nin: unAssignedList[key] } } ], checklistStatus: { $nin: [ 'submit' ] } } );
2806
+ // }
2807
+ // }
2808
+
2809
+
2810
+ // Promise.all( allQuestion.map( async ( ele ) => {
2811
+ // await processedchecklist.updateOne( { date_string: insertdata.date_string, date_iso: insertdata.date_iso, client_id: insertdata.client_id, checkListId: updatedchecklist._id, userEmail: ele.userEmail, store_id: ele.store_id }, ele );
2812
+ // } ) );
2813
+ // }
2814
+
2815
+ // tokenList.forEach( ( item ) => {
2816
+ // const title = `${getCLconfig.checkListName}`;
2817
+ // const description = `Checklist has been updated and re-published!`;
2818
+ // const fcmToken = item;
2819
+ // sendPushNotification( title, description, fcmToken );
2820
+ // } );
2821
+
2822
+ // notifyUserList.forEach( ( item ) => {
2823
+ // const title = `New Checklist Assigned ${item.storeName}`;
2824
+ // const description = `The ${getCLconfig.checkListName} checklist has been assigned to ${item.storeName} for the first time.complete to avoid compliance.`;
2825
+ // const fcmToken = item.token;
2826
+ // sendPushNotification( title, description, fcmToken );
2827
+ // } );
2828
+
2829
+ // let updateUserList = notifyUserList.map( ( ele ) => ele.id );
2830
+ // await assignedService.updateMany( { _id: { $in: updateUserList } }, { sendNotification: true } );
2831
+ // }
2832
+ // }
2833
+ // }
2834
+
2835
+ async function insertPCBulkV4( getCLconfig, checklistId, currentdate, updatedchecklist, date, startTimeIso, endTimeIso, insertdata, processId, oldData, editSubmit, showEdit ) {
2421
2836
  let getquestionQuery = [];
2422
2837
  if ( [ 'storeopenandclose', 'mobileusagedetection', 'uniformdetection' ].includes( getCLconfig.checkListType ) ) {
2423
2838
  getquestionQuery.push(
@@ -2456,12 +2871,158 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2456
2871
  }
2457
2872
  let allQuestion = await assignedService.aggregate( getquestionQuery );
2458
2873
  if ( allQuestion ) {
2874
+ let assignList = [];
2875
+ if ( getCLconfig.coverage == 'store' ) {
2876
+ let clusterList = allQuestion.filter( ( ele ) => ele?.clusterName ).map( ( item ) => item.assignId );
2877
+ if ( clusterList.length ) {
2878
+ let clusterDetails = await clusterServices.findcluster( { _id: { $in: clusterList } } );
2879
+ if ( clusterDetails.length ) {
2880
+ let idList = clusterDetails.flatMap( ( item ) => item.stores.map( ( ele ) => ele.store ) );
2881
+ let getStoreDetails = await storeService.find( { _id: { $in: idList } } );
2882
+ if ( getStoreDetails.length ) {
2883
+ assignList = await Promise.all( getStoreDetails.map( async ( store ) => {
2884
+ let userDetails = await userService.findOne( { email: store?.spocDetails?.[0]?.email, clientId: store.clientId } );
2885
+ if ( !userDetails ) {
2886
+ let data = {
2887
+ clientId: store.clientId,
2888
+ userName: store.spocDetails?.[0]?.name,
2889
+ mobileNumber: store.spocDetails?.[0]?.phone || '',
2890
+ email: store.spocDetails[0].email,
2891
+ password: '5dqFKAJj29PsV6P+kL+3Dw==',
2892
+ role: 'user',
2893
+ userType: 'client',
2894
+ rolespermission: [
2895
+ {
2896
+ featureName: 'Global',
2897
+ modules: [
2898
+ {
2899
+ name: 'Store',
2900
+ isAdd: false,
2901
+ isEdit: false,
2902
+
2903
+ },
2904
+ {
2905
+ name: 'User',
2906
+ isAdd: false,
2907
+ isEdit: false,
2908
+
2909
+ },
2910
+ {
2911
+ name: 'Camera',
2912
+ isAdd: false,
2913
+ isEdit: false,
2914
+
2915
+ },
2916
+ {
2917
+ name: 'Configuration',
2918
+ isAdd: false,
2919
+ isEdit: false,
2920
+
2921
+ },
2922
+ {
2923
+ name: 'Subscription',
2924
+ isAdd: false,
2925
+ isEdit: false,
2926
+
2927
+ },
2928
+ {
2929
+ name: 'Billing',
2930
+ isAdd: false,
2931
+ isEdit: false,
2932
+
2933
+ },
2934
+ ],
2935
+ },
2936
+ {
2937
+ featurName: 'TangoEye',
2938
+ modules: [
2939
+ {
2940
+ name: 'ZoneTag',
2941
+ isAdd: false,
2942
+ isEdit: false,
2943
+
2944
+ },
2945
+ ],
2946
+ },
2947
+ {
2948
+ featurName: 'TangoTrax',
2949
+ modules: [
2950
+ {
2951
+ name: 'checklist',
2952
+ isAdd: false,
2953
+ isEdit: false,
2954
+
2955
+ },
2956
+ {
2957
+ name: 'Task',
2958
+ isAdd: false,
2959
+ isEdit: false,
2960
+
2961
+ },
2962
+ ],
2963
+ },
2964
+ ],
2965
+ };
2966
+ userDetails = await userService.create( data );
2967
+ }
2968
+ let data = {
2969
+ store_id: store?.storeId,
2970
+ storeName: store?.storeName,
2971
+ userId: userDetails._id,
2972
+ userName: userDetails.userName,
2973
+ userEmail: userDetails.email,
2974
+ userPhone: userDetails?.mobileNumber,
2975
+ city: store?.storeProfile?.city,
2976
+ country: store?.storeProfile?.country,
2977
+ checkFlag: true,
2978
+ checkListId: getCLconfig._id,
2979
+ checkListName: getCLconfig.checkListName,
2980
+ client_id: getCLconfig.client_id,
2981
+ };
2982
+ return data;
2983
+ } ) );
2984
+ }
2985
+ }
2986
+ }
2987
+ allQuestion = allQuestion.filter( ( ele ) => !clusterList.includes( ele.assignId ) );
2988
+ }
2989
+ if ( getCLconfig.coverage == 'user' ) {
2990
+ let teamsList = allQuestion.filter( ( ele ) => ele?.teamName ).map( ( item ) => item.assignId );
2991
+ if ( teamsList.length ) {
2992
+ let teamDetails = await teamsServices.findteams( { _id: { $in: teamsList } } );
2993
+ if ( teamDetails.length ) {
2994
+ let idList = teamDetails.flatMap( ( item ) => item.users.map( ( ele ) => ele.userId ) );
2995
+ let getUserDetails = await userService.find( { _id: { $in: idList } } );
2996
+ if ( getUserDetails.length ) {
2997
+ assignList = getUserDetails.map( ( user ) => {
2998
+ let data = {
2999
+ store_id: '',
3000
+ storeName: '',
3001
+ userId: user._id,
3002
+ userName: user.userName,
3003
+ userEmail: user.email,
3004
+ userPhone: user?.mobileNumber,
3005
+ city: '',
3006
+ country: '',
3007
+ checkFlag: true,
3008
+ checkListId: getCLconfig._id,
3009
+ checkListName: getCLconfig.checkListName,
3010
+ client_id: getCLconfig.client_id,
3011
+ };
3012
+ return data;
3013
+ } );
3014
+ }
3015
+ }
3016
+ }
3017
+ allQuestion = allQuestion.filter( ( ele ) => !teamsList.includes( ele.assignId ) );
3018
+ }
3019
+ allQuestion = [ ...allQuestion, ...assignList ];
2459
3020
  let userIdList = [];
2460
3021
  let tokenList = [];
2461
3022
  let notifyUserList = [];
2462
3023
  let status = [ { checklistStatus: { $ne: 'open' } } ];
2463
-
2464
- for ( let element4 of allQuestion ) {
3024
+ let assignUserList = [];
3025
+ await Promise.all( allQuestion.map( async ( element4 ) => {
2465
3026
  let getToken = await userService.findOne( { _id: element4.userId }, { fcmToken: 1 } );
2466
3027
  if ( getToken && getToken?.fcmToken && element4?.sendNotification && showEdit ) {
2467
3028
  tokenList.push( getToken.fcmToken );
@@ -2488,7 +3049,7 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2488
3049
  ...status,
2489
3050
  ],
2490
3051
  userId: element4.userId,
2491
- store_id: element4.store_id,
3052
+ ...( getCLconfig.coverage == 'store' ) ? { store_id: element4.store_id }:{},
2492
3053
  $and: [ {
2493
3054
  date_iso: {
2494
3055
  $gte: new Date( startDate.format( 'YYYY-MM-DD' ) ),
@@ -2504,7 +3065,7 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2504
3065
  ...status,
2505
3066
  ],
2506
3067
  userId: element4.userId,
2507
- store_id: element4.store_id,
3068
+ ...( getCLconfig.coverage == 'store' ) ? { store_id: element4.store_id }:{},
2508
3069
  $and: [
2509
3070
  {
2510
3071
  date_iso: {
@@ -2525,7 +3086,7 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2525
3086
  { submitTime: { $exists: true } },
2526
3087
  ],
2527
3088
  userId: element4.userId,
2528
- store_id: element4.store_id,
3089
+ ...( getCLconfig.coverage == 'store' ) ? { store_id: element4.store_id }:{},
2529
3090
  };
2530
3091
  }
2531
3092
  let getsubmitDetails = await processedchecklist.find( query );
@@ -2617,7 +3178,6 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2617
3178
  }
2618
3179
  if ( getsubmitDetails[0]?.checklistStatus == 'submit' ) {
2619
3180
  userIdList.push( element4._id );
2620
- continue;
2621
3181
  }
2622
3182
  }
2623
3183
  }
@@ -2649,16 +3209,18 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2649
3209
  element4.approvalEnable = getCLconfig.approver.length ? true : false;
2650
3210
  element4.remainder = getCLconfig?.remainder || [];
2651
3211
  element4.restrictAttendance = getCLconfig?.restrictAttendance;
2652
- }
3212
+ assignUserList.push( { ...element4 } );
3213
+ } ) );
3214
+
2653
3215
  if ( userIdList.length ) {
2654
- allQuestion = allQuestion.filter( ( item ) => typeof item._id == 'undefined' );
3216
+ assignUserList = assignUserList.filter( ( item ) => typeof item._id == 'undefined' );
2655
3217
  }
2656
- if ( allQuestion ) {
3218
+ if ( assignUserList ) {
2657
3219
  if ( getCLconfig?.allowedMultiSubmit || processId ) {
2658
3220
  if ( processId ) {
2659
3221
  let processedDetails = await processedchecklist.findOne( { _id: processId } );
2660
3222
  if ( processedDetails ) {
2661
- let submitUser = allQuestion.find( ( item ) => item.userId.toString() == processedDetails.userId.toString() && item.store_id == processedDetails.store_id );
3223
+ let submitUser = assignUserList.find( ( item ) => getCLconfig.coverage == 'store' ? item.userId.toString() == processedDetails.userId.toString() && item.store_id == processedDetails.store_id : item.userId.toString() == processedDetails.userId.toString() );
2662
3224
  await processedchecklist.insertMany( submitUser );
2663
3225
  }
2664
3226
  } else {
@@ -2675,19 +3237,19 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2675
3237
  deleteInprogressQuery.redoStatus = false;
2676
3238
  let getInprogressData = await processedchecklist.find( deleteInprogressQuery, { userId: 1, store_id: 1 } );
2677
3239
  if ( getInprogressData ) {
2678
- allQuestion = allQuestion.filter( ( item ) => {
2679
- let inprogressData = getInprogressData.find( ( ele ) => ele.userId.toString() == item.userId.toString() && ele.store_id == item.store_id );
3240
+ assignUserList = assignUserList.filter( ( item ) => {
3241
+ let inprogressData = getInprogressData.find( ( ele ) => getCLconfig.coverage == 'store' ? ele.userId.toString() == item.userId.toString() && ele.store_id == item.store_id : ele.userId.toString() == item.userId.toString() );
2680
3242
  if ( !inprogressData ) {
2681
3243
  return item;
2682
3244
  }
2683
3245
  } );
2684
3246
  }
2685
- if ( allQuestion.length ) {
2686
- await processedchecklist.insertMany( allQuestion );
3247
+ if ( assignUserList.length ) {
3248
+ await processedchecklist.insertMany( assignUserList );
2687
3249
  }
2688
3250
  }
2689
3251
  } else {
2690
- let unAssignedList = allQuestion.reduce( ( acc, item ) => {
3252
+ let unAssignedList = assignUserList.reduce( ( acc, item ) => {
2691
3253
  if ( !acc[item.userEmail] ) {
2692
3254
  acc[item.userEmail]=[ item.store_id ];
2693
3255
  } else {
@@ -2707,7 +3269,7 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2707
3269
  }
2708
3270
 
2709
3271
 
2710
- Promise.all( allQuestion.map( async ( ele ) => {
3272
+ Promise.all( assignUserList.map( async ( ele ) => {
2711
3273
  await processedchecklist.updateOne( { date_string: insertdata.date_string, date_iso: insertdata.date_iso, client_id: insertdata.client_id, checkListId: updatedchecklist._id, userEmail: ele.userEmail, store_id: ele.store_id }, ele );
2712
3274
  } ) );
2713
3275
  }
@@ -3029,115 +3591,299 @@ async function assignUsers( data ) {
3029
3591
  let assignedData;
3030
3592
  if ( data.coverage == 'store' ) {
3031
3593
  if ( data?.type == 'cluster' ) {
3032
- let query = !data?.upload ? { _id: data.id } : { clusterName: data.clusterName, clientId: data.clientId };
3033
- let clusterDetails = await clusterServices.findOneCluster( query, { stores: 1, clusterName: 1 } );
3034
- if ( clusterDetails ) {
3594
+ let query = [
3595
+ {
3596
+ $addFields: {
3597
+ cluster: { $toLower: '$clusterName' },
3598
+ },
3599
+ },
3600
+ {
3601
+ $match: {
3602
+ clientId: data.clientId,
3603
+ ...( !data?.upload ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { cluster: data.clusterName.toLowerCase() },
3604
+ },
3605
+ },
3606
+ {
3607
+ $project: {
3608
+ stores: 1,
3609
+ clusterName: 1,
3610
+ },
3611
+ },
3612
+ ];
3613
+ let clusterDetails = await clusterServices.aggregate( query );
3614
+ if ( clusterDetails.length ) {
3035
3615
  assignedData = {
3036
3616
  checkFlag: true,
3037
3617
  checkListId: data.checklistId,
3038
3618
  checkListName: data.checkListName,
3039
3619
  client_id: data?.clientId,
3040
- clusterName: clusterDetails?.clusterName,
3041
- assignId: data?.id || clusterDetails?._id,
3620
+ clusterName: clusterDetails?.[0]?.clusterName,
3621
+ assignId: data?.id || clusterDetails?.[0]?._id,
3042
3622
  coverage: 'store',
3043
3623
  };
3044
3624
  }
3045
3625
  } else {
3046
- let query = !data?.upload ? { _id: data.id } : { storeName: data.storeName };
3047
- let storeDetails = await storeService.findOne( query );
3048
- if ( storeDetails ) {
3049
- let email = data?.upload ? data.userEmail : storeDetails?.spocDetails?.email;
3626
+ let query = [
3627
+ {
3628
+ $addFields: {
3629
+ store: { $toLower: '$storeName' },
3630
+ },
3631
+ },
3632
+ {
3633
+ $match: {
3634
+ clientId: data.clientId,
3635
+ ...( !data.upload ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { store: data.storeName.toLowerCase() },
3636
+ },
3637
+ },
3638
+ ];
3639
+ let storeDetails = await storeService.aggregate( query );
3640
+ if ( storeDetails.length ) {
3641
+ let email = data?.upload ? data.userEmail : storeDetails?.[0]?.spocDetails?.email;
3050
3642
  let userDetails = await userService.findOne( { email: email, clientId: data.clientId } );
3051
3643
  if ( !userDetails ) {
3052
3644
  let userData = {
3053
- userName: storeDetails.spocDetails?.[0]?.name,
3054
- email: storeDetails.spocDetails[0].email,
3055
- mobileNumber: storeDetails.spocDetails?.[0]?.phone,
3645
+ userName: storeDetails?.[0]?.spocDetails?.[0]?.name,
3646
+ email: storeDetails?.[0]?.spocDetails[0].email,
3647
+ mobileNumber: storeDetails?.[0]?.spocDetails?.[0]?.phone,
3056
3648
  clientId: data.clientId,
3057
3649
  };
3058
3650
  userDetails = createUser( userData );
3059
3651
  }
3060
3652
  assignedData = {
3061
- store_id: storeDetails.storeId,
3062
- storeName: storeDetails.storeName,
3653
+ store_id: storeDetails?.[0]?.storeId,
3654
+ storeName: storeDetails?.[0]?.storeName,
3063
3655
  userId: userDetails._id,
3064
3656
  userName: userDetails.userName,
3065
3657
  userEmail: userDetails.email,
3066
3658
  userPhone: userDetails?.mobileNumber,
3067
- city: storeDetails?.storeProfile?.city,
3068
- country: storeDetails?.storeProfile?.country,
3659
+ city: storeDetails?.[0]?.storeProfile?.city,
3660
+ country: storeDetails?.[0]?.storeProfile?.country,
3069
3661
  checkFlag: true,
3070
3662
  checkListId: data.checklistId,
3071
3663
  checkListName: data.checkListName,
3072
3664
  client_id: data.clientId,
3073
- assignId: data?.id || storeDetails._id,
3665
+ assignId: data?.id || storeDetails?.[0]?._id,
3074
3666
  coverage: 'store',
3075
3667
  };
3076
3668
  }
3077
3669
  }
3078
3670
  } else {
3079
3671
  if ( data.type == 'teams' ) {
3080
- let query = !data?.upload ? { _id: data.id } : { teamName: data.teamName, clientId: data.clientId };
3081
- let teamDetails = await teamsServices.findOneTeams( query, { users: 1, teamName: 1 } );
3082
- if ( teamDetails ) {
3672
+ let query = [
3673
+ {
3674
+ $addFields: {
3675
+ team: { $toLower: '$teamName' },
3676
+ },
3677
+ },
3678
+ {
3679
+ $match: {
3680
+ clientId: data.clientId,
3681
+ ...( !data?.upload ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { team: data.teamName.toLowerCase() },
3682
+ },
3683
+ },
3684
+ ];
3685
+ let teamDetails = await teamsServices.aggregate( query );
3686
+ if ( teamDetails.length ) {
3083
3687
  assignedData = {
3084
3688
  checkFlag: true,
3085
3689
  checkListId: data.checklistId,
3086
3690
  checkListName: data.checkListName,
3087
3691
  client_id: data.clientId,
3088
- teamName: teamDetails?.teamName,
3089
- assignId: data?.id || teamDetails?._id,
3692
+ teamName: teamDetails?.[0]?.teamName,
3693
+ assignId: data?.id || teamDetails?.[0]?._id,
3090
3694
  coverage: 'user',
3091
3695
  };
3092
3696
  }
3093
3697
  } else {
3094
- let query = !data?.upload ? { _id: data.id } : { email: data.email, clientId: data.clientId };
3095
- let userDetails = await userService.findOne( query );
3096
- if ( userDetails ) {
3698
+ let query = [
3699
+ {
3700
+ $addFields: {
3701
+ userEmail: { $toLower: '$email' },
3702
+ },
3703
+ },
3704
+ {
3705
+ $match: {
3706
+ clientId: data.clientId,
3707
+ ...( !data?.upload ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { userEmail: data.email.toLowerCase() },
3708
+ },
3709
+ },
3710
+ ];
3711
+ let userDetails = await userService.aggregate( query );
3712
+ if ( userDetails.length ) {
3097
3713
  assignedData = {
3098
- userId: userDetails._id,
3099
- userName: userDetails.userName,
3100
- userEmail: userDetails.email,
3101
- userPhone: userDetails?.mobileNumber,
3714
+ userId: userDetails?.[0]?._id,
3715
+ userName: userDetails?.[0]?.userName,
3716
+ userEmail: userDetails?.[0]?.email,
3717
+ userPhone: userDetails?.[0]?.mobileNumber,
3102
3718
  checkFlag: true,
3103
3719
  checkListId: data.checklistId,
3104
3720
  checkListName: data.checkListName,
3105
3721
  client_id: data.clientId,
3106
- assignId: data?.id || userDetails?._id,
3722
+ assignId: data?.id || userDetails?.[0]?._id,
3107
3723
  coverage: 'user',
3108
3724
  };
3109
3725
  }
3110
3726
  }
3111
3727
  }
3112
- if ( assignedData ) {
3728
+ if ( assignedData && !data.upload ) {
3113
3729
  await assignedService.updateOne( { checkListId: assignedData.checkListId, assignId: assignedData.assignId }, assignedData );
3730
+ } else {
3731
+ return assignedData;
3114
3732
  }
3115
3733
  }
3116
3734
 
3117
3735
  export async function checklistAssign( req, res ) {
3118
3736
  try {
3119
- if ( !req.body.coverage ) {
3120
- return res.sendError( 'coverage is required', 400 );
3121
- }
3122
- if ( !req.body.idList ) {
3123
- return res.sendError( 'Id list is required', 400 );
3124
- }
3125
3737
  if ( !req.body.checklistId ) {
3126
3738
  return res.sendError( 'Checklist id is required', 400 );
3127
3739
  }
3128
- if ( !req.body.clientId ) {
3129
- return res.sendError( 'Client id is required', 400 );
3740
+ if ( !req.body.coverage ) {
3741
+ return res.sendError( 'Coverage is required', 400 );
3742
+ }
3743
+ if ( !req.body.idList ) {
3744
+ return res.sendError( 'Id is required', 400 );
3130
3745
  }
3746
+
3131
3747
  let checklistDetails = await checklistService.findOne( { _id: req.body.checklistId } );
3132
3748
  if ( !checklistDetails ) {
3133
3749
  return res.sendError( 'No data found', 204 );
3134
3750
  }
3135
- req.body.checkListName = checklistDetails.checkListName;
3136
- await Promise.all( req.body.idList.map( async ( ele ) => {
3137
- let storeData = { ...req.body, type: ele.type, id: ele.id };
3138
- await assignUsers( storeData );
3139
- } ) );
3140
- return res.sendSuccess( 'Details updated successfully' );
3751
+ let idList;
3752
+ let assignList;
3753
+ if ( req.body.coverage == 'store' ) {
3754
+ let clusterDetails = await clusterServices.findcluster( { _id: { $in: req.body.idList } }, { stores: 1, clusterName: 1 } );
3755
+ if ( clusterDetails.length ) {
3756
+ idList = clusterDetails.flatMap( ( item ) => item.stores.map( ( ele ) => ele.store ) );
3757
+ let storeList = clusterDetails.filter( ( ele ) => !req.body.idList.includes( ele._id.toString() ) ).map( ( ele ) => ele._id );
3758
+ idList = [ ...idList, ...storeList ];
3759
+ } else {
3760
+ idList = req.body.idList;
3761
+ }
3762
+ let getStoreDetails = await storeService.find( { _id: { $in: idList } } );
3763
+ if ( !getStoreDetails.length ) {
3764
+ return res.sendError( 'No data found', 204 );
3765
+ }
3766
+ assignList = await Promise.all( getStoreDetails.map( async ( store ) => {
3767
+ let userDetails = await userService.findOne( { email: store?.spocDetails?.email, clientId: store.clientId } );
3768
+ if ( !userDetails ) {
3769
+ let data = {
3770
+ clientId: store.clientId,
3771
+ userName: store.spocDetails?.[0]?.name,
3772
+ mobileNumber: store.spocDetails?.[0]?.phone || '',
3773
+ email: store.spocDetails[0].email,
3774
+ password: '5dqFKAJj29PsV6P+kL+3Dw==',
3775
+ role: 'user',
3776
+ userType: 'client',
3777
+ rolespermission: [
3778
+ {
3779
+ featureName: 'Global',
3780
+ modules: [
3781
+ {
3782
+ name: 'Store',
3783
+ isAdd: false,
3784
+ isEdit: false,
3785
+
3786
+ },
3787
+ {
3788
+ name: 'User',
3789
+ isAdd: false,
3790
+ isEdit: false,
3791
+
3792
+ },
3793
+ {
3794
+ name: 'Camera',
3795
+ isAdd: false,
3796
+ isEdit: false,
3797
+
3798
+ },
3799
+ {
3800
+ name: 'Configuration',
3801
+ isAdd: false,
3802
+ isEdit: false,
3803
+
3804
+ },
3805
+ {
3806
+ name: 'Subscription',
3807
+ isAdd: false,
3808
+ isEdit: false,
3809
+
3810
+ },
3811
+ {
3812
+ name: 'Billing',
3813
+ isAdd: false,
3814
+ isEdit: false,
3815
+
3816
+ },
3817
+ ],
3818
+ },
3819
+ {
3820
+ featurName: 'TangoEye',
3821
+ modules: [
3822
+ {
3823
+ name: 'ZoneTag',
3824
+ isAdd: false,
3825
+ isEdit: false,
3826
+
3827
+ },
3828
+ ],
3829
+ },
3830
+ {
3831
+ featurName: 'TangoTrax',
3832
+ modules: [
3833
+ {
3834
+ name: 'checklist',
3835
+ isAdd: false,
3836
+ isEdit: false,
3837
+
3838
+ },
3839
+ {
3840
+ name: 'Task',
3841
+ isAdd: false,
3842
+ isEdit: false,
3843
+
3844
+ },
3845
+ ],
3846
+ },
3847
+ ],
3848
+ };
3849
+ userDetails = await userService.create( data );
3850
+ }
3851
+ let data = {
3852
+ store_id: store.storeId,
3853
+ storeName: store.storeName,
3854
+ userId: userDetails._id,
3855
+ userName: userDetails.userName,
3856
+ userEmail: userDetails.email,
3857
+ userPhone: userDetails?.mobileNumber,
3858
+ clusterName: clusterDetails?.clusterName,
3859
+ };
3860
+ return data;
3861
+ } ) );
3862
+ } else {
3863
+ let teamDetails = await teamsServices.findteams( { _id: { $in: req.body.idList } }, { users: 1, teamName: 1 } );
3864
+ if ( teamDetails.length ) {
3865
+ idList = teamDetails.flatMap( ( item ) => item.users.map( ( ele ) => ele.userId ) );
3866
+ let userList = teamDetails.filter( ( ele ) => !req.body.idList.includes( ele._id.toString() ) );
3867
+ idList = [ ...idList, ...userList ];
3868
+ } else {
3869
+ idList = req.body.idList;
3870
+ }
3871
+ let userDetails = await userService.find( { _id: { $in: idList } } );
3872
+ if ( !userDetails.length ) {
3873
+ return res.sendError( 'No data found', 204 );
3874
+ }
3875
+ assignList = [];
3876
+ userDetails.forEach( ( user ) => {
3877
+ assignList.push( {
3878
+ userId: user._id,
3879
+ userName: user.userName,
3880
+ userEmail: user.email,
3881
+ userPhone: user?.mobileNumber,
3882
+ teamName: teamDetails?.teamName,
3883
+ } );
3884
+ } );
3885
+ }
3886
+ return res.sendSuccess( { count: assignList.length, assignList } );
3141
3887
  } catch ( e ) {
3142
3888
  logger.error( { functionName: 'checklistAssign', error: e } );
3143
3889
  return res.sendError( e, 500 );
@@ -3156,7 +3902,7 @@ export async function checklistAssign( req, res ) {
3156
3902
  // if ( !checklistDetails ) {
3157
3903
  // return res.sendError( 'No data found', 204 );
3158
3904
  // }
3159
- // await assignedService.updateMany( { assignId: { $in: req.body.id } }, { isdeleted: true } );
3905
+ // await assignedService.deleteMany( { assignId: { $in: req.body.id } } );
3160
3906
  // return res.sendSuccess( 'Details removed successfully' );
3161
3907
  // } catch ( e ) {
3162
3908
  // logger.error( { functionName: 'removeAssign', error: e } );
@@ -3164,19 +3910,19 @@ export async function checklistAssign( req, res ) {
3164
3910
  // }
3165
3911
  // }
3166
3912
 
3167
- export async function deleteAssignList( req, res ) {
3168
- try {
3169
- if ( !req.body.checkListId ) {
3170
- return res.sendError( 'Checklist id is required', 400 );
3171
- }
3172
- let checklistDetails = await checklistService.findOne( { _id: req.body.checkListId, isdeleted: false } );
3173
- if ( !checklistDetails ) {
3174
- return res.sendError( 'No data found', 204 );
3175
- }
3176
- await assignedService.updateMany( { checkListId: req.body.checkListId }, { isdeleted: true } );
3177
- return res.sendSuccess( 'Assign details removed successfully' );
3178
- } catch ( e ) {
3179
- logger.error( { functionName: 'deleteAssignList', error: e } );
3180
- return res.sendError( e, 500 );
3181
- }
3182
- }
3913
+ // export async function deleteAssignList( req, res ) {
3914
+ // try {
3915
+ // if ( !req.body.checkListId ) {
3916
+ // return res.sendError( 'Checklist id is required', 400 );
3917
+ // }
3918
+ // let checklistDetails = await checklistService.findOne( { _id: req.body.checkListId, isdeleted: false } );
3919
+ // if ( !checklistDetails ) {
3920
+ // return res.sendError( 'No data found', 204 );
3921
+ // }
3922
+ // await assignedService.updateMany( { checkListId: req.body.checkListId }, { isdeleted: true } );
3923
+ // return res.sendSuccess( 'Assign details removed successfully' );
3924
+ // } catch ( e ) {
3925
+ // logger.error( { functionName: 'deleteAssignList', error: e } );
3926
+ // return res.sendError( e, 500 );
3927
+ // }
3928
+ // }