tango-app-api-trax 3.3.1-beta-17 → 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() );
@@ -972,7 +972,7 @@ export const assignedUserDetailsv1 = async ( req, res ) => {
972
972
  // let page = parseInt( req.query.offset ) || 0;
973
973
  // let skip = limit * page;
974
974
 
975
- let query = [ { $match: { checkListId: new ObjectId( req.params.checklistId ), ...( req.body.coverage ) ? { coverage: req.body.coverage } : {} } } ];
975
+ let query = [ { $match: { checkListId: new ObjectId( req.params.checklistId ), isdeleted: false } } ];
976
976
  if ( req.query.search.trim() && req.query.search.trim() != '' ) {
977
977
  let searchValue = req.query.search;
978
978
  searchValue = searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
@@ -1954,7 +1954,7 @@ export const validateUser = async ( req, res ) => {
1954
1954
  }
1955
1955
  };
1956
1956
 
1957
- export const validateUserv1 = async ( req, res ) => {
1957
+ export const validateUserv1 = async ( req, res, next ) => {
1958
1958
  try {
1959
1959
  if ( !req.body.assignedUsers.length ) {
1960
1960
  return res.sendError( 'Please Enter user Details', 400 );
@@ -2050,7 +2050,7 @@ export const validateUserv1 = async ( req, res ) => {
2050
2050
  return res.sendError( { validate: false, user: userDetails, store: storeDetails }, 400 );
2051
2051
  }
2052
2052
 
2053
- return res.sendSuccess( { validate: true } );
2053
+ next();
2054
2054
  } catch ( e ) {
2055
2055
  logger.error( 'validateUser 2=>', e );
2056
2056
  return res.sendError( e, 500 );
@@ -2506,7 +2506,7 @@ export async function insertSingleProcessData( checklistId, processId = 0, oldDa
2506
2506
  updatedchecklist = checklistDetails;
2507
2507
  }
2508
2508
  if ( updatedchecklist ) {
2509
- 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 );
2510
2510
  }
2511
2511
  }
2512
2512
  }
@@ -2517,322 +2517,7 @@ export async function insertSingleProcessData( checklistId, processId = 0, oldDa
2517
2517
  }
2518
2518
  };
2519
2519
 
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 ) {
2520
+ // async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedchecklist, date, startTimeIso, endTimeIso, insertdata, processId, oldData, editSubmit, showEdit ) {
2836
2521
  // let getquestionQuery = [];
2837
2522
  // if ( [ 'storeopenandclose', 'mobileusagedetection', 'uniformdetection' ].includes( getCLconfig.checkListType ) ) {
2838
2523
  // getquestionQuery.push(
@@ -2875,8 +2560,8 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2875
2560
  // let tokenList = [];
2876
2561
  // let notifyUserList = [];
2877
2562
  // let status = [ { checklistStatus: { $ne: 'open' } } ];
2878
- // let assignUserList = [];
2879
- // await Promise.all( allQuestion.map( async ( element4 ) => {
2563
+
2564
+ // for ( let element4 of allQuestion ) {
2880
2565
  // let getToken = await userService.findOne( { _id: element4.userId }, { fcmToken: 1 } );
2881
2566
  // if ( getToken && getToken?.fcmToken && element4?.sendNotification && showEdit ) {
2882
2567
  // tokenList.push( getToken.fcmToken );
@@ -2903,7 +2588,7 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2903
2588
  // ...status,
2904
2589
  // ],
2905
2590
  // userId: element4.userId,
2906
- // ...( getCLconfig.coverage == 'store' ) ? { store_id: element4.store_id }:{},
2591
+ // store_id: element4.store_id,
2907
2592
  // $and: [ {
2908
2593
  // date_iso: {
2909
2594
  // $gte: new Date( startDate.format( 'YYYY-MM-DD' ) ),
@@ -2919,7 +2604,7 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2919
2604
  // ...status,
2920
2605
  // ],
2921
2606
  // userId: element4.userId,
2922
- // ...( getCLconfig.coverage == 'store' ) ? { store_id: element4.store_id }:{},
2607
+ // store_id: element4.store_id,
2923
2608
  // $and: [
2924
2609
  // {
2925
2610
  // date_iso: {
@@ -2940,7 +2625,7 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
2940
2625
  // { submitTime: { $exists: true } },
2941
2626
  // ],
2942
2627
  // userId: element4.userId,
2943
- // ...( getCLconfig.coverage == 'store' ) ? { store_id: element4.store_id }:{},
2628
+ // store_id: element4.store_id,
2944
2629
  // };
2945
2630
  // }
2946
2631
  // let getsubmitDetails = await processedchecklist.find( query );
@@ -3032,6 +2717,7 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
3032
2717
  // }
3033
2718
  // if ( getsubmitDetails[0]?.checklistStatus == 'submit' ) {
3034
2719
  // userIdList.push( element4._id );
2720
+ // continue;
3035
2721
  // }
3036
2722
  // }
3037
2723
  // }
@@ -3063,90 +2749,550 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
3063
2749
  // element4.approvalEnable = getCLconfig.approver.length ? true : false;
3064
2750
  // element4.remainder = getCLconfig?.remainder || [];
3065
2751
  // element4.restrictAttendance = getCLconfig?.restrictAttendance;
3066
- // assignUserList.push( { ...element4 } );
3067
- // } ) );
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 ) {
2836
+ let getquestionQuery = [];
2837
+ if ( [ 'storeopenandclose', 'mobileusagedetection', 'uniformdetection' ].includes( getCLconfig.checkListType ) ) {
2838
+ getquestionQuery.push(
2839
+ {
2840
+ $match: {
2841
+ checkListId: new ObjectId( checklistId ),
2842
+ checkFlag: true,
2843
+ },
2844
+ },
2845
+ {
2846
+ $group: {
2847
+ '_id': '$store_id',
2848
+ 'store_id': { $first: '$store_id' },
2849
+ 'client_id': { $first: '$client_id' },
2850
+ 'storeName': { $first: '$storeName' },
2851
+ 'userId': { $first: '$userId' },
2852
+ 'userEmail': { $first: '$userEmail' },
2853
+ 'userName': { $first: '$userName' },
2854
+ 'checkFlag': { $first: '$checkFlag' },
2855
+ 'checkListId': { $first: '$checkListId' },
2856
+ 'checkListName': { $first: '$checkListName' },
2857
+ 'country': { $first: '$country' },
2858
+ 'createdAt': { $first: '$createdAt' },
2859
+ 'updatedAt': { $first: '$updatedAt' },
2860
+ },
2861
+ },
2862
+ );
2863
+ } else {
2864
+ getquestionQuery.push( {
2865
+ $match: {
2866
+ checkListId: new ObjectId( checklistId ),
2867
+ checkFlag: true,
2868
+ isdeleted: false,
2869
+ },
2870
+ } );
2871
+ }
2872
+ let allQuestion = await assignedService.aggregate( getquestionQuery );
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 ];
3020
+ let userIdList = [];
3021
+ let tokenList = [];
3022
+ let notifyUserList = [];
3023
+ let status = [ { checklistStatus: { $ne: 'open' } } ];
3024
+ let assignUserList = [];
3025
+ await Promise.all( allQuestion.map( async ( element4 ) => {
3026
+ let getToken = await userService.findOne( { _id: element4.userId }, { fcmToken: 1 } );
3027
+ if ( getToken && getToken?.fcmToken && element4?.sendNotification && showEdit ) {
3028
+ tokenList.push( getToken.fcmToken );
3029
+ }
3030
+ if ( !element4?.sendNotification ) {
3031
+ notifyUserList.push( { token: getToken?.fcmToken, storeName: element4.storeName, id: element4._id } );
3032
+ }
3033
+ if ( !getCLconfig?.allowedMultiSubmit ) {
3034
+ let query;
3035
+ if ( getCLconfig.allowOnce && ![ 'onetime', 'daily' ].includes( getCLconfig.schedule ) ) {
3036
+ if ( [ 'weekday', 'weekly', 'monthly' ].includes( getCLconfig.schedule ) ) {
3037
+ let startDate; let endDate;
3038
+ if ( [ 'weekday', 'weekly' ].includes( getCLconfig.schedule ) ) {
3039
+ startDate = dayjs.utc( date ).clone().startOf( 'week' );
3040
+ endDate = dayjs.utc( date ).clone().endOf( 'week' );
3041
+ } else {
3042
+ startDate = dayjs.utc( date ).clone().startOf( 'month' );
3043
+ endDate = dayjs.utc( date ).clone().endOf( 'month' );
3044
+ }
3045
+ query = {
3046
+ sourceCheckList_id: getCLconfig._id,
3047
+ $or: [
3048
+ { submitTime: { $exists: true } },
3049
+ ...status,
3050
+ ],
3051
+ userId: element4.userId,
3052
+ ...( getCLconfig.coverage == 'store' ) ? { store_id: element4.store_id }:{},
3053
+ $and: [ {
3054
+ date_iso: {
3055
+ $gte: new Date( startDate.format( 'YYYY-MM-DD' ) ),
3056
+ $lte: new Date( endDate.format( 'YYYY-MM-DD' ) ) },
3057
+ } ],
3058
+ };
3059
+ } else {
3060
+ if ( getCLconfig.schedule == 'range' ) {
3061
+ query = {
3062
+ sourceCheckList_id: getCLconfig._id,
3063
+ $or: [
3064
+ { submitTime: { $exists: true } },
3065
+ ...status,
3066
+ ],
3067
+ userId: element4.userId,
3068
+ ...( getCLconfig.coverage == 'store' ) ? { store_id: element4.store_id }:{},
3069
+ $and: [
3070
+ {
3071
+ date_iso: {
3072
+ $gte: new Date( dayjs( getCLconfig.configStartDate ).format( 'YYYY-MM-DD' ) ),
3073
+ $lte: new Date( dayjs( getCLconfig.configEndDate ).format( 'YYYY-MM-DD' ) ),
3074
+ },
3075
+ },
3076
+ ],
3077
+ };
3078
+ }
3079
+ }
3080
+ } else {
3081
+ query = {
3082
+ date_string: date,
3083
+ sourceCheckList_id: getCLconfig._id,
3084
+ $or: [
3085
+ ...status,
3086
+ { submitTime: { $exists: true } },
3087
+ ],
3088
+ userId: element4.userId,
3089
+ ...( getCLconfig.coverage == 'store' ) ? { store_id: element4.store_id }:{},
3090
+ };
3091
+ }
3092
+ let getsubmitDetails = await processedchecklist.find( query );
3093
+ function findDifferences( obj1, obj2 ) {
3094
+ return Object.keys( obj1 ).reduce( ( diff, key ) => {
3095
+ if ( !isEqual( obj1[key], obj2[key] ) ) {
3096
+ diff[key] = { value1: obj1[key], value2: obj2[key] };
3097
+ }
3098
+ return diff;
3099
+ }, {} );
3100
+ }
3101
+ if ( getsubmitDetails.length ) {
3102
+ if ( showEdit && ( ( editSubmit && getsubmitDetails[0].checklistStatus == 'submit' ) || getsubmitDetails[0].checklistStatus == 'inprogress' ) ) {
3103
+ let modifiedCount = 0;
3104
+ let questionList = insertdata.questionAnswers;
3105
+ if ( getsubmitDetails[0].checkListName != getCLconfig.checkListName ) {
3106
+ getsubmitDetails[0].checkListName = getCLconfig.checkListName;
3107
+ }
3108
+ if ( getsubmitDetails[0].checkListDescription != getCLconfig.checkListDescription ) {
3109
+ getsubmitDetails[0].checkListDescription = getCLconfig.checkListDescription;
3110
+ }
3111
+ let sectionList = [];
3112
+ for ( let [ index, section ] of questionList.entries() ) {
3113
+ let checkExists = getsubmitDetails[0].questionAnswers.findIndex( ( sec ) => sec.sectionName == section?.sectionOldName || sec.sectionName == section.sectionName );
3114
+ if ( checkExists != -1 ) {
3115
+ getsubmitDetails[0].questionAnswers[index].section_id = section.section_id;
3116
+ getsubmitDetails[0].questionAnswers[index].sectionName = section.sectionName;
3117
+ let question = [];
3118
+ section.questions.forEach( ( qns ) => {
3119
+ let findQuestion = getsubmitDetails[0].questionAnswers[index].questions.findIndex( ( ele ) => ele.qname.trim() == qns?.oldQname?.trim() || ele.qname.trim() == qns.qname.trim() );
3120
+ if ( findQuestion != -1 ) {
3121
+ let data = JSON.parse( JSON.stringify( getsubmitDetails[0].questionAnswers[index].questions[findQuestion] ) );
3122
+ getsubmitDetails[0].questionAnswers[index].questions[findQuestion].qno = qns.qno;
3123
+ delete data.userAnswer;
3124
+ delete data.parentanswer;
3125
+ delete data.remarks;
3126
+ delete data.linkquestionenabled;
3127
+ if ( data.descriptivetype == null ) {
3128
+ delete data.descriptivetype;
3129
+ }
3130
+ qns.answers.forEach( ( ans ) => {
3131
+ delete ans.validationAnswer;
3132
+ delete ans.answeroptionNumber;
3133
+ } );
3134
+ data.answers.forEach( ( ans ) => {
3135
+ delete ans.index;
3136
+ delete ans.validationAnswer;
3137
+ delete ans.answeroptionNumber;
3138
+ } );
3139
+ const compare = findDifferences( data, qns );
3140
+ if ( compare?.answerType || compare?.answers || compare?.linkType ) {
3141
+ logger.info( 'compare =>', compare );
3142
+ modifiedCount++;
3143
+ question.push( qns );
3144
+ } else {
3145
+ getsubmitDetails[0].questionAnswers[index].questions[findQuestion].qname = qns.qname;
3146
+ question.push( getsubmitDetails[0].questionAnswers[index].questions[findQuestion] );
3147
+ }
3148
+ } else {
3149
+ modifiedCount++;
3150
+ question.push( qns );
3151
+ }
3152
+ } );
3153
+ getsubmitDetails[0].questionAnswers[index].questions = question;
3154
+ sectionList.push( getsubmitDetails[0].questionAnswers[index] );
3155
+ } else {
3156
+ modifiedCount++;
3157
+ sectionList.push( section );
3158
+ }
3159
+ }
3160
+ getsubmitDetails[0].questionAnswers = sectionList;
3161
+ if ( modifiedCount ) {
3162
+ getsubmitDetails[0].checklistStatus = 'inprogress';
3163
+ getsubmitDetails[0].date_string = dayjs( currentdate ).format( 'YYYY-MM-DD' );
3164
+ getsubmitDetails[0].date_iso = new Date( date );
3165
+ getsubmitDetails[0].redoStatus = false;
3166
+ getsubmitDetails[0].approvalStatus = false;
3167
+ }
3168
+ let data = { ...getsubmitDetails[0]._doc };
3169
+ await processedchecklist.updateOne( { _id: getsubmitDetails[0]._id }, data );
3170
+ if ( editSubmit && getsubmitDetails[0].checklistStatus == 'submit' ) {
3171
+ console.log( editSubmit );
3172
+ let user = {
3173
+ _id: getsubmitDetails[0].userId,
3174
+ clientId: getsubmitDetails[0].client_id,
3175
+ };
3176
+ updateOpenSearch( user, { processedcheckListId: getsubmitDetails[0]._id, date: getsubmitDetails[0].date_string } );
3177
+ }
3178
+ }
3179
+ if ( getsubmitDetails[0]?.checklistStatus == 'submit' ) {
3180
+ userIdList.push( element4._id );
3181
+ }
3182
+ }
3183
+ }
3184
+ delete element4._id;
3185
+ delete element4.checkFlag;
3186
+ delete element4.isdeleted;
3187
+ delete element4.createdAt;
3188
+ delete element4.updatedAt;
3189
+ element4.checkListId = updatedchecklist._id;
3190
+ element4.checkListName = getCLconfig.checkListName;
3191
+ element4.checkListDescription = getCLconfig.checkListDescription;
3192
+ element4.date_iso = new Date( date );
3193
+ element4.date_string = dayjs( currentdate ).format( 'YYYY-MM-DD' );
3194
+ element4.allowedOverTime = getCLconfig.allowedOverTime;
3195
+ element4.allowedStoreLocation = getCLconfig.allowedStoreLocation;
3196
+ element4.scheduleStartTime = getCLconfig.scheduleStartTime;
3197
+ element4.scheduleStartTime_iso = startTimeIso.format();
3198
+ element4.scheduleEndTime = getCLconfig.scheduleEndTime;
3199
+ element4.scheduleEndTime_iso = endTimeIso.format();
3200
+ element4.createdBy = new ObjectId( getCLconfig.createdBy );
3201
+ element4.createdByName = getCLconfig.createdByName;
3202
+ element4.sourceCheckList_id = getCLconfig._id;
3203
+ element4.checkListType = getCLconfig.checkListType;
3204
+ element4.storeCount = getCLconfig.storeCount;
3205
+ element4.questionCount = getCLconfig.questionCount;
3206
+ element4.publishDate = getCLconfig.publishDate;
3207
+ element4.locationCount = getCLconfig.locationCount;
3208
+ element4.scheduleRepeatedType = getCLconfig.scheduleRepeatedType;
3209
+ element4.approvalEnable = getCLconfig.approver.length ? true : false;
3210
+ element4.remainder = getCLconfig?.remainder || [];
3211
+ element4.restrictAttendance = getCLconfig?.restrictAttendance;
3212
+ assignUserList.push( { ...element4 } );
3213
+ } ) );
3068
3214
 
