tango-app-api-trax 3.8.24 → 3.8.25-nike

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.
@@ -118,7 +118,7 @@ export async function startChecklist( req, res ) {
118
118
  $and: [
119
119
  { _id: new ObjectId( requestData.processedcheckListId ) },
120
120
  { userId: req.user._id },
121
- { date_string: requestData.date },
121
+ { $or: [ { date_string: requestData.date }, { redoStatus: true } ] },
122
122
  ],
123
123
  },
124
124
  } );
@@ -137,7 +137,9 @@ export async function startChecklist( req, res ) {
137
137
  let updateQuery = {};
138
138
  updateQuery._id = new ObjectId( requestData.processedcheckListId );
139
139
  updateQuery.userId = req.user._id;
140
- updateQuery.date_string = requestData.date;
140
+ if ( getBeforeChecklist?.[0]?.date_string == dayjs().format( 'YYYY-MM-DD' ) ) {
141
+ updateQuery.date_string = requestData.date;
142
+ }
141
143
 
142
144
  if ( PCLQusestion && PCLQusestion?.questionAnswers && PCLQusestion?.questionAnswers.length > 0 ) {
143
145
  await PCLQusestion.questionAnswers.forEach( ( section ) => {
@@ -317,7 +319,7 @@ export async function startChecklist( req, res ) {
317
319
  return res.sendError( 'something went wrong please try again', 500 );
318
320
  }
319
321
  } catch ( e ) {
320
- // console.log( 'e =>', e );
322
+ console.log( 'e =>', e );
321
323
  logger.error( { function: 'startChecklist', error: e, body: req.body } );
322
324
  return res.sendError( e, 500 );
323
325
  }
@@ -467,7 +469,7 @@ export async function startTask( req, res ) {
467
469
  } );
468
470
  const getUpdatedTask = await processedTask.aggregate( findQuery );
469
471
 
470
-
472
+ let cdnurl = JSON.parse( process.env.CDNURL );
471
473
  if ( !( task.checkListFrom && task.checkListFrom == 'api' ) ) {
472
474
  // const bucket = JSON.parse( process.env.BUCKET );
473
475
  await Promise.all(
@@ -661,12 +663,12 @@ export async function sopMobilechecklistQuestionValidatorv1( req, res, next ) {
661
663
  let sectionQuestion = requestSection.filter( ( secQuestion ) => secQuestion.qname == question.oldQname || secQuestion.qname == question.qname );
662
664
  if ( sectionQuestion.length ) {
663
665
  if ( requestData.submittype == 'submit' ) {
664
- if ( ( [ 'multipleImage', 'multiplechoicemultiple' ].includes( question.answerType ) || ( question.answerType =='dropdown' && question.allowMultiple ) ) ) {
666
+ if ( ( [ 'multipleImage', 'multiplechoicemultiple', 'image/video' ].includes( question.answerType ) || ( question.answerType =='dropdown' && question.allowMultiple ) ) ) {
665
667
  if ( ( !sectionQuestion[0].linkType || ( sectionQuestion[0].linkType && sectionQuestion[0].linkquestionenabled ) ) && ( sectionQuestion[0].Multianswer == null || sectionQuestion[0].Multianswer == '' || !sectionQuestion[0].Multianswer.length ) ) {
666
668
  validationCount++;
667
669
  }
668
670
  } else {
669
- if ( ( ![ 'multiplechoicemultiple', 'multipleImage' ].includes( question.answerType ) && ( question.answerType =='dropdown' && !question.allowMultiple ) ) && ( ( !sectionQuestion[0].linkType && ( sectionQuestion[0].answer == null || sectionQuestion[0].answer == '' ) ) || ( sectionQuestion[0].linkType && sectionQuestion[0].linkquestionenabled && ( sectionQuestion[0].answer == null || sectionQuestion[0].answer == '' ) ) ) ) {
671
+ if ( ( !sectionQuestion[0].linkType && ( sectionQuestion[0].answer == null || sectionQuestion[0].answer == '' ) ) || ( sectionQuestion[0].linkType && sectionQuestion[0].linkquestionenabled && ( sectionQuestion[0].answer == null || sectionQuestion[0].answer == '' ) ) ) {
670
672
  validationCount++;
671
673
  }
672
674
  }
@@ -1060,17 +1062,546 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
1060
1062
  }
1061
1063
  }
1062
1064
  }
1063
- let sectionData = {
1064
- 'section_id': section.id,
1065
- 'sectionName': section.name,
1066
- 'questions': newArray,
1067
- };
1068
- sectionFormat.push( sectionData );
1065
+ let sectionData = {
1066
+ 'section_id': section.id,
1067
+ 'sectionName': section.name,
1068
+ 'questions': newArray,
1069
+ };
1070
+ sectionFormat.push( sectionData );
1071
+ }
1072
+ }
1073
+
1074
+
1075
+ requestData.questionAnswers = sectionFormat;
1076
+ next();
1077
+ } catch ( error ) {
1078
+ logger.error( { function: 'sopMobilechecklistMultiSectionFormatter', error: error, body: req.body } );
1079
+ return res.sendError( error, 500 );
1080
+ }
1081
+ };
1082
+
1083
+ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next ) {
1084
+ try {
1085
+ let requestData = req.body;
1086
+ requestData.questionAnswers = typeof requestData.questionAnswers == 'string' ? JSON.parse( requestData.questionAnswers ) : requestData.questionAnswers;
1087
+ let getChecklistQA = await processedchecklist.findOne( { _id: new ObjectId( requestData.processedcheckListId ) }, { questionAnswers: 1, redoEdit: 1 } );
1088
+ let reqAnswers = requestData.questionAnswers;
1089
+ logger.error( { functionName: 'updatedPayload', message: reqAnswers } );
1090
+ let CLQAnswers = getChecklistQA.questionAnswers;
1091
+
1092
+ if ( requestData.submittype == 'submit' ) {
1093
+ if ( getChecklistQA?.redoEdit ) {
1094
+ requestData.submittype = 'draft';
1095
+ }
1096
+ reqAnswers.forEach( ( reqA ) => {
1097
+ if ( ![ 'multiplechoicemultiple', 'multipleImage', 'image/video' ].includes( reqA?.answerType ) && ( reqA.answerType == 'dropDown' && !reqA.allowMultiple ) ) {
1098
+ if ( ( !reqA.linkType && ( reqA.answer == null || reqA.answer == '' ) ) || ( reqA.linkType && reqA.linkquestionenabled && ( reqA.answer == null || reqA.answer == '' ) ) ) {
1099
+ return res.sendError( 'Please Fill All Fields', 400 );
1100
+ }
1101
+ }
1102
+ } );
1103
+ }
1104
+
1105
+ if ( requestData?.editSubmit && requestData?.editSubmit == 'true' ) {
1106
+ let sampleData = reqAnswers[0];
1107
+ CLQAnswers.forEach( ( section ) => {
1108
+ let requestSection = reqAnswers.filter( ( reqSection ) => reqSection.sectionName == section?.sectionOldName || reqSection.sectionName == section?.sectionName );
1109
+ if ( requestSection.length ) {
1110
+ requestSection.forEach( ( item ) => item.section_id = section.section_id );
1111
+ section.questions.forEach( ( question ) => {
1112
+ let sectionQuestion = requestSection.filter( ( secQuestion ) => secQuestion.qname == question.oldQname || secQuestion.qname == question.qname );
1113
+ if ( !sectionQuestion.length ) {
1114
+ let data = { ...requestSection[0] };
1115
+ data.answerType = question.answerType;
1116
+ data.qno = question.qno;
1117
+ data.uniqueqno = question.uniqueqno;
1118
+ data.uniqueqid = question.uniqueqid;
1119
+ data.qname = question.qname;
1120
+ data.oldQname = question?.oldQname || question.qname;
1121
+ data.answer = 'null';
1122
+ data.remarks = '';
1123
+ data.validationAnswer = '';
1124
+ data.Multianswer = [];
1125
+ data.linkType = question.linkType;
1126
+ data.linkquestionenabled = question.linkType;
1127
+ data.linkedQuestion = null;
1128
+ data.showLinked = null;
1129
+ data.parentanswer = '';
1130
+ data.dateRangeType = false;
1131
+ reqAnswers.push( data );
1132
+ }
1133
+ } );
1134
+ } else {
1135
+ section.questions.forEach( ( ele ) => {
1136
+ let data = { ...sampleData };
1137
+ data.section_id = section.section_id;
1138
+ data.sectionName = section.sectionName;
1139
+ data.answerType = ele.answerType;
1140
+ data.qno = ele.qno;
1141
+ data.uniqueqno = ele.uniqueqno;
1142
+ data.uniqueqid = ele.uniqueqid;
1143
+ data.qname = ele.qname;
1144
+ data.oldQname = ele?.oldQname || ele.qname;
1145
+ data.answer = 'null';
1146
+ data.remarks = '';
1147
+ data.validationAnswer = '';
1148
+ data.Multianswer = [];
1149
+ data.linkType = ele.linkType;
1150
+ data.linkquestionenabled = ele.linkType;
1151
+ data.linkedQuestion = null;
1152
+ data.showLinked = null;
1153
+ data.parentanswer = '';
1154
+ data.dateRangeType = false;
1155
+ reqAnswers.push( data );
1156
+ } );
1157
+ }
1158
+ } );
1159
+ }
1160
+
1161
+ let sectionFormat = [];
1162
+ let uniqueSections = {};
1163
+ reqAnswers.forEach( ( item ) => {
1164
+ const key = `${item.section_id}-${item.sectionName}`;
1165
+ if ( !uniqueSections[key] ) {
1166
+ uniqueSections[key] = { id: item.section_id, name: item.sectionName };
1167
+ }
1168
+ } );
1169
+ const uniqueArray = Object.values( uniqueSections );
1170
+ for ( let section of uniqueArray ) {
1171
+ let CLQSection = CLQAnswers.find( ( item ) => item.section_id.toString() == section.id.toString() );
1172
+ if ( CLQSection ) {
1173
+ let newArray = [];
1174
+ let qaAnswers = CLQSection.questions;
1175
+ let requestSection = reqAnswers.filter( ( item ) => item.section_id == section.id );
1176
+ for ( let i = 0; i < requestSection.length; i++ ) {
1177
+ for ( let j = 0; j < qaAnswers.length; j++ ) {
1178
+ if ( requestSection[i].qname == qaAnswers[j].oldQname || requestSection[i].qname == qaAnswers[j].qname ) {
1179
+ if ( qaAnswers[j].answerType == 'yes/no' ) {
1180
+ let qaans = JSON.parse( JSON.stringify( qaAnswers[j].answers ) );
1181
+ let yn = [];
1182
+ for ( let k = 0; k < qaans.length; k++ ) {
1183
+ if ( requestSection[i].answer == qaans[k].answer ) {
1184
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1185
+ if ( requestSection[i].validationAnswer ) {
1186
+ let validateAns = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
1187
+ if ( validateAns.length ) {
1188
+ let splitImgUrl = validateAns.split( '/' );
1189
+ if ( splitImgUrl.length > 1 ) {
1190
+ splitImgUrl.splice( 0, 3 );
1191
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1192
+ }
1193
+ }
1194
+ }
1195
+ } else {
1196
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1197
+ qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
1198
+ }
1199
+ if ( requestSection[i]?.runAIData && requestSection[i]?.runAIData?.length ) {
1200
+ qaans[k].runAIData = requestSection[i].runAIData;
1201
+ }
1202
+ yn.push( qaans[k] );
1203
+ }
1204
+ }
1205
+
1206
+ let structure = {};
1207
+ structure.qno = qaAnswers[j].qno;
1208
+ structure.uniqueqno = qaAnswers[j].uniqueqno;
1209
+ structure.uniqueqid = qaAnswers[j].uniqueqid;
1210
+ structure.qname = qaAnswers[j].qname;
1211
+ structure.answerType = qaAnswers[j].answerType;
1212
+ structure.runAI = qaAnswers[j].runAI;
1213
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1214
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1215
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1216
+ structure.remarks = requestSection[i].remarks;
1217
+ }
1218
+ structure.answers = qaAnswers[j].answers;
1219
+ structure.userAnswer = yn;
1220
+ structure.linkType = qaAnswers[j].linkType;
1221
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1222
+ structure.parentanswer = requestSection[i].parentanswer;
1223
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage || '';
1224
+ structure.multiQuestionReferenceImage = qaAnswers[j].multiQuestionReferenceImage || [];
1225
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1226
+ structure.compliance = qaAnswers[j]?.compliance;
1227
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1228
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
1229
+ if ( qaAnswers[j]?.taskId ) {
1230
+ structure.taskId = qaAnswers[j]?.taskId;
1231
+ structure.task = true;
1232
+ }
1233
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1234
+ structure.redo = false;
1235
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
1236
+ structure.redo = qaAnswers[j]?.redo;
1237
+ }
1238
+ if ( qaAnswers[j]?.redoComment ) {
1239
+ structure.redoComment = qaAnswers[j]?.redoComment;
1240
+ };
1241
+ newArray.push( structure );
1242
+ } else if ( qaAnswers[j].answerType == 'multiplechoicesingle' || ( qaAnswers[j].answerType == 'dropdown' && !qaAnswers[j].allowMultiple ) ) {
1243
+ let qaans = JSON.parse( JSON.stringify( qaAnswers[j].answers ) ); ;
1244
+ let ms = [];
1245
+ for ( let k = 0; k < qaans.length; k++ ) {
1246
+ if ( requestSection[i].answer == qaans[k].answer ) {
1247
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1248
+ if ( requestSection[i].validationAnswer ) {
1249
+ let validationAnswer = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
1250
+ if ( validationAnswer.length ) {
1251
+ let splitImgUrl = validationAnswer.split( '/' );
1252
+ if ( splitImgUrl.length > 1 ) {
1253
+ splitImgUrl.splice( 0, 3 );
1254
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1255
+ }
1256
+ }
1257
+ }
1258
+ } else {
1259
+ if ( qaans[k].validationType == 'Capture Multiple Image with description' ) {
1260
+ qaans[k]['validationImage']=[];
1261
+ if ( requestSection[i].validationImage.length ) {
1262
+ for ( let image of requestSection[i].validationImage ) {
1263
+ let validationAnswer = decodeURIComponent( image.split( '?' )[0] );
1264
+ if ( validationAnswer.length ) {
1265
+ let splitImgUrl = validationAnswer.split( '/' );
1266
+ if ( splitImgUrl.length > 1 ) {
1267
+ splitImgUrl.splice( 0, 3 );
1268
+ qaans[k].validationImage.push( splitImgUrl.join( '/' ) || '' );
1269
+ }
1270
+ };
1271
+ }
1272
+ }
1273
+ }
1274
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1275
+ qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
1276
+ }
1277
+ if ( requestSection[i]?.runAIData && requestSection[i]?.runAIData?.length ) {
1278
+ qaans[k].runAIData = requestSection[i].runAIData;
1279
+ }
1280
+ ms.push( qaans[k] );
1281
+ }
1282
+ }
1283
+ let structure = {};
1284
+ structure.qno = qaAnswers[j].qno;
1285
+ structure.uniqueqno = qaAnswers[j].uniqueqno;
1286
+ structure.uniqueqid = qaAnswers[j].uniqueqid;
1287
+ structure.qname = qaAnswers[j].qname;
1288
+ structure.answerType = qaAnswers[j].answerType;
1289
+ structure.runAI = qaAnswers[j].runAI;
1290
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1291
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1292
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1293
+ structure.remarks = requestSection[i].remarks;
1294
+ }
1295
+ structure.answers = qaAnswers[j].answers;
1296
+ structure.userAnswer = ms;
1297
+ structure.linkType = qaAnswers[j].linkType;
1298
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1299
+ structure.parentanswer = requestSection[i].parentanswer;
1300
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage || '';
1301
+ structure.multiQuestionReferenceImage = qaAnswers[j].multiQuestionReferenceImage || [];
1302
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1303
+ structure.allowMultiple = qaAnswers[j].allowMultiple;
1304
+ structure.compliance = qaAnswers[j]?.compliance;
1305
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1306
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
1307
+ if ( qaAnswers[j]?.taskId ) {
1308
+ structure.taskId = qaAnswers[j]?.taskId;
1309
+ structure.task = true;
1310
+ }
1311
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1312
+ structure.redo = false;
1313
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
1314
+ structure.redo = qaAnswers[j]?.redo;
1315
+ }
1316
+ if ( qaAnswers[j]?.redoComment ) {
1317
+ structure.redoComment = qaAnswers[j]?.redoComment;
1318
+ };
1319
+ newArray.push( structure );
1320
+ } else if ( qaAnswers[j].answerType == 'multiplechoicemultiple' || ( qaAnswers[j].answerType == 'dropdown' && qaAnswers[j].allowMultiple ) ) {
1321
+ let qaans = JSON.parse( JSON.stringify( qaAnswers[j].answers ) );
1322
+ let mcmo = [];
1323
+ for ( let k = 0; k < qaans.length; k++ ) {
1324
+ let separatedArray = typeof requestSection[i].Multianswer == 'string' ? JSON.parse( requestSection[i].Multianswer ) : requestSection[i].Multianswer;
1325
+ for ( let s = 0; s < separatedArray.length; s++ ) {
1326
+ if ( ( separatedArray[s].answer == qaans[k].answer ) ) {
1327
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1328
+ if ( separatedArray[s].validationAnswer ) {
1329
+ let validationAnswer = decodeURIComponent( separatedArray[s].validationAnswer.split( '?' )[0] );
1330
+ if ( validationAnswer.length ) {
1331
+ let splitImgUrl = validationAnswer.split( '/' );
1332
+ if ( splitImgUrl.length > 1 ) {
1333
+ splitImgUrl.splice( 0, 3 );
1334
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1335
+ }
1336
+ }
1337
+ }
1338
+ } else {
1339
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1340
+ qaans[k].validationAnswer = separatedArray[s].validationAnswer || '';
1341
+ }
1342
+ if ( separatedArray[s]?.runAIData && separatedArray[s]?.runAIData?.length ) {
1343
+ qaans[k].runAIData = separatedArray[s].runAIData;
1344
+ }
1345
+
1346
+ mcmo.push( qaans[k] );
1347
+ }
1348
+ }
1349
+ }
1350
+
1351
+ qaAnswers[j].answers.forEach( ( ele ) => {
1352
+ if ( typeof ele?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1353
+ ele.redo = false;
1354
+ }
1355
+ } );
1356
+
1357
+ let structure = {};
1358
+ structure.qno = qaAnswers[j].qno;
1359
+ structure.uniqueqno = qaAnswers[j].uniqueqno;
1360
+ structure.qno = qaAnswers[j].qno;
1361
+ structure.qname = qaAnswers[j].qname;
1362
+ structure.answerType = qaAnswers[j].answerType;
1363
+ structure.runAI = qaAnswers[j].runAI;
1364
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1365
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1366
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1367
+ structure.remarks = requestSection[i].remarks;
1368
+ }
1369
+ structure.answers = qaAnswers[j].answers;
1370
+ structure.userAnswer = mcmo;
1371
+ structure.linkType = qaAnswers[j].linkType;
1372
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1373
+ structure.parentanswer = requestSection[i].parentanswer;
1374
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage || '';
1375
+ structure.multiQuestionReferenceImage = qaAnswers[j].multiQuestionReferenceImage || [];
1376
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1377
+ structure.allowMultiple = qaAnswers[j].allowMultiple;
1378
+ structure.compliance = qaAnswers[j]?.compliance;
1379
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1380
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
1381
+ if ( qaAnswers[j]?.taskId ) {
1382
+ structure.taskId = qaAnswers[j]?.taskId;
1383
+ structure.task = true;
1384
+ }
1385
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1386
+ structure.redo = false;
1387
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
1388
+ structure.redo = qaAnswers[j]?.redo;
1389
+ }
1390
+ if ( qaAnswers[j]?.redoComment ) {
1391
+ structure.redoComment = qaAnswers[j]?.redoComment;
1392
+ };
1393
+ newArray.push( structure );
1394
+ } else if ( [ 'image/video', 'multipleImage' ].includes( qaAnswers[j].answerType ) ) {
1395
+ let separatedArray = typeof requestSection[i].Multianswer == 'string' ? JSON.parse( requestSection[i].Multianswer ) : requestSection[i].Multianswer;
1396
+ let mcmi = [];
1397
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1398
+ // for (let k = 0; k < qaans.length; k++) {
1399
+ for ( let s = 0; s < separatedArray.length; s++ ) {
1400
+ if ( separatedArray[s].answer && separatedArray[s].answer !='' ) {
1401
+ let newAnswer = {};
1402
+ let validationAnswer = decodeURIComponent( separatedArray[s].answer.split( '?' )[0] );
1403
+ if ( validationAnswer.length ) {
1404
+ let splitImgUrl = validationAnswer.split( '/' );
1405
+ if ( splitImgUrl.length > 1 ) {
1406
+ splitImgUrl.splice( 0, 3 );
1407
+ newAnswer.answer = splitImgUrl.join( '/' ) || '';
1408
+ }
1409
+ }
1410
+
1411
+ newAnswer.answeroptionNumber = 0;
1412
+ newAnswer.sopFlag = false;
1413
+ newAnswer.validation = false;
1414
+ newAnswer.validationType = '';
1415
+ newAnswer.referenceImage = '';
1416
+ newAnswer.allowUploadfromGallery = false;
1417
+ newAnswer.descriptivetype = '';
1418
+ newAnswer.showLinked = false;
1419
+ newAnswer.linkedQuestion = 0;
1420
+ newAnswer.nestedQuestion = [];
1421
+ newAnswer.index = s;
1422
+ newAnswer.runAI = qaAnswers[j].answers[0]?.runAI || false;
1423
+ newAnswer.runAIFeatures = qaAnswers[j].answers[0]?.runAIFeatures || [];
1424
+ newAnswer.runAIDescription = qaAnswers[j].answers[0]?.runAIDescription || '';
1425
+ if ( qaAnswers[j].answerType == 'image/video' ) {
1426
+ newAnswer.answerType = separatedArray[s].answerType;
1427
+ }
1428
+ if ( separatedArray[s]?.runAIData && separatedArray[s]?.runAIData?.length ) {
1429
+ newAnswer.runAIData = separatedArray[s].runAIData;
1430
+ }
1431
+ mcmi.push( newAnswer );
1432
+ }
1433
+ }
1434
+ }
1435
+ // }
1436
+ let structure = {};
1437
+ structure.qno = qaAnswers[j].qno;
1438
+ structure.uniqueqno = qaAnswers[j].uniqueqno;
1439
+ structure.uniqueqid = qaAnswers[j].uniqueqid;
1440
+ structure.qname = qaAnswers[j].qname;
1441
+ structure.answerType = qaAnswers[j].answerType;
1442
+ structure.runAI = qaAnswers[j].runAI;
1443
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1444
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1445
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1446
+ structure.remarks = requestSection[i].remarks;
1447
+ }
1448
+ structure.answers = qaAnswers[j].answers;
1449
+ structure.userAnswer = mcmi;
1450
+ structure.linkType = qaAnswers[j].linkType;
1451
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1452
+ structure.parentanswer = requestSection[i].parentanswer;
1453
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage || '';
1454
+ structure.multiQuestionReferenceImage = qaAnswers[j].multiQuestionReferenceImage || [];
1455
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1456
+ structure.compliance = qaAnswers[j]?.compliance;
1457
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1458
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
1459
+ if ( qaAnswers[j]?.taskId ) {
1460
+ structure.taskId = qaAnswers[j]?.taskId;
1461
+ structure.task = true;
1462
+ }
1463
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1464
+ structure.redo = false;
1465
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
1466
+ structure.redo = qaAnswers[j]?.redo;
1467
+ }
1468
+ if ( qaAnswers[j]?.redoComment ) {
1469
+ structure.redoComment = qaAnswers[j]?.redoComment;
1470
+ };
1471
+ newArray.push( structure );
1472
+ } else {
1473
+ let des = [];
1474
+ if ( requestSection[i].answer != 'null' && requestSection[i].answer != '' && requestSection[i].answer != null ) {
1475
+ let validationAnswer = '';
1476
+ if ( requestSection[i].answer.split( '?' ).length > 1 || requestSection[i].answer.includes( 'https://' ) ) {
1477
+ validationAnswer = decodeURIComponent( requestSection[i].answer.split( '?' )[0] );
1478
+ }
1479
+ if ( validationAnswer.length ) {
1480
+ let splitImgUrl = validationAnswer.split( '/' );
1481
+ if ( splitImgUrl.length > 1 ) {
1482
+ splitImgUrl.splice( 0, 3 );
1483
+ requestSection[i].answer = splitImgUrl.join( '/' );
1484
+ }
1485
+ }
1486
+ let ansstructure = {
1487
+ answer: requestSection[i].answer,
1488
+ answeroptionNumber: 1,
1489
+ sopFlag: false,
1490
+ validation: false,
1491
+ validationType: '',
1492
+ validationAnswer: '',
1493
+ referenceImage: qaAnswers[j].answers[0].referenceImage || '',
1494
+ multiReferenceImage: qaAnswers[j].answers[0].multiReferenceImage || [],
1495
+ showLinked: qaAnswers[j].answers[0].showLinked,
1496
+ linkedQuestion: qaAnswers[j].answers[0].linkedQuestion,
1497
+ runAI: qaAnswers[j].answers[0]?.runAI || false,
1498
+ runAIFeatures: qaAnswers[j]?.answers[0]?.runAIFeatures || [],
1499
+ runAIDescription: qaAnswers[j].answers[0]?.runAIDescription || '',
1500
+ };
1501
+ if ( qaAnswers[j].answerType == 'date' ) {
1502
+ ansstructure.dateRangeType = requestSection[i].dateRangeType || false;
1503
+ if ( qaAnswers[j].compliance ) {
1504
+ if ( qaAnswers[j].compliance ) {
1505
+ let ansArray = requestSection[i].answer.split( ',' );
1506
+ if ( ansArray.includes( '-' ) ) {
1507
+ ansArray = requestSection[i].answer.split( '-' );
1508
+ }
1509
+ if ( ansArray.length > 1 && ansArray.some( ( date ) => dayjs( date, 'DD MMM YYYY' ).format( 'DD-MM-YYYY' ) >= qaAnswers[j]?.answers[0]?.startDate && dayjs( date, 'DD MMM YYYY' ) <= qaAnswers[j]?.answers[0]?.endDate ) ) {
1510
+ ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.matchedCount;
1511
+ } else if ( ansArray.length > 1 && ( dayjs( ansArray?.[0], 'DD MMM YYYY' ).format( 'DD-MM-YYYY' ) >= qaAnswers[j]?.answers[0]?.startDate && dayjs( ansArray?.[1], 'DD MMM YYYY' ).format( 'DD-MM-YYYY' ) <= qaAnswers[j]?.answers[0]?.endDate ) ) {
1512
+ ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.matchedCount;
1513
+ } else if ( dayjs( requestSection[i].answer, 'DD MMM YYYY' ) >= qaAnswers[j]?.answers[0]?.startDate && dayjs( requestSection[i].answer, 'DD MMM YYYY' ) <= qaAnswers[j]?.answers[0]?.endDate ) {
1514
+ ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.matchedCount;
1515
+ } else {
1516
+ ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.notMatchedCount;
1517
+ }
1518
+ }
1519
+ }
1520
+ }
1521
+ if ( qaAnswers[j].answerType == 'linearscale' ) {
1522
+ if ( qaAnswers[j].compliance ) {
1523
+ let linearAnswer = requestSection[i].answer.split( 'to' );
1524
+ if ( parseInt( linearAnswer?.[0] ) >= qaAnswers[j]?.answers[0]?.minValue && parseInt( linearAnswer?.[1] ) <= qaAnswers[j]?.answers[0]?.maxValue ) {
1525
+ ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.matchedCount;
1526
+ } else {
1527
+ ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.notMatchedCount;
1528
+ }
1529
+ }
1530
+ ansstructure.linearType = qaAnswers[j]?.answers[0]?.linearType;
1531
+ ansstructure.matchedCount = qaAnswers[j]?.answers[0]?.matchedCount;
1532
+ ansstructure.notMatchedCount = qaAnswers[j]?.answers[0]?.notMatchedCount;
1533
+ }
1534
+ if ( qaAnswers[j].answerType == 'image' ) {
1535
+ ansstructure.matchedCount = qaAnswers[j]?.answers[0]?.matchedCount;
1536
+ ansstructure.notMatchedCount = qaAnswers[j]?.answers[0]?.notMatchedCount;
1537
+ }
1538
+ if ( requestSection[i]?.runAIData && requestSection[i]?.runAIData?.length ) {
1539
+ ansstructure.runAIData = requestSection[i].runAIData;
1540
+ }
1541
+ des.push( ansstructure );
1542
+ }
1543
+ let structure = {};
1544
+ structure.qno = qaAnswers[j].qno;
1545
+ structure.uniqueqno = qaAnswers[j].uniqueqno;
1546
+ structure.uniqueqid = qaAnswers[j].uniqueqid;
1547
+ structure.qname = qaAnswers[j].qname;
1548
+ structure.answerType = qaAnswers[j].answerType;
1549
+ structure.runAI = qaAnswers[j].runAI;
1550
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1551
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1552
+ structure.remarks = requestSection[i].remarks;
1553
+ structure.answers = qaAnswers[j].answers;
1554
+ structure.userAnswer = des;
1555
+ structure.linkType = qaAnswers[j].linkType;
1556
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1557
+ structure.parentanswer = requestSection[i].parentanswer;
1558
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage || '';
1559
+ structure.multiQuestionReferenceImage = qaAnswers[j].multiQuestionReferenceImage || [];
1560
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1561
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1562
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
1563
+ structure.compliance = qaAnswers[j]?.compliance;
1564
+ if ( qaAnswers[j]?.taskId ) {
1565
+ structure.taskId = qaAnswers[j]?.taskId;
1566
+ structure.task = true;
1567
+ }
1568
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1569
+ structure.redo = false;
1570
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
1571
+ structure.redo = qaAnswers[j]?.redo;
1572
+ }
1573
+ if ( qaAnswers[j]?.redoComment ) {
1574
+ structure.redoComment = qaAnswers[j]?.redoComment;
1575
+ };
1576
+ newArray.push( structure );
1577
+ }
1578
+ }
1579
+ }
1580
+ }
1581
+ let sectionData = {
1582
+ 'section_id': section.id,
1583
+ 'sectionName': section.name,
1584
+ 'questions': newArray,
1585
+ };
1586
+ sectionFormat.push( sectionData );
1587
+ }
1588
+ }
1589
+
1590
+
1591
+ requestData.questionAnswers = sectionFormat;
1592
+ logger.error( { message: requestData.questionAnswers, error: 'QuestionanswersPayload' } );
1593
+
1594
+ if ( requestData.submittype == 'submit' ) {
1595
+ for ( let section of sectionFormat ) {
1596
+ for ( let question of section.questions ) {
1597
+ let mustValidate = !question.linkType || ( question.linkType && question.linkquestionenabled );
1598
+ if ( mustValidate && ( !question.userAnswer || !question.userAnswer.length ) ) {
1599
+ return res.sendError( 'Please Fill All Fields', 400 );
1600
+ }
1601
+ }
1069
1602
  }
1070
1603
  }
