tango-app-api-task 1.0.0-beta.2 → 1.0.0-beta.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-task",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.20",
4
4
  "description": "Task",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -24,7 +24,7 @@
24
24
  "mongodb": "^6.10.0",
25
25
  "nodemon": "^3.1.7",
26
26
  "npm": "^10.9.2",
27
- "tango-api-schema": "^2.1.99",
27
+ "tango-api-schema": "^2.2.8",
28
28
  "tango-app-api-middleware": "^3.1.43-alpha.10",
29
29
  "winston": "^3.17.0",
30
30
  "winston-daily-rotate-file": "^5.0.0"
@@ -83,13 +83,15 @@ export async function createUpdateTask( req, res ) {
83
83
  if ( data.upsertedId || req.body._id ) {
84
84
  checkListId = data?.upsertedId || req.body._id;
85
85
  let logInsertData = {
86
- action: req.body?._id ? 'checklistupdate' : 'checklistCreate',
86
+ action: req.body?._id ? 'taskUpdate' : 'taskCreate',
87
+ type: 'task',
87
88
  checklistId: checkListId,
88
89
  checkListName: inputBody.checklistName,
89
90
  createdBy: req.user._id,
90
91
  createdByName: req.user.userName,
91
92
  client_id: req.body.clientId,
92
93
  sections: inputBody?.sections,
94
+ createdByEmail: req.user.email,
93
95
  };
94
96
  await checklistLogs.create( logInsertData );
95
97
  let sectionList = [];
@@ -168,10 +170,16 @@ export async function createUpdateTask( req, res ) {
168
170
  }
169
171
  await taskQuestionService.insertMany( sectionList );
170
172
  let message = req.body?._id ? 'Task Updated Successfully' : 'Task Created Successfully';
173
+ if ( inputBody.submitType == 'save' ) {
174
+ message = 'Saved in draft successfully';
175
+ }
171
176
  return res.sendSuccess( { checklistId: checkListId, message: message } );
172
177
  } else {
173
178
  if ( inputBody.submitType == 'save' ) {
179
+ let message = 'Saved in draft successfully';
174
180
  return res.sendSuccess( { checklistId: checkListId, message: message } );
181
+ } else {
182
+ return res.sendSuccess( { message: 'Question is required' } );
175
183
  }
176
184
  }
177
185
  } else {
@@ -362,6 +370,13 @@ export async function validateUser( req, res ) {
362
370
  return res.sendSuccess( { validate: false, user: userEmail, store: storeId, domain: domainExists } );
363
371
  }
364
372
 
373
+ if ( req.body.hasOwnProperty( 'delete' ) ) {
374
+ let checkExists = await taskAssignService.findOne( { userEmail: req.body.assignedUsers[0].userEmail, storeName: req.body.assignedUsers[0].storeName, checkListId: req.body.id } );
375
+ if ( checkExists ) {
376
+ return res.sendError( 'User already exists', 400 );
377
+ }
378
+ }
379
+
365
380
  let id = await uploadUser( req, res );
366
381
  if ( id ) {
367
382
  return res.sendSuccess( { validate: true, id } );
@@ -429,9 +444,9 @@ export async function validateUser( req, res ) {
429
444
  }
430
445
  } else {
431
446
  if ( req.body.hasOwnProperty( 'delete' ) ) {
432
- let checkExists = await taskAssignService.findOne( { userEmail: req.body.assignedUsers[0].userEmail, storeName: req.body.assignedUsers[0].storeName } );
447
+ let checkExists = await taskAssignService.findOne( { userEmail: req.body.assignedUsers[0].userEmail, storeName: req.body.assignedUsers[0].storeName, checkListId: req.body.id } );
433
448
  if ( checkExists ) {
434
- return res.sendSuccess( 'User already exists' );
449
+ return res.sendError( 'User already exists', 400 );
435
450
  }
436
451
  }
437
452
 
@@ -652,11 +667,14 @@ export async function taskConfig( req, res ) {
652
667
 
653
668
  let logInsertData = {
654
669
  action: inputBody.submitType == 'publish' ? 'taskPublishUsingConfigPage' : 'taskConfigDraft',
670
+ type: 'task',
655
671
  checklistId: inputBody._id,
656
672
  checkListName: inputBody?.checkListName,
657
673
  createdBy: req.user._id,
658
674
  createdByName: req.user.userName,
659
675
  client_id: inputBody.client_id,
676
+ createdByEmail: req.user.email,
677
+ approver: inputBody?.approver || [],
660
678
  };
661
679
  await checklistLogs.create( logInsertData );
662
680
 
@@ -688,6 +706,7 @@ export async function taskConfig( req, res ) {
688
706
  checkListId: inputBody._id,
689
707
  type: 'task',
690
708
  client_id: inputBody.client_id,
709
+ checkListName: inputBody?.checkListName,
691
710
  } );
692
711
  }
693
712
  } );