3069
- // if ( userIdList.length ) {
3070
- // assignUserList = assignUserList.filter( ( item ) => typeof item._id == 'undefined' );
3071
- // }
3072
- // if ( assignUserList ) {
3073
- // if ( getCLconfig?.allowedMultiSubmit || processId ) {
3074
- // if ( processId ) {
3075
- // let processedDetails = await processedchecklist.findOne( { _id: processId } );
3076
- // if ( processedDetails ) {
3077
- // 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() );
3078
- // await processedchecklist.insertMany( submitUser );
3079
- // }
3080
- // } else {
3081
- // let deleteInprogressQuery = {
3082
- // date_string: insertdata.date_string,
3083
- // date_iso: insertdata.date_iso,
3084
- // client_id: insertdata.client_id,
3085
- // checkListId: updatedchecklist._id,
3086
- // checklistStatus: { $nin: [ 'submit', 'inprogress' ] },
3087
- // redoStatus: false,
3088
- // };
3089
- // await processedchecklist.deleteMany( deleteInprogressQuery );
3090
- // deleteInprogressQuery.checklistStatus = 'inprogress';
3091
- // deleteInprogressQuery.redoStatus = false;
3092
- // let getInprogressData = await processedchecklist.find( deleteInprogressQuery, { userId: 1, store_id: 1 } );
3093
- // if ( getInprogressData ) {
3094
- // assignUserList = assignUserList.filter( ( item ) => {
3095
- // 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() );
3096
- // if ( !inprogressData ) {
3097
- // return item;
3098
- // }
3099
- // } );
3100
- // }
3101
- // if ( assignUserList.length ) {
3102
- // await processedchecklist.insertMany( assignUserList );
3103
- // }
3104
- // }
3105
- // } else {
3106
- // let unAssignedList = assignUserList.reduce( ( acc, item ) => {
3107
- // if ( !acc[item.userEmail] ) {
3108
- // acc[item.userEmail]=[ item.store_id ];
3109
- // } else {
3110
- // acc[item.userEmail].push( item.store_id );
3111
- // }
3112
- // return acc;
3113
- // }, {} );
3215
+ if ( userIdList.length ) {
3216
+ assignUserList = assignUserList.filter( ( item ) => typeof item._id == 'undefined' );
3217
+ }
3218
+ if ( assignUserList ) {
3219
+ if ( getCLconfig?.allowedMultiSubmit || processId ) {
3220
+ if ( processId ) {
3221
+ let processedDetails = await processedchecklist.findOne( { _id: processId } );
3222
+ if ( processedDetails ) {
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() );
3224
+ await processedchecklist.insertMany( submitUser );
3225
+ }
3226
+ } else {
3227
+ let deleteInprogressQuery = {
3228
+ date_string: insertdata.date_string,
3229
+ date_iso: insertdata.date_iso,
3230
+ client_id: insertdata.client_id,
3231
+ checkListId: updatedchecklist._id,
3232
+ checklistStatus: { $nin: [ 'submit', 'inprogress' ] },
3233
+ redoStatus: false,
3234
+ };
3235
+ await processedchecklist.deleteMany( deleteInprogressQuery );
3236
+ deleteInprogressQuery.checklistStatus = 'inprogress';
3237
+ deleteInprogressQuery.redoStatus = false;
3238
+ let getInprogressData = await processedchecklist.find( deleteInprogressQuery, { userId: 1, store_id: 1 } );
3239
+ if ( getInprogressData ) {
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() );
3242
+ if ( !inprogressData ) {
3243
+ return item;
3244
+ }
3245
+ } );
3246
+ }
3247
+ if ( assignUserList.length ) {
3248
+ await processedchecklist.insertMany( assignUserList );
3249
+ }
3250
+ }
3251
+ } else {
3252
+ let unAssignedList = assignUserList.reduce( ( acc, item ) => {
3253
+ if ( !acc[item.userEmail] ) {
3254
+ acc[item.userEmail]=[ item.store_id ];
3255
+ } else {
3256
+ acc[item.userEmail].push( item.store_id );
3257
+ }
3258
+ return acc;
3259
+ }, {} );
3114
3260
 
3115
- // let userList = Object.keys( unAssignedList );
3261
+ let userList = Object.keys( unAssignedList );
3116
3262
 
3117
- // 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' ] } } );
3263
+ 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' ] } } );
3118
3264
 
3119
- // for ( let key in unAssignedList ) {
3120
- // if ( unAssignedList.hasOwnProperty( key ) ) {
3121
- // 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' ] } } );
3122
- // }
3123
- // }
3265
+ for ( let key in unAssignedList ) {
3266
+ if ( unAssignedList.hasOwnProperty( key ) ) {
3267
+ 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' ] } } );
3268
+ }
3269
+ }
3124
3270
 
3125
3271
 
3126
- // Promise.all( assignUserList.map( async ( ele ) => {
3127
- // 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 );
3128
- // } ) );
3129
- // }
3272
+ Promise.all( assignUserList.map( async ( ele ) => {
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 );
3274
+ } ) );
3275
+ }
3130
3276
 
