tango-app-api-trax 3.4.1-alpha-20 → 3.4.1-approvecheck-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2463,6 +2463,7 @@ export const flagChecklistTableV1 = async ( req, res ) => {
2463
2463
  'Store Id': element?.storeId,
2464
2464
  'Store Spoc Email': element?.storeSpocEmail,
2465
2465
  'Detections': element?.detections,
2466
+ 'Detection Status': element?.detectionStatus,
2466
2467
  } );
2467
2468
  } );
2468
2469
  return await download( exportdata, res );
@@ -2487,7 +2488,7 @@ export const flagChecklistTableV1 = async ( req, res ) => {
2487
2488
  if ( reqestData.export ) {
2488
2489
  reqestData.limit = 10000;
2489
2490
  reqestData.offset = 0;
2490
- let LamdaURL = 'https://vpcejaftccr3jzqf5wrdkks7yy0krqix.lambda-url.ap-south-1.on.aws/';
2491
+ let LamdaURL = 'https://bnauly42yuztzrgtttodzt3bcm0oqrsi.lambda-url.ap-south-1.on.aws/';
2491
2492
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
2492
2493
  if ( resultData ) {
2493
2494
  if ( resultData.status_code == '200' ) {
@@ -2503,6 +2504,7 @@ export const flagChecklistTableV1 = async ( req, res ) => {
2503
2504
  'Store Id': element?.storeId,
2504
2505
  // 'Store Spoc Email': element?.SpocEmail,
2505
2506
  'Detections': element?.detections,
2507
+ 'Detection Status': element?.detectionStatus,
2506
2508
  } );
2507
2509
  } );
2508
2510
  return await download( exportdata, res );
@@ -2515,7 +2517,7 @@ export const flagChecklistTableV1 = async ( req, res ) => {
2515
2517
  }
2516
2518
  console.log( reqestData );
2517
2519
 
2518
- let LamdaURL = 'https://vpcejaftccr3jzqf5wrdkks7yy0krqix.lambda-url.ap-south-1.on.aws/';
2520
+ let LamdaURL = 'https://bnauly42yuztzrgtttodzt3bcm0oqrsi.lambda-url.ap-south-1.on.aws/';
2519
2521
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
2520
2522
  // console.log( 'resultData =>', resultData );
2521
2523
  if ( resultData ) {
@@ -2756,3 +2758,466 @@ export const checklistDropdownV1 = async ( req, res ) => {
2756
2758
  return res.sendError( { error: error }, 500 );
2757
2759
  }
2758
2760
  };
2761
+
2762
+ export const flagComparisonCardsV2 = async ( req, res ) => {
2763
+ try {
2764
+ let requestData = req.body;
2765
+ let toDate = new Date( requestData.toDate );
2766
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
2767
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
2768
+ toDate.setUTCHours( 23, 59, 59, 59 );
2769
+
2770
+ let rangeOneFromDate;
2771
+ let rangeOneToDate;
2772
+ let rangeTwoFromDate;
2773
+ let rangeTwoToDate;
2774
+ if ( requestData.dateType == 'weekly' ) {
2775
+ rangeOneFromDate = new Date( requestData.toDate );
2776
+ rangeOneFromDate.setDate( rangeOneFromDate.getDate() - 6 );
2777
+ rangeOneToDate = new Date( requestData.toDate );
2778
+ rangeOneToDate = new Date( rangeOneToDate.getTime() - userTimezoneOffset );
2779
+ rangeOneToDate.setUTCHours( 23, 59, 59, 59 );
2780
+ rangeTwoToDate = new Date( requestData.toDate );
2781
+ rangeTwoToDate.setDate( rangeTwoToDate.getDate() - 7 );
2782
+ rangeTwoFromDate = new Date( rangeTwoToDate );
2783
+ rangeTwoFromDate.setDate( rangeTwoFromDate.getDate() - 6 );
2784
+ rangeTwoToDate = new Date( rangeTwoToDate.getTime() - userTimezoneOffset );
2785
+ rangeTwoToDate.setUTCHours( 23, 59, 59, 59 );
2786
+ } else if ( requestData.dateType == 'monthly' ) {
2787
+ rangeOneFromDate = new Date( requestData.toDate );
2788
+ rangeOneFromDate.setDate( rangeOneFromDate.getDate() - 30 );
2789
+ rangeOneToDate = new Date( requestData.toDate );
2790
+ rangeOneToDate = new Date( rangeOneToDate.getTime() - userTimezoneOffset );
2791
+ rangeOneToDate.setUTCHours( 23, 59, 59, 59 );
2792
+ rangeTwoToDate = new Date( requestData.toDate );
2793
+ rangeTwoToDate.setDate( rangeTwoToDate.getDate() - 31 );
2794
+ rangeTwoFromDate = new Date( rangeTwoToDate );
2795
+ rangeTwoFromDate.setDate( rangeTwoFromDate.getDate() - 30 );
2796
+ rangeTwoToDate = new Date( rangeTwoToDate.getTime() - userTimezoneOffset );
2797
+ rangeTwoToDate.setUTCHours( 23, 59, 59, 59 );
2798
+ } else {
2799
+ rangeOneFromDate = new Date( requestData.toDate );
2800
+ rangeOneFromDate.setDate( rangeOneFromDate.getDate() - 0 );
2801
+ rangeOneToDate = new Date( requestData.toDate );
2802
+ rangeOneToDate = new Date( rangeOneToDate.getTime() - userTimezoneOffset );
2803
+ rangeOneToDate.setUTCHours( 23, 59, 59, 59 );
2804
+ rangeTwoToDate = new Date( requestData.toDate );
2805
+ rangeTwoToDate.setDate( rangeTwoToDate.getDate() - 1 );
2806
+ rangeTwoFromDate = new Date( rangeTwoToDate );
2807
+ rangeTwoFromDate.setDate( rangeTwoFromDate.getDate() - 0 );
2808
+ rangeTwoToDate = new Date( rangeTwoToDate.getTime() - userTimezoneOffset );
2809
+ rangeTwoToDate.setUTCHours( 23, 59, 59, 59 );
2810
+ }
2811
+
2812
+ const createFindQuery = ( fromDate, toDate ) => [
2813
+ { $match: { client_id: requestData.clientId,
2814
+ date_iso: { $gte: fromDate, $lte: toDate },
2815
+ $or: [ { store_id: { $in: requestData.storeId } }, { store_id: { $eq: '' }, userEmail: { $in: requestData.userEmailes } }, { aiStoreList: { $in: requestData.storeId } } ],
2816
+ } },
2817
+ {
2818
+ $project: {
2819
+ timeFlag: 1,
2820
+ questionFlag: 1,
2821
+ },
2822
+ },
2823
+ {
2824
+ $group: {
2825
+ _id: '',
2826
+ totalFlag: { $sum: { $add: [ '$questionFlag', '$timeFlag' ] } },
2827
+ questionFlag: { $sum: '$questionFlag' },
2828
+ delayInSubmission: { $sum: '$timeFlag' },
2829
+ },
2830
+ },
2831
+ ];
2832
+
2833
+ const calculateComparison = ( rangeOneVal, rangeTwoVal ) => {
2834
+ if ( rangeOneVal === 0 ) {
2835
+ return { comparisonData: 0, ComparisonFlag: false };
2836
+ }
2837
+
2838
+ let comparisonData = Math.abs( ( ( rangeOneVal - rangeTwoVal ) / rangeTwoVal ) * 100 );
2839
+ if ( comparisonData === Infinity ) comparisonData = 0;
2840
+ comparisonData = comparisonData % 1 === 0 ? comparisonData.toFixed( 0 ) : comparisonData.toFixed( 1 );
2841
+ comparisonData = parseInt( comparisonData );
2842
+ const ComparisonFlag = rangeOneVal > rangeTwoVal;
2843
+ return { comparisonData, ComparisonFlag };
2844
+ };
2845
+
2846
+ const getDetectionCount = async ( fromDate, toDate ) => {
2847
+ const detectionPayload = {
2848
+ fromDate: dayjs( fromDate ).format( 'YYYY-MM-DD' ),
2849
+ toDate: dayjs( toDate ).startOf( 'day' ).format( 'YYYY-MM-DD' ),
2850
+ storeId: requestData.storeId,
2851
+ clientId: requestData.clientId,
2852
+ };
2853
+
2854
+ const publishedAiChecklists = await checklistconfigService.find(
2855
+ {
2856
+ client_id: requestData.clientId,
2857
+ publish: true,
2858
+ checkListType: { $in: [ 'customerunattended', 'mobileusagedetection', 'staffleftinthemiddle', 'storeopenandclose', 'uniformdetection', 'cleaning', 'scrum' ] },
2859
+ },
2860
+ { checkListType: 1 },
2861
+ );
2862
+ if ( !publishedAiChecklists?.length ) {
2863
+ return 0;
2864
+ }
2865
+
2866
+ const LamdaURL = 'https://f65azvtljclaxp6l7rnx65cdmm0lcgvp.lambda-url.ap-south-1.on.aws/';
2867
+ const resultData = await LamdaServiceCall( LamdaURL, detectionPayload );
2868
+ const published = publishedAiChecklists.map( ( val ) => val?.checkListType );
2869
+ if ( resultData?.status_code === '200' ) {
2870
+ let result = 0;
2871
+ published.forEach( ( item ) => {
2872
+ if ( resultData[item] ) {
2873
+ result += resultData[item];
2874
+ }
2875
+ } );
2876
+ return result;
2877
+ }
2878
+ return 0;
2879
+ };
2880
+
2881
+ const [ rangeOneData, rangeTwoData, rangeOneDetectionCount, rangeTwoDetectionCount ] = await Promise.all( [
2882
+ processedchecklistService.aggregate( createFindQuery( rangeOneFromDate, rangeOneToDate ) ),
2883
+ processedchecklistService.aggregate( createFindQuery( rangeTwoFromDate, rangeTwoToDate ) ),
2884
+ getDetectionCount( rangeOneFromDate, rangeOneToDate ),
2885
+ getDetectionCount( rangeTwoFromDate, rangeTwoToDate ),
2886
+ ] );
2887
+
2888
+ const flagComparisonCards = {
2889
+ totalComparisonFlag: { comparisonData: 0, ComparisonFlag: false },
2890
+ questionComparisonFlag: { comparisonData: 0, ComparisonFlag: false },
2891
+ delayInSubmissionComparisonFlag: { comparisonData: 0, ComparisonFlag: false },
2892
+ detectionComparisonFlag: { comparisonData: 0, ComparisonFlag: false },
2893
+ };
2894
+
2895
+ if ( rangeOneData.length && rangeTwoData.length ) {
2896
+ let rangeOneTotalFlag = rangeOneData[0].totalFlag + rangeOneDetectionCount || 0;
2897
+ let rangeTwoTotalFlag = rangeTwoData[0].totalFlag + rangeTwoDetectionCount || 0;
2898
+ flagComparisonCards.totalComparisonFlag = calculateComparison( rangeOneTotalFlag, rangeTwoTotalFlag );
2899
+ flagComparisonCards.questionComparisonFlag = calculateComparison( rangeOneData[0].questionFlag, rangeTwoData[0].questionFlag );
2900
+ flagComparisonCards.delayInSubmissionComparisonFlag = calculateComparison( rangeOneData[0].delayInSubmission, rangeTwoData[0].delayInSubmission );
2901
+ flagComparisonCards.detectionComparisonFlag = calculateComparison( rangeOneDetectionCount, rangeTwoDetectionCount );
2902
+ }
2903
+
2904
+ return res.sendSuccess( { flagComparisonCards } );
2905
+ } catch ( error ) {
2906
+ console.log( 'error =>', error );
2907
+ logger.error( { error, function: 'flagComparisonCardsV1' } );
2908
+ return res.sendError( error, 500 );
2909
+ }
2910
+ };
2911
+
2912
+ export const flagTablesV2 = async ( req, res ) => {
2913
+ try {
2914
+ let requestData = req.body;
2915
+ let fromDate = new Date( requestData.fromDate );
2916
+ let toDate = new Date( requestData.toDate );
2917
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
2918
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
2919
+ toDate.setUTCHours( 23, 59, 59, 59 );
2920
+ let result = {};
2921
+
2922
+ let findQuery = [];
2923
+ let findAndQuery = [];
2924
+ findAndQuery.push(
2925
+ { client_id: requestData.clientId },
2926
+ { date_iso: { $gte: fromDate, $lte: toDate } },
2927
+ { $or: [ { store_id: { $in: requestData.storeId } }, { store_id: { $eq: '' }, userEmail: { $in: requestData.userEmailes } }, { aiStoreList: { $in: requestData.storeId } } ] },
2928
+ );
2929
+
2930
+ if ( requestData?.filter === 'all' ) {
2931
+ findAndQuery.push( { $or: [ { checkListType: { $in: [ 'custom', 'customerunattended', 'mobileusagedetection', 'staffleftinthemiddle', 'storeopenandclose', 'uniformdetection', 'cleaning', 'scrum' ] } } ] } );
2932
+ } else if ( requestData?.filter === 'question' ) {
2933
+ findAndQuery.push( { checkListType: 'custom' } );
2934
+ findAndQuery.push( { questionFlag: { $gte: 1 } } );
2935
+ } else if ( requestData?.filter === 'time' ) {
2936
+ findAndQuery.push( { checkListType: 'custom' } );
2937
+ findAndQuery.push( { timeFlag: { $gte: 1 } } );
2938
+ } else if ( requestData?.filter === 'detection' ) {
2939
+ findAndQuery.push( { checkListType: { $in: [ 'customerunattended', 'mobileusagedetection', 'staffleftinthemiddle', 'storeopenandclose', 'uniformdetection', 'cleaning', 'scrum' ] } } );
2940
+ }
2941
+
2942
+ findQuery.push( { $match: { $and: findAndQuery } } );
2943
+
2944
+ if ( requestData.searchValue && requestData.searchValue != '' ) {
2945
+ findQuery.push( { $match: { $or: [ { checkListName: { $regex: requestData.searchValue, $options: 'i' } } ] } } );
2946
+ }
2947
+
2948
+ findQuery.push( {
2949
+ $project: {
2950
+ sourceCheckList_id: 1,
2951
+ checkListId: 1,
2952
+ checkListName: 1,
2953
+ coverage: 1,
2954
+ storeCount: 1,
2955
+ createdBy: 1,
2956
+ createdByName: 1,
2957
+ checklistStatus: 1,
2958
+ timeFlag: 1,
2959
+ questionFlag: 1,
2960
+ questionCount: 1,
2961
+ mobileDetectionFlag: 1,
2962
+ storeOpenCloseFlag: 1,
2963
+ uniformDetectionFlag: 1,
2964
+ checkListType: 1,
2965
+ scheduleRepeatedType: 1,
2966
+ store_id: 1,
2967
+ aiStoreList: 1,
2968
+ },
2969
+ } );
2970
+
2971
+ findQuery.push( {
2972
+ $group: {
2973
+ _id: '$sourceCheckList_id',
2974
+ checkListName: { $last: '$checkListName' },
2975
+ checkListChar: { $last: { $substr: [ '$checkListName', 0, 2 ] } },
2976
+ coverage: { $last: '$coverage' },
2977
+ sourceCheckList_id: { $last: '$sourceCheckList_id' },
2978
+ checkListType: { $last: '$checkListType' },
2979
+ storeCount: { $sum: 1 },
2980
+ storeCountAi: { $max: '$storeCount' },
2981
+ flaggedStores: { $sum: { $cond: [
2982
+ {
2983
+ $or: [
2984
+ { '$gt': [ '$timeFlag', 0 ] },
2985
+ { '$gt': [ '$questionFlag', 0 ] },
2986
+ { '$gt': [ '$mobileDetectionFlag', 0 ] },
2987
+ { '$gt': [ '$storeOpenCloseFlag', 0 ] },
2988
+ { '$gt': [ '$uniformDetectionFlag', 0 ] },
2989
+ { '$gt': [ '$customerUnattended', 0 ] },
2990
+ { '$gt': [ '$staffLeftInTheMiddle', 0 ] },
2991
+ { '$gt': [ '$cleaning', 0 ] },
2992
+ { '$gt': [ '$scrum', 0 ] },
2993
+ ],
2994
+ },
2995
+ 1,
2996
+ 0,
2997
+ ] } },
2998
+ flagCount: {
2999
+ $sum: {
3000
+ $add: [ '$questionFlag', '$timeFlag' ],
3001
+ },
3002
+ },
3003
+ submittedChecklist: {
3004
+ $sum: {
3005
+ $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, 1, 0 ],
3006
+ },
3007
+ },
3008
+ questionFlag: { $sum: '$questionFlag' },
3009
+ timeFlag: { $sum: '$timeFlag' },
3010
+ questionCount: { $sum: '$questionCount' },
3011
+ aiStoreList: { $max: '$aiStoreList' },
3012
+ aiStoreListNew: { $sum: { $size: '$aiStoreList' } },
3013
+ submittedQuestionCount: { $sum: { $cond: [ { $eq: [ '$checklistStatus', 'submit' ] }, '$questionCount', 0 ] } },
3014
+ },
3015
+ } );
3016
+
3017
+ findQuery.push( {
3018
+ $project: {
3019
+ assignedStores: '$storeCount',
3020
+ assignedStoresAi: '$aiStoreListNew',
3021
+ checkListName: 1,
3022
+ coverage: 1,
3023
+ checkListChar: 1,
3024
+ sourceCheckList_id: 1,
3025
+ checkListType: 1,
3026
+ flagType: 1,
3027
+ uniqueFlaggedStores: 1,
3028
+ flaggedStores: '$flaggedStores',
3029
+ flagCount: 1,
3030
+ questionCount: 1,
3031
+ correctAnswers: { $subtract: [ '$submittedQuestionCount', '$questionFlag' ] },
3032
+ customQuestionFlagCount: '$questionFlag',
3033
+ customTimeFlagCount: '$timeFlag',
3034
+ aiStoreList: 1,
3035
+ },
3036
+ } );
3037
+
3038
+ findQuery.push( {
3039
+ $project: {
3040
+ checkListName: 1,
3041
+ checkListChar: 1,
3042
+ coverage: {
3043
+ $concat: [
3044
+ { $toUpper: { $substr: [ { $ifNull: [ '$coverage', '' ] }, 0, 1 ] } },
3045
+ { $substr: [ { $ifNull: [ '$coverage', '' ] }, 1, { $strLenCP: { $ifNull: [ '$coverage', '' ] } } ] },
3046
+ ],
3047
+ },
3048
+ sourceCheckList_id: 1,
3049
+ checkListType: 1,
3050
+ flagType: 1,
3051
+ assignedStores: 1,
3052
+ assignedStoresAi: 1,
3053
+ flaggedStores: 1,
3054
+ flagCount: 1,
3055
+ uniqueFlaggedStores: 1,
3056
+ complianceRate: {
3057
+ $cond: {
3058
+ if: { $eq: [ '$questionCount', 0 ] },
3059
+ then: 0,
3060
+ else: {
3061
+ $round: [ { $multiply: [ { $divide: [ '$correctAnswers', '$questionCount' ] }, 100 ] }, 2 ],
3062
+ },
3063
+ },
3064
+ },
3065
+ customQuestionFlagCount: 1,
3066
+ customTimeFlagCount: 1,
3067
+ aiStoreList: 1,
3068
+ },
3069
+ } );
3070
+
3071
+ let getTotalCount = await processedchecklistService.aggregate( findQuery );
3072
+ if ( !getTotalCount.length ) {
3073
+ return res.sendError( { error: 'No Data Found' }, 204 );
3074
+ }
3075
+
3076
+ if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
3077
+ findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
3078
+ } else {
3079
+ findQuery.push( { $sort: { ['submittedChecklist']: -1 } } );
3080
+ }
3081
+
3082
+ if ( !requestData.export ) {
3083
+ let limit = parseInt( requestData?.limit ) || 10;
3084
+ let skip = limit * ( requestData?.offset ) || 0;
3085
+ findQuery.push( { $skip: skip }, { $limit: limit } );
3086
+ }
3087
+
3088
+
3089
+ let getChecklistPerformanceData = await processedchecklistService.aggregate( findQuery );
3090
+
3091
+ if ( getChecklistPerformanceData.length ) {
3092
+ const detectionChecklists = getChecklistPerformanceData?.filter( ( val ) => val?.checkListType !== 'custom' );
3093
+
3094
+ if ( detectionChecklists?.length ) {
3095
+ const detectionPayload = {
3096
+ 'fromDate': requestData.fromDate,
3097
+ 'toDate': requestData.toDate,
3098
+ 'storeId': requestData.storeId,
3099
+ 'clientId': requestData.clientId,
3100
+ };
3101
+
3102
+ let LamdaURL = 'https://f65azvtljclaxp6l7rnx65cdmm0lcgvp.lambda-url.ap-south-1.on.aws/';
3103
+ let resultData = await LamdaServiceCall( LamdaURL, detectionPayload );
3104
+ if ( resultData ) {
3105
+ if ( resultData.status_code == '200' ) {
3106
+ for ( let index = 0; index < getChecklistPerformanceData.length; index++ ) {
3107
+ if ( getChecklistPerformanceData[index].checkListType !== 'custom' ) {
3108
+ getChecklistPerformanceData[index].flagCount = resultData?.[getChecklistPerformanceData[index]?.checkListType] || 0;
3109
+ getChecklistPerformanceData[index].flaggedStores = resultData?.[`${getChecklistPerformanceData[index]?.checkListType}_flaggedstores`] || 0;
3110
+ getChecklistPerformanceData[index].assignedStores = getChecklistPerformanceData[index]?.aiStoreList?.filter( ( element ) => requestData.storeId.includes( element ) )?.length || 0;
3111
+ getChecklistPerformanceData[index].complianceRate = ( 100- ( getChecklistPerformanceData[index].flaggedStores / getChecklistPerformanceData[index].assignedStores ) * 100 );
3112
+ getChecklistPerformanceData[index].complianceRate = Math.min( 100, Math.max( 0, getChecklistPerformanceData[index].complianceRate ) );
3113
+ getChecklistPerformanceData[index].complianceRate = ( getChecklistPerformanceData[index].complianceRate % 1 === 0 ) ? getChecklistPerformanceData[index].complianceRate.toFixed( 0 ) : getChecklistPerformanceData[index].complianceRate.toFixed( 2 );
3114
+ }
3115
+ }
3116
+ }
3117
+ }
3118
+ }
3119
+ }
3120
+
3121
+ if ( requestData.export ) {
3122
+ const exportdata = [];
3123
+ getChecklistPerformanceData.forEach( ( element ) => {
3124
+ const data = {
3125
+ 'Checklist Name': element.checkListName,
3126
+ 'Question Flags': element?.customQuestionFlagCount,
3127
+ 'Not Submitted Flags': element?.customTimeFlagCount,
3128
+ 'Detection Flags': element?.flagCount,
3129
+ 'Coverage': element?.coverage,
3130
+ 'Assigned Stores': element?.checkListType === 'custom' ? element?.assignedStores : element?.assignedStoresAi,
3131
+ 'Flagged Stores': element?.flaggedStores,
3132
+ 'Compliance': element?.complianceRate,
3133
+ // 'Flag Type': element?.checkListType === 'custom' ? 'Question' : 'Detection',
3134
+ };
3135
+ if ( requestData?.filter === 'question' ) {
3136
+ data['Flags'] = data['Question Flags'];
3137
+ delete data['Question Flags'];
3138
+ delete data['Flagged Count'];
3139
+ delete data['Not Submitted Flags'];
3140
+ delete data['Detection Flags'];
3141
+ } else if ( requestData?.filter === 'time' ) {
3142
+ data['Flags'] = data['Not Submitted Flags'];
3143
+ delete data['Question Flags'];
3144
+ delete data['Flagged Count'];
3145
+ delete data['Not Submitted Flags'];
3146
+ delete data['Detection Flags'];
3147
+ } else if ( requestData?.filter === 'detection' ) {
3148
+ data['Flags'] = data['Detection Flags'];
3149
+ delete data['Question Flags'];
3150
+ delete data['Flagged Count'];
3151
+ delete data['Not Submitted Flags'];
3152
+ delete data['Detection Flags'];
3153
+ }
3154
+ exportdata.push( data );
3155
+ } );
3156
+ return await download( exportdata, res );
3157
+ }
3158
+
3159
+ result.totalCount = getTotalCount.length;
3160
+ result.checklistPerformance = getChecklistPerformanceData;
3161
+ return res.sendSuccess( result );
3162
+ } catch ( error ) {
3163
+ console.log( 'error =>', error );
3164
+ logger.error( { error: error, message: req.query, function: 'flagTablesV2' } );
3165
+ return res.sendError( { error: error }, 500 );
3166
+ }
3167
+ };
3168
+
3169
+ export const checklistCountByType = async ( req, res ) => {
3170
+ try {
3171
+ let { clientId, fromDate, toDate } = req.body;
3172
+
3173
+ fromDate = new Date( fromDate );
3174
+ toDate = new Date( toDate );
3175
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
3176
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
3177
+ toDate.setUTCHours( 23, 59, 59, 999 );
3178
+
3179
+ let findQuery = [
3180
+ {
3181
+ $match: {
3182
+ client_id: clientId,
3183
+ date_iso: { $gte: fromDate, $lte: toDate },
3184
+ },
3185
+ },
3186
+ {
3187
+ $group: {
3188
+ _id: '$checkListType',
3189
+ count: { $sum: 1 },
3190
+ },
3191
+ },
3192
+ {
3193
+ $group: {
3194
+ _id: null,
3195
+ aiChecklistCount: { $sum: { $cond: [ { $ne: [ '$_id', 'custom' ] }, '$count', 0 ] } },
3196
+ customChecklistCount: { $sum: { $cond: [ { $eq: [ '$_id', 'custom' ] }, '$count', 0 ] } },
3197
+ },
3198
+ },
3199
+ { $project: { _id: 0 } },
3200
+ ];
3201
+
3202
+ let [ result ] = await processedchecklistconfigService.aggregate( findQuery );
3203
+ result = result || { aiChecklistCount: 0, customChecklistCount: 0 };
3204
+ // result.customChecklistCount = 0;
3205
+ // result.aiChecklistCount = 0;
3206
+ let showCards = 'allCards';
3207
+ if ( result.customChecklistCount > 0 && result.aiChecklistCount > 0 ) {
3208
+ showCards = 'allCards';
3209
+ } else if ( result.customChecklistCount > 0 && result.aiChecklistCount <= 0 ) {
3210
+ showCards = 'customCards';
3211
+ } else if ( result.customChecklistCount <= 0 && result.aiChecklistCount > 0 ) {
3212
+ showCards = 'aiCards';
3213
+ }
3214
+ result.showCards = showCards;
3215
+ return res.sendSuccess( {
3216
+ ...result,
3217
+ } );
3218
+ } catch ( error ) {
3219
+ logger.error( { error, function: 'checklistCountByType' } );
3220
+ return res.sendError( { error }, 500 );
3221
+ }
3222
+ };
3223
+