tango-app-api-trax 3.4.0-flag-10 → 3.4.0-soplink2-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -91,6 +91,260 @@ export async function storeListv1( req, res ) {
91
91
  }
92
92
  }
93
93
 
94
+ export async function startChecklistv1( req, res ) {
95
+ try {
96
+ let requestData = req.body;
97
+ let findQuery = [];
98
+ findQuery.push( {
99
+ $match: {
100
+ $and: [
101
+ { _id: new ObjectId( requestData.processedcheckListId ) },
102
+ { userId: req.user._id },
103
+ { date_string: requestData.date },
104
+ ],
105
+ },
106
+ } );
107
+
108
+ let getBeforeChecklist = await processedchecklist.aggregate( findQuery );
109
+ if ( !getBeforeChecklist.length ) {
110
+ return res.sendError( 'Check List Got Edited Please Fill Again', 422 );
111
+ }
112
+
113
+ if ( getBeforeChecklist[0].checklistStatus != 'open' ) {
114
+ return res.sendError( 'already check list started', 400 );
115
+ }
116
+
117
+ logger.info( `v5 => Checklist Started => store Name: ${getBeforeChecklist[0].storeName || ''}, User Email: ${getBeforeChecklist[0].userEmail}, Checklist Name: ${getBeforeChecklist[0].checkListName}` );
118
+ let PCLQusestion = await PCLconfig.findOne( { _id: new ObjectId( getBeforeChecklist[0].checkListId ) } );
119
+ let updateQuery = {};
120
+ updateQuery._id = new ObjectId( requestData.processedcheckListId );
121
+ updateQuery.userId = req.user._id;
122
+ updateQuery.date_string = requestData.date;
123
+
124
+ if ( PCLQusestion && PCLQusestion?.questionAnswers && PCLQusestion?.questionAnswers.length > 0 ) {
125
+ await PCLQusestion.questionAnswers.forEach( ( section ) => {
126
+ section.questions.forEach( ( question ) => {
127
+ if ( question.answerType == 'multiplechoicemultiple' ) {
128
+ let Multianswer = [];
129
+ if ( question.answers.length > 0 ) {
130
+ question.answers.forEach( ( answer, ansIndex ) => {
131
+ Multianswer.push( { 'answer': null, 'no': ansIndex, 'validationAnswer': null } );
132
+ answer.index = ansIndex;
133
+ } );
134
+ question.Multianswer = Multianswer;
135
+ }
136
+ } else if ( question.answerType == 'multipleImage' ) {
137
+ let Multianswer = [];
138
+ if ( question.answers.length > 0 ) {
139
+ question.answers.forEach( ( answer, ansIndex ) => {
140
+ Multianswer.push( { 'answer': null, 'no': ansIndex, 'validationAnswer': null } );
141
+ answer.index = ansIndex;
142
+ } );
143
+ question.Multianswer = Multianswer;
144
+ }
145
+ } else {
146
+ question.Multianswer = [];
147
+ }
148
+ } );
149
+ } );
150
+ }
151
+
152
+ let updateData = {};
153
+ let currentDateTime;
154
+ if ( getBeforeChecklist[0].storeName && getBeforeChecklist[0].storeName!='' ) {
155
+ let storeTimeZone = await storeService.findOne( { storeName: { $regex: getBeforeChecklist[0].storeName, $options: 'i' }, clientId: getBeforeChecklist[0].client_id }, { 'storeProfile.timeZone': 1 } );
156
+ if ( storeTimeZone?.storeProfile?.timeZone ) {
157
+ currentDateTime = dayjs().tz( storeTimeZone?.storeProfile?.timeZone );
158
+ } else {
159
+ currentDateTime = dayjs();
160
+ }
161
+ } else {
162
+ currentDateTime = dayjs();
163
+ }
164
+ updateData.checklistStatus = 'inprogress';
165
+ updateData.startMobileTime = requestData?.currentTime;
166
+ updateData.questionAnswers = getBeforeChecklist[0]?.redoStatus ? getBeforeChecklist[0].questionAnswers : PCLQusestion.questionAnswers;
167
+ // const newDateTime = currentDateTime.add(5, 'hours').add(30, 'minutes');
168
+ // updateData.startTime_string = newDateTime.format('hh:mm A, DD MMM');
169
+ updateData.startTime_string = currentDateTime.format( 'hh:mm A, DD MMM YYYY' );
170
+ updateData.startTime = dayjs.utc( updateData.startTime_string, 'hh:mm A, DD MMM YYYY' ).format();
171
+
172
+ let updatechecklist = await processedchecklist.updateOne( updateQuery, updateData );
173
+ PCLQusestion.startTime = dayjs.utc( updateData.startTime_string, 'hh:mm A, DD MMM YYYY' ).format();
174
+ PCLQusestion.save();
175
+ if ( updatechecklist.modifiedCount > 0 ) {
176
+ findQuery.push( {
177
+ $project: {
178
+ checkListName: { $ifNull: [ '$checkListName', '' ] },
179
+ coverage: { $ifNull: [ '$coverage', '' ] },
180
+ client_id: { $ifNull: [ '$client_id', '' ] },
181
+ scheduleStartTime: { $ifNull: [ '$scheduleStartTime', '' ] },
182
+ scheduleStartTime_iso: { $ifNull: [ '$scheduleStartTime_iso', '' ] },
183
+ scheduleEndTime: { $ifNull: [ '$scheduleEndTime', '' ] },
184
+ scheduleEndTime_iso: { $ifNull: [ '$scheduleEndTime_iso', '' ] },
185
+ checklistStatus: { $ifNull: [ '$checklistStatus', '' ] },
186
+ checkListId: { $ifNull: [ '$checkListId', '' ] },
187
+ startTime: { $ifNull: [ '$startTime', '' ] },
188
+ submitTime: { $ifNull: [ '$submitTime', '' ] },
189
+ allowedOverTime: { $ifNull: [ '$allowedOverTime', '' ] },
190
+ // allowedStoreLocation: { $ifNull: [ '$checallowedStoreLocationkListName', false ] },
191
+ allowedStoreLocation: {
192
+ $cond: {
193
+ if: { $eq: [ '$client_id', '11' ] },
194
+ then: false,
195
+ else: {
196
+ '$cond': {
197
+ 'if': { '$eq': [ '$coverage', 'user' ] },
198
+ 'then': false,
199
+ 'else': { '$ifNull': [ '$allowedStoreLocation', false ] },
200
+ },
201
+ },
202
+ },
203
+ },
204
+ questionAnswers: { $ifNull: [ '$questionAnswers', '' ] },
205
+ redoStatus: { $ifNull: [ '$redoStatus', '' ] },
206
+ },
207
+ } );
208
+ let getupdatedchecklist = await processedchecklist.aggregate( findQuery );
209
+ // let bucket = JSON.parse( process.env.BUCKET );
210
+
211
+ let logInsertData = {
212
+ store_id: getBeforeChecklist[0]?.store_id || '',
213
+ storeName: getBeforeChecklist[0]?.storeName || '',
214
+ action: 'started',
215
+ checklistId: getBeforeChecklist[0].sourceCheckList_id,
216
+ processedChecklistId: getBeforeChecklist[0]._id,
217
+ checkListName: getBeforeChecklist[0].checkListName,
218
+ type: getBeforeChecklist[0].checkListType,
219
+ client_id: req.user.clientId,
220
+ userEmail: getBeforeChecklist[0].userEmail || '',
221
+ userName: getBeforeChecklist[0].userName || '',
222
+ coverage: getBeforeChecklist[0].coverage || '',
223
+ };
224
+ await checklistLogs.create( logInsertData );
225
+ let getchecklist = getupdatedchecklist;
226
+ let questions = [];
227
+ // function processQuestion( question, section, questions, nested=false ) {
228
+ // let findExists = questions.find( ( item ) => item?.qno && item.qno == question.qno );
229
+ // if ( findExists && nested ) {
230
+ // let findIndex = questions.findIndex( ( item ) => item?.qno && item.qno == question.qno );
231
+ // questions.splice( findIndex, 1 );
232
+ // questions.push( question );
233
+ // }
234
+ // if ( !findExists ) {
235
+ // questions.push( question );
236
+ // for ( let answer of question.answers ) {
237
+ // if ( answer.showLinked && answer?.linkedQuestion != '' ) {
238
+ // let linkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
239
+ // if ( linkedQuestion ) {
240
+ // processQuestion( linkedQuestion, section, questions, true );
241
+ // }
242
+ // }
243
+ // }
244
+ // }
245
+ // }
246
+ for ( let [ index, data ] of getchecklist.entries() ) {
247
+ for ( let [ secIndex, section ] of data.questionAnswers.entries() ) {
248
+ questions = [];
249
+ for ( let [ questionIndex, question ] of section.questions.entries() ) {
250
+ let linkedQuestions;
251
+ let uniqueShow = false;
252
+ if ( !question.linkType ) {
253
+ if ( !question?.uniqueNo ) {
254
+ uniqueShow = true;
255
+ question.uniqueNo = parseInt( getOtp() ) + Date.now() + questionIndex;
256
+ }
257
+ questions.push( question );
258
+ linkedQuestions = [];
259
+ let questionList;
260
+ if ( uniqueShow ) {
261
+ questionList = new Map( section.questions.map( ( ele ) => [ ele.qno, ele ] ) );
262
+ } else {
263
+ let uniqueList = question.answers.flatMap( ( ele ) => ele.nestedQuestion );
264
+ let filteredQuestion = section.questions.filter( ( ele ) => uniqueList.includes( ele?.uniqueNo ) );
265
+ questionList = new Map( filteredQuestion.map( ( ele ) => [ ele.uniqueNo, ele ] ) );
266
+ }
267
+ question.answers.forEach( ( answer ) => {
268
+ if ( answer.linkedQuestion ) {
269
+ let nesedQuestion = [];
270
+ answer.oldNestedQuestion = !uniqueShow ? JSON.parse( JSON.stringify( answer.oldNestedQuestion ) ) : JSON.parse( JSON.stringify( answer.nestedQuestion ) );
271
+ answer.nestedQuestion.forEach( ( qn, qidx ) => {
272
+ let getQn = questionList.get( qn );
273
+ if ( getQn ) {
274
+ let randomNo;
275
+ if ( !getQn?.uniqueNo || uniqueShow ) {
276
+ randomNo = parseInt( getOtp() ) + Date.now() + qidx;
277
+ } else {
278
+ randomNo = getQn?.uniqueNo;
279
+ }
280
+ getQn = JSON.parse( JSON.stringify( getQn ) );
281
+ getQn = { ...getQn, uniqueNo: randomNo };
282
+ nesedQuestion.push( getQn );
283
+ if ( answer.linkedQuestion == getQn.qno ) {
284
+ answer.oldLinkedQuestion = answer.linkedQuestion;
285
+ answer.linkedQuestion = randomNo;
286
+ }
287
+ answer.nestedQuestion[qidx] = randomNo;
288
+ }
289
+ } );
290
+ nesedQuestion.forEach( ( ele ) => {
291
+ let nestedLinkqn = [];
292
+ ele.answers.forEach( ( ans ) => {
293
+ if ( ans.linkedQuestion ) {
294
+ ans.oldNestedQuestion = !uniqueShow ? JSON.parse( JSON.stringify( ans.oldNestedQuestion ) ) : JSON.parse( JSON.stringify( ans.nestedQuestion ) );
295
+ ans.nestedQuestion.forEach( ( nested, idx ) => {
296
+ let findRandom = nesedQuestion.find( ( qn ) => ( uniqueShow ? qn.qno == nested : qn.uniqueNo == nested ) && !nestedLinkqn.includes( qn.uniqueNo ) && !qn.updated );
297
+ if ( findRandom ) {
298
+ if ( ans.linkedQuestion == findRandom.qno ) {
299
+ ans.oldLinkedQuestion = ans.linkedQuestion;
300
+ ans.linkedQuestion = findRandom.uniqueNo;
301
+ }
302
+ ans.nestedQuestion[idx] = findRandom.uniqueNo;
303
+ nestedLinkqn.push( findRandom.uniqueNo );
304
+ }
305
+ } );
306
+ }
307
+ } );
308
+ ele.updated = true;
309
+ } );
310
+ linkedQuestions.push( ...nesedQuestion );
311
+ }
312
+ } );
313
+ questions.push( ...linkedQuestions );
314
+ }
315
+ // processQuestion( question, section, questions );
316
+ }
317
+ getchecklist[index].questionAnswers[secIndex].questions = questions;
318
+ }
319
+ }
320
+ if ( !getchecklist[0].redoStatus ) {
321
+ await processedchecklist.updateOne( updateQuery, { questionAnswers: getchecklist[0].questionAnswers } );
322
+ }
323
+ getupdatedchecklist[0].questionAnswers.forEach( ( section ) => {
324
+ section.questions.forEach( async ( question ) => {
325
+ if ( question.questionReferenceImage && question.questionReferenceImage!='' ) {
326
+ question.questionReferenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + question.questionReferenceImage;
327
+ // question.questionReferenceImage = await signedUrl( { Bucket: bucket.sop, file_path: decodeURIComponent( question.questionReferenceImage ) } );
328
+ }
329
+ question.answers.forEach( async ( answer ) => {
330
+ if ( answer.referenceImage != '' ) {
331
+ answer.referenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.referenceImage;
332
+ // answer.referenceImage = await signedUrl( { Bucket: bucket.sop, file_path: decodeURIComponent( answer.referenceImage ) } );
333
+ }
334
+ } );
335
+ } );
336
+ } );
337
+ return res.sendSuccess( getchecklist );
338
+ } else {
339
+ return res.sendError( 'something went wrong please try again', 500 );
340
+ }
341
+ } catch ( e ) {
342
+ console.log( 'e =>', e );
343
+ logger.error( { function: 'startChecklist', error: e, body: req.body } );
344
+ return res.sendError( e, 500 );
345
+ }
346
+ }
347
+
94
348
  export async function startChecklist( req, res ) {
95
349
  try {
96
350
  let requestData = req.body;
@@ -670,21 +924,360 @@ export async function sopMobileTaskQuestionValidator( req, res, next ) {
670
924
  } else {
671
925
  next();
672
926
  }
673
- } else {
674
- next();
927
+ } else {
928
+ next();
929
+ }
930
+ } catch ( e ) {
931
+ logger.error( { function: 'sopMobileTaskQuestionValidator', error: e, body: req.body } );
932
+ return res.sendError( e, 500 );
933
+ }
934
+ };
935
+
936
+ export async function sopMobilechecklistMultiSectionFormatter( req, res, next ) {
937
+ try {
938
+ let requestData = req.body;
939
+ requestData.questionAnswers = typeof requestData.questionAnswers == 'string' ? JSON.parse( requestData.questionAnswers ) : requestData.questionAnswers;
940
+ let getChecklistQA = await processedchecklist.findOne( { _id: new ObjectId( requestData.processedcheckListId ) }, { questionAnswers: 1 } );
941
+ let reqAnswers = requestData.questionAnswers;
942
+ let CLQAnswers = getChecklistQA.questionAnswers;
943
+
944
+ if ( requestData.submittype == 'submit' ) {
945
+ reqAnswers.forEach( ( reqA ) => {
946
+ if ( ![ 'multiplechoicemultiple', 'multipleImage' ].includes( reqA?.answerType ) ) {
947
+ if ( ( !reqA.linkType && ( reqA.answer == null || reqA.answer == '' ) ) || ( reqA.linkType && reqA.linkquestionenabled && ( reqA.answer == null || reqA.answer == '' ) ) ) {
948
+ return res.sendError( 'Please Fill All Fields', 400 );
949
+ }
950
+ }
951
+ } );
952
+ }
953
+
954
+ let sectionFormat = [];
955
+ let uniqueSections = {};
956
+ reqAnswers.forEach( ( item ) => {
957
+ const key = `${item.section_id}-${item.sectionName}`;
958
+ if ( !uniqueSections[key] ) {
959
+ uniqueSections[key] = { id: item.section_id, name: item.sectionName };
960
+ }
961
+ } );
962
+ const uniqueArray = Object.values( uniqueSections );
963
+ for ( let section of uniqueArray ) {
964
+ let CLQSection = CLQAnswers.find( ( item ) => item.section_id == section.id );
965
+ if ( CLQSection ) {
966
+ let newArray = [];
967
+ let qaAnswers = CLQSection.questions;
968
+ let requestSection = reqAnswers.filter( ( item ) => item.section_id == section.id );
969
+ for ( let i = 0; i < requestSection.length; i++ ) {
970
+ for ( let j = 0; j < qaAnswers.length; j++ ) {
971
+ if ( requestSection[i].qno == qaAnswers[j].qno ) {
972
+ if ( qaAnswers[j].answerType == 'yes/no' ) {
973
+ let qaans = qaAnswers[j].answers;
974
+ let yn = [];
975
+ for ( let k = 0; k < qaans.length; k++ ) {
976
+ if ( requestSection[i].answer == qaans[k].answer ) {
977
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
978
+ if ( requestSection[i].validationAnswer ) {
979
+ let validateAns = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
980
+ if ( validateAns.length ) {
981
+ let splitImgUrl = validateAns.split( '/' );
982
+ if ( splitImgUrl.length > 1 ) {
983
+ splitImgUrl.splice( 0, 3 );
984
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
985
+ }
986
+ }
987
+ }
988
+ } else {
989
+ qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
990
+ qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
991
+ }
992
+ yn.push( qaans[k] );
993
+ }
994
+ }
995
+ let structure = {};
996
+ structure.qno = qaAnswers[j].qno;
997
+ structure.qname = qaAnswers[j].qname;
998
+ structure.answerType = qaAnswers[j].answerType;
999
+ structure.runAI = qaAnswers[j].runAI;
1000
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1001
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1002
+ structure.remarks = requestSection[i].remarks;
1003
+ structure.answers = qaAnswers[j].answers;
1004
+ structure.userAnswer = yn;
1005
+ structure.linkType = qaAnswers[j].linkType;
1006
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1007
+ structure.parentanswer = requestSection[i].parentanswer;
1008
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1009
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1010
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1011
+ if ( qaAnswers[j]?.taskId ) {
1012
+ structure.taskId = qaAnswers[j]?.taskId;
1013
+ structure.task = true;
1014
+ }
1015
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1016
+ structure.redo = false;
1017
+ } else if ( requestData.submittype === 'draft' ) {
1018
+ structure.redo = qaAnswers[j]?.redo;
1019
+ }
1020
+ if ( qaAnswers[j]?.redoComment ) {
1021
+ structure.redoComment = qaAnswers[j]?.redoComment;
1022
+ };
1023
+ newArray.push( structure );
1024
+ } else if ( qaAnswers[j].answerType == 'multiplechoicesingle' ) {
1025
+ let qaans = qaAnswers[j].answers;
1026
+ let ms = [];
1027
+ for ( let k = 0; k < qaans.length; k++ ) {
1028
+ if ( requestSection[i].answer == qaans[k].answer ) {
1029
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1030
+ if ( requestSection[i].validationAnswer ) {
1031
+ let validationAnswer = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
1032
+ if ( validationAnswer.length ) {
1033
+ let splitImgUrl = validationAnswer.split( '/' );
1034
+ if ( splitImgUrl.length > 1 ) {
1035
+ splitImgUrl.splice( 0, 3 );
1036
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1037
+ }
1038
+ }
1039
+ }
1040
+ } else {
1041
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1042
+ qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
1043
+ }
1044
+ ms.push( qaans[k] );
1045
+ }
1046
+ }
1047
+ let structure = {};
1048
+ structure.qno = qaAnswers[j].qno;
1049
+ structure.qname = qaAnswers[j].qname;
1050
+ structure.answerType = qaAnswers[j].answerType;
1051
+ structure.runAI = qaAnswers[j].runAI;
1052
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1053
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1054
+ structure.remarks = requestSection[i].remarks;
1055
+ structure.answers = qaAnswers[j].answers;
1056
+ structure.userAnswer = ms;
1057
+ structure.linkType = qaAnswers[j].linkType;
1058
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1059
+ structure.parentanswer = requestSection[i].parentanswer;
1060
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1061
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1062
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1063
+ if ( qaAnswers[j]?.taskId ) {
1064
+ structure.taskId = qaAnswers[j]?.taskId;
1065
+ structure.task = true;
1066
+ }
1067
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1068
+ structure.redo = false;
1069
+ } else if ( requestData.submittype === 'draft' ) {
1070
+ structure.redo = qaAnswers[j]?.redo;
1071
+ }
1072
+ if ( qaAnswers[j]?.redoComment ) {
1073
+ structure.redoComment = qaAnswers[j]?.redoComment;
1074
+ };
1075
+ newArray.push( structure );
1076
+ } else if ( qaAnswers[j].answerType == 'multiplechoicemultiple' ) {
1077
+ let qaans = qaAnswers[j].answers;
1078
+ let mcmo = [];
1079
+ for ( let k = 0; k < qaans.length; k++ ) {
1080
+ let separatedArray = typeof requestSection[i].Multianswer == 'string' ? JSON.parse( requestSection[i].Multianswer ) : requestSection[i].Multianswer;
1081
+ for ( let s = 0; s < separatedArray.length; s++ ) {
1082
+ if ( separatedArray[s].answer == qaans[k].answer ) {
1083
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1084
+ if ( separatedArray[s].validationAnswer ) {
1085
+ let validationAnswer = decodeURIComponent( separatedArray[s].validationAnswer.split( '?' )[0] );
1086
+ if ( validationAnswer.length ) {
1087
+ let splitImgUrl = validationAnswer.split( '/' );
1088
+ if ( splitImgUrl.length > 1 ) {
1089
+ splitImgUrl.splice( 0, 3 );
1090
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1091
+ }
1092
+ }
1093
+ }
1094
+ } else {
1095
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1096
+ qaans[k].validationAnswer = separatedArray[s].validationAnswer || '';
1097
+ }
1098
+
1099
+ mcmo.push( qaans[k] );
1100
+ }
1101
+ }
1102
+ }
1103
+ let structure = {};
1104
+ structure.qno = qaAnswers[j].qno;
1105
+ structure.qname = qaAnswers[j].qname;
1106
+ structure.answerType = qaAnswers[j].answerType;
1107
+ structure.runAI = qaAnswers[j].runAI;
1108
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1109
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1110
+ structure.remarks = requestSection[i].remarks;
1111
+ structure.answers = qaAnswers[j].answers;
1112
+ structure.userAnswer = mcmo;
1113
+ structure.linkType = qaAnswers[j].linkType;
1114
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1115
+ structure.parentanswer = requestSection[i].parentanswer;
1116
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1117
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1118
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1119
+ if ( qaAnswers[j]?.taskId ) {
1120
+ structure.taskId = qaAnswers[j]?.taskId;
1121
+ structure.task = true;
1122
+ }
1123
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1124
+ structure.redo = false;
1125
+ } else if ( requestData.submittype === 'draft' ) {
1126
+ structure.redo = qaAnswers[j]?.redo;
1127
+ }
1128
+ if ( qaAnswers[j]?.redoComment ) {
1129
+ structure.redoComment = qaAnswers[j]?.redoComment;
1130
+ };
1131
+ newArray.push( structure );
1132
+ } else if ( qaAnswers[j].answerType == 'multipleImage' ) {
1133
+ let separatedArray = typeof requestSection[i].Multianswer == 'string' ? JSON.parse( requestSection[i].Multianswer ) : requestSection[i].Multianswer;
1134
+ let mcmi = [];
1135
+ // for (let k = 0; k < qaans.length; k++) {
1136
+ for ( let s = 0; s < separatedArray.length; s++ ) {
1137
+ if ( separatedArray[s].answer && separatedArray[s].answer !='' ) {
1138
+ let newAnswer = {};
1139
+ let validationAnswer = decodeURIComponent( separatedArray[s].answer.split( '?' )[0] );
1140
+ if ( validationAnswer.length ) {
1141
+ let splitImgUrl = validationAnswer.split( '/' );
1142
+ if ( splitImgUrl.length > 1 ) {
1143
+ splitImgUrl.splice( 0, 3 );
1144
+ newAnswer.answer = splitImgUrl.join( '/' ) || '';
1145
+ }
1146
+ }
1147
+
1148
+ newAnswer.answeroptionNumber = 0;
1149
+ newAnswer.sopFlag = false;
1150
+ newAnswer.validation = false;
1151
+ newAnswer.validationType = '';
1152
+ newAnswer.referenceImage = '';
1153
+ newAnswer.allowUploadfromGallery = false;
1154
+ newAnswer.runAI = false;
1155
+ newAnswer.descriptivetype = '';
1156
+ newAnswer.showLinked = false;
1157
+ newAnswer.linkedQuestion = 0;
1158
+ newAnswer.nestedQuestion = [];
1159
+ newAnswer.index = s;
1160
+ mcmi.push( newAnswer );
1161
+ }
1162
+ }
1163
+ // }
1164
+ let structure = {};
1165
+ structure.qno = qaAnswers[j].qno;
1166
+ structure.qname = qaAnswers[j].qname;
1167
+ structure.answerType = qaAnswers[j].answerType;
1168
+ structure.runAI = qaAnswers[j].runAI;
1169
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1170
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1171
+ structure.remarks = requestSection[i].remarks;
1172
+ structure.answers = qaAnswers[j].answers;
1173
+ structure.userAnswer = mcmi;
1174
+ structure.linkType = qaAnswers[j].linkType;
1175
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1176
+ structure.parentanswer = requestSection[i].parentanswer;
1177
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1178
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1179
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1180
+ if ( qaAnswers[j]?.taskId ) {
1181
+ structure.taskId = qaAnswers[j]?.taskId;
1182
+ structure.task = true;
1183
+ }
1184
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1185
+ structure.redo = false;
1186
+ } else if ( requestData.submittype === 'draft' ) {
1187
+ structure.redo = qaAnswers[j]?.redo;
1188
+ }
1189
+ if ( qaAnswers[j]?.redoComment ) {
1190
+ structure.redoComment = qaAnswers[j]?.redoComment;
1191
+ };
1192
+ newArray.push( structure );
1193
+ } else {
1194
+ let des = [];
1195
+ if ( requestSection[i].answer != 'null' && requestSection[i].answer != '' && requestSection[i].answer != null ) {
1196
+ let validationAnswer = '';
1197
+ if ( requestSection[i].answer.split( '?' ).length > 1 ) {
1198
+ validationAnswer = decodeURIComponent( requestSection[i].answer.split( '?' )[0] );
1199
+ }
1200
+ if ( validationAnswer.length ) {
1201
+ let splitImgUrl = validationAnswer.split( '/' );
1202
+ if ( splitImgUrl.length > 1 ) {
1203
+ splitImgUrl.splice( 0, 3 );
1204
+ requestSection[i].answer = splitImgUrl.join( '/' );
1205
+ }
1206
+ }
1207
+ let ansstructure = {
1208
+ answer: requestSection[i].answer,
1209
+ answeroptionNumber: 1,
1210
+ sopFlag: false,
1211
+ validation: false,
1212
+ validationType: '',
1213
+ validationAnswer: '',
1214
+ referenceImage: qaAnswers[j].answers[0].referenceImage,
1215
+ showLinked: qaAnswers[j].answers[0].showLinked,
1216
+ linkedQuestion: qaAnswers[j].answers[0].linkedQuestion,
1217
+ };
1218
+ if ( qaAnswers[j].answerType == 'date' ) {
1219
+ ansstructure.dateRangeType = requestSection[i].dateRangeType || false;
1220
+ }
1221
+ des.push( ansstructure );
1222
+ }
1223
+ let structure = {};
1224
+ structure.qno = qaAnswers[j].qno;
1225
+ structure.qname = qaAnswers[j].qname;
1226
+ structure.answerType = qaAnswers[j].answerType;
1227
+ structure.runAI = qaAnswers[j].runAI;
1228
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1229
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1230
+ structure.remarks = requestSection[i].remarks;
1231
+ structure.answers = qaAnswers[j].answers;
1232
+ structure.userAnswer = des;
1233
+ structure.linkType = qaAnswers[j].linkType;
1234
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1235
+ structure.parentanswer = requestSection[i].parentanswer;
1236
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1237
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1238
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1239
+ if ( qaAnswers[j]?.taskId ) {
1240
+ structure.taskId = qaAnswers[j]?.taskId;
1241
+ structure.task = true;
1242
+ }
1243
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1244
+ structure.redo = false;
1245
+ } else if ( requestData.submittype === 'draft' ) {
1246
+ structure.redo = qaAnswers[j]?.redo;
1247
+ }
1248
+ if ( qaAnswers[j]?.redoComment ) {
1249
+ structure.redoComment = qaAnswers[j]?.redoComment;
1250
+ };
1251
+ newArray.push( structure );
1252
+ }
1253
+ }
1254
+ }
1255
+ }
1256
+ let sectionData = {
1257
+ 'section_id': section.id,
1258
+ 'sectionName': section.name,
1259
+ 'questions': newArray,
1260
+ };
1261
+ sectionFormat.push( sectionData );
1262
+ }
675
1263
  }