@@ -734,15 +753,15 @@ export async function taskConfig( req, res ) {
734
753
  let tDate = dayjs().format( 'YYYY-MM-DD' ) + ' ' + dayjs().format( 'hh:mm A' );
735
754
  let currentDate = dayjs( tDate, 'YYYY-MM-DD hh:mm A' ).format();
736
755
  if ( configDetails.scheduleEndTimeISO >= currentDate ) {
737
- let deleteQuery = {
738
- $and: [
739
- { date_string: dayjs().format( 'YYYY-MM-DD' ) },
740
- { sourceCheckList_id: new ObjectId( req.params.checklistId ) },
741
- { scheduleEndTime_iso: { $gt: currentDate } },
742
- ],
743
- };
744
- deleteQuery.$and.push( { checklistStatus: { $ne: 'submit' } } );
745
- await taskProcessedService.deleteMany( deleteQuery );
756
+ // let deleteQuery = {
757
+ // $and: [
758
+ // { date_string: dayjs().format( 'YYYY-MM-DD' ) },
759
+ // { sourceCheckList_id: new ObjectId( req.params.checklistId ) },
760
+ // { scheduleEndTime_iso: { $gt: currentDate } },
761
+ // ],
762
+ // };
763
+ // deleteQuery.$and.push( { checklistStatus: { $ne: 'submit' } } );
764
+ // await taskProcessedService.deleteMany( deleteQuery );
746
765
  await insertSingleProcessData( inputBody._id );
747
766
  } else {
748
767
  logger.info( `Schudled End Time Breached Checklist publish true => Checklist Name: ${checklistDetails.checkListName}` );
@@ -751,12 +770,8 @@ export async function taskConfig( req, res ) {
751
770
  futureDaysDataRemove( currentDate, req.params.checklistId );
752
771
  }
753
772
  if ( response?.modifiedCount || response?.matchedCount || response?.upsertedCount ) {
754
- return res.sendSuccess( { id: inputBody._id, message: 'Configuration Updated Successfully' } );
755
- }
756
- if ( inputBody.submitType == 'save' ) {
757
- if ( response?.modifiedCount || response?.matchedCount || response?.upsertedCount ) {
758
- return res.sendSuccess( { message: 'Configuration Updated Successfully' } );
759
- }
773
+ let message = inputBody.submitType == 'publish' ? 'Configuration Updated Successfully' : 'Saved in draft successfully';
774
+ return res.sendSuccess( { id: inputBody._id, message: message } );
760
775
  }
761
776
  } catch ( e ) {
762
777
  logger.error( { functionName: 'taskConfig =>', error: e, message: req.body } );
@@ -806,6 +821,9 @@ export async function insertSingleProcessData( checklistId ) {
806
821
  insertdata.locationCount = getCLconfig.locationCount;
807
822
  insertdata.allowedMultiSubmit = false;
808
823
  insertdata.scheduleRepeatedType = 'daily';
824
+ insertdata.remainder = getCLconfig?.remainder || [];
825
+ insertdata.approver = getCLconfig?.approver || [];
826
+ insertdata.restrictAttendance = getCLconfig?.restrictAttendance;
809
827
  let collectSections = [];
810
828
  let sectionQuery = [];
811
829
  sectionQuery.push( {
@@ -827,21 +845,55 @@ export async function insertSingleProcessData( checklistId ) {
827
845
  }
828
846
  insertdata.questionAnswers = collectSections;
829
847
  insertdata.updatedAt = new Date();
830
- let insertdataquery = {};
831
- insertdataquery.date_string = insertdata.date_string;
832
- insertdataquery.date_iso = insertdata.date_iso;
833
- insertdataquery.client_id = insertdata.client_id;
834
- insertdataquery.sourceCheckList_id = insertdata.sourceCheckList_id;
835
- let updatedchecklist;
836
- let checklistDetails = await taskProcessedConfigService.findOne( insertdataquery );
837
- if ( !checklistDetails ) {
838
- updatedchecklist = await taskProcessedConfigService.insert( insertdata );
839
- } else {
840
- await taskProcessedConfigService.updateOne( { _id: checklistDetails._id }, insertdata );
841
- updatedchecklist = checklistDetails;
842
- }
843
- if ( updatedchecklist ) {
844
- insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedchecklist, date, startTimeIso, endTimeIso, insertdata );
848
+ function getDaysBeforeEndDate( endDate ) {
849
+ const end = dayjs.utc( endDate );
850
+ const now = dayjs.utc();
851
+
852
+ if ( end.isBefore( now ) ) {
853
+ throw new Error( 'End date must be in the future.' );
854
+ }
855
+
856
+ const daysArray = [];
857
+ let currentDate = now.startOf( 'day' );
858
+
859
+ while ( currentDate.isBefore( end ) ) {
860
+ daysArray.push( currentDate.format( ) );
861
+ currentDate = currentDate.add( 1, 'day' );
862
+ }
863
+
864
+ return daysArray;
865
+ };
866
+
867
+ const dateArray = getDaysBeforeEndDate( endDate );
868
+
869
+ for ( const dateVal of dateArray ) {
870
+ const insertdataquery = {
871
+ date_string: dayjs( dateVal ).format( 'YYYY-MM-DD' ),
872
+ date_iso: dayjs( dateVal ).toDate(),
873
+ client_id: insertdata.client_id,
874
+ sourceCheckList_id: insertdata.sourceCheckList_id,
875
+ };
876
+
877
+ const insertDataPerDay = {
878
+ ...insertdata,
879
+ date_string: dayjs( dateVal ).format( 'YYYY-MM-DD' ),
880
+ date_iso: dayjs( dateVal ).toDate(),
881
+ approver: getCLconfig?.approver,
882
+ };
883
+
884
+ let updatedchecklist;
885
+ let checklistDetails = await taskProcessedConfigService.findOne( insertdataquery );
886
+
887
+ if ( !checklistDetails ) {
888
+ updatedchecklist = await taskProcessedConfigService.insert( insertDataPerDay );
889
+ } else {
890
+ await taskProcessedConfigService.updateOne( { _id: checklistDetails._id }, insertDataPerDay );
891
+ updatedchecklist = checklistDetails;
892
+ }
893
+
894
+ if ( updatedchecklist ) {
895
+ await insertPCBulkV3( getCLconfig, checklistId, updatedchecklist, dateVal, startTimeIso, endTimeIso, insertdata );
896
+ }
845
897
  }
846
898
  }
847
899
  }
@@ -852,18 +904,18 @@ export async function insertSingleProcessData( checklistId ) {
852
904
  }
853
905
  };
854
906
 
855
- async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedchecklist, date, startTimeIso, endTimeIso, insertdata ) {
907
+ async function insertPCBulkV3( getCLconfig, checklistId, updatedchecklist, date, startTimeIso, endTimeIso, insertdata ) {
856
908
  let getquestionQuery = [];
857
909
  getquestionQuery.push( {
858
910
  $match: {
859
911
  checkListId: new ObjectId( checklistId ),
860
912
  checkFlag: true,
861
913
  isdeleted: false,
862
- // ...( getCLconfig?.reinitiate ) ? { reinitiate: true } : {},
863
914
  },
864
915
  } );
865
916
  let allQuestion = await taskAssignService.aggregate( getquestionQuery );
866
917
 
918
+
867
919
  if ( allQuestion ) {
868
920
  let userIdList = [];
869
921
  for ( let element4 of allQuestion ) {
@@ -893,7 +945,7 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
893
945
  element4.checkListName = getCLconfig.checkListName;
894
946
  element4.checkListDescription = getCLconfig.checkListDescription;
895
947
  element4.date_iso = new Date( dayjs( date, 'YYYY-MM-DD' ).format( 'YYYY-MM-DD' ) );
896
- element4.date_string = dayjs( currentdate ).format( 'YYYY-MM-DD' );
948
+ element4.date_string = dayjs( date ).format( 'YYYY-MM-DD' );
897
949
  element4.allowedOverTime = false;
898
950
  element4.allowedStoreLocation = getCLconfig.allowedStoreLocation;
899
951
  element4.scheduleStartTime = '12:00 AM';
@@ -911,19 +963,43 @@ async function insertPCBulkV3( getCLconfig, checklistId, currentdate, updatedche
911
963
  element4.scheduleRepeatedType = 'daily';
912
964
  element4.approvalEnable = getCLconfig.approver.length ? true : false;
913
965
  element4.priorityType = getCLconfig.priorityType;
966
+ element4.remainder = getCLconfig?.remainder || [];
967
+ element4.restrictAttendance = getCLconfig?.restrictAttendance;
914
968
  }
915
969
  if ( userIdList.length ) {
916
970
  allQuestion = allQuestion.filter( ( item ) => typeof item._id == 'undefined' );
917
971
  }
972
+
918
973
  if ( allQuestion ) {
919
- let assigndeletequery = {};
920
- assigndeletequery.date_string = insertdata.date_string;
921
- assigndeletequery.date_iso = insertdata.date_iso;
922
- assigndeletequery.client_id = insertdata.client_id;
923
- assigndeletequery.checkListId = updatedchecklist._id;
924
- assigndeletequery.checklistStatus = { $nin: [ 'submit' ] };
925
- await taskProcessedService.deleteMany( assigndeletequery );
926
- await taskProcessedService.insertMany( allQuestion );
974
+ let assigndeletequery = {
975
+ date_iso: date,
976
+ client_id: insertdata.client_id,
977
+ checkListId: updatedchecklist._id,
978
+ redoStatus: false,
979
+ checklistStatus: { $nin: [ 'submit' ] },
980
+ };
981
+
982
+ if ( assigndeletequery.checkListId && assigndeletequery.date_iso ) {
983
+ await taskProcessedService.deleteMany( assigndeletequery );
984
+ }
985
+
986
+ const retainTaskQuery = {
987
+ date_iso: date,
988
+ client_id: insertdata.client_id,
989
+ checkListId: updatedchecklist._id,
990
+ redoStatus: true,
991
+ checklistStatus: { $nin: [ 'submit' ] },
992
+ };
993
+
994
+ const taskToRetain = await taskProcessedService.find( retainTaskQuery );
995
+
996
+ const insertList = allQuestion.filter( ( item2 ) =>
997
+ !taskToRetain.some( ( item1 ) =>
998
+ item1.store_id === item2.store_id && item1.userEmail === item2.userEmail,
999
+ ),
1000
+ );
1001
+
1002
+ await taskProcessedService.insertMany( insertList );
927
1003
  logger.info( { function: 'insertPCBulk_v3', query: assigndeletequery } );
928
1004
  }
929
1005
  }
@@ -1007,22 +1083,14 @@ export async function reinitiateTask( req, res ) {
1007
1083
  if ( !taskDetails ) {
1008
1084
  return res.sendError( 'no data found', 204 );
1009
1085
  }
1010
- let data = {};
1011
- data.scheduleDate = req.body.scheduleDate;
1012
- data.scheduleEndTime = req.body.scheduleEndTime;
1013
- data.scheduleEndTimeISO = dayjs.utc( req.body.scheduleEndTime, 'hh:mm A' ).format();
1014
- data.allowedStoreLocation = req.body.allowedStoreLocation;
1015
- data.approver = req.body.approver;
1016
- data.priorityType = req.body.priorityType;
1017
- data.restrictAttendance = req.body.restrictAttendance;
1018
- data.reinitiate = true;
1086
+ let endDate = dayjs( req.body.scheduleDate ).format( 'YYYY-MM-DD' ) + ' ' + req.body.scheduleEndTime;
1087
+ endDate = dayjs.utc( endDate, 'YYYY-MM-DD hh:mm A' ).format();
1019
1088
 
1020
1089
  if ( req.body?.approver.length ) {
1021
1090
  let data = [];
1022
1091
  let existEmail = await traxApprover.find( { checkListId: req.body.taskId, isdeleted: false } );
1023
1092
  let mailList = existEmail.map( ( item ) => item.userEmail );
1024
1093
  existEmail = req.body?.approver.filter( ( item ) => mailList.includes( item.value ) ).map( ( item ) => item.value );
1025
- let userMailList = req.body?.approver.map( ( ele ) => ele.value );
1026
1094
  req.body?.approver.forEach( ( ele ) => {
1027
1095
  if ( !existEmail.includes( ele.value ) ) {
1028
1096
  data.push( {
@@ -1033,16 +1101,59 @@ export async function reinitiateTask( req, res ) {
1033
1101
  } );
1034
1102
  }
1035
1103
  } );
1036
-
1037
- await traxApprover.updateMany( { checkListId: req.body.taskId, userEmail: { $nin: userMailList } }, { isdeleted: true } );
1038
1104
  if ( data.length ) {
1039
1105
  await traxApprover.insertMany( data );
1040
1106
  }
1041
1107
  }
1042
1108
 
1043
- await taskService.updateOne( { _id: req.body.taskId }, data );
1044
- await taskAssignService.updateOne( { userEmail: req.body.userEmail, storeName: req.body.storeName, checkListId: req.body.taskId }, { reinitiate: true } );
1045
- await insertSingleProcessData( req.body.taskId );
1109
+ function getDaysBeforeEndDate( endDate ) {
1110
+ const end = dayjs.utc( endDate );
1111
+ const now = dayjs.utc( req.body.previousEndDate ).add( 1, 'day' );
1112
+
1113
+ const daysArray = [];
1114
+ if ( !end.isBefore( now ) ) {
1115
+ let currentDate = now.startOf( 'day' );
1116
+
1117
+ while ( currentDate.isBefore( end ) ) {
1118
+ daysArray.push( currentDate.format( ) );
1119
+ currentDate = currentDate.add( 1, 'day' );
1120
+ }
1121
+ }
1122
+ return daysArray;
1123
+ };
1124
+
1125
+ const dateArray = getDaysBeforeEndDate( endDate );
1126
+ await taskProcessedService.updateMany( { userEmail: req.body.userEmail, store_id: req.body.store_id, date_iso: { $gte: new Date( dayjs().format( 'YYYY-MM-DD' ) ) }, sourceCheckList_id: req.body.taskId }, { scheduleEndTime_iso: endDate, priorityType: req.body.priorityType, allowedStoreLocation: req.body.allowedStoreLocation } );
1127
+ await taskProcessedConfigService.updateMany( { date_iso: { $gte: new Date( dayjs().format( 'YYYY-MM-DD' ) ) }, sourceCheckList_id: req.body.taskId }, { scheduleEndTime_iso: endDate, priorityType: req.body.priorityType, allowedStoreLocation: req.body.allowedStoreLocation } );
1128
+
1129
+ if ( dateArray.length ) {
1130
+ let getPreviousUserTaskDate = await taskProcessedService.findOne( { userEmail: req.body.userEmail, store_id: req.body.store_id, sourceCheckList_id: req.body.taskId } );
1131
+ if ( !getPreviousUserTaskDate ) {
1132
+ return res.sendError( 'No data found', 204 );
1133
+ }
1134
+ let getPreviousConfigDate = await taskProcessedConfigService.findOne( { sourceCheckList_id: req.body.taskId } );
1135
+
1136
+ for ( let date of dateArray ) {
1137
+ let configData = {
1138
+ ...getPreviousConfigDate._doc,
1139
+ date_string: dayjs( date ).format( 'YYYY-MM-DD' ),
1140
+ date_iso: new Date( dayjs( date ).format( 'YYYY-MM-DD' ) ),
1141
+ };
1142
+
1143
+ let userTaskData = {
1144
+ ...getPreviousUserTaskDate._doc,
1145
+ date_string: dayjs( date ).format( 'YYYY-MM-DD' ),
1146
+ date_iso: new Date( dayjs( date ).format( 'YYYY-MM-DD' ) ),
1147
+ };
1148
+
1149
+ delete configData._id;
1150
+ delete userTaskData._id;
1151
+
1152
+ await taskProcessedConfigService.insert( configData );
1153
+ await taskProcessedService.insert( userTaskData );
1154
+ }
1155
+ }
1156
+
1046
1157
  return res.sendSuccess( 'Task reinitaite SUccessfully' );
1047
1158
  } catch ( e ) {
1048
1159
  logger.error( { functionName: 'reinitiateTask', error: e } );
@@ -1096,14 +1207,6 @@ export async function createChecklistTask( req, res ) {
1096
1207
  data['approver'] = { name: req.user.userName, value: req.user.email };
1097
1208
  }
1098
1209
 
1099
- let taskNameDetails = await taskService.find( { checkListName: { $regex: data.checkListName, $options: 'i' }, client_id: data.client_id, isdeleted: false } );
1100
- if ( taskNameDetails.length ) {
1101
- let nameLength = taskNameDetails.length;
1102
- data.checkListName = data.checkListName.split( '(' )[0] + '(' + nameLength +')';
1103
- } else {
1104
- data.checkListName = data.checkListName;
1105
- }
1106
-
1107
1210
  let response = await taskService.create( data );
1108
1211
  if ( response?.approver.length ) {
1109
1212
  let data = [];
@@ -1124,7 +1227,9 @@ export async function createChecklistTask( req, res ) {
1124
1227
  inputBody.question[0].questionReferenceImage.forEach( ( ele ) => {
1125
1228
  let imgUrl = decodeURIComponent( ele.split( '?' )[0] );
1126
1229
  let url = imgUrl.split( '/' );
1127
- url.splice( 0, 3 );
1230
+ if ( url.includes( 'https:' ) || url.includes( 'http:' ) ) {
1231
+ url.splice( 0, 3 );
1232
+ }
1128
1233
  images.push( url.join( '/' ) );
1129
1234
  } );
1130
1235
  inputBody.question[0].questionReferenceImage = images;
@@ -1134,7 +1239,7 @@ export async function createChecklistTask( req, res ) {
1134
1239
  inputBody.question[0].answers[0].referenceImage.forEach( ( ele ) => {
1135
1240
  let imgUrl = decodeURIComponent( ele.split( '?' )[0] );
1136
1241
  let url = imgUrl.split( '/' );
1137
- if ( url.includes( 'https' ) || url.includes( 'http' ) ) {
1242
+ if ( url.includes( 'https:' ) || url.includes( 'http:' ) ) {
1138
1243
  url.splice( 0, 3 );
1139
1244
  }
1140
1245
  images.push( url.join( '/' ) );
@@ -1224,43 +1329,48 @@ export async function approveTask( req, res ) {
1224
1329
  let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
1225
1330
  toDate = new Date( toDate.getTime() - userTimezoneOffset );
1226
1331
  toDate.setUTCHours( 23, 59, 59, 59 );
1227
- let query = { sourceCheckList_id: req.body.sourceCheckList_id, date_iso: { $gte: req.body.fromDate, $lte: toDate }, checklistStatus: 'submit', approvalEnable: true };
1332
+ let query = { sourceCheckList_id: req.body.sourceCheckList_id, date_iso: { $gte: req.body.fromDate, $lte: toDate }, $or: [ { checklistStatus: 'submit' }, { redoStatus: true } ], approvalEnable: true };
1228
1333
  if ( req.body?.storeId?.length ) {
1229
1334
  query['store_id'] = { $in: req.body.storeId };
1230
1335
  }
1231
- let taskDetails = await taskProcessedService.find( query, { _id: 1 } );
1336
+ let taskDetails = await taskProcessedService.find( query, { _id: 1, checklistStatus: 1, storeName: 1 } );
1232
1337
  if ( !taskDetails.length ) {
1233
1338
  return res.sendError( 'No data found', 204 );
1234
1339
  }
1235
- let idList = taskDetails.map( ( item ) => item._id );
1236
- let updateResponse = await taskProcessedService.updateMany( { _id: { $in: idList } }, { approvalStatus: true } );
1237
- if ( updateResponse.modifiedCount || updateResponse.matchedCount ) {
1238
- let params = {
1239
- 'payload': {
1240
- sourceCheckList_id: req.body.sourceCheckList_id,
1241
- fromDate: req.body.fromDate,
1242
- toDate: req.body.toDate,
1243
- store_id: req.body.storeId,
1244
- },
1245
- 'upsert': {
1246
- approvalStatus: true,
1247
- },
1248
- };
1249
- const requestOptions = {
1250
- method: 'POST',
1251
- headers: {
1252
- 'Content-Type': 'application/json',
1253
- },
1254
- body: JSON.stringify( params ),
1255
- };
1256
- let url = JSON.parse( process.env.LAMBDAURL );
1340
+ let idList = taskDetails.filter( ( ele ) => ele.checklistStatus == 'submit' ).map( ( item ) => item._id );
1341
+ if ( idList.length ) {
1342
+ let updateResponse = await taskProcessedService.updateMany( { _id: { $in: idList } }, { approvalStatus: true } );
1343
+ if ( updateResponse.modifiedCount || updateResponse.matchedCount ) {
1344
+ let params = {
1345
+ 'payload': {
1346
+ sourceCheckList_id: req.body.sourceCheckList_id,
1347
+ fromDate: req.body.fromDate,
1348
+ toDate: req.body.toDate,
1349
+ store_id: req.body.storeId,
1350
+ },
1351
+ 'upsert': {
1352
+ approvalStatus: true,
1353
+ },
1354
+ };
1355
+ const requestOptions = {
1356
+ method: 'POST',
1357
+ headers: {
1358
+ 'Content-Type': 'application/json',
1359
+ },
1360
+ body: JSON.stringify( params ),
1361
+ };
1362
+ let url = JSON.parse( process.env.LAMBDAURL );
1257
1363
 
1258
- let searchResponse = await fetch( url.approveTask, requestOptions );
1259
- if ( searchResponse.ok ) {
1260
- return res.sendSuccess( 'Task Approved successfully' );
1261
- } else {
1262
- return res.sendError( 'Something went wrong', 500 );
1364
+ let searchResponse = await fetch( url.approveTask, requestOptions );
1365
+ if ( searchResponse.ok ) {
1366
+ return res.sendSuccess( 'Task has been approved the submitted stores' );
1367
+ } else {
1368
+ return res.sendError( 'Something went wrong', 500 );
1369
+ }
1263
1370
  }
1371
+ } else {
1372
+ let redoList = taskDetails.filter( ( ele ) => ele.checklistStatus == 'open' );
1373
+ return res.sendError( `${redoList.length} stores has not been approved since those stores are not submitted`, 400 );
1264
1374
  }
1265
1375
  } catch ( e ) {
1266
1376
  logger.error( { function: 'approveTask', error: e, message: req.body } );
@@ -1289,15 +1399,18 @@ export async function redoTask( req, res ) {
1289
1399
  if ( sectionIndex == -1 ) {
1290
1400
  return res.sendError( 'section is not found', 400 );
1291
1401
  }
1292
- let data = { ...question[sectionIndex].questions[req.body.payload.qno - 1], redo: true };
1402
+ let data = { ...question[sectionIndex].questions[req.body.payload.qno - 1], redo: true, redoComment: req.body.payload?.checklistDescription || '' };
1293
1403
  let userAnswer = data.userAnswer;
1404
+ console.log( userAnswer );
1294
1405
  question[sectionIndex].questions[req.body.payload.qno - 1] = data;
1406
+ question[sectionIndex].questions[req.body.payload.qno - 1].remarks = '';
1295
1407
  question[sectionIndex].questions[req.body.payload.qno - 1].userAnswer = [];
1296
1408
  taskDetails.questionAnswers = question;
1297
1409
  let updateData = {
1298
1410
  checklistStatus: 'open',
1299
1411
  redoStatus: true,
1300
1412
  questionAnswers: question,
1413
+ ...( dayjs.utc( taskDetails.scheduleEndTime_iso ).format( 'YYYY-MM-DD' ) == dayjs().format( 'YYYY-MM-DD' ) ) ? { scheduleEndTime_iso: dayjs.utc().endOf( 'day' ) } :{},
1301
1414
  };
1302
1415
 
1303
1416
  let response = await taskProcessedService.updateOne( { _id: req.body.payload._id }, updateData );
@@ -1322,6 +1435,7 @@ export async function redoTask( req, res ) {
1322
1435
  submitedBy: taskDetails.userName,
1323
1436
  submitTime: taskDetails.submitTime,
1324
1437
  };
1438
+ console.log( data );
1325
1439
  await checklistLogs.create( data );
1326
1440
  const requestOptions = {
1327
1441
  method: 'POST',
@@ -1341,7 +1455,7 @@ export async function redoTask( req, res ) {
1341
1455
  return res.sendError( 'Something went wrong', 500 );
1342
1456
  }
1343
1457
  } catch ( e ) {
1344
- logger.error( { function: 'redoChecklist', error: e, message: req.body } );
1458
+ logger.error( { function: 'redoTask', error: e, message: req.body } );
1345
1459
  return res.sendError( e, 500 );
1346
1460
  }
1347
1461
  }
@@ -1512,12 +1626,23 @@ export async function approvalstatus( req, res ) {
1512
1626
  }
1513
1627
  let filterApprover = Approver.filter( ( data ) => data.userEmail=== req.user.email );
1514
1628
  if ( filterApprover.length === 0 ) {
1629
+ if ( ( req?.user?.userType == 'client' && req.user.role == 'superadmin' ) ) {
1630
+ let url = JSON.parse( process.env.LAMBDAURL );
1631
+ let resultData = await LamdaServiceCall( url.approvalstatustask, req.body );
1632
+ if ( resultData ) {
1633
+ console.log( resultData.status_code );
1634
+ if ( resultData.status_code == '200' ) {
1635
+ return res.sendSuccess( resultData );
1636
+ }
1637
+ }
1638
+ return res.sendError( 'No Content', 204 );
1639
+ }
1515
1640
  return res.sendSuccess( 'suceess' );
1516
1641
  }
1517
1642
 
1518
1643
 
1519
1644
  let url = JSON.parse( process.env.LAMBDAURL );
1520
- let resultData = await LamdaServiceCall( url.approvalstatus, req.body );
1645
+ let resultData = await LamdaServiceCall( url.approvalstatustask, req.body );
1521
1646
  console.log( resultData );
1522
1647
  if ( resultData ) {
1523
1648
  if ( resultData.status_code == '200' ) {
@@ -1743,6 +1868,7 @@ export const updatePublish = async ( req, res ) => {
1743
1868
  createdBy: req.user._id,
1744
1869
  createdByName: req.user.userName,
1745
1870
  client_id: req.body.clientId,
1871
+ createdByEmail: req.user.email,
1746
1872
  };
1747
1873
  await checklistLogs.create( logInsertData );
1748
1874
  return res.sendSuccess( { checklistName: getCheckDetails.checkListName, message: 'Updated Successfully' } );
@@ -1766,7 +1892,7 @@ export const duplicateChecklist = async ( req, res ) => {
1766
1892
 
1767
1893
  let dupDetails = { ...checkDetails._doc };
1768
1894
  let name = `^${dupDetails.checkListName.split( '(' )[0]}\\(.*\\)$`;
1769
- let checkListNameDetails = await taskService.find( { checkListName: { $regex: name }, client_id: req.query.clientId, isdeleted: false } );
1895
+ let checkListNameDetails = await taskService.find( { checkListName: { $regex: name }, client_id: req.query.clientId } );
1770
1896
  if ( checkListNameDetails.length ) {
1771
1897
  let nameLength = checkListNameDetails.length + 1;
1772
1898
  dupDetails.checkListName = dupDetails.checkListName.split( '(' )[0] + '(' + nameLength +')';
@@ -1786,6 +1912,7 @@ export const duplicateChecklist = async ( req, res ) => {
1786
1912
  createdBy: req.user._id,
1787
1913
  createdByName: req.user.userName,
1788
1914
  client_id: req.query.clientId,
1915
+ createdByEmail: req.user.email,
1789
1916
  };
1790
1917
  await checklistLogs.create( logInsertData );
1791
1918
  taskService.create( dupDetails ).then( async ( data ) => {
@@ -107,11 +107,16 @@ export const taskTableV1 = async ( req, res ) => {
107
107
  let offset = parseInt( req.body.offset - 1 ) || 0;
108
108
  let page = offset * limit;
109
109
  let query = [];
110
+ let fromDate = new Date( req.body.fromDate );
111
+ let toDate = new Date( req.body.toDate );
112
+ let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
113
+ toDate = new Date( toDate.getTime() - userTimezoneOffset );
114
+ toDate.setUTCHours( 23, 59, 59, 59 );
110
115
 
111
116
  query.push(
112
117
  {
113
118
  $match: {
114
- // checkListType: 'task',
119
+ date_iso: { $gte: fromDate, $lte: toDate },
115
120
  client_id: req.body.clientId,
116
121
  isdeleted: false,
117
122
  },
@@ -218,28 +223,22 @@ export const taskTableV1 = async ( req, res ) => {
218
223
  export const taskInfoTableV1 = async ( req, res ) => {
219
224
  try {
220
225
  let requestData = req.body;
221
- // let fromDate = new Date( requestData.fromDate );
222
- // let toDate = new Date( requestData.toDate );
223
- // let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
224
- // toDate = new Date( toDate.getTime() - userTimezoneOffset );
225
- // toDate.setUTCHours( 23, 59, 59, 59 );
226
226
  let result = {};
227
227
 
228
228
  let findQuery = [];
229
229
  let findAndQuery = [];
230
230
 
231
231
  findAndQuery.push(
232
- // { date_iso: { $gte: fromDate, $lte: toDate } },
232
+ { date_iso: { $lte: new Date( dayjs().format( 'YYYY-MM-DD' ) ) } },
233
233
  { client_id: requestData.clientId },
234
234
  { store_id: { $in: requestData.storeId } },
235
- // { checkListType: { $eq: 'task' } },
236
235
  { sourceCheckList_id: new mongoose.Types.ObjectId( requestData.taskId ) },
237
236
  );
238
237
  if ( requestData.checklistStatus && requestData.checklistStatus != 'All' ) {
239
238
  if ( requestData.checklistStatus == 'redo' ) {
240
239
  findAndQuery.push(
241
240
  { redoStatus: true },
242
- { checklistStatus: { $ne: 'submit' } } );
241
+ );
243
242
  } else {
244
243
  findAndQuery.push( { checklistStatus: requestData.checklistStatus } );
245
244
  }
@@ -259,8 +258,40 @@ export const taskInfoTableV1 = async ( req, res ) => {
259
258
  findQuery.push( { $match: { $or: [ query ] } } );
260
259
  }
261
260
 
261
+ findQuery.push( { $sort: { date_iso: -1, userName: 1 } } );
262
+
263
+ findQuery.push(
264
+ {
265
+ $group: {
266
+ _id: { userEmail: '$userEmail', storeId: '$store_id', checklistStatus: '$checklistStatus' },
267
+ checkListName: { $first: '$checkListName' },
268
+ createdByName: { $first: '$createdByName' },
269
+ userName: { $first: '$userName' },
270
+ userEmail: { $first: '$userEmail' },
271
+ checklistStatus: { $first: '$checklistStatus' },
272
+ submitTime_string: { $first: '$submitTime_string' },
273
+ storeName: { $first: '$storeName' },
274
+ checkListType: { $first: '$checkListType' },
275
+ scheduleRepeatedType: { $first: '$scheduleRepeatedType' },
276
+ date_iso: { $first: '$date_iso' },
277
+ store_id: { $first: '$store_id' },
278
+ timeFlag: { $first: '$timeFlag' },
279
+ date_string: { $first: '$date_string' },
280
+ sourceCheckList_id: { $first: '$sourceCheckList_id' },
281
+ reinitiateStatus: { $first: '$reinitiateStatus' },
282
+ scheduleEndTime_iso: { $first: '$scheduleEndTime_iso' },
283
+ processedchecklistId: { $first: '$_id' },
284
+ redoStatus: { $first: '$redoStatus' },
285
+ priorityType: { $first: '$priorityType' },
286
+ allowedStoreLocation: { $first: '$allowedStoreLocation' },
287
+ restrictAttendance: { $first: '$restrictAttendance' },
288
+ },
289
+ },
290
+ );
291
+
262
292
  findQuery.push( {
263
293
  $project: {
294
+ _id: 0,
264
295
  checkListName: 1,
265
296
  createdByName: 1,
266
297
  userName: 1,
@@ -277,12 +308,17 @@ export const taskInfoTableV1 = async ( req, res ) => {
277
308
  sourceCheckList_id: 1,
278
309
  reinitiateStatus: 1,
279
310
  scheduleEndTime_iso: 1,
280
-
311
+ processedchecklistId: 1,
312
+ redoStatus: 1,
313
+ priorityType: 1,
314
+ allowedStoreLocation: 1,
315
+ restrictAttendance: 1,
281
316
  },
282
317
  } );
283
318
 
284
319
  findQuery.push( {
285
320
  $project: {
321
+ _id: 0,
286
322
  checkListName: 1,
287
323
  checkListChar: { $substr: [ '$checkListName', 0, 2 ] },
288
324
  createdByName: 1,
@@ -316,13 +352,18 @@ export const taskInfoTableV1 = async ( req, res ) => {
316
352
  checklistDate: '$date_string',
317
353
  sourceCheckList_id: 1,
318
354
  scheduleEndTime_iso: 1,
355
+ processedchecklistId: 1,
356
+ redoStatus: 1,
357
+ priorityType: 1,
358
+ allowedStoreLocation: 1,
359
+ restrictAttendance: 1,
319
360
  },
320
361
  } );
321
362
 
322
363
  if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
323
364
  findQuery.push( { $sort: { [requestData.sortColumnName]: requestData.sortBy } } );
324
365
  } else {
325
- findQuery.push( { $sort: { ['date_iso']: -1 } } );
366
+ findQuery.push( { $sort: { userEmail: 1 } } );
326
367
  }
327
368
 
328
369
  let limit = parseInt( requestData?.limit ) || 10;
@@ -348,7 +389,8 @@ export const taskInfoTableV1 = async ( req, res ) => {
348
389
 
349
390
  result.totalCount = taskInfoData[0].count[0].total;
350
391
  for ( let i = 0; i < taskInfoData[0].data.length; i++ ) {
351
- taskInfoData[0].data[i].scheduleEndTime_iso = dayjs( taskInfoData[0].data[i].scheduleEndTime_iso ).format( 'DD MMM YYYY' );
392
+ taskInfoData[0].data[i].date = dayjs.utc( taskInfoData[0].data[i].scheduleEndTime_iso ).format();
393
+ taskInfoData[0].data[i].scheduleEndTime_iso = dayjs.utc( taskInfoData[0].data[i].scheduleEndTime_iso ).format( 'DD MMM YYYY' );
352
394
  }
353
395
  result.taskInfo = taskInfoData[0].data;
354
396
  return res.sendSuccess( result );
@@ -689,13 +731,13 @@ export const taskDropdownListV1 = async ( req, res ) => {
689
731
  findQuery.push(
690
732
  {
691
733
  $group: {
692
- _id: '$sourceCheckList_id',
734
+ _id: '$_id',
693
735
  checkListName: { $first: '$checkListName' },
694
736
  checkListType: { $first: '$checkListType' },
695
737
  createdByName: { $first: '$createdByName' },
696
738
  storeCount: { $first: '$storeCount' },
697
- scheduleEndTimeISO: { $first: '$scheduleEndTime_iso' },
698
- submitTime_string: { $first: '$submitTime_string' },
739
+ scheduleEndTimeISO: { $first: '$scheduleEndTimeISO' },
740
+ // submitTime_string: { $first: '$submitTime_string' },
699
741
  },
700
742
  },
701
743
  );
@@ -709,7 +751,7 @@ export const taskDropdownListV1 = async ( req, res ) => {
709
751
  createdByName: 1,
710
752
  storeCount: 1,
711
753
  scheduleEndTimeISO: 1,
712
- submitTime_string: 1,
754
+ // submitTime_string: 1,
713
755
  },
714
756
  } );
715
757
  if ( requestData.sortColumnName && requestData.sortColumnName != '' && requestData.sortBy && requestData.sortBy !='' ) {
@@ -717,7 +759,7 @@ export const taskDropdownListV1 = async ( req, res ) => {
717
759
  } else {
718
760
  findQuery.push( { $sort: { ['checkListNameLowercase']: 1 } } );
719
761
  }
720
- let getChecklistData = await processedTaskService.aggregate( findQuery );
762
+ let getChecklistData = await taskService.aggregate( findQuery );
721
763
  if ( !getChecklistData.length ) {
722
764
  return res.sendError( { error: 'No Data Found' }, 204 );
723
765
  }
@@ -1000,14 +1042,14 @@ export async function taskDetails( req, res ) {
1000
1042
  const exportResult = [];
1001
1043
  for ( let task of taskDetails[0].data ) {
1002
1044
  exportResult.push( {
1003
- 'Task Name': task?.checkListName ||'',
1045
+ 'Task Name': task?.checkListName ||'--',
1004
1046
  'Created by': task?.createdByName ||'--',
1005
- 'Created On': dayjs.utc( task?.publishDate ).format( 'DD MMM, YYYY' ) || '',
1047
+ 'Created On': dayjs.utc( task?.publishDate ).format( 'DD MMM, YYYY' ) || '--',
1006
1048
  'Priority': task?.priorityType || '--',
1007
1049
  'Assigned To': task?.storeCount ||'--',
1008
1050
  'Submitted': task?.submitCount ||'--',
1009
1051
  'Redo': task?.redoCount ||'--',
1010
- 'Due on': dayjs.utc( task?.scheduleEndTime_iso ).format( 'DD MMM, YYYY' ) || '',
1052
+ 'Due on': dayjs.utc( task?.scheduleEndTime_iso ).format( 'DD MMM, YYYY' ) || '--',
1011
1053
  } );
1012
1054
  }
1013
1055
  await download( exportResult, res );
@@ -13,7 +13,7 @@ taskRouter
13
13
  .post( '/upload', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'Task', permissions: [ 'isEdit' ] } ] } ), taskController.validateUser )
14
14
  .post( '/uploadImage', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'Task', permissions: [ 'isEdit' ] } ] } ), taskController.uploadImage )
15
15
  .post( '/config', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'Task', permissions: [ 'isEdit' ] } ] } ), taskController.taskConfig )
16
- .post( '/reinitiate', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'Task', permissions: [ 'isEdit' ] } ] } ), taskController.reinitiateTask )
16
+ .post( '/reinitiate', isAllowedSessionHandler, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'Task', permissions: [ 'isEdit' ] } ] } ), taskController.reinitiateTask )
17
17
  .post( '/checklistTask', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'Task', permissions: [ 'isEdit' ] } ] } ), taskController.createChecklistTask )
18
18
  .post( '/updateApprove', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'Task', permissions: [ 'isEdit' ] } ] } ), taskController.approveTask )
19
19
  .post( '/redo', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'Task', permissions: [ 'isEdit' ] } ] } ), taskController.redoTask )
@@ -5,7 +5,7 @@ export const find = async ( query = {}, field={} ) => {
5
5
  };
6
6
 
7
7
  export const findOne = async ( query = {}, field={} ) => {
8
- return model.taskProcessedConfigModel.findOne( query, field );
8
+ return model.taskProcessedConfigModel.findOne( query, field ).sort( { date_iso: -1 } );
9
9
  };
10
10
 
11
11
  export const updateMany = async ( query = {}, record={} ) => {
@@ -5,7 +5,7 @@ export const find = async ( query = {}, field={} ) => {
5
5
  };
6
6
 
7
7
  export const findOne = async ( query = {}, field={} ) => {
8
- return model.taskProcessedModel.findOne( query, field );
8
+ return model.taskProcessedModel.findOne( query, field ).sort( { date_iso: -1 } );
9
9
  };
10
10
 
11
11
  export const updateMany = async ( query = {}, record={} ) => {
@@ -28,3 +28,7 @@ export const aggregate = async ( query = [] ) => {
28
28
  return model.taskProcessedModel.aggregate( query );
29
29
  };
30
30
 
31
+ export const insert = async ( query = [] ) => {
32
+ return model.taskProcessedModel.create( query );
33
+ };
34
+