1071
1604
 
1072
-
1073
- requestData.questionAnswers = sectionFormat;
1074
1605
  next();
1075
1606
  } catch ( error ) {
1076
1607
  logger.error( { function: 'sopMobilechecklistMultiSectionFormatter', error: error, body: req.body } );
@@ -1078,16 +1609,19 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
1078
1609
  }
1079
1610
  };
1080
1611
 
1081
- export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next ) {
1612
+ export async function sopMobilechecklistMultiSectionFormatterv2( req, res, next ) {
1082
1613
  try {
1083
1614
  let requestData = req.body;
1084
1615
  requestData.questionAnswers = typeof requestData.questionAnswers == 'string' ? JSON.parse( requestData.questionAnswers ) : requestData.questionAnswers;
1085
- let getChecklistQA = await processedchecklist.findOne( { _id: new ObjectId( requestData.processedcheckListId ) }, { questionAnswers: 1 } );
1616
+ let getChecklistQA = await processedchecklist.findOne( { _id: new ObjectId( requestData.processedcheckListId ) }, { questionAnswers: 1, redoEdit: 1 } );
1086
1617
  let reqAnswers = requestData.questionAnswers;
1087
1618
  logger.error( { functionName: 'updatedPayload', message: reqAnswers } );
1088
1619
  let CLQAnswers = getChecklistQA.questionAnswers;
1089
1620
 
1090
1621
  if ( requestData.submittype == 'submit' ) {
1622
+ if ( getChecklistQA?.redoEdit ) {
1623
+ requestData.submittype = 'draft';
1624
+ }
1091
1625
  reqAnswers.forEach( ( reqA ) => {
1092
1626
  if ( ![ 'multiplechoicemultiple', 'multipleImage', 'image/video' ].includes( reqA?.answerType ) && ( reqA.answerType == 'dropDown' && !reqA.allowMultiple ) ) {
1093
1627
  if ( ( !reqA.linkType && ( reqA.answer == null || reqA.answer == '' ) ) || ( reqA.linkType && reqA.linkquestionenabled && ( reqA.answer == null || reqA.answer == '' ) ) ) {
@@ -1172,26 +1706,31 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1172
1706
  for ( let j = 0; j < qaAnswers.length; j++ ) {
1173
1707
  if ( requestSection[i].qname == qaAnswers[j].oldQname || requestSection[i].qname == qaAnswers[j].qname ) {
1174
1708
  if ( qaAnswers[j].answerType == 'yes/no' ) {
1175
- let qaans = qaAnswers[j].answers;
1709
+ let qaans = JSON.parse( JSON.stringify( qaAnswers[j].answers ) );
1176
1710
  let yn = [];
1177
- for ( let k = 0; k < qaans.length; k++ ) {
1178
- if ( requestSection[i].answer == qaans[k].answer ) {
1179
- if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1180
- if ( requestSection[i].validationAnswer ) {
1181
- let validateAns = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
1182
- if ( validateAns.length ) {
1183
- let splitImgUrl = validateAns.split( '/' );
1184
- if ( splitImgUrl.length > 1 ) {
1185
- splitImgUrl.splice( 0, 3 );
1186
- qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1711
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1712
+ for ( let k = 0; k < qaans.length; k++ ) {
1713
+ if ( requestSection[i].answer == qaans[k].answer ) {
1714
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1715
+ if ( requestSection[i].validationAnswer ) {
1716
+ let validateAns = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
1717
+ if ( validateAns.length ) {
1718
+ let splitImgUrl = validateAns.split( '/' );
1719
+ if ( splitImgUrl.length > 1 ) {
1720
+ splitImgUrl.splice( 0, 3 );
1721
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1722
+ }
1187
1723
  }
1188
1724
  }
1725
+ } else {
1726
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1727
+ qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
1189
1728
  }
1190
- } else {
1191
- // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1192
- qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
1729
+ if ( requestSection[i]?.runAIData && requestSection[i]?.runAIData?.length ) {
1730
+ qaans[k].runAIData = requestSection[i].runAIData;
1731
+ }
1732
+ yn.push( qaans[k] );
1193
1733
  }
1194
- yn.push( qaans[k] );
1195
1734
  }
1196
1735
  }
1197
1736
  let structure = {};
@@ -1203,7 +1742,9 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1203
1742
  structure.runAI = qaAnswers[j].runAI;
1204
1743
  structure.runAIDescription = qaAnswers[j].runAIDescription;
1205
1744
  structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1206
- structure.remarks = requestSection[i].remarks;
1745
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1746
+ structure.remarks = requestSection[i].remarks;
1747
+ }
1207
1748
  structure.answers = qaAnswers[j].answers;
1208
1749
  structure.userAnswer = yn;
1209
1750
  structure.linkType = qaAnswers[j].linkType;
@@ -1229,41 +1770,46 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1229
1770
  };
1230
1771
  newArray.push( structure );
1231
1772
  } else if ( qaAnswers[j].answerType == 'multiplechoicesingle' || ( qaAnswers[j].answerType == 'dropdown' && !qaAnswers[j].allowMultiple ) ) {
1232
- let qaans = qaAnswers[j].answers;
1773
+ let qaans = JSON.parse( JSON.stringify( qaAnswers[j].answers ) ); ;
1233
1774
  let ms = [];
1234
- for ( let k = 0; k < qaans.length; k++ ) {
1235
- if ( requestSection[i].answer == qaans[k].answer ) {
1236
- if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1237
- if ( requestSection[i].validationAnswer ) {
1238
- let validationAnswer = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
1239
- if ( validationAnswer.length ) {
1240
- let splitImgUrl = validationAnswer.split( '/' );
1241
- if ( splitImgUrl.length > 1 ) {
1242
- splitImgUrl.splice( 0, 3 );
1243
- qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1775
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1776
+ for ( let k = 0; k < qaans.length; k++ ) {
1777
+ if ( requestSection[i].answer == qaans[k].answer ) {
1778
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1779
+ if ( requestSection[i].validationAnswer ) {
1780
+ let validationAnswer = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
1781
+ if ( validationAnswer.length ) {
1782
+ let splitImgUrl = validationAnswer.split( '/' );
1783
+ if ( splitImgUrl.length > 1 ) {
1784
+ splitImgUrl.splice( 0, 3 );
1785
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1786
+ }
1244
1787
  }
1245
1788
  }
1246
- }
1247
- } else {
1248
- if ( qaans[k].validationType == 'Capture Multiple Image with description' ) {
1249
- qaans[k]['validationImage']=[];
1250
- if ( requestSection[i].validationImage.length ) {
1251
- for ( let image of requestSection[i].validationImage ) {
1252
- let validationAnswer = decodeURIComponent( image.split( '?' )[0] );
1253
- if ( validationAnswer.length ) {
1254
- let splitImgUrl = validationAnswer.split( '/' );
1255
- if ( splitImgUrl.length > 1 ) {
1256
- splitImgUrl.splice( 0, 3 );
1257
- qaans[k].validationImage.push( splitImgUrl.join( '/' ) || '' );
1258
- }
1259
- };
1789
+ } else {
1790
+ if ( qaans[k].validationType == 'Capture Multiple Image with description' ) {
1791
+ qaans[k]['validationImage']=[];
1792
+ if ( requestSection[i].validationImage.length ) {
1793
+ for ( let image of requestSection[i].validationImage ) {
1794
+ let validationAnswer = decodeURIComponent( image.split( '?' )[0] );
1795
+ if ( validationAnswer.length ) {
1796
+ let splitImgUrl = validationAnswer.split( '/' );
1797
+ if ( splitImgUrl.length > 1 ) {
1798
+ splitImgUrl.splice( 0, 3 );
1799
+ qaans[k].validationImage.push( splitImgUrl.join( '/' ) || '' );
1800
+ }
1801
+ };
1802
+ }
1260
1803
  }
1261
1804
  }
1805
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1806
+ qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
1262
1807
  }
1263
- // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1264
- qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
1808
+ if ( requestSection[i]?.runAIData && requestSection[i]?.runAIData?.length ) {
1809
+ qaans[k].runAIData = requestSection[i].runAIData;
1810
+ }
1811
+ ms.push( qaans[k] );
1265
1812
  }
1266
- ms.push( qaans[k] );
1267
1813
  }
1268
1814
  }
1269
1815
  let structure = {};
@@ -1275,7 +1821,9 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1275
1821
  structure.runAI = qaAnswers[j].runAI;
1276
1822
  structure.runAIDescription = qaAnswers[j].runAIDescription;
1277
1823
  structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1278
- structure.remarks = requestSection[i].remarks;
1824
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1825
+ structure.remarks = requestSection[i].remarks;
1826
+ }
1279
1827
  structure.answers = qaAnswers[j].answers;
1280
1828
  structure.userAnswer = ms;
1281
1829
  structure.linkType = qaAnswers[j].linkType;
@@ -1302,32 +1850,44 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1302
1850
  };
1303
1851
  newArray.push( structure );
1304
1852
  } else if ( qaAnswers[j].answerType == 'multiplechoicemultiple' || ( qaAnswers[j].answerType == 'dropdown' && qaAnswers[j].allowMultiple ) ) {
1305
- let qaans = qaAnswers[j].answers;
1853
+ let qaans = JSON.parse( JSON.stringify( qaAnswers[j].answers ) );
1306
1854
  let mcmo = [];
1307
- for ( let k = 0; k < qaans.length; k++ ) {
1308
- let separatedArray = typeof requestSection[i].Multianswer == 'string' ? JSON.parse( requestSection[i].Multianswer ) : requestSection[i].Multianswer;
1309
- for ( let s = 0; s < separatedArray.length; s++ ) {
1310
- if ( separatedArray[s].answer == qaans[k].answer ) {
1311
- if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1312
- if ( separatedArray[s].validationAnswer ) {
1313
- let validationAnswer = decodeURIComponent( separatedArray[s].validationAnswer.split( '?' )[0] );
1314
- if ( validationAnswer.length ) {
1315
- let splitImgUrl = validationAnswer.split( '/' );
1316
- if ( splitImgUrl.length > 1 ) {
1317
- splitImgUrl.splice( 0, 3 );
1318
- qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1855
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1856
+ for ( let k = 0; k < qaans.length; k++ ) {
1857
+ let separatedArray = typeof requestSection[i].Multianswer == 'string' ? JSON.parse( requestSection[i].Multianswer ) : requestSection[i].Multianswer;
1858
+ for ( let s = 0; s < separatedArray.length; s++ ) {
1859
+ if ( ( separatedArray[s].answer == qaans[k].answer ) ) {
1860
+ if ( ( getChecklistQA?.redoEdit && qaans[k].redo == separatedArray[s].redo ) || !getChecklistQA?.redoEdit ) {
1861
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1862
+ if ( separatedArray[s].validationAnswer ) {
1863
+ let validationAnswer = decodeURIComponent( separatedArray[s].validationAnswer.split( '?' )[0] );
1864
+ if ( validationAnswer.length ) {
1865
+ let splitImgUrl = validationAnswer.split( '/' );
1866
+ if ( splitImgUrl.length > 1 ) {
1867
+ splitImgUrl.splice( 0, 3 );
1868
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1869
+ }
1870
+ }
1319
1871
  }
1872
+ } else {
1873
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1874
+ qaans[k].validationAnswer = separatedArray[s].validationAnswer || '';
1875
+ }
1876
+ if ( separatedArray[s]?.runAIData && separatedArray[s]?.runAIData?.length ) {
1877
+ qaans[k].runAIData = separatedArray[s].runAIData;
1320
1878
  }
1321
1879
  }
1322
- } else {
1323
- // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1324
- qaans[k].validationAnswer = separatedArray[s].validationAnswer || '';
1880
+ mcmo.push( qaans[k] );
1325
1881
  }
1326
-
1327
- mcmo.push( qaans[k] );
1328
1882
  }
1329
1883
  }
1330
1884
  }
1885
+ qaAnswers[j].answers.forEach( ( ele ) => {
1886
+ if ( typeof ele?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1887
+ ele.redo = false;
1888
+ }
1889
+ } );
1890
+
1331
1891
  let structure = {};
1332
1892
  structure.qno = qaAnswers[j].qno;
1333
1893
  structure.uniqueqno = qaAnswers[j].uniqueqno;
@@ -1337,7 +1897,9 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1337
1897
  structure.runAI = qaAnswers[j].runAI;
1338
1898
  structure.runAIDescription = qaAnswers[j].runAIDescription;
1339
1899
  structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1340
- structure.remarks = requestSection[i].remarks;
1900
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1901
+ structure.remarks = requestSection[i].remarks;
1902
+ }
1341
1903
  structure.answers = qaAnswers[j].answers;
1342
1904
  structure.userAnswer = mcmo;
1343
1905
  structure.linkType = qaAnswers[j].linkType;
@@ -1366,37 +1928,42 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1366
1928
  } else if ( [ 'image/video', 'multipleImage' ].includes( qaAnswers[j].answerType ) ) {
1367
1929
  let separatedArray = typeof requestSection[i].Multianswer == 'string' ? JSON.parse( requestSection[i].Multianswer ) : requestSection[i].Multianswer;
1368
1930
  let mcmi = [];
1931
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1369
1932
  // for (let k = 0; k < qaans.length; k++) {
1370
- for ( let s = 0; s < separatedArray.length; s++ ) {
1371
- if ( separatedArray[s].answer && separatedArray[s].answer !='' ) {
1372
- let newAnswer = {};
1373
- let validationAnswer = decodeURIComponent( separatedArray[s].answer.split( '?' )[0] );
1374
- if ( validationAnswer.length ) {
1375
- let splitImgUrl = validationAnswer.split( '/' );
1376
- if ( splitImgUrl.length > 1 ) {
1377
- splitImgUrl.splice( 0, 3 );
1378
- newAnswer.answer = splitImgUrl.join( '/' ) || '';
1933
+ for ( let s = 0; s < separatedArray.length; s++ ) {
1934
+ if ( separatedArray[s].answer && separatedArray[s].answer !='' ) {
1935
+ let newAnswer = {};
1936
+ let validationAnswer = decodeURIComponent( separatedArray[s].answer.split( '?' )[0] );
1937
+ if ( validationAnswer.length ) {
1938
+ let splitImgUrl = validationAnswer.split( '/' );
1939
+ if ( splitImgUrl.length > 1 ) {
1940
+ splitImgUrl.splice( 0, 3 );
1941
+ newAnswer.answer = splitImgUrl.join( '/' ) || '';
1942
+ }
1379
1943
  }
1380
- }
1381
1944
 
1382
- newAnswer.answeroptionNumber = 0;
1383
- newAnswer.sopFlag = false;
1384
- newAnswer.validation = false;
1385
- newAnswer.validationType = '';
1386
- newAnswer.referenceImage = '';
1387
- newAnswer.allowUploadfromGallery = false;
1388
- newAnswer.descriptivetype = '';
1389
- newAnswer.showLinked = false;
1390
- newAnswer.linkedQuestion = 0;
1391
- newAnswer.nestedQuestion = [];
1392
- newAnswer.index = s;
1393
- newAnswer.runAI = qaAnswers[j].answers[0]?.runAI || false;
1394
- newAnswer.runAIFeatures = qaAnswers[j].answers[0]?.runAIFeatures || [];
1395
- newAnswer.runAIDescription = qaAnswers[j].answers[0]?.runAIDescription || '';
1396
- if ( qaAnswers[j].answerType == 'image/video' ) {
1397
- newAnswer.answerType = separatedArray[s].answerType;
1945
+ newAnswer.answeroptionNumber = 0;
1946
+ newAnswer.sopFlag = false;
1947
+ newAnswer.validation = false;
1948
+ newAnswer.validationType = '';
1949
+ newAnswer.referenceImage = '';
1950
+ newAnswer.allowUploadfromGallery = false;
1951
+ newAnswer.descriptivetype = '';
1952
+ newAnswer.showLinked = false;
1953
+ newAnswer.linkedQuestion = 0;
1954
+ newAnswer.nestedQuestion = [];
1955
+ newAnswer.index = s;
1956
+ newAnswer.runAI = qaAnswers[j].answers[0]?.runAI || false;
1957
+ newAnswer.runAIFeatures = qaAnswers[j].answers[0]?.runAIFeatures || [];
1958
+ newAnswer.runAIDescription = qaAnswers[j].answers[0]?.runAIDescription || '';
1959
+ if ( qaAnswers[j].answerType == 'image/video' ) {
1960
+ newAnswer.answerType = separatedArray[s].answerType;
1961
+ }
1962
+ if ( separatedArray[s]?.runAIData && separatedArray[s]?.runAIData?.length ) {
1963
+ newAnswer.runAIData = separatedArray[s].runAIData;
1964
+ }
1965
+ mcmi.push( newAnswer );
1398
1966
  }
1399
- mcmi.push( newAnswer );
1400
1967
  }
1401
1968
  }
1402
1969
  // }
@@ -1409,7 +1976,9 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1409
1976
  structure.runAI = qaAnswers[j].runAI;
1410
1977
  structure.runAIDescription = qaAnswers[j].runAIDescription;
1411
1978
  structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1412
- structure.remarks = requestSection[i].remarks;
1979
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
1980
+ structure.remarks = requestSection[i].remarks;
1981
+ }
1413
1982
  structure.answers = qaAnswers[j].answers;
1414
1983
  structure.userAnswer = mcmi;
1415
1984
  structure.linkType = qaAnswers[j].linkType;
@@ -1436,71 +2005,76 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1436
2005
  newArray.push( structure );
1437
2006
  } else {
1438
2007
  let des = [];
1439
- if ( requestSection[i].answer != 'null' && requestSection[i].answer != '' && requestSection[i].answer != null ) {
1440
- let validationAnswer = '';
1441
- if ( requestSection[i].answer.split( '?' ).length > 1 ) {
1442
- validationAnswer = decodeURIComponent( requestSection[i].answer.split( '?' )[0] );
1443
- }
1444
- if ( validationAnswer.length ) {
1445
- let splitImgUrl = validationAnswer.split( '/' );
1446
- if ( splitImgUrl.length > 1 ) {
1447
- splitImgUrl.splice( 0, 3 );
1448
- requestSection[i].answer = splitImgUrl.join( '/' );
2008
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
2009
+ if ( requestSection[i].answer != 'null' && requestSection[i].answer != '' && requestSection[i].answer != null ) {
2010
+ let validationAnswer = '';
2011
+ if ( requestSection[i].answer.split( '?' ).length > 1 || requestSection[i].answer.includes( 'https://' ) ) {
2012
+ validationAnswer = decodeURIComponent( requestSection[i].answer.split( '?' )[0] );
1449
2013
  }
1450
- }
1451
- let ansstructure = {
1452
- answer: requestSection[i].answer,
1453
- answeroptionNumber: 1,
1454
- sopFlag: false,
1455
- validation: false,
1456
- validationType: '',
1457
- validationAnswer: '',
1458
- referenceImage: qaAnswers[j].answers[0].referenceImage || '',
1459
- multiReferenceImage: qaAnswers[j].answers[0].multiReferenceImage || [],
1460
- showLinked: qaAnswers[j].answers[0].showLinked,
1461
- linkedQuestion: qaAnswers[j].answers[0].linkedQuestion,
1462
- runAI: qaAnswers[j].answers[0]?.runAI || false,
1463
- runAIFeatures: qaAnswers[j]?.answers[0]?.runAIFeatures || [],
1464
- runAIDescription: qaAnswers[j].answers[0]?.runAIDescription || '',
1465
- };
1466
- if ( qaAnswers[j].answerType == 'date' ) {
1467
- ansstructure.dateRangeType = requestSection[i].dateRangeType || false;
1468
- if ( qaAnswers[j].compliance ) {
2014
+ if ( validationAnswer.length ) {
2015
+ let splitImgUrl = validationAnswer.split( '/' );
2016
+ if ( splitImgUrl.length > 1 ) {
2017
+ splitImgUrl.splice( 0, 3 );
2018
+ requestSection[i].answer = splitImgUrl.join( '/' );
2019
+ }
2020
+ }
2021
+ let ansstructure = {
2022
+ answer: requestSection[i].answer,
2023
+ answeroptionNumber: 1,
2024
+ sopFlag: false,
2025
+ validation: false,
2026
+ validationType: '',
2027
+ validationAnswer: '',
2028
+ referenceImage: qaAnswers[j].answers[0].referenceImage || '',
2029
+ multiReferenceImage: qaAnswers[j].answers[0].multiReferenceImage || [],
2030
+ showLinked: qaAnswers[j].answers[0].showLinked,
2031
+ linkedQuestion: qaAnswers[j].answers[0].linkedQuestion,
2032
+ runAI: qaAnswers[j].answers[0]?.runAI || false,
2033
+ runAIFeatures: qaAnswers[j]?.answers[0]?.runAIFeatures || [],
2034
+ runAIDescription: qaAnswers[j].answers[0]?.runAIDescription || '',
2035
+ };
2036
+ if ( qaAnswers[j].answerType == 'date' ) {
2037
+ ansstructure.dateRangeType = requestSection[i].dateRangeType || false;
1469
2038
  if ( qaAnswers[j].compliance ) {
1470
- let ansArray = requestSection[i].answer.split( ',' );
1471
- if ( ansArray.includes( '-' ) ) {
1472
- ansArray = requestSection[i].answer.split( '-' );
2039
+ if ( qaAnswers[j].compliance ) {
2040
+ let ansArray = requestSection[i].answer.split( ',' );
2041
+ if ( ansArray.includes( '-' ) ) {
2042
+ ansArray = requestSection[i].answer.split( '-' );
2043
+ }
2044
+ if ( ansArray.length > 1 && ansArray.some( ( date ) => dayjs( date, 'DD MMM YYYY' ).format( 'DD-MM-YYYY' ) >= qaAnswers[j]?.answers[0]?.startDate && dayjs( date, 'DD MMM YYYY' ) <= qaAnswers[j]?.answers[0]?.endDate ) ) {
2045
+ ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.matchedCount;
2046
+ } else if ( ansArray.length > 1 && ( dayjs( ansArray?.[0], 'DD MMM YYYY' ).format( 'DD-MM-YYYY' ) >= qaAnswers[j]?.answers[0]?.startDate && dayjs( ansArray?.[1], 'DD MMM YYYY' ).format( 'DD-MM-YYYY' ) <= qaAnswers[j]?.answers[0]?.endDate ) ) {
2047
+ ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.matchedCount;
2048
+ } else if ( dayjs( requestSection[i].answer, 'DD MMM YYYY' ) >= qaAnswers[j]?.answers[0]?.startDate && dayjs( requestSection[i].answer, 'DD MMM YYYY' ) <= qaAnswers[j]?.answers[0]?.endDate ) {
2049
+ ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.matchedCount;
2050
+ } else {
2051
+ ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.notMatchedCount;
2052
+ }
1473
2053
  }
1474
- if ( ansArray.length > 1 && ansArray.some( ( date ) => dayjs( date, 'DD MMM YYYY' ).format( 'DD-MM-YYYY' ) >= qaAnswers[j]?.answers[0]?.startDate && dayjs( date, 'DD MMM YYYY' ) <= qaAnswers[j]?.answers[0]?.endDate ) ) {
1475
- ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.matchedCount;
1476
- } else if ( ansArray.length > 1 && ( dayjs( ansArray?.[0], 'DD MMM YYYY' ).format( 'DD-MM-YYYY' ) >= qaAnswers[j]?.answers[0]?.startDate && dayjs( ansArray?.[1], 'DD MMM YYYY' ).format( 'DD-MM-YYYY' ) <= qaAnswers[j]?.answers[0]?.endDate ) ) {
1477
- ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.matchedCount;
1478
- } else if ( dayjs( requestSection[i].answer, 'DD MMM YYYY' ) >= qaAnswers[j]?.answers[0]?.startDate && dayjs( requestSection[i].answer, 'DD MMM YYYY' ) <= qaAnswers[j]?.answers[0]?.endDate ) {
2054
+ }
2055
+ }
2056
+ if ( qaAnswers[j].answerType == 'linearscale' ) {
2057
+ if ( qaAnswers[j].compliance ) {
2058
+ let linearAnswer = requestSection[i].answer.split( 'to' );
2059
+ if ( parseInt( linearAnswer?.[0] ) >= qaAnswers[j]?.answers[0]?.minValue && parseInt( linearAnswer?.[1] ) <= qaAnswers[j]?.answers[0]?.maxValue ) {
1479
2060
  ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.matchedCount;
1480
2061
  } else {
1481
2062
  ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.notMatchedCount;
1482
2063
  }
1483
2064
  }
2065
+ ansstructure.linearType = qaAnswers[j]?.answers[0]?.linearType;
2066
+ ansstructure.matchedCount = qaAnswers[j]?.answers[0]?.matchedCount;
2067
+ ansstructure.notMatchedCount = qaAnswers[j]?.answers[0]?.notMatchedCount;
1484
2068
  }
1485
- }
1486
- if ( qaAnswers[j].answerType == 'linearscale' ) {
1487
- if ( qaAnswers[j].compliance ) {
1488
- let linearAnswer = requestSection[i].answer.split( 'to' );
1489
- if ( parseInt( linearAnswer?.[0] ) >= qaAnswers[j]?.answers[0]?.minValue && parseInt( linearAnswer?.[1] ) <= qaAnswers[j]?.answers[0]?.maxValue ) {
1490
- ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.matchedCount;
1491
- } else {
1492
- ansstructure.complianceScore = qaAnswers[j]?.answers[0]?.notMatchedCount;
1493
- }
2069
+ if ( qaAnswers[j].answerType == 'image' ) {
2070
+ ansstructure.matchedCount = qaAnswers[j]?.answers[0]?.matchedCount;
2071
+ ansstructure.notMatchedCount = qaAnswers[j]?.answers[0]?.notMatchedCount;
1494
2072
  }
1495
- ansstructure.linearType = qaAnswers[j]?.answers[0]?.linearType;
1496
- ansstructure.matchedCount = qaAnswers[j]?.answers[0]?.matchedCount;
1497
- ansstructure.notMatchedCount = qaAnswers[j]?.answers[0]?.notMatchedCount;
1498
- }
1499
- if ( qaAnswers[j].answerType == 'image' ) {
1500
- ansstructure.matchedCount = qaAnswers[j]?.answers[0]?.matchedCount;
1501
- ansstructure.notMatchedCount = qaAnswers[j]?.answers[0]?.notMatchedCount;
2073
+ if ( requestSection[i]?.runAIData && requestSection[i]?.runAIData?.length ) {
2074
+ ansstructure.runAIData = requestSection[i].runAIData;
2075
+ }
2076
+ des.push( ansstructure );
1502
2077
  }
1503
- des.push( ansstructure );
1504
2078
  }
1505
2079
  let structure = {};
1506
2080
  structure.qno = qaAnswers[j].qno;
@@ -1511,7 +2085,9 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1511
2085
  structure.runAI = qaAnswers[j].runAI;
1512
2086
  structure.runAIDescription = qaAnswers[j].runAIDescription;
1513
2087
  structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1514
- structure.remarks = requestSection[i].remarks;
2088
+ if ( ( getChecklistQA?.redoEdit && qaAnswers[j].redo == requestSection[i].redo ) || !getChecklistQA?.redoEdit ) {
2089
+ structure.remarks = requestSection[i].remarks;
2090
+ }
1515
2091
  structure.answers = qaAnswers[j].answers;
1516
2092
  structure.userAnswer = des;
1517
2093
  structure.linkType = qaAnswers[j].linkType;
@@ -1552,6 +2128,18 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1552
2128
 
1553
2129
  requestData.questionAnswers = sectionFormat;
1554
2130
  logger.error( { message: requestData.questionAnswers, error: 'QuestionanswersPayload' } );
2131
+
2132
+ if ( requestData.submittype == 'submit' ) {
2133
+ for ( let section of sectionFormat ) {
2134
+ for ( let question of section.questions ) {
2135
+ let mustValidate = !question.linkType || ( question.linkType && question.linkquestionenabled );
2136
+ if ( mustValidate && ( !question.userAnswer || !question.userAnswer.length ) ) {
2137
+ return res.sendError( 'Please Fill All Fields', 400 );
2138
+ }
2139
+ }
2140
+ }
2141
+ }
2142
+
1555
2143
  next();
1556
2144
  } catch ( error ) {
1557
2145
  logger.error( { function: 'sopMobilechecklistMultiSectionFormatter', error: error, body: req.body } );
@@ -1932,7 +2520,7 @@ export async function sopMobileTaskMultiSectionFormatter( req, res, next ) {
1932
2520
  let des = [];
1933
2521
  if ( requestSection[i].answer != 'null' && requestSection[i].answer != '' && requestSection[i].answer != null ) {
1934
2522
  let validationAnswer = '';
1935
- if ( requestSection[i].answer.split( '?' ).length > 1 ) {
2523
+ if ( requestSection[i].answer.split( '?' ).length > 1 || requestSection[i].answer.includes( 'https://' ) ) {
1936
2524
  validationAnswer = decodeURIComponent( requestSection[i].answer.split( '?' )[0] );
1937
2525
  }
1938
2526
  if ( validationAnswer.length ) {
@@ -2031,7 +2619,13 @@ function QuestionFlag( req, res ) {
2031
2619
 
2032
2620
  async function updateRecurringFlagTracker( processedChecklist, questionAnswers, currentDateTime ) {
2033
2621
  try {
2034
- if ( !processedChecklist?.store_id || !processedChecklist?.sourceCheckList_id ) return;
2622
+ if ( !processedChecklist?.sourceCheckList_id ) return;
2623
+
2624
+ const isUserBased = processedChecklist?.coverage === 'user';
2625
+ const storeId = isUserBased ? '' : ( processedChecklist?.store_id || '' );
2626
+ const userId = isUserBased ? ( processedChecklist?.userId ? String( processedChecklist.userId ) : ( processedChecklist?.userEmail || '' ) ) : '';
2627
+ if ( !isUserBased && !storeId ) return;
2628
+ if ( isUserBased && !userId ) return;
2035
2629
 
2036
2630
  const today = currentDateTime ? currentDateTime.format( 'YYYY-MM-DD' ) : dayjs().format( 'YYYY-MM-DD' );
2037
2631
  const yesterday = dayjs( today, 'YYYY-MM-DD' ).subtract( 1, 'day' ).format( 'YYYY-MM-DD' );
@@ -2039,7 +2633,7 @@ async function updateRecurringFlagTracker( processedChecklist, questionAnswers,
2039
2633
  const trackerKeyBase = {
2040
2634
  client_id: processedChecklist.client_id,
2041
2635
  sourceCheckList_id: processedChecklist.sourceCheckList_id,
2042
- store_id: processedChecklist.store_id,
2636
+ ...( isUserBased ) ? { user_id: userId } : { store_id: storeId },
2043
2637
  };
2044
2638
 
2045
2639
  const existingRows = await recurringFlagTracker.find( trackerKeyBase, { section_id: 1, qno: 1, consecutiveCount: 1, lastFlaggedDate: 1 } );
@@ -2059,7 +2653,7 @@ async function updateRecurringFlagTracker( processedChecklist, questionAnswers,
2059
2653
  const flaggedNow = Array.isArray( question?.userAnswer ) && question.userAnswer.some( ( a ) => a?.sopFlag === true );
2060
2654
  const key = `${sectionId}::${qno}`;
2061
2655
  const existing = existingMap.get( key );
2062
-
2656
+ console.log( flaggedNow, 'test' );
2063
2657
  if ( flaggedNow ) {
2064
2658
  let newCount;
2065
2659
  if ( !existing ) {
@@ -2079,8 +2673,11 @@ async function updateRecurringFlagTracker( processedChecklist, questionAnswers,
2079
2673
  filter: { ...trackerKeyBase, section_id: sectionId, qno },
2080
2674
  update: {
2081
2675
  $set: {
2676
+ coverage: isUserBased ? 'user' : 'store',
2082
2677
  checkListName: processedChecklist.checkListName,
2083
- storeName: processedChecklist.storeName,
2678
+ storeName: isUserBased ? '' : ( processedChecklist.storeName || '' ),
2679
+ userName: processedChecklist.userName || '',
2680
+ userEmail: processedChecklist.userEmail || '',
2084
2681
  sectionName: section?.sectionName || '',
2085
2682
  qname: question?.qname || '',
2086
2683
  consecutiveCount: newCount,
@@ -2103,9 +2700,13 @@ async function updateRecurringFlagTracker( processedChecklist, questionAnswers,
2103
2700
  }
2104
2701
  } );
2105
2702
  } );
2106
-
2107
2703
  if ( ops.length ) {
2108
- await recurringFlagTracker.bulkWrite( ops );
2704
+ try {
2705
+ let data = await recurringFlagTracker.bulkWrite( ops, { ordered: false } );
2706
+ console.log( 'recurringFlagTracker bulkWrite result', data );
2707
+ } catch ( bulkErr ) {
2708
+ logger.error( { function: 'updateRecurringFlagTracker.bulkWrite', code: bulkErr?.code, message: bulkErr?.message, writeErrors: bulkErr?.writeErrors } );
2709
+ }
2109
2710
  }
2110
2711
  } catch ( error ) {
2111
2712
  logger.error( { function: 'updateRecurringFlagTracker', error } );
@@ -2159,6 +2760,9 @@ export async function submitChecklist( req, res ) {
2159
2760
  let flagCount = QuestionFlag( req, res );
2160
2761
  updateData.questionFlag = flagCount;
2161
2762
  updateData.submitTime_string = currentDateTime.format( 'hh:mm A, DD MMM YYYY' );
2763
+ if ( getchecklist?.[0]?.redoEdit != undefined ) {
2764
+ updateData.redoEdit = false;
2765
+ }
2162
2766
  if ( requestData.deviceDetails && requestData.deviceDetails != '' ) {
2163
2767
  updateData.deviceDetails =JSON.parse( requestData.deviceDetails );
2164
2768
  }
@@ -2247,6 +2851,7 @@ export async function submitChecklist( req, res ) {
2247
2851
  // };
2248
2852
  // await detectionService.create( detectionData );
2249
2853
  if ( requestData.submittype == 'submit' ) {
2854
+ console.log( checklistDetails?.recurringFlag?.notifyType?.length );
2250
2855
  if ( checklistDetails?.recurringFlag?.notifyType?.length ) {
2251
2856
  updateRecurringFlagTracker( getchecklist[0], requestData.questionAnswers, currentDateTime );
2252
2857
  }
@@ -2430,7 +3035,8 @@ export async function submitTask( req, res ) {
2430
3035
  'storeResponseTime': currentDateTime.format( 'HH:mm:ss - MMM DD, YYYY' ),
2431
3036
  'message_mode': 'retriggered',
2432
3037
  };
2433
- sendMessageToQueue( `${JSON.parse( process.env.SQS ).url}${JSON.parse( process.env.SQS ).storeHygiene}`, JSON.stringify( sqsData ) );
3038
+ let sqsResponse = await sendMessageToQueue( `${JSON.parse( process.env.SQS ).url}${JSON.parse( process.env.SQS ).storeHygiene}`, JSON.stringify( sqsData ) );
3039
+ console.log( sqsResponse );
2434
3040
  }
2435
3041
  if ( !excludedChecklists.includes( checklist.checkListName ) ) {
2436
3042
  const query1 = [
@@ -2935,6 +3541,9 @@ export async function dashboardv1( req, res ) {
2935
3541
  // const { store_id, date } = req.query;
2936
3542
  const { date } = req.query;
2937
3543
  const userId = req.user._id;
3544
+ let fromDate = new Date( req.query.date );
3545
+ let toDate = new Date( req.query.date );
3546
+ toDate.setDate( toDate.getDate() + 1 );
2938
3547
 
2939
3548
  // //Get User Based Checklist //
2940
3549
  const clientId = { client_id: req.user.clientId };
@@ -2951,7 +3560,7 @@ export async function dashboardv1( req, res ) {
2951
3560
  ...storeMatch,
2952
3561
  };
2953
3562
  const buildPipeline = ( matchExtraConditions = {} ) => [
2954
- { $match: { ...baseMatch, ...matchExtraConditions } },
3563
+ { $match: { $or: [ { ...baseMatch, ...matchExtraConditions }, { $and: [ { redoStatus: true }, { scheduleStartTime_iso: { $lt: toDate } }, { scheduleEndTime_iso: { $gte: fromDate } }, { userId }, { ...storeMatch } ] } ] } },
2955
3564
  {
2956
3565
  $facet: {
2957
3566
  total: [ { $count: 'total' } ],
@@ -2971,9 +3580,7 @@ export async function dashboardv1( req, res ) {
2971
3580
  },
2972
3581
  ];
2973
3582
 
2974
- let fromDate = new Date( req.query.date );
2975
- let toDate = new Date( req.query.date );
2976
- toDate.setDate( toDate.getDate() + 1 );
3583
+
2977
3584
  const taskBaseMatch = {
2978
3585
  // eslint-disable-next-line camelcase
2979
3586
  // store_id,
@@ -3022,6 +3629,8 @@ export async function dashboardv1( req, res ) {
3022
3629
  processedTask.aggregate( taskQuery ),
3023
3630
  ] );
3024
3631
 
3632
+ console.log( checklistResult );
3633
+
3025
3634
  const checklistResultData =
3026
3635
  checklistResult.status === 'fulfilled' ?
3027
3636
  processResult( checklistResult.value ) :
@@ -3151,6 +3760,9 @@ export async function checklistv1( req, res ) {
3151
3760
  // const { store_id, date, checklistStatus, searchValue } = req.query;
3152
3761
  const { date, checklistStatus, searchValue } = req.query;
3153
3762
  const userId = req.user._id;
3763
+ let fromDate = new Date( req.query.date );
3764
+ let toDate = new Date( req.query.date );
3765
+ toDate.setDate( toDate.getDate() + 1 );
3154
3766
 
3155
3767
  // Get User Based Checklist //
3156
3768
  const clientId = { client_id: req.user.clientId };
@@ -3162,16 +3774,18 @@ export async function checklistv1( req, res ) {
3162
3774
  // eslint-disable-next-line camelcase
3163
3775
  // { store_id },
3164
3776
  { userId },
3165
- { date_string: date },
3777
+ { $or: [ { date_string: date }, { $and: [ { redoStatus: true }, { scheduleStartTime_iso: { $lt: toDate } }, { scheduleEndTime_iso: { $gte: fromDate } } ] } ] },
3166
3778
  { timeFlagStatus: true },
3167
3779
  ...matchExtraConditions,
3168
3780
  clientId,
3169
3781
  storeMatch,
3170
3782
  ];
3783
+
3171
3784
  if ( checklistStatus ) {
3172
3785
  matchConditions.push( { checklistStatus } );
3173
3786
  }
3174
3787
 
3788
+
3175
3789
  const pipeline = [
3176
3790
  { $match: { $and: matchConditions } },
3177
3791
  ...( searchValue ?
@@ -3224,9 +3838,7 @@ export async function checklistv1( req, res ) {
3224
3838
  return pipeline;
3225
3839
  };
3226
3840
 
3227
- let fromDate = new Date( req.query.date );
3228
- let toDate = new Date( req.query.date );
3229
- toDate.setDate( toDate.getDate() + 1 );
3841
+
3230
3842
  const taskBuildPipeline = ( matchExtraConditions = [], projectExtraConditions = {} ) => {
3231
3843
  const matchConditions = [
3232
3844
  // eslint-disable-next-line camelcase
@@ -3606,7 +4218,7 @@ export async function questionList( req, res ) {
3606
4218
  checkvalidation = answer.validationAnswer;
3607
4219
  }
3608
4220
  }
3609
- Multianswer.push( { 'answer': answer.answer, 'no': ansIndex, 'validationAnswer': checkvalidation } );
4221
+ Multianswer.push( { 'answer': answer.answer, 'no': ansIndex, 'validationAnswer': checkvalidation, ...( answer.redo && { redo: answer.redo } ) } );
3610
4222
  getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].index = ansIndex;
3611
4223
  }
3612
4224