676
- } catch ( e ) {
677
- logger.error( { function: 'sopMobileTaskQuestionValidator', error: e, body: req.body } );
678
- return res.sendError( e, 500 );
1264
+
1265
+
1266
+ requestData.questionAnswers = sectionFormat;
1267
+ next();
1268
+ } catch ( error ) {
1269
+ logger.error( { function: 'sopMobilechecklistMultiSectionFormatter', error: error, body: req.body } );
1270
+ return res.sendError( error, 500 );
679
1271
  }
680
1272
  };
681
1273
 
682
- export async function sopMobilechecklistMultiSectionFormatter( req, res, next ) {
1274
+ export async function sopMobilechecklistMultiSectionFormatterv2( req, res, next ) {
683
1275
  try {
684
1276
  let requestData = req.body;
685
1277
  requestData.questionAnswers = typeof requestData.questionAnswers == 'string' ? JSON.parse( requestData.questionAnswers ) : requestData.questionAnswers;
686
1278
  let getChecklistQA = await processedchecklist.findOne( { _id: new ObjectId( requestData.processedcheckListId ) }, { questionAnswers: 1 } );
687
1279
  let reqAnswers = requestData.questionAnswers;
1280
+ logger.error( { functionName: 'updatedPayload', message: reqAnswers } );
688
1281
  let CLQAnswers = getChecklistQA.questionAnswers;
689
1282
 
690
1283
  if ( requestData.submittype == 'submit' ) {
@@ -697,6 +1290,58 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
697
1290
  } );
698
1291
  }
699
1292
 
1293
+ if ( requestData?.editSubmit && requestData?.editSubmit == 'true' ) {
1294
+ let sampleData = reqAnswers[0];
1295
+ CLQAnswers.forEach( ( section ) => {
1296
+ let requestSection = reqAnswers.filter( ( reqSection ) => reqSection.sectionName == section?.sectionOldName || reqSection.sectionName == section?.sectionName );
1297
+ if ( requestSection.length ) {
1298
+ requestSection.forEach( ( item ) => item.section_id = section.section_id );
1299
+ section.questions.forEach( ( question ) => {
1300
+ let sectionQuestion = requestSection.filter( ( secQuestion ) => secQuestion.qname == question.oldQname || secQuestion.qname == question.qname );
1301
+ if ( !sectionQuestion.length ) {
1302
+ let data = { ...requestSection[0] };
1303
+ data.answerType = question.answerType;
1304
+ data.qno = question.qno;
1305
+ data.qname = question.qname;
1306
+ data.oldQname = question?.oldQname || question.qname;
1307
+ data.answer = 'null';
1308
+ data.remarks = '';
1309
+ data.validationAnswer = '';
1310
+ data.Multianswer = [];
1311
+ data.linkType = question.linkType;
1312
+ data.linkquestionenabled = question.linkType;
1313
+ data.linkedQuestion = null;
1314
+ data.showLinked = null;
1315
+ data.parentanswer = '';
1316
+ data.dateRangeType = false;
1317
+ reqAnswers.push( data );
1318
+ }
1319
+ } );
1320
+ } else {
1321
+ section.questions.forEach( ( ele ) => {
1322
+ let data = { ...sampleData };
1323
+ data.section_id = section.section_id;
1324
+ data.sectionName = section.sectionName;
1325
+ data.answerType = ele.answerType;
1326
+ data.qno = ele.qno;
1327
+ data.qname = ele.qname;
1328
+ data.oldQname = ele?.oldQname || ele.qname;
1329
+ data.answer = 'null';
1330
+ data.remarks = '';
1331
+ data.validationAnswer = '';
1332
+ data.Multianswer = [];
1333
+ data.linkType = ele.linkType;
1334
+ data.linkquestionenabled = ele.linkType;
1335
+ data.linkedQuestion = null;
1336
+ data.showLinked = null;
1337
+ data.parentanswer = '';
1338
+ data.dateRangeType = false;
1339
+ reqAnswers.push( data );
1340
+ } );
1341
+ }
1342
+ } );
1343
+ }
1344
+
700
1345
  let sectionFormat = [];
701
1346
  let uniqueSections = {};
702
1347
  reqAnswers.forEach( ( item ) => {
@@ -707,18 +1352,23 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
707
1352
  } );
708
1353
  const uniqueArray = Object.values( uniqueSections );
709
1354
  for ( let section of uniqueArray ) {
710
- let CLQSection = CLQAnswers.find( ( item ) => item.section_id == section.id );
1355
+ let CLQSection = CLQAnswers.find( ( item ) => item.section_id.toString() == section.id.toString() );
711
1356
  if ( CLQSection ) {
712
1357
  let newArray = [];
713
1358
  let qaAnswers = CLQSection.questions;
714
1359
  let requestSection = reqAnswers.filter( ( item ) => item.section_id == section.id );
715
1360
  for ( let i = 0; i < requestSection.length; i++ ) {
716
1361
  for ( let j = 0; j < qaAnswers.length; j++ ) {
717
- if ( requestSection[i].qno == qaAnswers[j].qno ) {
1362
+ if ( ( requestSection[i].qname == qaAnswers[j].oldQname || requestSection[i].qname == qaAnswers[j].qname ) && ( !qaAnswers[j].uniqueNo || ( qaAnswers[j].uniqueNo && requestSection[i].uniqueNo == qaAnswers[j].uniqueNo ) ) ) {
718
1363
  if ( qaAnswers[j].answerType == 'yes/no' ) {
719
1364
  let qaans = qaAnswers[j].answers;
720
1365
  let yn = [];
721
1366
  for ( let k = 0; k < qaans.length; k++ ) {
1367
+ // let requestAnswer = typeof requestSection[i].questionAnswers == 'string' ? JSON.parse( requestSection[i].questionAnswers ) : requestSection[i].questionAnswers;
1368
+ // let findAnswer = requestAnswer.find( ( ele ) => ele.answer == qaans[k].answer && ele.);
1369
+ // if ( findAnswer ) {
1370
+ // qaans[k].nestedQuestion = typeof findAnswer.nestedQuestion == 'string' ? JSON.parse( findAnswer.nestedQuestion ) : findAnswer.nestedQuestion;
1371
+ // }
722
1372
  if ( requestSection[i].answer == qaans[k].answer ) {
723
1373
  if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
724
1374
  if ( requestSection[i].validationAnswer ) {
@@ -732,7 +1382,7 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
732
1382
  }
733
1383
  }
734
1384
  } else {
735
- qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1385
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
736
1386
  qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
737
1387
  }
738
1388
  yn.push( qaans[k] );
@@ -753,14 +1403,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
753
1403
  structure.parentanswer = requestSection[i].parentanswer;
754
1404
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
755
1405
  structure.descriptivetype = qaAnswers[j].descriptivetype;
756
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1406
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1407
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
757
1408
  if ( qaAnswers[j]?.taskId ) {
758
1409
  structure.taskId = qaAnswers[j]?.taskId;
759
1410
  structure.task = true;
760
1411
  }
761
1412
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
762
1413
  structure.redo = false;
763
- } else if ( requestData.submittype === 'draft' ) {
1414
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
764
1415
  structure.redo = qaAnswers[j]?.redo;
765
1416
  }
766
1417
  if ( qaAnswers[j]?.redoComment ) {
@@ -771,6 +1422,11 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
771
1422
  let qaans = qaAnswers[j].answers;
772
1423
  let ms = [];
773
1424
  for ( let k = 0; k < qaans.length; k++ ) {
1425
+ // let requestAnswer = typeof requestSection[i].questionAnswers == 'string' ? JSON.parse( requestSection[i].questionAnswers ) : requestSection[i].questionAnswers;
1426
+ // let findAnswer= requestAnswer.find( ( ele ) => ele.answer == qaans[k].answer );
1427
+ // if ( findAnswer ) {
1428
+ // qaans[k].nestedQuestion = typeof findAnswer.nestedQuestion == 'string' ? JSON.parse( findAnswer.nestedQuestion ) : findAnswer.nestedQuestion;
1429
+ // }
774
1430
  if ( requestSection[i].answer == qaans[k].answer ) {
775
1431
  if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
776
1432
  if ( requestSection[i].validationAnswer ) {
@@ -805,14 +1461,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
805
1461
  structure.parentanswer = requestSection[i].parentanswer;
806
1462
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
807
1463
  structure.descriptivetype = qaAnswers[j].descriptivetype;
808
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1464
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1465
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
809
1466
  if ( qaAnswers[j]?.taskId ) {
810
1467
  structure.taskId = qaAnswers[j]?.taskId;
811
1468
  structure.task = true;
812
1469
  }
813
1470
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
814
1471
  structure.redo = false;
815
- } else if ( requestData.submittype === 'draft' ) {
1472
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
816
1473
  structure.redo = qaAnswers[j]?.redo;
817
1474
  }
818
1475
  if ( qaAnswers[j]?.redoComment ) {
@@ -861,14 +1518,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
861
1518
  structure.parentanswer = requestSection[i].parentanswer;
862
1519
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
863
1520
  structure.descriptivetype = qaAnswers[j].descriptivetype;
864
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1521
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1522
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
865
1523
  if ( qaAnswers[j]?.taskId ) {
866
1524
  structure.taskId = qaAnswers[j]?.taskId;
867
1525
  structure.task = true;
868
1526
  }
869
1527
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
870
1528
  structure.redo = false;
871
- } else if ( requestData.submittype === 'draft' ) {
1529
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
872
1530
  structure.redo = qaAnswers[j]?.redo;
873
1531
  }
874
1532
  if ( qaAnswers[j]?.redoComment ) {
@@ -922,14 +1580,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
922
1580
  structure.parentanswer = requestSection[i].parentanswer;
923
1581
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
924
1582
  structure.descriptivetype = qaAnswers[j].descriptivetype;
925
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1583
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1584
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
926
1585
  if ( qaAnswers[j]?.taskId ) {
927
1586
  structure.taskId = qaAnswers[j]?.taskId;
928
1587
  structure.task = true;
929
1588
  }
930
1589
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
931
1590
  structure.redo = false;
932
- } else if ( requestData.submittype === 'draft' ) {
1591
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
933
1592
  structure.redo = qaAnswers[j]?.redo;
934
1593
  }
935
1594
  if ( qaAnswers[j]?.redoComment ) {
@@ -940,7 +1599,7 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
940
1599
  let des = [];
941
1600
  if ( requestSection[i].answer != 'null' && requestSection[i].answer != '' && requestSection[i].answer != null ) {
942
1601
  let validationAnswer = '';
943
- if ( requestSection[i].answer.split( '?' ).length > 1 ) {
1602
+ if ( requestSection[i].answer.includes( 'https' ) ) {
944
1603
  validationAnswer = decodeURIComponent( requestSection[i].answer.split( '?' )[0] );
945
1604
  }
946
1605
  if ( validationAnswer.length ) {
@@ -981,14 +1640,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
981
1640
  structure.parentanswer = requestSection[i].parentanswer;
982
1641
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
983
1642
  structure.descriptivetype = qaAnswers[j].descriptivetype;
984
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1643
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1644
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
985
1645
  if ( qaAnswers[j]?.taskId ) {
986
1646
  structure.taskId = qaAnswers[j]?.taskId;
987
1647
  structure.task = true;
988
1648
  }
989
1649
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
990
1650
  structure.redo = false;
991
- } else if ( requestData.submittype === 'draft' ) {
1651
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
992
1652
  structure.redo = qaAnswers[j]?.redo;
993
1653
  }
994
1654
  if ( qaAnswers[j]?.redoComment ) {
@@ -1010,6 +1670,7 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
1010
1670
 
1011
1671
 
1012
1672
  requestData.questionAnswers = sectionFormat;
1673
+ logger.error( { message: requestData.questionAnswers, error: 'QuestionanswersPayload' } );
1013
1674
  next();
1014
1675
  } catch ( error ) {
1015
1676
  logger.error( { function: 'sopMobilechecklistMultiSectionFormatter', error: error, body: req.body } );
@@ -2552,7 +3213,7 @@ export async function checklistv1( req, res ) {
2552
3213
  ...projectExtraConditions,
2553
3214
  client_id: { $ifNull: [ '$client_id', '' ] },
2554
3215
  coverage: { $ifNull: [ '$coverage', '' ] },
2555
- taskType: { $ifNull: [ '$type', '' ] },
3216
+ taskType: { $ifNull: [ '$planoType', '' ] },
2556
3217
  },
2557
3218
  },
2558
3219
  ];
@@ -2602,6 +3263,262 @@ export async function checklistv1( req, res ) {
2602
3263
  }
2603
3264
  }
2604
3265
 
3266
+ export async function questionListv1( req, res ) {
3267
+ try {
3268
+ let requestData = req.query;
3269
+ if ( !requestData.processedcheckListId ) {
3270
+ res.sendError( 'processedcheckListId is Required', 400 );
3271
+ }
3272
+
3273
+ let findQuery = [];
3274
+ let findAndQuery = [];
3275
+ findAndQuery.push( { _id: new ObjectId( requestData.processedcheckListId ) } );
3276
+ findQuery.push( { $match: { $and: findAndQuery } } );
3277
+
3278
+ findQuery.push( {
3279
+ $project: {
3280
+ checkListName: { $ifNull: [ '$checkListName', '' ] },
3281
+ scheduleStartTime: { $ifNull: [ '$scheduleStartTime', '' ] },
3282
+ scheduleStartTime_iso: { $ifNull: [ '$scheduleStartTime_iso', '' ] },
3283
+ scheduleEndTime: { $ifNull: [ '$scheduleEndTime', '' ] },
3284
+ scheduleEndTime_iso: { $ifNull: [ '$scheduleEndTime_iso', '' ] },
3285
+ checklistStatus: { $ifNull: [ '$checklistStatus', '' ] },
3286
+ checkListId: { $ifNull: [ '$checkListId', '' ] },
3287
+ startTime: { $ifNull: [ '$startTime', '' ] },
3288
+ submitTime: { $ifNull: [ '$submitTime', '' ] },
3289
+ allowedOverTime: { $ifNull: [ '$allowedOverTime', '' ] },
3290
+ // allowedStoreLocation: { $ifNull: [ '$allowedStoreLocation', '' ] },
3291
+ allowedStoreLocation: {
3292
+ $cond: {
3293
+ if: { $eq: [ '$client_id', '11' ] },
3294
+ then: false,
3295
+ else: {
3296
+ '$cond': {
3297
+ 'if': { '$eq': [ '$coverage', 'user' ] },
3298
+ 'then': false,
3299
+ 'else': { '$ifNull': [ '$allowedStoreLocation', false ] },
3300
+ },
3301
+ },
3302
+ },
3303
+ },
3304
+ reinitiateStatus: { $ifNull: [ '$reinitiateStatus', '' ] },
3305
+ questionAnswers: { $ifNull: [ '$questionAnswers', '' ] },
3306
+ getchecklist: { $ifNull: [ '$getchecklist', '' ] },
3307
+ userEmail: { $ifNull: [ '$userEmail', '' ] },
3308
+ storeName: { $ifNull: [ '$storeName', '' ] },
3309
+ redoStatus: { $ifNull: [ '$redoStatus', false ] },
3310
+ rawImageUpload: { $ifNull: [ '$rawImageUpload', false ] },
3311
+ },
3312
+ } );
3313
+
3314
+ let getchecklist = await processedchecklist.aggregate( findQuery );
3315
+ if ( !getchecklist.length ) {
3316
+ return res.sendError( 'Check List Got Edited Please Fill Again', 422 );
3317
+ } else {
3318
+ logger.info( `v5 => Checklist Continue => store Name: ${getchecklist[0].storeName}, User Email: ${getchecklist[0].userEmail}, Checklist Name: ${getchecklist[0].checkListName}` );
3319
+ // let bucket = JSON.parse( process.env.BUCKET );
3320
+ for ( let [ secIndex, section ] of getchecklist[0].questionAnswers.entries() ) {
3321
+ for ( let [ questionIndex, question ] of section.questions.entries() ) {
3322
+ let Multianswer = [];
3323
+ if ( getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage && getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage !='' ) {
3324
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage = await signedUrl( { file_path: decodeURIComponent( getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage ), Bucket: bucket.sop } );
3325
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage;
3326
+ }
3327
+ for ( let [ ansIndex, answer ] of question.answers.entries() ) {
3328
+ if ( question.answerType == 'multiplechoicemultiple' ) {
3329
+ let checkvalidation = null;
3330
+ if ( answer.validationAnswer && answer.validationAnswer !='' ) {
3331
+ if ( answer.validationType != 'Descriptive Answer' ) {
3332
+ // checkvalidation = await signedUrl( { file_path: decodeURIComponent( answer.validationAnswer ), Bucket: bucket.sop } );
3333
+ checkvalidation = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.validationAnswer;
3334
+ } else {
3335
+ checkvalidation = answer.validationAnswer;
3336
+ }
3337
+ }
3338
+ Multianswer.push( { 'answer': answer.answer, 'no': ansIndex, 'validationAnswer': checkvalidation } );
3339
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].index = ansIndex;
3340
+ }
3341
+ if ( answer.referenceImage != '' ) {
3342
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].referenceImage = await signedUrl( { file_path: decodeURIComponent( answer.referenceImage ), Bucket: bucket.sop } );
3343
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].referenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.referenceImage;
3344
+ }
3345
+ if ( ( answer.validationType == 'Capture Image' || answer.validationType == 'Capture Video' ) && answer.validationAnswer && answer.validationAnswer != '' ) {
3346
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].validationAnswer = await signedUrl( { file_path: decodeURIComponent( answer.validationAnswer ), Bucket: bucket.sop } );
3347
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].validationAnswer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.validationAnswer;
3348
+ }
3349
+ }
3350
+ if ( question?.userAnswer ) {
3351
+ for ( let [ userAnsIndex, userAns ] of question.userAnswer.entries() ) {
3352
+ if ( ( userAns.validationType == 'Capture Image' || userAns.validationType == 'Capture Video' ) && userAns.validationAnswer && userAns.validationAnswer != '' ) {
3353
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].validationAnswer = await signedUrl( { file_path: decodeURIComponent( userAns.validationAnswer ), Bucket: bucket.sop } );
3354
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].validationAnswer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.validationAnswer;
3355
+ }
3356
+ if ( [ 'image', 'descriptiveImage', 'video' ].includes( question.answerType ) && userAns.answer != '' ) {
3357
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].answer = await signedUrl( { file_path: decodeURIComponent( userAns.answer ), Bucket: bucket.sop } );
3358
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].answer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.answer;
3359
+ }
3360
+ if ( userAns.referenceImage != '' ) {
3361
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].referenceImage = await signedUrl( { file_path: decodeURIComponent( userAns.referenceImage ), Bucket: bucket.sop } );
3362
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].referenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.referenceImage;
3363
+ }
3364
+ if ( question.answerType == 'multiplechoicemultiple' ) {
3365
+ let ansIndex = Multianswer.findIndex( ( item ) => item.answer == userAns.answer );
3366
+ if ( ansIndex ) {
3367
+ Multianswer[ansIndex].validationAnswer = userAns.validationAnswer;
3368
+ }
3369
+ }
3370
+ if ( question.answerType == 'multipleImage' ) {
3371
+ if ( userAns && userAns.answer && userAns.answer !='' ) {
3372
+ // let manswer = await signedUrl( { file_path: decodeURIComponent( userAns.answer ), Bucket: bucket.sop } );
3373
+ let manswer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.answer;
3374
+ Multianswer.push( { 'answer': manswer, 'no': userAnsIndex, 'validationAnswer': '' } );
3375
+ } else {
3376
+
3377
+ }
3378
+ }
3379
+ }
3380
+ }
3381
+ if ( question.answerType == 'multiplechoicemultiple' ) {
3382
+ Multianswer.forEach( ( item ) => {
3383
+ if ( item.validationAnswer == null ) {
3384
+ item.answer = null;
3385
+ }
3386
+ } );
3387
+ if ( Multianswer.length ) {
3388
+ question.Multianswer = Multianswer;
3389
+ } else {
3390
+ question.Multianswer = Multianswer;
3391
+ }
3392
+ }
3393
+ if ( question.answerType == 'multipleImage' ) {
3394
+ if ( Multianswer.length ) {
3395
+ question.Multianswer = Multianswer;
3396
+ } else {
3397
+ Multianswer.push( { 'answer': null, 'no': 0, 'validationAnswer': null } );
3398
+ question.Multianswer = Multianswer;
3399
+ }
3400
+ }
3401
+ }
3402
+ }
3403
+
3404
+ let questions = [];
3405
+ // function processQuestion( question, section, questions, nested=false ) {
3406
+ // let findExists = questions.find( ( item ) => item?.qno && item.qno == question.qno );
3407
+ // if ( findExists && nested ) {
3408
+ // let findIndex = questions.findIndex( ( item ) => item?.qno && item.qno == question.qno );
3409
+ // questions.splice( findIndex, 1 );
3410
+ // questions.push( question );
3411
+ // for ( let answer of question.answers ) {
3412
+ // if ( answer.showLinked && answer?.linkedQuestion != '' ) {
3413
+ // let linkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
3414
+ // if ( linkedQuestion ) {
3415
+ // processQuestion( linkedQuestion, section, questions, true );
3416
+ // }
3417
+ // }
3418
+ // }
3419
+ // }
3420
+ // if ( !findExists ) {
3421
+ // questions.push( question );
3422
+ // for ( let answer of question.answers ) {
3423
+ // if ( answer.showLinked && answer?.linkedQuestion != '' ) {
3424
+ // let linkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
3425
+ // if ( linkedQuestion ) {
3426
+ // processQuestion( linkedQuestion, section, questions, true );
3427
+ // }
3428
+ // }
3429
+ // }
3430
+ // }
3431
+ // }
3432
+
3433
+ for ( let [ index, data ] of getchecklist.entries() ) {
3434
+ for ( let [ secIndex, section ] of data.questionAnswers.entries() ) {
3435
+ questions = [];
3436
+ for ( let [ questionIndex, question ] of section.questions.entries() ) {
3437
+ let linkedQuestions;
3438
+ let uniqueShow = false;
3439
+ if ( !question.linkType ) {
3440
+ if ( !question?.uniqueNo ) {
3441
+ uniqueShow = true;
3442
+ question.uniqueNo = parseInt( getOtp() ) + Date.now() + questionIndex;
3443
+ }
3444
+ questions.push( question );
3445
+ linkedQuestions = [];
3446
+ let questionList;
3447
+ if ( uniqueShow ) {
3448
+ questionList = new Map( section.questions.map( ( ele ) => [ ele.qno, ele ] ) );
3449
+ } else {
3450
+ let uniqueList = question.answers.flatMap( ( ele ) => ele.nestedQuestion );
3451
+ let filteredQuestion = section.questions.filter( ( ele ) => uniqueList.includes( ele?.uniqueNo ) );
3452
+ questionList = new Map( filteredQuestion.map( ( ele ) => [ ele.uniqueNo, ele ] ) );
3453
+ }
3454
+ question.answers.forEach( ( answer ) => {
3455
+ if ( answer.linkedQuestion ) {
3456
+ let nesedQuestion = [];
3457
+ answer.oldNestedQuestion = !uniqueShow ? JSON.parse( JSON.stringify( answer.oldNestedQuestion ) ) : JSON.parse( JSON.stringify( answer.nestedQuestion ) );
3458
+ answer.nestedQuestion.forEach( ( qn, qidx ) => {
3459
+ let getQn = questionList.get( qn );
3460
+ if ( getQn ) {
3461
+ let randomNo;
3462
+ if ( !getQn?.uniqueNo || uniqueShow ) {
3463
+ randomNo = parseInt( getOtp() ) + Date.now() + qidx;
3464
+ } else {
3465
+ randomNo = getQn?.uniqueNo;
3466
+ }
3467
+ getQn = JSON.parse( JSON.stringify( getQn ) );
3468
+ getQn = { ...getQn, uniqueNo: randomNo };
3469
+ nesedQuestion.push( getQn );
3470
+ if ( answer.linkedQuestion == getQn.qno ) {
3471
+ answer.oldLinkedQuestion = answer.linkedQuestion;
3472
+ answer.linkedQuestion = randomNo;
3473
+ }
3474
+ answer.nestedQuestion[qidx] = randomNo;
3475
+ }
3476
+ } );
3477
+ console.log( nesedQuestion );
3478
+ nesedQuestion.forEach( ( ele ) => {
3479
+ let nestedLinkqn = [];
3480
+ ele.answers.forEach( ( ans ) => {
3481
+ if ( ans.linkedQuestion ) {
3482
+ ans.oldNestedQuestion = !uniqueShow ? JSON.parse( JSON.stringify( ans.oldNestedQuestion ) ) : JSON.parse( JSON.stringify( ans.nestedQuestion ) );
3483
+ ans.nestedQuestion.forEach( ( nested, idx ) => {
3484
+ console.log( nested, uniqueShow, 'nested', nestedLinkqn );
3485
+ let findRandom = nesedQuestion.find( ( qn ) => ( uniqueShow ? qn.qno == nested : qn.uniqueNo == nested ) && !nestedLinkqn.includes( qn.uniqueNo ) && !qn.updated );
3486
+ console.log( findRandom );
3487
+ if ( findRandom ) {
3488
+ if ( ans.linkedQuestion == findRandom.qno ) {
3489
+ ans.oldLinkedQuestion = ans.linkedQuestion;
3490
+ ans.linkedQuestion = findRandom.uniqueNo;
3491
+ }
3492
+ ans.nestedQuestion[idx] = findRandom.uniqueNo;
3493
+ nestedLinkqn.push( findRandom.uniqueNo );
3494
+ }
3495
+ } );
3496
+ }
3497
+ } );
3498
+ ele.updated = true;
3499
+ } );
3500
+ linkedQuestions.push( ...nesedQuestion );
3501
+ }
3502
+ } );
3503
+ questions.push( ...linkedQuestions );
3504
+ }
3505
+ // processQuestion( question, section, questions );
3506
+ }
3507
+ getchecklist[index].questionAnswers[secIndex].questions = questions;
3508
+ }
3509
+ }
3510
+ if ( !getchecklist[0].redoStatus ) {
3511
+ await processedchecklist.updateOne( { _id: requestData.processedcheckListId }, { questionAnswers: getchecklist[0].questionAnswers } );
3512
+ }
3513
+
3514
+ return res.sendSuccess( getchecklist );
3515
+ }
3516
+ } catch ( e ) {
3517
+ logger.error( { function: 'questionList', error: e, body: req.body } );
3518
+ return res.sendError( e, 500 );
3519
+ }
3520
+ }
3521
+
2605
3522
  export async function questionList( req, res ) {
2606
3523
  try {
2607
3524
  let requestData = req.query;
@@ -3176,10 +4093,12 @@ export async function uploadAnswerImage( req, res ) {
3176
4093
  }
3177
4094
 
3178
4095
  if ( imageUrl != '' ) {
3179
- imageUrl = await signedUrl( { file_path: imageUrl.Key, Bucket: bucket.sop } );
4096
+ // imageUrl = await signedUrl( { file_path: imageUrl.Key, Bucket: bucket.sop } );
4097
+ imageUrl = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + imageUrl.Key;
3180
4098
 
3181
4099
  return res.sendSuccess( {
3182
4100
  bucketName: bucket.sop,
4101
+ path: imageUrl.Key,
3183
4102
  imageUrl: imageUrl,
3184
4103
  message: 'Image uploaded successfully!',
3185
4104
  } );
@@ -3516,11 +4435,19 @@ export async function updatePlanoStatus( req, res ) {
3516
4435
  if ( !processCheckDetails ) {
3517
4436
  return res.sendError( 'No data found', 204 );
3518
4437
  }
3519
- processCheckDetails.checklistStatus = req.body.status;
3520
- processCheckDetails.save();
4438
+ let storeTimeZone = await storeService.findOne( { storeName: { $regex: processCheckDetails.storeName, $options: 'i' }, clientId: processCheckDetails.client_id }, { 'storeProfile.timeZone': 1 } );
4439
+ let currentDateTime;
4440
+ if ( storeTimeZone?.storeProfile?.timeZone ) {
4441
+ currentDateTime = dayjs().tz( storeTimeZone?.storeProfile?.timeZone );
4442
+ } else {
4443
+ currentDateTime = requestData?.currentTime ? dayjs( requestData.currentTime, 'HH:mm:ss' ) : dayjs();
4444
+ }
4445
+ let submitTimeString = currentDateTime.format( 'hh:mm A, DD MMM YYYY' );
4446
+ await processedchecklist.updateOne( { _id: req.body.id }, { checklistStatus: req.body.status, ...( req.body.status == 'inprogress' ) ? { startTime_string: submitTimeString } : { submitTime_string: submitTimeString } } );
3521
4447
  return res.sendSuccess( 'Status updated Successfully' );
3522
4448
  } catch ( e ) {
3523
4449
  logger.error( { error: e, function: 'updatePlanoStatus' } );
3524
4450
  return res.sendError( e, 500 );
3525
4451
  }
3526
4452
  }
4453
+