tango-app-api-trax 3.4.0-alpha-1 → 3.4.0-alpha-3

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.
@@ -90,6 +90,260 @@ export async function storeListv1( req, res ) {
90
90
  }
91
91
  }
92
92
 
93
+ export async function startChecklistv1( req, res ) {
94
+ try {
95
+ let requestData = req.body;
96
+ let findQuery = [];
97
+ findQuery.push( {
98
+ $match: {
99
+ $and: [
100
+ { _id: new ObjectId( requestData.processedcheckListId ) },
101
+ { userId: req.user._id },
102
+ { date_string: requestData.date },
103
+ ],
104
+ },
105
+ } );
106
+
107
+ let getBeforeChecklist = await processedchecklist.aggregate( findQuery );
108
+ if ( !getBeforeChecklist.length ) {
109
+ return res.sendError( 'Check List Got Edited Please Fill Again', 422 );
110
+ }
111
+
112
+ if ( getBeforeChecklist[0].checklistStatus != 'open' ) {
113
+ return res.sendError( 'already check list started', 400 );
114
+ }
115
+
116
+ logger.info( `v5 => Checklist Started => store Name: ${getBeforeChecklist[0].storeName || ''}, User Email: ${getBeforeChecklist[0].userEmail}, Checklist Name: ${getBeforeChecklist[0].checkListName}` );
117
+ let PCLQusestion = await PCLconfig.findOne( { _id: new ObjectId( getBeforeChecklist[0].checkListId ) } );
118
+ let updateQuery = {};
119
+ updateQuery._id = new ObjectId( requestData.processedcheckListId );
120
+ updateQuery.userId = req.user._id;
121
+ updateQuery.date_string = requestData.date;
122
+
123
+ if ( PCLQusestion && PCLQusestion?.questionAnswers && PCLQusestion?.questionAnswers.length > 0 ) {
124
+ await PCLQusestion.questionAnswers.forEach( ( section ) => {
125
+ section.questions.forEach( ( question ) => {
126
+ if ( question.answerType == 'multiplechoicemultiple' ) {
127
+ let Multianswer = [];
128
+ if ( question.answers.length > 0 ) {
129
+ question.answers.forEach( ( answer, ansIndex ) => {
130
+ Multianswer.push( { 'answer': null, 'no': ansIndex, 'validationAnswer': null } );
131
+ answer.index = ansIndex;
132
+ } );
133
+ question.Multianswer = Multianswer;
134
+ }
135
+ } else if ( question.answerType == 'multipleImage' ) {
136
+ let Multianswer = [];
137
+ if ( question.answers.length > 0 ) {
138
+ question.answers.forEach( ( answer, ansIndex ) => {
139
+ Multianswer.push( { 'answer': null, 'no': ansIndex, 'validationAnswer': null } );
140
+ answer.index = ansIndex;
141
+ } );
142
+ question.Multianswer = Multianswer;
143
+ }
144
+ } else {
145
+ question.Multianswer = [];
146
+ }
147
+ } );
148
+ } );
149
+ }
150
+
151
+ let updateData = {};
152
+ let currentDateTime;
153
+ if ( getBeforeChecklist[0].storeName && getBeforeChecklist[0].storeName!='' ) {
154
+ let storeTimeZone = await storeService.findOne( { storeName: { $regex: getBeforeChecklist[0].storeName, $options: 'i' }, clientId: getBeforeChecklist[0].client_id }, { 'storeProfile.timeZone': 1 } );
155
+ if ( storeTimeZone?.storeProfile?.timeZone ) {
156
+ currentDateTime = dayjs().tz( storeTimeZone?.storeProfile?.timeZone );
157
+ } else {
158
+ currentDateTime = dayjs();
159
+ }
160
+ } else {
161
+ currentDateTime = dayjs();
162
+ }
163
+ updateData.checklistStatus = 'inprogress';
164
+ updateData.startMobileTime = requestData?.currentTime;
165
+ updateData.questionAnswers = getBeforeChecklist[0]?.redoStatus ? getBeforeChecklist[0].questionAnswers : PCLQusestion.questionAnswers;
166
+ // const newDateTime = currentDateTime.add(5, 'hours').add(30, 'minutes');
167
+ // updateData.startTime_string = newDateTime.format('hh:mm A, DD MMM');
168
+ updateData.startTime_string = currentDateTime.format( 'hh:mm A, DD MMM YYYY' );
169
+ updateData.startTime = dayjs.utc( updateData.startTime_string, 'hh:mm A, DD MMM YYYY' ).format();
170
+
171
+ let updatechecklist = await processedchecklist.updateOne( updateQuery, updateData );
172
+ PCLQusestion.startTime = dayjs.utc( updateData.startTime_string, 'hh:mm A, DD MMM YYYY' ).format();
173
+ PCLQusestion.save();
174
+ if ( updatechecklist.modifiedCount > 0 ) {
175
+ findQuery.push( {
176
+ $project: {
177
+ checkListName: { $ifNull: [ '$checkListName', '' ] },
178
+ coverage: { $ifNull: [ '$coverage', '' ] },
179
+ client_id: { $ifNull: [ '$client_id', '' ] },
180
+ scheduleStartTime: { $ifNull: [ '$scheduleStartTime', '' ] },
181
+ scheduleStartTime_iso: { $ifNull: [ '$scheduleStartTime_iso', '' ] },
182
+ scheduleEndTime: { $ifNull: [ '$scheduleEndTime', '' ] },
183
+ scheduleEndTime_iso: { $ifNull: [ '$scheduleEndTime_iso', '' ] },
184
+ checklistStatus: { $ifNull: [ '$checklistStatus', '' ] },
185
+ checkListId: { $ifNull: [ '$checkListId', '' ] },
186
+ startTime: { $ifNull: [ '$startTime', '' ] },
187
+ submitTime: { $ifNull: [ '$submitTime', '' ] },
188
+ allowedOverTime: { $ifNull: [ '$allowedOverTime', '' ] },
189
+ // allowedStoreLocation: { $ifNull: [ '$checallowedStoreLocationkListName', false ] },
190
+ allowedStoreLocation: {
191
+ $cond: {
192
+ if: { $eq: [ '$client_id', '11' ] },
193
+ then: false,
194
+ else: {
195
+ '$cond': {
196
+ 'if': { '$eq': [ '$coverage', 'user' ] },
197
+ 'then': false,
198
+ 'else': { '$ifNull': [ '$allowedStoreLocation', false ] },
199
+ },
200
+ },
201
+ },
202
+ },
203
+ questionAnswers: { $ifNull: [ '$questionAnswers', '' ] },
204
+ redoStatus: { $ifNull: [ '$redoStatus', '' ] },
205
+ },
206
+ } );
207
+ let getupdatedchecklist = await processedchecklist.aggregate( findQuery );
208
+ // let bucket = JSON.parse( process.env.BUCKET );
209
+
210
+ let logInsertData = {
211
+ store_id: getBeforeChecklist[0]?.store_id || '',
212
+ storeName: getBeforeChecklist[0]?.storeName || '',
213
+ action: 'started',
214
+ checklistId: getBeforeChecklist[0].sourceCheckList_id,
215
+ processedChecklistId: getBeforeChecklist[0]._id,
216
+ checkListName: getBeforeChecklist[0].checkListName,
217
+ type: getBeforeChecklist[0].checkListType,
218
+ client_id: req.user.clientId,
219
+ userEmail: getBeforeChecklist[0].userEmail || '',
220
+ userName: getBeforeChecklist[0].userName || '',
221
+ coverage: getBeforeChecklist[0].coverage || '',
222
+ };
223
+ await checklistLogs.create( logInsertData );
224
+ let getchecklist = getupdatedchecklist;
225
+ let questions = [];
226
+ // function processQuestion( question, section, questions, nested=false ) {
227
+ // let findExists = questions.find( ( item ) => item?.qno && item.qno == question.qno );
228
+ // if ( findExists && nested ) {
229
+ // let findIndex = questions.findIndex( ( item ) => item?.qno && item.qno == question.qno );
230
+ // questions.splice( findIndex, 1 );
231
+ // questions.push( question );
232
+ // }
233
+ // if ( !findExists ) {
234
+ // questions.push( question );
235
+ // for ( let answer of question.answers ) {
236
+ // if ( answer.showLinked && answer?.linkedQuestion != '' ) {
237
+ // let linkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
238
+ // if ( linkedQuestion ) {
239
+ // processQuestion( linkedQuestion, section, questions, true );
240
+ // }
241
+ // }
242
+ // }
243
+ // }
244
+ // }
245
+ for ( let [ index, data ] of getchecklist.entries() ) {
246
+ for ( let [ secIndex, section ] of data.questionAnswers.entries() ) {
247
+ questions = [];
248
+ for ( let [ questionIndex, question ] of section.questions.entries() ) {
249
+ let linkedQuestions;
250
+ let uniqueShow = false;
251
+ if ( !question.linkType ) {
252
+ if ( !question?.uniqueNo ) {
253
+ uniqueShow = true;
254
+ question.uniqueNo = parseInt( getOtp() ) + Date.now() + questionIndex;
255
+ }
256
+ questions.push( question );
257
+ linkedQuestions = [];
258
+ let questionList;
259
+ if ( uniqueShow ) {
260
+ questionList = new Map( section.questions.map( ( ele ) => [ ele.qno, ele ] ) );
261
+ } else {
262
+ let uniqueList = question.answers.flatMap( ( ele ) => ele.nestedQuestion );
263
+ let filteredQuestion = section.questions.filter( ( ele ) => uniqueList.includes( ele?.uniqueNo ) );
264
+ questionList = new Map( filteredQuestion.map( ( ele ) => [ ele.uniqueNo, ele ] ) );
265
+ }
266
+ question.answers.forEach( ( answer ) => {
267
+ if ( answer.linkedQuestion ) {
268
+ let nesedQuestion = [];
269
+ answer.oldNestedQuestion = !uniqueShow ? JSON.parse( JSON.stringify( answer.oldNestedQuestion ) ) : JSON.parse( JSON.stringify( answer.nestedQuestion ) );
270
+ answer.nestedQuestion.forEach( ( qn, qidx ) => {
271
+ let getQn = questionList.get( qn );
272
+ if ( getQn ) {
273
+ let randomNo;
274
+ if ( !getQn?.uniqueNo || uniqueShow ) {
275
+ randomNo = parseInt( getOtp() ) + Date.now() + qidx;
276
+ } else {
277
+ randomNo = getQn?.uniqueNo;
278
+ }
279
+ getQn = JSON.parse( JSON.stringify( getQn ) );
280
+ getQn = { ...getQn, uniqueNo: randomNo };
281
+ nesedQuestion.push( getQn );
282
+ if ( answer.linkedQuestion == getQn.qno ) {
283
+ answer.oldLinkedQuestion = answer.linkedQuestion;
284
+ answer.linkedQuestion = randomNo;
285
+ }
286
+ answer.nestedQuestion[qidx] = randomNo;
287
+ }
288
+ } );
289
+ nesedQuestion.forEach( ( ele ) => {
290
+ let nestedLinkqn = [];
291
+ ele.answers.forEach( ( ans ) => {
292
+ if ( ans.linkedQuestion ) {
293
+ ans.oldNestedQuestion = !uniqueShow ? JSON.parse( JSON.stringify( ans.oldNestedQuestion ) ) : JSON.parse( JSON.stringify( ans.nestedQuestion ) );
294
+ ans.nestedQuestion.forEach( ( nested, idx ) => {
295
+ let findRandom = nesedQuestion.find( ( qn ) => ( uniqueShow ? qn.qno == nested : qn.uniqueNo == nested ) && !nestedLinkqn.includes( qn.uniqueNo ) && !qn.updated );
296
+ if ( findRandom ) {
297
+ if ( ans.linkedQuestion == findRandom.qno ) {
298
+ ans.oldLinkedQuestion = ans.linkedQuestion;
299
+ ans.linkedQuestion = findRandom.uniqueNo;
300
+ }
301
+ ans.nestedQuestion[idx] = findRandom.uniqueNo;
302
+ nestedLinkqn.push( findRandom.uniqueNo );
303
+ }
304
+ } );
305
+ }
306
+ } );
307
+ ele.updated = true;
308
+ } );
309
+ linkedQuestions.push( ...nesedQuestion );
310
+ }
311
+ } );
312
+ questions.push( ...linkedQuestions );
313
+ }
314
+ // processQuestion( question, section, questions );
315
+ }
316
+ getchecklist[index].questionAnswers[secIndex].questions = questions;
317
+ }
318
+ }
319
+ if ( !getchecklist[0].redoStatus ) {
320
+ await processedchecklist.updateOne( updateQuery, { questionAnswers: getchecklist[0].questionAnswers } );
321
+ }
322
+ getupdatedchecklist[0].questionAnswers.forEach( ( section ) => {
323
+ section.questions.forEach( async ( question ) => {
324
+ if ( question.questionReferenceImage && question.questionReferenceImage!='' ) {
325
+ question.questionReferenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + question.questionReferenceImage;
326
+ // question.questionReferenceImage = await signedUrl( { Bucket: bucket.sop, file_path: decodeURIComponent( question.questionReferenceImage ) } );
327
+ }
328
+ question.answers.forEach( async ( answer ) => {
329
+ if ( answer.referenceImage != '' ) {
330
+ answer.referenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.referenceImage;
331
+ // answer.referenceImage = await signedUrl( { Bucket: bucket.sop, file_path: decodeURIComponent( answer.referenceImage ) } );
332
+ }
333
+ } );
334
+ } );
335
+ } );
336
+ return res.sendSuccess( getchecklist );
337
+ } else {
338
+ return res.sendError( 'something went wrong please try again', 500 );
339
+ }
340
+ } catch ( e ) {
341
+ console.log( 'e =>', e );
342
+ logger.error( { function: 'startChecklist', error: e, body: req.body } );
343
+ return res.sendError( e, 500 );
344
+ }
345
+ }
346
+
93
347
  export async function startChecklist( req, res ) {
94
348
  try {
95
349
  let requestData = req.body;
@@ -627,21 +881,360 @@ export async function sopMobileTaskQuestionValidator( req, res, next ) {
627
881
  } else {
628
882
  next();
629
883
  }
630
- } else {
631
- next();
884
+ } else {
885
+ next();
886
+ }
887
+ } catch ( e ) {
888
+ logger.error( { function: 'sopMobileTaskQuestionValidator', error: e, body: req.body } );
889
+ return res.sendError( e, 500 );
890
+ }
891
+ };
892
+
893
+ export async function sopMobilechecklistMultiSectionFormatter( req, res, next ) {
894
+ try {
895
+ let requestData = req.body;
896
+ requestData.questionAnswers = typeof requestData.questionAnswers == 'string' ? JSON.parse( requestData.questionAnswers ) : requestData.questionAnswers;
897
+ let getChecklistQA = await processedchecklist.findOne( { _id: new ObjectId( requestData.processedcheckListId ) }, { questionAnswers: 1 } );
898
+ let reqAnswers = requestData.questionAnswers;
899
+ let CLQAnswers = getChecklistQA.questionAnswers;
900
+
901
+ if ( requestData.submittype == 'submit' ) {
902
+ reqAnswers.forEach( ( reqA ) => {
903
+ if ( ![ 'multiplechoicemultiple', 'multipleImage' ].includes( reqA?.answerType ) ) {
904
+ if ( ( !reqA.linkType && ( reqA.answer == null || reqA.answer == '' ) ) || ( reqA.linkType && reqA.linkquestionenabled && ( reqA.answer == null || reqA.answer == '' ) ) ) {
905
+ return res.sendError( 'Please Fill All Fields', 400 );
906
+ }
907
+ }
908
+ } );
909
+ }
910
+
911
+ let sectionFormat = [];
912
+ let uniqueSections = {};
913
+ reqAnswers.forEach( ( item ) => {
914
+ const key = `${item.section_id}-${item.sectionName}`;
915
+ if ( !uniqueSections[key] ) {
916
+ uniqueSections[key] = { id: item.section_id, name: item.sectionName };
917
+ }
918
+ } );
919
+ const uniqueArray = Object.values( uniqueSections );
920
+ for ( let section of uniqueArray ) {
921
+ let CLQSection = CLQAnswers.find( ( item ) => item.section_id == section.id );
922
+ if ( CLQSection ) {
923
+ let newArray = [];
924
+ let qaAnswers = CLQSection.questions;
925
+ let requestSection = reqAnswers.filter( ( item ) => item.section_id == section.id );
926
+ for ( let i = 0; i < requestSection.length; i++ ) {
927
+ for ( let j = 0; j < qaAnswers.length; j++ ) {
928
+ if ( requestSection[i].qno == qaAnswers[j].qno ) {
929
+ if ( qaAnswers[j].answerType == 'yes/no' ) {
930
+ let qaans = qaAnswers[j].answers;
931
+ let yn = [];
932
+ for ( let k = 0; k < qaans.length; k++ ) {
933
+ if ( requestSection[i].answer == qaans[k].answer ) {
934
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
935
+ if ( requestSection[i].validationAnswer ) {
936
+ let validateAns = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
937
+ if ( validateAns.length ) {
938
+ let splitImgUrl = validateAns.split( '/' );
939
+ if ( splitImgUrl.length > 1 ) {
940
+ splitImgUrl.splice( 0, 3 );
941
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
942
+ }
943
+ }
944
+ }
945
+ } else {
946
+ qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
947
+ qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
948
+ }
949
+ yn.push( qaans[k] );
950
+ }
951
+ }
952
+ let structure = {};
953
+ structure.qno = qaAnswers[j].qno;
954
+ structure.qname = qaAnswers[j].qname;
955
+ structure.answerType = qaAnswers[j].answerType;
956
+ structure.runAI = qaAnswers[j].runAI;
957
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
958
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
959
+ structure.remarks = requestSection[i].remarks;
960
+ structure.answers = qaAnswers[j].answers;
961
+ structure.userAnswer = yn;
962
+ structure.linkType = qaAnswers[j].linkType;
963
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
964
+ structure.parentanswer = requestSection[i].parentanswer;
965
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
966
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
967
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
968
+ if ( qaAnswers[j]?.taskId ) {
969
+ structure.taskId = qaAnswers[j]?.taskId;
970
+ structure.task = true;
971
+ }
972
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
973
+ structure.redo = false;
974
+ } else if ( requestData.submittype === 'draft' ) {
975
+ structure.redo = qaAnswers[j]?.redo;
976
+ }
977
+ if ( qaAnswers[j]?.redoComment ) {
978
+ structure.redoComment = qaAnswers[j]?.redoComment;
979
+ };
980
+ newArray.push( structure );
981
+ } else if ( qaAnswers[j].answerType == 'multiplechoicesingle' ) {
982
+ let qaans = qaAnswers[j].answers;
983
+ let ms = [];
984
+ for ( let k = 0; k < qaans.length; k++ ) {
985
+ if ( requestSection[i].answer == qaans[k].answer ) {
986
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
987
+ if ( requestSection[i].validationAnswer ) {
988
+ let validationAnswer = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
989
+ if ( validationAnswer.length ) {
990
+ let splitImgUrl = validationAnswer.split( '/' );
991
+ if ( splitImgUrl.length > 1 ) {
992
+ splitImgUrl.splice( 0, 3 );
993
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
994
+ }
995
+ }
996
+ }
997
+ } else {
998
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
999
+ qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
1000
+ }
1001
+ ms.push( qaans[k] );
1002
+ }
1003
+ }
1004
+ let structure = {};
1005
+ structure.qno = qaAnswers[j].qno;
1006
+ structure.qname = qaAnswers[j].qname;
1007
+ structure.answerType = qaAnswers[j].answerType;
1008
+ structure.runAI = qaAnswers[j].runAI;
1009
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1010
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1011
+ structure.remarks = requestSection[i].remarks;
1012
+ structure.answers = qaAnswers[j].answers;
1013
+ structure.userAnswer = ms;
1014
+ structure.linkType = qaAnswers[j].linkType;
1015
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1016
+ structure.parentanswer = requestSection[i].parentanswer;
1017
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1018
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1019
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1020
+ if ( qaAnswers[j]?.taskId ) {
1021
+ structure.taskId = qaAnswers[j]?.taskId;
1022
+ structure.task = true;
1023
+ }
1024
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1025
+ structure.redo = false;
1026
+ } else if ( requestData.submittype === 'draft' ) {
1027
+ structure.redo = qaAnswers[j]?.redo;
1028
+ }
1029
+ if ( qaAnswers[j]?.redoComment ) {
1030
+ structure.redoComment = qaAnswers[j]?.redoComment;
1031
+ };
1032
+ newArray.push( structure );
1033
+ } else if ( qaAnswers[j].answerType == 'multiplechoicemultiple' ) {
1034
+ let qaans = qaAnswers[j].answers;
1035
+ let mcmo = [];
1036
+ for ( let k = 0; k < qaans.length; k++ ) {
1037
+ let separatedArray = typeof requestSection[i].Multianswer == 'string' ? JSON.parse( requestSection[i].Multianswer ) : requestSection[i].Multianswer;
1038
+ for ( let s = 0; s < separatedArray.length; s++ ) {
1039
+ if ( separatedArray[s].answer == qaans[k].answer ) {
1040
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1041
+ if ( separatedArray[s].validationAnswer ) {
1042
+ let validationAnswer = decodeURIComponent( separatedArray[s].validationAnswer.split( '?' )[0] );
1043
+ if ( validationAnswer.length ) {
1044
+ let splitImgUrl = validationAnswer.split( '/' );
1045
+ if ( splitImgUrl.length > 1 ) {
1046
+ splitImgUrl.splice( 0, 3 );
1047
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1048
+ }
1049
+ }
1050
+ }
1051
+ } else {
1052
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1053
+ qaans[k].validationAnswer = separatedArray[s].validationAnswer || '';
1054
+ }
1055
+
1056
+ mcmo.push( qaans[k] );
1057
+ }
1058
+ }
1059
+ }
1060
+ let structure = {};
1061
+ structure.qno = qaAnswers[j].qno;
1062
+ structure.qname = qaAnswers[j].qname;
1063
+ structure.answerType = qaAnswers[j].answerType;
1064
+ structure.runAI = qaAnswers[j].runAI;
1065
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1066
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1067
+ structure.remarks = requestSection[i].remarks;
1068
+ structure.answers = qaAnswers[j].answers;
1069
+ structure.userAnswer = mcmo;
1070
+ structure.linkType = qaAnswers[j].linkType;
1071
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1072
+ structure.parentanswer = requestSection[i].parentanswer;
1073
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1074
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1075
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1076
+ if ( qaAnswers[j]?.taskId ) {
1077
+ structure.taskId = qaAnswers[j]?.taskId;
1078
+ structure.task = true;
1079
+ }
1080
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1081
+ structure.redo = false;
1082
+ } else if ( requestData.submittype === 'draft' ) {
1083
+ structure.redo = qaAnswers[j]?.redo;
1084
+ }
1085
+ if ( qaAnswers[j]?.redoComment ) {
1086
+ structure.redoComment = qaAnswers[j]?.redoComment;
1087
+ };
1088
+ newArray.push( structure );
1089
+ } else if ( qaAnswers[j].answerType == 'multipleImage' ) {
1090
+ let separatedArray = typeof requestSection[i].Multianswer == 'string' ? JSON.parse( requestSection[i].Multianswer ) : requestSection[i].Multianswer;
1091
+ let mcmi = [];
1092
+ // for (let k = 0; k < qaans.length; k++) {
1093
+ for ( let s = 0; s < separatedArray.length; s++ ) {
1094
+ if ( separatedArray[s].answer && separatedArray[s].answer !='' ) {
1095
+ let newAnswer = {};
1096
+ let validationAnswer = decodeURIComponent( separatedArray[s].answer.split( '?' )[0] );
1097
+ if ( validationAnswer.length ) {
1098
+ let splitImgUrl = validationAnswer.split( '/' );
1099
+ if ( splitImgUrl.length > 1 ) {
1100
+ splitImgUrl.splice( 0, 3 );
1101
+ newAnswer.answer = splitImgUrl.join( '/' ) || '';
1102
+ }
1103
+ }
1104
+
1105
+ newAnswer.answeroptionNumber = 0;
1106
+ newAnswer.sopFlag = false;
1107
+ newAnswer.validation = false;
1108
+ newAnswer.validationType = '';
1109
+ newAnswer.referenceImage = '';
1110
+ newAnswer.allowUploadfromGallery = false;
1111
+ newAnswer.runAI = false;
1112
+ newAnswer.descriptivetype = '';
1113
+ newAnswer.showLinked = false;
1114
+ newAnswer.linkedQuestion = 0;
1115
+ newAnswer.nestedQuestion = [];
1116
+ newAnswer.index = s;
1117
+ mcmi.push( newAnswer );
1118
+ }
1119
+ }
1120
+ // }
1121
+ let structure = {};
1122
+ structure.qno = qaAnswers[j].qno;
1123
+ structure.qname = qaAnswers[j].qname;
1124
+ structure.answerType = qaAnswers[j].answerType;
1125
+ structure.runAI = qaAnswers[j].runAI;
1126
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1127
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1128
+ structure.remarks = requestSection[i].remarks;
1129
+ structure.answers = qaAnswers[j].answers;
1130
+ structure.userAnswer = mcmi;
1131
+ structure.linkType = qaAnswers[j].linkType;
1132
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1133
+ structure.parentanswer = requestSection[i].parentanswer;
1134
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1135
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1136
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1137
+ if ( qaAnswers[j]?.taskId ) {
1138
+ structure.taskId = qaAnswers[j]?.taskId;
1139
+ structure.task = true;
1140
+ }
1141
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1142
+ structure.redo = false;
1143
+ } else if ( requestData.submittype === 'draft' ) {
1144
+ structure.redo = qaAnswers[j]?.redo;
1145
+ }
1146
+ if ( qaAnswers[j]?.redoComment ) {
1147
+ structure.redoComment = qaAnswers[j]?.redoComment;
1148
+ };
1149
+ newArray.push( structure );
1150
+ } else {
1151
+ let des = [];
1152
+ if ( requestSection[i].answer != 'null' && requestSection[i].answer != '' && requestSection[i].answer != null ) {
1153
+ let validationAnswer = '';
1154
+ if ( requestSection[i].answer.split( '?' ).length > 1 ) {
1155
+ validationAnswer = decodeURIComponent( requestSection[i].answer.split( '?' )[0] );
1156
+ }
1157
+ if ( validationAnswer.length ) {
1158
+ let splitImgUrl = validationAnswer.split( '/' );
1159
+ if ( splitImgUrl.length > 1 ) {
1160
+ splitImgUrl.splice( 0, 3 );
1161
+ requestSection[i].answer = splitImgUrl.join( '/' );
1162
+ }
1163
+ }
1164
+ let ansstructure = {
1165
+ answer: requestSection[i].answer,
1166
+ answeroptionNumber: 1,
1167
+ sopFlag: false,
1168
+ validation: false,
1169
+ validationType: '',
1170
+ validationAnswer: '',
1171
+ referenceImage: qaAnswers[j].answers[0].referenceImage,
1172
+ showLinked: qaAnswers[j].answers[0].showLinked,
1173
+ linkedQuestion: qaAnswers[j].answers[0].linkedQuestion,
1174
+ };
1175
+ if ( qaAnswers[j].answerType == 'date' ) {
1176
+ ansstructure.dateRangeType = requestSection[i].dateRangeType || false;
1177
+ }
1178
+ des.push( ansstructure );
1179
+ }
1180
+ let structure = {};
1181
+ structure.qno = qaAnswers[j].qno;
1182
+ structure.qname = qaAnswers[j].qname;
1183
+ structure.answerType = qaAnswers[j].answerType;
1184
+ structure.runAI = qaAnswers[j].runAI;
1185
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1186
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1187
+ structure.remarks = requestSection[i].remarks;
1188
+ structure.answers = qaAnswers[j].answers;
1189
+ structure.userAnswer = des;
1190
+ structure.linkType = qaAnswers[j].linkType;
1191
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1192
+ structure.parentanswer = requestSection[i].parentanswer;
1193
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1194
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1195
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1196
+ if ( qaAnswers[j]?.taskId ) {
1197
+ structure.taskId = qaAnswers[j]?.taskId;
1198
+ structure.task = true;
1199
+ }
1200
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1201
+ structure.redo = false;
1202
+ } else if ( requestData.submittype === 'draft' ) {
1203
+ structure.redo = qaAnswers[j]?.redo;
1204
+ }
1205
+ if ( qaAnswers[j]?.redoComment ) {
1206
+ structure.redoComment = qaAnswers[j]?.redoComment;
1207
+ };
1208
+ newArray.push( structure );
1209
+ }
1210
+ }
1211
+ }
1212
+ }
1213
+ let sectionData = {
1214
+ 'section_id': section.id,
1215
+ 'sectionName': section.name,
1216
+ 'questions': newArray,
1217
+ };
1218
+ sectionFormat.push( sectionData );
1219
+ }
632
1220
  }