3131
- // tokenList.forEach( ( item ) => {
3132
- // const title = `${getCLconfig.checkListName}`;
3133
- // const description = `Checklist has been updated and re-published!`;
3134
- // const fcmToken = item;
3135
- // sendPushNotification( title, description, fcmToken );
3136
- // } );
3277
+ tokenList.forEach( ( item ) => {
3278
+ const title = `${getCLconfig.checkListName}`;
3279
+ const description = `Checklist has been updated and re-published!`;
3280
+ const fcmToken = item;
3281
+ sendPushNotification( title, description, fcmToken );
3282
+ } );
3137
3283
 
3138
- // notifyUserList.forEach( ( item ) => {
3139
- // const title = `New Checklist Assigned ${item.storeName}`;
3140
- // const description = `The ${getCLconfig.checkListName} checklist has been assigned to ${item.storeName} for the first time.complete to avoid compliance.`;
3141
- // const fcmToken = item.token;
3142
- // sendPushNotification( title, description, fcmToken );
3143
- // } );
3284
+ notifyUserList.forEach( ( item ) => {
3285
+ const title = `New Checklist Assigned ${item.storeName}`;
3286
+ const description = `The ${getCLconfig.checkListName} checklist has been assigned to ${item.storeName} for the first time.complete to avoid compliance.`;
3287
+ const fcmToken = item.token;
3288
+ sendPushNotification( title, description, fcmToken );
3289
+ } );
3144
3290
 
3145
- // let updateUserList = notifyUserList.map( ( ele ) => ele.id );
3146
- // await assignedService.updateMany( { _id: { $in: updateUserList } }, { sendNotification: true } );
3147
- // }
3148
- // }
3149
- // }
3291
+ let updateUserList = notifyUserList.map( ( ele ) => ele.id );
3292
+ await assignedService.updateMany( { _id: { $in: updateUserList } }, { sendNotification: true } );
3293
+ }
3294
+ }
3295
+ }
3150
3296
 
3151
3297
  async function updateOpenSearch( user, data ) {
3152
3298
  let findQuery = [ {
@@ -3579,8 +3725,10 @@ async function assignUsers( data ) {
3579
3725
  }
3580
3726
  }
3581
3727
  }
3582
- if ( assignedData ) {
3728
+ if ( assignedData && !data.upload ) {
3583
3729
  await assignedService.updateOne( { checkListId: assignedData.checkListId, assignId: assignedData.assignId }, assignedData );
3730
+ } else {
3731
+ return assignedData;
3584
3732
  }
3585
3733
  }
3586
3734
 
@@ -3616,7 +3764,7 @@ export async function checklistAssign( req, res ) {
3616
3764
  return res.sendError( 'No data found', 204 );
3617
3765
  }
3618
3766
  assignList = await Promise.all( getStoreDetails.map( async ( store ) => {
3619
- let userDetails = await userService.findOne( { userEmail: store?.spocDetails?.email, clientId: store.clientId } );
3767
+ let userDetails = await userService.findOne( { email: store?.spocDetails?.email, clientId: store.clientId } );
3620
3768
  if ( !userDetails ) {
3621
3769
  let data = {
3622
3770
  clientId: store.clientId,