633
- } catch ( e ) {
634
- logger.error( { function: 'sopMobileTaskQuestionValidator', error: e, body: req.body } );
635
- return res.sendError( e, 500 );
1221
+
1222
+
1223
+ requestData.questionAnswers = sectionFormat;
1224
+ next();
1225
+ } catch ( error ) {
1226
+ logger.error( { function: 'sopMobilechecklistMultiSectionFormatter', error: error, body: req.body } );
1227
+ return res.sendError( error, 500 );
636
1228
  }
637
1229
  };
638
1230
 
639
- export async function sopMobilechecklistMultiSectionFormatter( req, res, next ) {
1231
+ export async function sopMobilechecklistMultiSectionFormatterv2( req, res, next ) {
640
1232
  try {
641
1233
  let requestData = req.body;
642
1234
  requestData.questionAnswers = typeof requestData.questionAnswers == 'string' ? JSON.parse( requestData.questionAnswers ) : requestData.questionAnswers;
643
1235
  let getChecklistQA = await processedchecklist.findOne( { _id: new ObjectId( requestData.processedcheckListId ) }, { questionAnswers: 1 } );
644
1236
  let reqAnswers = requestData.questionAnswers;
1237
+ logger.error( { functionName: 'updatedPayload', message: reqAnswers } );
645
1238
  let CLQAnswers = getChecklistQA.questionAnswers;
646
1239
 
647
1240
  if ( requestData.submittype == 'submit' ) {
@@ -654,6 +1247,58 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
654
1247
  } );
655
1248
  }
656
1249
 
1250
+ if ( requestData?.editSubmit && requestData?.editSubmit == 'true' ) {
1251
+ let sampleData = reqAnswers[0];
1252
+ CLQAnswers.forEach( ( section ) => {
1253
+ let requestSection = reqAnswers.filter( ( reqSection ) => reqSection.sectionName == section?.sectionOldName || reqSection.sectionName == section?.sectionName );
1254
+ if ( requestSection.length ) {
1255
+ requestSection.forEach( ( item ) => item.section_id = section.section_id );
1256
+ section.questions.forEach( ( question ) => {
1257
+ let sectionQuestion = requestSection.filter( ( secQuestion ) => secQuestion.qname == question.oldQname || secQuestion.qname == question.qname );
1258
+ if ( !sectionQuestion.length ) {
1259
+ let data = { ...requestSection[0] };
1260
+ data.answerType = question.answerType;
1261
+ data.qno = question.qno;
1262
+ data.qname = question.qname;
1263
+ data.oldQname = question?.oldQname || question.qname;
1264
+ data.answer = 'null';
1265
+ data.remarks = '';
1266
+ data.validationAnswer = '';
1267
+ data.Multianswer = [];
1268
+ data.linkType = question.linkType;
1269
+ data.linkquestionenabled = question.linkType;
1270
+ data.linkedQuestion = null;
1271
+ data.showLinked = null;
1272
+ data.parentanswer = '';
1273
+ data.dateRangeType = false;
1274
+ reqAnswers.push( data );
1275
+ }
1276
+ } );
1277
+ } else {
1278
+ section.questions.forEach( ( ele ) => {
1279
+ let data = { ...sampleData };
1280
+ data.section_id = section.section_id;
1281
+ data.sectionName = section.sectionName;
1282
+ data.answerType = ele.answerType;
1283
+ data.qno = ele.qno;
1284
+ data.qname = ele.qname;
1285
+ data.oldQname = ele?.oldQname || ele.qname;
1286
+ data.answer = 'null';
1287
+ data.remarks = '';
1288
+ data.validationAnswer = '';
1289
+ data.Multianswer = [];
1290
+ data.linkType = ele.linkType;
1291
+ data.linkquestionenabled = ele.linkType;
1292
+ data.linkedQuestion = null;
1293
+ data.showLinked = null;
1294
+ data.parentanswer = '';
1295
+ data.dateRangeType = false;
1296
+ reqAnswers.push( data );
1297
+ } );
1298
+ }
1299
+ } );
1300
+ }
1301
+
657
1302
  let sectionFormat = [];
658
1303
  let uniqueSections = {};
659
1304
  reqAnswers.forEach( ( item ) => {
@@ -664,18 +1309,23 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
664
1309
  } );
665
1310
  const uniqueArray = Object.values( uniqueSections );
666
1311
  for ( let section of uniqueArray ) {
667
- let CLQSection = CLQAnswers.find( ( item ) => item.section_id == section.id );
1312
+ let CLQSection = CLQAnswers.find( ( item ) => item.section_id.toString() == section.id.toString() );
668
1313
  if ( CLQSection ) {
669
1314
  let newArray = [];
670
1315
  let qaAnswers = CLQSection.questions;
671
1316
  let requestSection = reqAnswers.filter( ( item ) => item.section_id == section.id );
672
1317
  for ( let i = 0; i < requestSection.length; i++ ) {
673
1318
  for ( let j = 0; j < qaAnswers.length; j++ ) {
674
- if ( requestSection[i].qno == qaAnswers[j].qno ) {
1319
+ 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 ) ) ) {
675
1320
  if ( qaAnswers[j].answerType == 'yes/no' ) {
676
1321
  let qaans = qaAnswers[j].answers;
677
1322
  let yn = [];
678
1323
  for ( let k = 0; k < qaans.length; k++ ) {
1324
+ // let requestAnswer = typeof requestSection[i].questionAnswers == 'string' ? JSON.parse( requestSection[i].questionAnswers ) : requestSection[i].questionAnswers;
1325
+ // let findAnswer = requestAnswer.find( ( ele ) => ele.answer == qaans[k].answer && ele.);
1326
+ // if ( findAnswer ) {
1327
+ // qaans[k].nestedQuestion = typeof findAnswer.nestedQuestion == 'string' ? JSON.parse( findAnswer.nestedQuestion ) : findAnswer.nestedQuestion;
1328
+ // }
679
1329
  if ( requestSection[i].answer == qaans[k].answer ) {
680
1330
  if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
681
1331
  if ( requestSection[i].validationAnswer ) {
@@ -689,7 +1339,7 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
689
1339
  }
690
1340
  }
691
1341
  } else {
692
- qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1342
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
693
1343
  qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
694
1344
  }
695
1345
  yn.push( qaans[k] );
@@ -710,14 +1360,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
710
1360
  structure.parentanswer = requestSection[i].parentanswer;
711
1361
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
712
1362
  structure.descriptivetype = qaAnswers[j].descriptivetype;
713
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1363
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1364
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
714
1365
  if ( qaAnswers[j]?.taskId ) {
715
1366
  structure.taskId = qaAnswers[j]?.taskId;
716
1367
  structure.task = true;
717
1368
  }
718
1369
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
719
1370
  structure.redo = false;
720
- } else if ( requestData.submittype === 'draft' ) {
1371
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
721
1372
  structure.redo = qaAnswers[j]?.redo;
722
1373
  }
723
1374
  if ( qaAnswers[j]?.redoComment ) {
@@ -728,6 +1379,11 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
728
1379
  let qaans = qaAnswers[j].answers;
729
1380
  let ms = [];
730
1381
  for ( let k = 0; k < qaans.length; k++ ) {
1382
+ // let requestAnswer = typeof requestSection[i].questionAnswers == 'string' ? JSON.parse( requestSection[i].questionAnswers ) : requestSection[i].questionAnswers;
1383
+ // let findAnswer= requestAnswer.find( ( ele ) => ele.answer == qaans[k].answer );
1384
+ // if ( findAnswer ) {
1385
+ // qaans[k].nestedQuestion = typeof findAnswer.nestedQuestion == 'string' ? JSON.parse( findAnswer.nestedQuestion ) : findAnswer.nestedQuestion;
1386
+ // }
731
1387
  if ( requestSection[i].answer == qaans[k].answer ) {
732
1388
  if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
733
1389
  if ( requestSection[i].validationAnswer ) {
@@ -762,14 +1418,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
762
1418
  structure.parentanswer = requestSection[i].parentanswer;
763
1419
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
764
1420
  structure.descriptivetype = qaAnswers[j].descriptivetype;
765
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1421
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1422
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
766
1423
  if ( qaAnswers[j]?.taskId ) {
767
1424
  structure.taskId = qaAnswers[j]?.taskId;
768
1425
  structure.task = true;
769
1426
  }
770
1427
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
771
1428
  structure.redo = false;
772
- } else if ( requestData.submittype === 'draft' ) {
1429
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
773
1430
  structure.redo = qaAnswers[j]?.redo;
774
1431
  }
775
1432
  if ( qaAnswers[j]?.redoComment ) {
@@ -818,14 +1475,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
818
1475
  structure.parentanswer = requestSection[i].parentanswer;
819
1476
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
820
1477
  structure.descriptivetype = qaAnswers[j].descriptivetype;
821
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1478
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1479
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
822
1480
  if ( qaAnswers[j]?.taskId ) {
823
1481
  structure.taskId = qaAnswers[j]?.taskId;
824
1482
  structure.task = true;
825
1483
  }
826
1484
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
827
1485
  structure.redo = false;
828
- } else if ( requestData.submittype === 'draft' ) {
1486
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
829
1487
  structure.redo = qaAnswers[j]?.redo;
830
1488
  }
831
1489
  if ( qaAnswers[j]?.redoComment ) {
@@ -879,14 +1537,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
879
1537
  structure.parentanswer = requestSection[i].parentanswer;
880
1538
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
881
1539
  structure.descriptivetype = qaAnswers[j].descriptivetype;
882
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1540
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1541
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
883
1542
  if ( qaAnswers[j]?.taskId ) {
884
1543
  structure.taskId = qaAnswers[j]?.taskId;
885
1544
  structure.task = true;
886
1545
  }
887
1546
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
888
1547
  structure.redo = false;
889
- } else if ( requestData.submittype === 'draft' ) {
1548
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
890
1549
  structure.redo = qaAnswers[j]?.redo;
891
1550
  }
892
1551
  if ( qaAnswers[j]?.redoComment ) {
@@ -897,7 +1556,7 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
897
1556
  let des = [];
898
1557
  if ( requestSection[i].answer != 'null' && requestSection[i].answer != '' && requestSection[i].answer != null ) {
899
1558
  let validationAnswer = '';
900
- if ( requestSection[i].answer.split( '?' ).length > 1 ) {
1559
+ if ( requestSection[i].answer.includes( 'https' ) ) {
901
1560
  validationAnswer = decodeURIComponent( requestSection[i].answer.split( '?' )[0] );
902
1561
  }
903
1562
  if ( validationAnswer.length ) {
@@ -938,14 +1597,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
938
1597
  structure.parentanswer = requestSection[i].parentanswer;
939
1598
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
940
1599
  structure.descriptivetype = qaAnswers[j].descriptivetype;
941
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1600
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1601
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
942
1602
  if ( qaAnswers[j]?.taskId ) {
943
1603
  structure.taskId = qaAnswers[j]?.taskId;
944
1604
  structure.task = true;
945
1605
  }
946
1606
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
947
1607
  structure.redo = false;
948
- } else if ( requestData.submittype === 'draft' ) {
1608
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
949
1609
  structure.redo = qaAnswers[j]?.redo;
950
1610
  }
951
1611
  if ( qaAnswers[j]?.redoComment ) {
@@ -967,6 +1627,7 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
967
1627
 
968
1628
 
969
1629
  requestData.questionAnswers = sectionFormat;
1630
+ logger.error( { message: requestData.questionAnswers, error: 'QuestionanswersPayload' } );
970
1631
  next();
971
1632
  } catch ( error ) {
972
1633
  logger.error( { function: 'sopMobilechecklistMultiSectionFormatter', error: error, body: req.body } );
@@ -2462,7 +3123,7 @@ export async function checklistv1( req, res ) {
2462
3123
  ...projectExtraConditions,
2463
3124
  client_id: { $ifNull: [ '$client_id', '' ] },
2464
3125
  coverage: { $ifNull: [ '$coverage', '' ] },
2465
- taskType: { $ifNull: [ '$type', '' ] },
3126
+ taskType: { $ifNull: [ '$planoType', '' ] },
2466
3127
  },
2467
3128
  },
2468
3129
  ];
@@ -2512,6 +3173,262 @@ export async function checklistv1( req, res ) {
2512
3173
  }
2513
3174
  }
2514
3175
 
3176
+ export async function questionListv1( req, res ) {
3177
+ try {
3178
+ let requestData = req.query;
3179
+ if ( !requestData.processedcheckListId ) {
3180
+ res.sendError( 'processedcheckListId is Required', 400 );
3181
+ }
3182
+
3183
+ let findQuery = [];
3184
+ let findAndQuery = [];
3185
+ findAndQuery.push( { _id: new ObjectId( requestData.processedcheckListId ) } );
3186
+ findQuery.push( { $match: { $and: findAndQuery } } );
3187
+
3188
+ findQuery.push( {
3189
+ $project: {
3190
+ checkListName: { $ifNull: [ '$checkListName', '' ] },
3191
+ scheduleStartTime: { $ifNull: [ '$scheduleStartTime', '' ] },
3192
+ scheduleStartTime_iso: { $ifNull: [ '$scheduleStartTime_iso', '' ] },
3193
+ scheduleEndTime: { $ifNull: [ '$scheduleEndTime', '' ] },
3194
+ scheduleEndTime_iso: { $ifNull: [ '$scheduleEndTime_iso', '' ] },
3195
+ checklistStatus: { $ifNull: [ '$checklistStatus', '' ] },
3196
+ checkListId: { $ifNull: [ '$checkListId', '' ] },
3197
+ startTime: { $ifNull: [ '$startTime', '' ] },
3198
+ submitTime: { $ifNull: [ '$submitTime', '' ] },
3199
+ allowedOverTime: { $ifNull: [ '$allowedOverTime', '' ] },
3200
+ // allowedStoreLocation: { $ifNull: [ '$allowedStoreLocation', '' ] },
3201
+ allowedStoreLocation: {
3202
+ $cond: {
3203
+ if: { $eq: [ '$client_id', '11' ] },
3204
+ then: false,
3205
+ else: {
3206
+ '$cond': {
3207
+ 'if': { '$eq': [ '$coverage', 'user' ] },
3208
+ 'then': false,
3209
+ 'else': { '$ifNull': [ '$allowedStoreLocation', false ] },
3210
+ },
3211
+ },
3212
+ },
3213
+ },
3214
+ reinitiateStatus: { $ifNull: [ '$reinitiateStatus', '' ] },
3215
+ questionAnswers: { $ifNull: [ '$questionAnswers', '' ] },
3216
+ getchecklist: { $ifNull: [ '$getchecklist', '' ] },
3217
+ userEmail: { $ifNull: [ '$userEmail', '' ] },
3218
+ storeName: { $ifNull: [ '$storeName', '' ] },
3219
+ redoStatus: { $ifNull: [ '$redoStatus', false ] },
3220
+ rawImageUpload: { $ifNull: [ '$rawImageUpload', false ] },
3221
+ },
3222
+ } );
3223
+
3224
+ let getchecklist = await processedchecklist.aggregate( findQuery );
3225
+ if ( !getchecklist.length ) {
3226
+ return res.sendError( 'Check List Got Edited Please Fill Again', 422 );
3227
+ } else {
3228
+ logger.info( `v5 => Checklist Continue => store Name: ${getchecklist[0].storeName}, User Email: ${getchecklist[0].userEmail}, Checklist Name: ${getchecklist[0].checkListName}` );
3229
+ // let bucket = JSON.parse( process.env.BUCKET );
3230
+ for ( let [ secIndex, section ] of getchecklist[0].questionAnswers.entries() ) {
3231
+ for ( let [ questionIndex, question ] of section.questions.entries() ) {
3232
+ let Multianswer = [];
3233
+ if ( getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage && getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage !='' ) {
3234
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage = await signedUrl( { file_path: decodeURIComponent( getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage ), Bucket: bucket.sop } );
3235
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage;
3236
+ }
3237
+ for ( let [ ansIndex, answer ] of question.answers.entries() ) {
3238
+ if ( question.answerType == 'multiplechoicemultiple' ) {
3239
+ let checkvalidation = null;
3240
+ if ( answer.validationAnswer && answer.validationAnswer !='' ) {
3241
+ if ( answer.validationType != 'Descriptive Answer' ) {
3242
+ // checkvalidation = await signedUrl( { file_path: decodeURIComponent( answer.validationAnswer ), Bucket: bucket.sop } );
3243
+ checkvalidation = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.validationAnswer;
3244
+ } else {
3245
+ checkvalidation = answer.validationAnswer;
3246
+ }
3247
+ }
3248
+ Multianswer.push( { 'answer': answer.answer, 'no': ansIndex, 'validationAnswer': checkvalidation } );
3249
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].index = ansIndex;
3250
+ }
3251
+ if ( answer.referenceImage != '' ) {
3252
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].referenceImage = await signedUrl( { file_path: decodeURIComponent( answer.referenceImage ), Bucket: bucket.sop } );
3253
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].referenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.referenceImage;
3254
+ }
3255
+ if ( ( answer.validationType == 'Capture Image' || answer.validationType == 'Capture Video' ) && answer.validationAnswer && answer.validationAnswer != '' ) {
3256
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].validationAnswer = await signedUrl( { file_path: decodeURIComponent( answer.validationAnswer ), Bucket: bucket.sop } );
3257
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].validationAnswer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.validationAnswer;
3258
+ }
3259
+ }
3260
+ if ( question?.userAnswer ) {
3261
+ for ( let [ userAnsIndex, userAns ] of question.userAnswer.entries() ) {
3262
+ if ( ( userAns.validationType == 'Capture Image' || userAns.validationType == 'Capture Video' ) && userAns.validationAnswer && userAns.validationAnswer != '' ) {
3263
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].validationAnswer = await signedUrl( { file_path: decodeURIComponent( userAns.validationAnswer ), Bucket: bucket.sop } );
3264
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].validationAnswer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.validationAnswer;
3265
+ }
3266
+ if ( [ 'image', 'descriptiveImage', 'video' ].includes( question.answerType ) && userAns.answer != '' ) {
3267
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].answer = await signedUrl( { file_path: decodeURIComponent( userAns.answer ), Bucket: bucket.sop } );
3268
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].answer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.answer;
3269
+ }
3270
+ if ( userAns.referenceImage != '' ) {
3271
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].referenceImage = await signedUrl( { file_path: decodeURIComponent( userAns.referenceImage ), Bucket: bucket.sop } );
3272
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].referenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.referenceImage;
3273
+ }
3274
+ if ( question.answerType == 'multiplechoicemultiple' ) {
3275
+ let ansIndex = Multianswer.findIndex( ( item ) => item.answer == userAns.answer );
3276
+ if ( ansIndex ) {
3277
+ Multianswer[ansIndex].validationAnswer = userAns.validationAnswer;
3278
+ }
3279
+ }
3280
+ if ( question.answerType == 'multipleImage' ) {
3281
+ if ( userAns && userAns.answer && userAns.answer !='' ) {
3282
+ // let manswer = await signedUrl( { file_path: decodeURIComponent( userAns.answer ), Bucket: bucket.sop } );
3283
+ let manswer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.answer;
3284
+ Multianswer.push( { 'answer': manswer, 'no': userAnsIndex, 'validationAnswer': '' } );
3285
+ } else {
3286
+
3287
+ }
3288
+ }
3289
+ }
3290
+ }
3291
+ if ( question.answerType == 'multiplechoicemultiple' ) {
3292
+ Multianswer.forEach( ( item ) => {
3293
+ if ( item.validationAnswer == null ) {
3294
+ item.answer = null;
3295
+ }
3296
+ } );
3297
+ if ( Multianswer.length ) {
3298
+ question.Multianswer = Multianswer;
3299
+ } else {
3300
+ question.Multianswer = Multianswer;
3301
+ }
3302
+ }
3303
+ if ( question.answerType == 'multipleImage' ) {
3304
+ if ( Multianswer.length ) {
3305
+ question.Multianswer = Multianswer;
3306
+ } else {
3307
+ Multianswer.push( { 'answer': null, 'no': 0, 'validationAnswer': null } );
3308
+ question.Multianswer = Multianswer;
3309
+ }
3310
+ }
3311
+ }
3312
+ }
3313
+
3314
+ let questions = [];
3315
+ // function processQuestion( question, section, questions, nested=false ) {
3316
+ // let findExists = questions.find( ( item ) => item?.qno && item.qno == question.qno );
3317
+ // if ( findExists && nested ) {
3318
+ // let findIndex = questions.findIndex( ( item ) => item?.qno && item.qno == question.qno );
3319
+ // questions.splice( findIndex, 1 );
3320
+ // questions.push( question );
3321
+ // for ( let answer of question.answers ) {
3322
+ // if ( answer.showLinked && answer?.linkedQuestion != '' ) {
3323
+ // let linkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
3324
+ // if ( linkedQuestion ) {
3325
+ // processQuestion( linkedQuestion, section, questions, true );
3326
+ // }
3327
+ // }
3328
+ // }
3329
+ // }
3330
+ // if ( !findExists ) {
3331
+ // questions.push( question );
3332
+ // for ( let answer of question.answers ) {
3333
+ // if ( answer.showLinked && answer?.linkedQuestion != '' ) {
3334
+ // let linkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
3335
+ // if ( linkedQuestion ) {
3336
+ // processQuestion( linkedQuestion, section, questions, true );
3337
+ // }
3338
+ // }
3339
+ // }
3340
+ // }
3341
+ // }
3342
+
3343
+ for ( let [ index, data ] of getchecklist.entries() ) {
3344
+ for ( let [ secIndex, section ] of data.questionAnswers.entries() ) {
3345
+ questions = [];
3346
+ for ( let [ questionIndex, question ] of section.questions.entries() ) {
3347
+ let linkedQuestions;
3348
+ let uniqueShow = false;
3349
+ if ( !question.linkType ) {
3350
+ if ( !question?.uniqueNo ) {
3351
+ uniqueShow = true;
3352
+ question.uniqueNo = parseInt( getOtp() ) + Date.now() + questionIndex;
3353
+ }
3354
+ questions.push( question );
3355
+ linkedQuestions = [];
3356
+ let questionList;
3357
+ if ( uniqueShow ) {
3358
+ questionList = new Map( section.questions.map( ( ele ) => [ ele.qno, ele ] ) );
3359
+ } else {
3360
+ let uniqueList = question.answers.flatMap( ( ele ) => ele.nestedQuestion );
3361
+ let filteredQuestion = section.questions.filter( ( ele ) => uniqueList.includes( ele?.uniqueNo ) );
3362
+ questionList = new Map( filteredQuestion.map( ( ele ) => [ ele.uniqueNo, ele ] ) );
3363
+ }
3364
+ question.answers.forEach( ( answer ) => {
3365
+ if ( answer.linkedQuestion ) {
3366
+ let nesedQuestion = [];
3367
+ answer.oldNestedQuestion = !uniqueShow ? JSON.parse( JSON.stringify( answer.oldNestedQuestion ) ) : JSON.parse( JSON.stringify( answer.nestedQuestion ) );
3368
+ answer.nestedQuestion.forEach( ( qn, qidx ) => {
3369
+ let getQn = questionList.get( qn );
3370
+ if ( getQn ) {
3371
+ let randomNo;
3372
+ if ( !getQn?.uniqueNo || uniqueShow ) {
3373
+ randomNo = parseInt( getOtp() ) + Date.now() + qidx;
3374
+ } else {
3375
+ randomNo = getQn?.uniqueNo;
3376
+ }
3377
+ getQn = JSON.parse( JSON.stringify( getQn ) );
3378
+ getQn = { ...getQn, uniqueNo: randomNo };
3379
+ nesedQuestion.push( getQn );
3380
+ if ( answer.linkedQuestion == getQn.qno ) {
3381
+ answer.oldLinkedQuestion = answer.linkedQuestion;
3382
+ answer.linkedQuestion = randomNo;
3383
+ }
3384
+ answer.nestedQuestion[qidx] = randomNo;
3385
+ }
3386
+ } );
3387
+ console.log( nesedQuestion );
3388
+ nesedQuestion.forEach( ( ele ) => {
3389
+ let nestedLinkqn = [];
3390
+ ele.answers.forEach( ( ans ) => {
3391
+ if ( ans.linkedQuestion ) {
3392
+ ans.oldNestedQuestion = !uniqueShow ? JSON.parse( JSON.stringify( ans.oldNestedQuestion ) ) : JSON.parse( JSON.stringify( ans.nestedQuestion ) );
3393
+ ans.nestedQuestion.forEach( ( nested, idx ) => {
3394
+ console.log( nested, uniqueShow, 'nested', nestedLinkqn );
3395
+ let findRandom = nesedQuestion.find( ( qn ) => ( uniqueShow ? qn.qno == nested : qn.uniqueNo == nested ) && !nestedLinkqn.includes( qn.uniqueNo ) && !qn.updated );
3396
+ console.log( findRandom );
3397
+ if ( findRandom ) {
3398
+ if ( ans.linkedQuestion == findRandom.qno ) {
3399
+ ans.oldLinkedQuestion = ans.linkedQuestion;
3400
+ ans.linkedQuestion = findRandom.uniqueNo;
3401
+ }
3402
+ ans.nestedQuestion[idx] = findRandom.uniqueNo;
3403
+ nestedLinkqn.push( findRandom.uniqueNo );
3404
+ }
3405
+ } );
3406
+ }
3407
+ } );
3408
+ ele.updated = true;
3409
+ } );
3410
+ linkedQuestions.push( ...nesedQuestion );
3411
+ }
3412
+ } );
3413
+ questions.push( ...linkedQuestions );
3414
+ }
3415
+ // processQuestion( question, section, questions );
3416
+ }
3417
+ getchecklist[index].questionAnswers[secIndex].questions = questions;
3418
+ }
3419
+ }
3420
+ if ( !getchecklist[0].redoStatus ) {
3421
+ await processedchecklist.updateOne( { _id: requestData.processedcheckListId }, { questionAnswers: getchecklist[0].questionAnswers } );
3422
+ }
3423
+
3424
+ return res.sendSuccess( getchecklist );
3425
+ }
3426
+ } catch ( e ) {
3427
+ logger.error( { function: 'questionList', error: e, body: req.body } );
3428
+ return res.sendError( e, 500 );
3429
+ }
3430
+ }
3431
+
2515
3432
  export async function questionList( req, res ) {
2516
3433
  try {
2517
3434
  let requestData = req.query;
@@ -3086,10 +4003,12 @@ export async function uploadAnswerImage( req, res ) {
3086
4003
  }
3087
4004
 
3088
4005
  if ( imageUrl != '' ) {
3089
- imageUrl = await signedUrl( { file_path: imageUrl.Key, Bucket: bucket.sop } );
4006
+ // imageUrl = await signedUrl( { file_path: imageUrl.Key, Bucket: bucket.sop } );
4007
+ imageUrl = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + imageUrl.Key;
3090
4008
 
3091
4009
  return res.sendSuccess( {
3092
4010
  bucketName: bucket.sop,
4011
+ path: imageUrl.Key,
3093
4012
  imageUrl: imageUrl,
3094
4013
  message: 'Image uploaded successfully!',
3095
4014
  } );
@@ -3426,11 +4345,19 @@ export async function updatePlanoStatus( req, res ) {
3426
4345
  if ( !processCheckDetails ) {
3427
4346
  return res.sendError( 'No data found', 204 );
3428
4347
  }
3429
- processCheckDetails.checklistStatus = req.body.status;
3430
- processCheckDetails.save();
4348
+ let storeTimeZone = await storeService.findOne( { storeName: { $regex: processCheckDetails.storeName, $options: 'i' }, clientId: processCheckDetails.client_id }, { 'storeProfile.timeZone': 1 } );
4349
+ let currentDateTime;
4350
+ if ( storeTimeZone?.storeProfile?.timeZone ) {
4351
+ currentDateTime = dayjs().tz( storeTimeZone?.storeProfile?.timeZone );
4352
+ } else {
4353
+ currentDateTime = requestData?.currentTime ? dayjs( requestData.currentTime, 'HH:mm:ss' ) : dayjs();
4354
+ }
4355
+ let submitTimeString = currentDateTime.format( 'hh:mm A, DD MMM YYYY' );
4356
+ await processedchecklist.updateOne( { _id: req.body.id }, { checklistStatus: req.body.status, ...( req.body.status == 'inprogress' ) ? { startTime_string: submitTimeString } : { submitTime_string: submitTimeString } } );
3431
4357
  return res.sendSuccess( 'Status updated Successfully' );
3432
4358
  } catch ( e ) {
3433
4359
  logger.error( { error: e, function: 'updatePlanoStatus' } );
3434
4360
  return res.sendError( e, 500 );
3435
4361
  }
3436
4362
  }
4363
+