tango-app-api-trax 3.4.0-alpha-0 → 3.4.0-alpha-2

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,242 @@ 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 [ questionIdx, question ] of section.questions.entries() ) {
249
+ let linkedQuestions;
250
+ if ( !question.linkType ) {
251
+ question.uniqueNo = parseInt( getOtp() )+ Date.now() + questionIdx;
252
+ questions.push( question );
253
+ linkedQuestions = [];
254
+ let questionList = new Map( section.questions.map( ( ele ) => [ ele.qno, ele ] ) );
255
+ question.answers.forEach( ( answer ) => {
256
+ if ( answer.linkedQuestion ) {
257
+ let nesedQuestion = [];
258
+ answer.oldNestedQuestion = JSON.parse( JSON.stringify( answer.nestedQuestion ) );
259
+ answer.nestedQuestion.forEach( ( qn, qidx ) => {
260
+ let getQn = questionList.get( qn );
261
+ if ( getQn ) {
262
+ let randomNo = parseInt( getOtp() ) + Date.now() + qidx;
263
+ getQn = JSON.parse( JSON.stringify( getQn ) );
264
+ getQn = { ...getQn, uniqueNo: randomNo };
265
+ nesedQuestion.push( getQn );
266
+ if ( answer.linkedQuestion == getQn.qno ) {
267
+ answer.oldLinkedQuestion = answer.linkedQuestion;
268
+ answer.linkedQuestion = randomNo;
269
+ }
270
+ answer.nestedQuestion[qidx] = randomNo;
271
+ }
272
+ } );
273
+ nesedQuestion.forEach( ( ele ) => {
274
+ let nestedLinkqn = [];
275
+ ele.answers.forEach( ( ans ) => {
276
+ if ( ans.linkedQuestion ) {
277
+ ans.oldNestedQuestion = JSON.parse( JSON.stringify( ans.nestedQuestion ) );
278
+ ans.nestedQuestion.forEach( ( nested, idx ) => {
279
+ let findRandom = nesedQuestion.find( ( qn ) => qn.qno == nested && !nestedLinkqn.includes( qn.uniqueNo ) && !qn.updated );
280
+ if ( findRandom ) {
281
+ if ( ans.linkedQuestion == findRandom.qno ) {
282
+ ans.oldLinkedQuestion = ans.linkedQuestion;
283
+ ans.linkedQuestion = findRandom.uniqueNo;
284
+ }
285
+ ans.nestedQuestion[idx] = findRandom.uniqueNo;
286
+ nestedLinkqn.push( findRandom.uniqueNo );
287
+ }
288
+ } );
289
+ }
290
+ } );
291
+ ele.updated = true;
292
+ } );
293
+ linkedQuestions.push( ...nesedQuestion );
294
+ }
295
+ } );
296
+ questions.push( ...linkedQuestions );
297
+ }
298
+ // processQuestion( question, section, questions );
299
+ }
300
+ getchecklist[index].questionAnswers[secIndex].questions = questions;
301
+ }
302
+ }
303
+ await processedchecklist.updateOne( updateQuery, { questionAnswers: getchecklist[0].questionAnswers } );
304
+ getupdatedchecklist[0].questionAnswers.forEach( ( section ) => {
305
+ section.questions.forEach( async ( question ) => {
306
+ if ( question.questionReferenceImage && question.questionReferenceImage!='' ) {
307
+ question.questionReferenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + question.questionReferenceImage;
308
+ // question.questionReferenceImage = await signedUrl( { Bucket: bucket.sop, file_path: decodeURIComponent( question.questionReferenceImage ) } );
309
+ }
310
+ question.answers.forEach( async ( answer ) => {
311
+ if ( answer.referenceImage != '' ) {
312
+ answer.referenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.referenceImage;
313
+ // answer.referenceImage = await signedUrl( { Bucket: bucket.sop, file_path: decodeURIComponent( answer.referenceImage ) } );
314
+ }
315
+ } );
316
+ } );
317
+ } );
318
+ return res.sendSuccess( getchecklist );
319
+ } else {
320
+ return res.sendError( 'something went wrong please try again', 500 );
321
+ }
322
+ } catch ( e ) {
323
+ console.log( 'e =>', e );
324
+ logger.error( { function: 'startChecklist', error: e, body: req.body } );
325
+ return res.sendError( e, 500 );
326
+ }
327
+ }
328
+
93
329
  export async function startChecklist( req, res ) {
94
330
  try {
95
331
  let requestData = req.body;
@@ -627,21 +863,360 @@ export async function sopMobileTaskQuestionValidator( req, res, next ) {
627
863
  } else {
628
864
  next();
629
865
  }
630
- } else {
631
- next();
866
+ } else {
867
+ next();
868
+ }
869
+ } catch ( e ) {
870
+ logger.error( { function: 'sopMobileTaskQuestionValidator', error: e, body: req.body } );
871
+ return res.sendError( e, 500 );
872
+ }
873
+ };
874
+
875
+ export async function sopMobilechecklistMultiSectionFormatter( req, res, next ) {
876
+ try {
877
+ let requestData = req.body;
878
+ requestData.questionAnswers = typeof requestData.questionAnswers == 'string' ? JSON.parse( requestData.questionAnswers ) : requestData.questionAnswers;
879
+ let getChecklistQA = await processedchecklist.findOne( { _id: new ObjectId( requestData.processedcheckListId ) }, { questionAnswers: 1 } );
880
+ let reqAnswers = requestData.questionAnswers;
881
+ let CLQAnswers = getChecklistQA.questionAnswers;
882
+
883
+ if ( requestData.submittype == 'submit' ) {
884
+ reqAnswers.forEach( ( reqA ) => {
885
+ if ( ![ 'multiplechoicemultiple', 'multipleImage' ].includes( reqA?.answerType ) ) {
886
+ if ( ( !reqA.linkType && ( reqA.answer == null || reqA.answer == '' ) ) || ( reqA.linkType && reqA.linkquestionenabled && ( reqA.answer == null || reqA.answer == '' ) ) ) {
887
+ return res.sendError( 'Please Fill All Fields', 400 );
888
+ }
889
+ }
890
+ } );
891
+ }
892
+
893
+ let sectionFormat = [];
894
+ let uniqueSections = {};
895
+ reqAnswers.forEach( ( item ) => {
896
+ const key = `${item.section_id}-${item.sectionName}`;
897
+ if ( !uniqueSections[key] ) {
898
+ uniqueSections[key] = { id: item.section_id, name: item.sectionName };
899
+ }
900
+ } );
901
+ const uniqueArray = Object.values( uniqueSections );
902
+ for ( let section of uniqueArray ) {
903
+ let CLQSection = CLQAnswers.find( ( item ) => item.section_id == section.id );
904
+ if ( CLQSection ) {
905
+ let newArray = [];
906
+ let qaAnswers = CLQSection.questions;
907
+ let requestSection = reqAnswers.filter( ( item ) => item.section_id == section.id );
908
+ for ( let i = 0; i < requestSection.length; i++ ) {
909
+ for ( let j = 0; j < qaAnswers.length; j++ ) {
910
+ if ( requestSection[i].qno == qaAnswers[j].qno ) {
911
+ if ( qaAnswers[j].answerType == 'yes/no' ) {
912
+ let qaans = qaAnswers[j].answers;
913
+ let yn = [];
914
+ for ( let k = 0; k < qaans.length; k++ ) {
915
+ if ( requestSection[i].answer == qaans[k].answer ) {
916
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
917
+ if ( requestSection[i].validationAnswer ) {
918
+ let validateAns = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
919
+ if ( validateAns.length ) {
920
+ let splitImgUrl = validateAns.split( '/' );
921
+ if ( splitImgUrl.length > 1 ) {
922
+ splitImgUrl.splice( 0, 3 );
923
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
924
+ }
925
+ }
926
+ }
927
+ } else {
928
+ qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
929
+ qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
930
+ }
931
+ yn.push( qaans[k] );
932
+ }
933
+ }
934
+ let structure = {};
935
+ structure.qno = qaAnswers[j].qno;
936
+ structure.qname = qaAnswers[j].qname;
937
+ structure.answerType = qaAnswers[j].answerType;
938
+ structure.runAI = qaAnswers[j].runAI;
939
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
940
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
941
+ structure.remarks = requestSection[i].remarks;
942
+ structure.answers = qaAnswers[j].answers;
943
+ structure.userAnswer = yn;
944
+ structure.linkType = qaAnswers[j].linkType;
945
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
946
+ structure.parentanswer = requestSection[i].parentanswer;
947
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
948
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
949
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
950
+ if ( qaAnswers[j]?.taskId ) {
951
+ structure.taskId = qaAnswers[j]?.taskId;
952
+ structure.task = true;
953
+ }
954
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
955
+ structure.redo = false;
956
+ } else if ( requestData.submittype === 'draft' ) {
957
+ structure.redo = qaAnswers[j]?.redo;
958
+ }
959
+ if ( qaAnswers[j]?.redoComment ) {
960
+ structure.redoComment = qaAnswers[j]?.redoComment;
961
+ };
962
+ newArray.push( structure );
963
+ } else if ( qaAnswers[j].answerType == 'multiplechoicesingle' ) {
964
+ let qaans = qaAnswers[j].answers;
965
+ let ms = [];
966
+ for ( let k = 0; k < qaans.length; k++ ) {
967
+ if ( requestSection[i].answer == qaans[k].answer ) {
968
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
969
+ if ( requestSection[i].validationAnswer ) {
970
+ let validationAnswer = decodeURIComponent( requestSection[i].validationAnswer.split( '?' )[0] );
971
+ if ( validationAnswer.length ) {
972
+ let splitImgUrl = validationAnswer.split( '/' );
973
+ if ( splitImgUrl.length > 1 ) {
974
+ splitImgUrl.splice( 0, 3 );
975
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
976
+ }
977
+ }
978
+ }
979
+ } else {
980
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
981
+ qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
982
+ }
983
+ ms.push( qaans[k] );
984
+ }
985
+ }
986
+ let structure = {};
987
+ structure.qno = qaAnswers[j].qno;
988
+ structure.qname = qaAnswers[j].qname;
989
+ structure.answerType = qaAnswers[j].answerType;
990
+ structure.runAI = qaAnswers[j].runAI;
991
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
992
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
993
+ structure.remarks = requestSection[i].remarks;
994
+ structure.answers = qaAnswers[j].answers;
995
+ structure.userAnswer = ms;
996
+ structure.linkType = qaAnswers[j].linkType;
997
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
998
+ structure.parentanswer = requestSection[i].parentanswer;
999
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1000
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1001
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1002
+ if ( qaAnswers[j]?.taskId ) {
1003
+ structure.taskId = qaAnswers[j]?.taskId;
1004
+ structure.task = true;
1005
+ }
1006
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1007
+ structure.redo = false;
1008
+ } else if ( requestData.submittype === 'draft' ) {
1009
+ structure.redo = qaAnswers[j]?.redo;
1010
+ }
1011
+ if ( qaAnswers[j]?.redoComment ) {
1012
+ structure.redoComment = qaAnswers[j]?.redoComment;
1013
+ };
1014
+ newArray.push( structure );
1015
+ } else if ( qaAnswers[j].answerType == 'multiplechoicemultiple' ) {
1016
+ let qaans = qaAnswers[j].answers;
1017
+ let mcmo = [];
1018
+ for ( let k = 0; k < qaans.length; k++ ) {
1019
+ let separatedArray = typeof requestSection[i].Multianswer == 'string' ? JSON.parse( requestSection[i].Multianswer ) : requestSection[i].Multianswer;
1020
+ for ( let s = 0; s < separatedArray.length; s++ ) {
1021
+ if ( separatedArray[s].answer == qaans[k].answer ) {
1022
+ if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
1023
+ if ( separatedArray[s].validationAnswer ) {
1024
+ let validationAnswer = decodeURIComponent( separatedArray[s].validationAnswer.split( '?' )[0] );
1025
+ if ( validationAnswer.length ) {
1026
+ let splitImgUrl = validationAnswer.split( '/' );
1027
+ if ( splitImgUrl.length > 1 ) {
1028
+ splitImgUrl.splice( 0, 3 );
1029
+ qaans[k].validationAnswer = splitImgUrl.join( '/' ) || '';
1030
+ }
1031
+ }
1032
+ }
1033
+ } else {
1034
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1035
+ qaans[k].validationAnswer = separatedArray[s].validationAnswer || '';
1036
+ }
1037
+
1038
+ mcmo.push( qaans[k] );
1039
+ }
1040
+ }
1041
+ }
1042
+ let structure = {};
1043
+ structure.qno = qaAnswers[j].qno;
1044
+ structure.qname = qaAnswers[j].qname;
1045
+ structure.answerType = qaAnswers[j].answerType;
1046
+ structure.runAI = qaAnswers[j].runAI;
1047
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1048
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1049
+ structure.remarks = requestSection[i].remarks;
1050
+ structure.answers = qaAnswers[j].answers;
1051
+ structure.userAnswer = mcmo;
1052
+ structure.linkType = qaAnswers[j].linkType;
1053
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1054
+ structure.parentanswer = requestSection[i].parentanswer;
1055
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1056
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1057
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1058
+ if ( qaAnswers[j]?.taskId ) {
1059
+ structure.taskId = qaAnswers[j]?.taskId;
1060
+ structure.task = true;
1061
+ }
1062
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1063
+ structure.redo = false;
1064
+ } else if ( requestData.submittype === 'draft' ) {
1065
+ structure.redo = qaAnswers[j]?.redo;
1066
+ }
1067
+ if ( qaAnswers[j]?.redoComment ) {
1068
+ structure.redoComment = qaAnswers[j]?.redoComment;
1069
+ };
1070
+ newArray.push( structure );
1071
+ } else if ( qaAnswers[j].answerType == 'multipleImage' ) {
1072
+ let separatedArray = typeof requestSection[i].Multianswer == 'string' ? JSON.parse( requestSection[i].Multianswer ) : requestSection[i].Multianswer;
1073
+ let mcmi = [];
1074
+ // for (let k = 0; k < qaans.length; k++) {
1075
+ for ( let s = 0; s < separatedArray.length; s++ ) {
1076
+ if ( separatedArray[s].answer && separatedArray[s].answer !='' ) {
1077
+ let newAnswer = {};
1078
+ let validationAnswer = decodeURIComponent( separatedArray[s].answer.split( '?' )[0] );
1079
+ if ( validationAnswer.length ) {
1080
+ let splitImgUrl = validationAnswer.split( '/' );
1081
+ if ( splitImgUrl.length > 1 ) {
1082
+ splitImgUrl.splice( 0, 3 );
1083
+ newAnswer.answer = splitImgUrl.join( '/' ) || '';
1084
+ }
1085
+ }
1086
+
1087
+ newAnswer.answeroptionNumber = 0;
1088
+ newAnswer.sopFlag = false;
1089
+ newAnswer.validation = false;
1090
+ newAnswer.validationType = '';
1091
+ newAnswer.referenceImage = '';
1092
+ newAnswer.allowUploadfromGallery = false;
1093
+ newAnswer.runAI = false;
1094
+ newAnswer.descriptivetype = '';
1095
+ newAnswer.showLinked = false;
1096
+ newAnswer.linkedQuestion = 0;
1097
+ newAnswer.nestedQuestion = [];
1098
+ newAnswer.index = s;
1099
+ mcmi.push( newAnswer );
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 = mcmi;
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 {
1133
+ let des = [];
1134
+ if ( requestSection[i].answer != 'null' && requestSection[i].answer != '' && requestSection[i].answer != null ) {
1135
+ let validationAnswer = '';
1136
+ if ( requestSection[i].answer.split( '?' ).length > 1 ) {
1137
+ validationAnswer = decodeURIComponent( requestSection[i].answer.split( '?' )[0] );
1138
+ }
1139
+ if ( validationAnswer.length ) {
1140
+ let splitImgUrl = validationAnswer.split( '/' );
1141
+ if ( splitImgUrl.length > 1 ) {
1142
+ splitImgUrl.splice( 0, 3 );
1143
+ requestSection[i].answer = splitImgUrl.join( '/' );
1144
+ }
1145
+ }
1146
+ let ansstructure = {
1147
+ answer: requestSection[i].answer,
1148
+ answeroptionNumber: 1,
1149
+ sopFlag: false,
1150
+ validation: false,
1151
+ validationType: '',
1152
+ validationAnswer: '',
1153
+ referenceImage: qaAnswers[j].answers[0].referenceImage,
1154
+ showLinked: qaAnswers[j].answers[0].showLinked,
1155
+ linkedQuestion: qaAnswers[j].answers[0].linkedQuestion,
1156
+ };
1157
+ if ( qaAnswers[j].answerType == 'date' ) {
1158
+ ansstructure.dateRangeType = requestSection[i].dateRangeType || false;
1159
+ }
1160
+ des.push( ansstructure );
1161
+ }
1162
+ let structure = {};
1163
+ structure.qno = qaAnswers[j].qno;
1164
+ structure.qname = qaAnswers[j].qname;
1165
+ structure.answerType = qaAnswers[j].answerType;
1166
+ structure.runAI = qaAnswers[j].runAI;
1167
+ structure.runAIDescription = qaAnswers[j].runAIDescription;
1168
+ structure.allowUploadfromGallery = qaAnswers[j].allowUploadfromGallery;
1169
+ structure.remarks = requestSection[i].remarks;
1170
+ structure.answers = qaAnswers[j].answers;
1171
+ structure.userAnswer = des;
1172
+ structure.linkType = qaAnswers[j].linkType;
1173
+ structure.linkquestionenabled = requestSection[i].linkquestionenabled;
1174
+ structure.parentanswer = requestSection[i].parentanswer;
1175
+ structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
1176
+ structure.descriptivetype = qaAnswers[j].descriptivetype;
1177
+ // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1178
+ if ( qaAnswers[j]?.taskId ) {
1179
+ structure.taskId = qaAnswers[j]?.taskId;
1180
+ structure.task = true;
1181
+ }
1182
+ if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
1183
+ structure.redo = false;
1184
+ } else if ( requestData.submittype === 'draft' ) {
1185
+ structure.redo = qaAnswers[j]?.redo;
1186
+ }
1187
+ if ( qaAnswers[j]?.redoComment ) {
1188
+ structure.redoComment = qaAnswers[j]?.redoComment;
1189
+ };
1190
+ newArray.push( structure );
1191
+ }
1192
+ }
1193
+ }
1194
+ }
1195
+ let sectionData = {
1196
+ 'section_id': section.id,
1197
+ 'sectionName': section.name,
1198
+ 'questions': newArray,
1199
+ };
1200
+ sectionFormat.push( sectionData );
1201
+ }
632
1202
  }
633
- } catch ( e ) {
634
- logger.error( { function: 'sopMobileTaskQuestionValidator', error: e, body: req.body } );
635
- return res.sendError( e, 500 );
1203
+
1204
+
1205
+ requestData.questionAnswers = sectionFormat;
1206
+ next();
1207
+ } catch ( error ) {
1208
+ logger.error( { function: 'sopMobilechecklistMultiSectionFormatter', error: error, body: req.body } );
1209
+ return res.sendError( error, 500 );
636
1210
  }
637
1211
  };
638
1212
 
639
- export async function sopMobilechecklistMultiSectionFormatter( req, res, next ) {
1213
+ export async function sopMobilechecklistMultiSectionFormatterv2( req, res, next ) {
640
1214
  try {
641
1215
  let requestData = req.body;
642
1216
  requestData.questionAnswers = typeof requestData.questionAnswers == 'string' ? JSON.parse( requestData.questionAnswers ) : requestData.questionAnswers;
643
1217
  let getChecklistQA = await processedchecklist.findOne( { _id: new ObjectId( requestData.processedcheckListId ) }, { questionAnswers: 1 } );
644
1218
  let reqAnswers = requestData.questionAnswers;
1219
+ logger.error( { functionName: 'updatedPayload', message: reqAnswers } );
645
1220
  let CLQAnswers = getChecklistQA.questionAnswers;
646
1221
 
647
1222
  if ( requestData.submittype == 'submit' ) {
@@ -654,6 +1229,58 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
654
1229
  } );
655
1230
  }
656
1231
 
1232
+ if ( requestData?.editSubmit && requestData?.editSubmit == 'true' ) {
1233
+ let sampleData = reqAnswers[0];
1234
+ CLQAnswers.forEach( ( section ) => {
1235
+ let requestSection = reqAnswers.filter( ( reqSection ) => reqSection.sectionName == section?.sectionOldName || reqSection.sectionName == section?.sectionName );
1236
+ if ( requestSection.length ) {
1237
+ requestSection.forEach( ( item ) => item.section_id = section.section_id );
1238
+ section.questions.forEach( ( question ) => {
1239
+ let sectionQuestion = requestSection.filter( ( secQuestion ) => secQuestion.qname == question.oldQname || secQuestion.qname == question.qname );
1240
+ if ( !sectionQuestion.length ) {
1241
+ let data = { ...requestSection[0] };
1242
+ data.answerType = question.answerType;
1243
+ data.qno = question.qno;
1244
+ data.qname = question.qname;
1245
+ data.oldQname = question?.oldQname || question.qname;
1246
+ data.answer = 'null';
1247
+ data.remarks = '';
1248
+ data.validationAnswer = '';
1249
+ data.Multianswer = [];
1250
+ data.linkType = question.linkType;
1251
+ data.linkquestionenabled = question.linkType;
1252
+ data.linkedQuestion = null;
1253
+ data.showLinked = null;
1254
+ data.parentanswer = '';
1255
+ data.dateRangeType = false;
1256
+ reqAnswers.push( data );
1257
+ }
1258
+ } );
1259
+ } else {
1260
+ section.questions.forEach( ( ele ) => {
1261
+ let data = { ...sampleData };
1262
+ data.section_id = section.section_id;
1263
+ data.sectionName = section.sectionName;
1264
+ data.answerType = ele.answerType;
1265
+ data.qno = ele.qno;
1266
+ data.qname = ele.qname;
1267
+ data.oldQname = ele?.oldQname || ele.qname;
1268
+ data.answer = 'null';
1269
+ data.remarks = '';
1270
+ data.validationAnswer = '';
1271
+ data.Multianswer = [];
1272
+ data.linkType = ele.linkType;
1273
+ data.linkquestionenabled = ele.linkType;
1274
+ data.linkedQuestion = null;
1275
+ data.showLinked = null;
1276
+ data.parentanswer = '';
1277
+ data.dateRangeType = false;
1278
+ reqAnswers.push( data );
1279
+ } );
1280
+ }
1281
+ } );
1282
+ }
1283
+
657
1284
  let sectionFormat = [];
658
1285
  let uniqueSections = {};
659
1286
  reqAnswers.forEach( ( item ) => {
@@ -664,18 +1291,23 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
664
1291
  } );
665
1292
  const uniqueArray = Object.values( uniqueSections );
666
1293
  for ( let section of uniqueArray ) {
667
- let CLQSection = CLQAnswers.find( ( item ) => item.section_id == section.id );
1294
+ let CLQSection = CLQAnswers.find( ( item ) => item.section_id.toString() == section.id.toString() );
668
1295
  if ( CLQSection ) {
669
1296
  let newArray = [];
670
1297
  let qaAnswers = CLQSection.questions;
671
1298
  let requestSection = reqAnswers.filter( ( item ) => item.section_id == section.id );
672
1299
  for ( let i = 0; i < requestSection.length; i++ ) {
673
1300
  for ( let j = 0; j < qaAnswers.length; j++ ) {
674
- if ( requestSection[i].qno == qaAnswers[j].qno ) {
1301
+ 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
1302
  if ( qaAnswers[j].answerType == 'yes/no' ) {
676
1303
  let qaans = qaAnswers[j].answers;
677
1304
  let yn = [];
678
1305
  for ( let k = 0; k < qaans.length; k++ ) {
1306
+ // let requestAnswer = typeof requestSection[i].questionAnswers == 'string' ? JSON.parse( requestSection[i].questionAnswers ) : requestSection[i].questionAnswers;
1307
+ // let findAnswer = requestAnswer.find( ( ele ) => ele.answer == qaans[k].answer && ele.);
1308
+ // if ( findAnswer ) {
1309
+ // qaans[k].nestedQuestion = typeof findAnswer.nestedQuestion == 'string' ? JSON.parse( findAnswer.nestedQuestion ) : findAnswer.nestedQuestion;
1310
+ // }
679
1311
  if ( requestSection[i].answer == qaans[k].answer ) {
680
1312
  if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
681
1313
  if ( requestSection[i].validationAnswer ) {
@@ -689,7 +1321,7 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
689
1321
  }
690
1322
  }
691
1323
  } else {
692
- qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
1324
+ // qaans[k].descriptivetype = qaAnswers[j].descriptivetype || '';
693
1325
  qaans[k].validationAnswer = requestSection[i].validationAnswer || '';
694
1326
  }
695
1327
  yn.push( qaans[k] );
@@ -710,14 +1342,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
710
1342
  structure.parentanswer = requestSection[i].parentanswer;
711
1343
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
712
1344
  structure.descriptivetype = qaAnswers[j].descriptivetype;
713
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1345
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1346
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
714
1347
  if ( qaAnswers[j]?.taskId ) {
715
1348
  structure.taskId = qaAnswers[j]?.taskId;
716
1349
  structure.task = true;
717
1350
  }
718
1351
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
719
1352
  structure.redo = false;
720
- } else if ( requestData.submittype === 'draft' ) {
1353
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
721
1354
  structure.redo = qaAnswers[j]?.redo;
722
1355
  }
723
1356
  if ( qaAnswers[j]?.redoComment ) {
@@ -728,6 +1361,11 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
728
1361
  let qaans = qaAnswers[j].answers;
729
1362
  let ms = [];
730
1363
  for ( let k = 0; k < qaans.length; k++ ) {
1364
+ // let requestAnswer = typeof requestSection[i].questionAnswers == 'string' ? JSON.parse( requestSection[i].questionAnswers ) : requestSection[i].questionAnswers;
1365
+ // let findAnswer= requestAnswer.find( ( ele ) => ele.answer == qaans[k].answer );
1366
+ // if ( findAnswer ) {
1367
+ // qaans[k].nestedQuestion = typeof findAnswer.nestedQuestion == 'string' ? JSON.parse( findAnswer.nestedQuestion ) : findAnswer.nestedQuestion;
1368
+ // }
731
1369
  if ( requestSection[i].answer == qaans[k].answer ) {
732
1370
  if ( qaans[k].validationType == 'Capture Image' || qaans[k].validationType == 'Capture Video' ) {
733
1371
  if ( requestSection[i].validationAnswer ) {
@@ -762,14 +1400,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
762
1400
  structure.parentanswer = requestSection[i].parentanswer;
763
1401
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
764
1402
  structure.descriptivetype = qaAnswers[j].descriptivetype;
765
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1403
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1404
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
766
1405
  if ( qaAnswers[j]?.taskId ) {
767
1406
  structure.taskId = qaAnswers[j]?.taskId;
768
1407
  structure.task = true;
769
1408
  }
770
1409
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
771
1410
  structure.redo = false;
772
- } else if ( requestData.submittype === 'draft' ) {
1411
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
773
1412
  structure.redo = qaAnswers[j]?.redo;
774
1413
  }
775
1414
  if ( qaAnswers[j]?.redoComment ) {
@@ -818,14 +1457,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
818
1457
  structure.parentanswer = requestSection[i].parentanswer;
819
1458
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
820
1459
  structure.descriptivetype = qaAnswers[j].descriptivetype;
821
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1460
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1461
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
822
1462
  if ( qaAnswers[j]?.taskId ) {
823
1463
  structure.taskId = qaAnswers[j]?.taskId;
824
1464
  structure.task = true;
825
1465
  }
826
1466
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
827
1467
  structure.redo = false;
828
- } else if ( requestData.submittype === 'draft' ) {
1468
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
829
1469
  structure.redo = qaAnswers[j]?.redo;
830
1470
  }
831
1471
  if ( qaAnswers[j]?.redoComment ) {
@@ -879,14 +1519,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
879
1519
  structure.parentanswer = requestSection[i].parentanswer;
880
1520
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
881
1521
  structure.descriptivetype = qaAnswers[j].descriptivetype;
882
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1522
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1523
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
883
1524
  if ( qaAnswers[j]?.taskId ) {
884
1525
  structure.taskId = qaAnswers[j]?.taskId;
885
1526
  structure.task = true;
886
1527
  }
887
1528
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
888
1529
  structure.redo = false;
889
- } else if ( requestData.submittype === 'draft' ) {
1530
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
890
1531
  structure.redo = qaAnswers[j]?.redo;
891
1532
  }
892
1533
  if ( qaAnswers[j]?.redoComment ) {
@@ -897,7 +1538,7 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
897
1538
  let des = [];
898
1539
  if ( requestSection[i].answer != 'null' && requestSection[i].answer != '' && requestSection[i].answer != null ) {
899
1540
  let validationAnswer = '';
900
- if ( requestSection[i].answer.split( '?' ).length > 1 ) {
1541
+ if ( requestSection[i].answer.includes( 'https' ) ) {
901
1542
  validationAnswer = decodeURIComponent( requestSection[i].answer.split( '?' )[0] );
902
1543
  }
903
1544
  if ( validationAnswer.length ) {
@@ -938,14 +1579,15 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
938
1579
  structure.parentanswer = requestSection[i].parentanswer;
939
1580
  structure.questionReferenceImage = qaAnswers[j].questionReferenceImage;
940
1581
  structure.descriptivetype = qaAnswers[j].descriptivetype;
941
- // structure.parentQuestion = requestSection[i]?.parentQuestion || qaAnswers[j].qno;
1582
+ structure.uniqueNo = parseInt( requestSection[i].uniqueNo );
1583
+ structure.oldQname = qaAnswers[j]?.oldQname || qaAnswers[j].qname;
942
1584
  if ( qaAnswers[j]?.taskId ) {
943
1585
  structure.taskId = qaAnswers[j]?.taskId;
944
1586
  structure.task = true;
945
1587
  }
946
1588
  if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'submit' ) {
947
1589
  structure.redo = false;
948
- } else if ( requestData.submittype === 'draft' ) {
1590
+ } else if ( typeof qaAnswers[j]?.redo === 'boolean' && requestData.submittype === 'draft' ) {
949
1591
  structure.redo = qaAnswers[j]?.redo;
950
1592
  }
951
1593
  if ( qaAnswers[j]?.redoComment ) {
@@ -967,6 +1609,7 @@ export async function sopMobilechecklistMultiSectionFormatter( req, res, next )
967
1609
 
968
1610
 
969
1611
  requestData.questionAnswers = sectionFormat;
1612
+ logger.error( { message: requestData.questionAnswers, error: 'QuestionanswersPayload' } );
970
1613
  next();
971
1614
  } catch ( error ) {
972
1615
  logger.error( { function: 'sopMobilechecklistMultiSectionFormatter', error: error, body: req.body } );
@@ -2462,7 +3105,7 @@ export async function checklistv1( req, res ) {
2462
3105
  ...projectExtraConditions,
2463
3106
  client_id: { $ifNull: [ '$client_id', '' ] },
2464
3107
  coverage: { $ifNull: [ '$coverage', '' ] },
2465
- taskType: { $ifNull: [ '$type', '' ] },
3108
+ taskType: { $ifNull: [ '$planoType', '' ] },
2466
3109
  },
2467
3110
  },
2468
3111
  ];
@@ -2512,6 +3155,261 @@ export async function checklistv1( req, res ) {
2512
3155
  }
2513
3156
  }
2514
3157
 
3158
+ export async function questionListv1( req, res ) {
3159
+ try {
3160
+ let requestData = req.query;
3161
+ if ( !requestData.processedcheckListId ) {
3162
+ res.sendError( 'processedcheckListId is Required', 400 );
3163
+ }
3164
+
3165
+ let findQuery = [];
3166
+ let findAndQuery = [];
3167
+ findAndQuery.push( { _id: new ObjectId( requestData.processedcheckListId ) } );
3168
+ findQuery.push( { $match: { $and: findAndQuery } } );
3169
+
3170
+ findQuery.push( {
3171
+ $project: {
3172
+ checkListName: { $ifNull: [ '$checkListName', '' ] },
3173
+ scheduleStartTime: { $ifNull: [ '$scheduleStartTime', '' ] },
3174
+ scheduleStartTime_iso: { $ifNull: [ '$scheduleStartTime_iso', '' ] },
3175
+ scheduleEndTime: { $ifNull: [ '$scheduleEndTime', '' ] },
3176
+ scheduleEndTime_iso: { $ifNull: [ '$scheduleEndTime_iso', '' ] },
3177
+ checklistStatus: { $ifNull: [ '$checklistStatus', '' ] },
3178
+ checkListId: { $ifNull: [ '$checkListId', '' ] },
3179
+ startTime: { $ifNull: [ '$startTime', '' ] },
3180
+ submitTime: { $ifNull: [ '$submitTime', '' ] },
3181
+ allowedOverTime: { $ifNull: [ '$allowedOverTime', '' ] },
3182
+ // allowedStoreLocation: { $ifNull: [ '$allowedStoreLocation', '' ] },
3183
+ allowedStoreLocation: {
3184
+ $cond: {
3185
+ if: { $eq: [ '$client_id', '11' ] },
3186
+ then: false,
3187
+ else: {
3188
+ '$cond': {
3189
+ 'if': { '$eq': [ '$coverage', 'user' ] },
3190
+ 'then': false,
3191
+ 'else': { '$ifNull': [ '$allowedStoreLocation', false ] },
3192
+ },
3193
+ },
3194
+ },
3195
+ },
3196
+ reinitiateStatus: { $ifNull: [ '$reinitiateStatus', '' ] },
3197
+ questionAnswers: { $ifNull: [ '$questionAnswers', '' ] },
3198
+ getchecklist: { $ifNull: [ '$getchecklist', '' ] },
3199
+ userEmail: { $ifNull: [ '$userEmail', '' ] },
3200
+ storeName: { $ifNull: [ '$storeName', '' ] },
3201
+ redoStatus: { $ifNull: [ '$redoStatus', false ] },
3202
+ rawImageUpload: { $ifNull: [ '$rawImageUpload', false ] },
3203
+ },
3204
+ } );
3205
+
3206
+ let getchecklist = await processedchecklist.aggregate( findQuery );
3207
+ if ( !getchecklist.length ) {
3208
+ return res.sendError( 'Check List Got Edited Please Fill Again', 422 );
3209
+ } else {
3210
+ logger.info( `v5 => Checklist Continue => store Name: ${getchecklist[0].storeName}, User Email: ${getchecklist[0].userEmail}, Checklist Name: ${getchecklist[0].checkListName}` );
3211
+ // let bucket = JSON.parse( process.env.BUCKET );
3212
+ for ( let [ secIndex, section ] of getchecklist[0].questionAnswers.entries() ) {
3213
+ for ( let [ questionIndex, question ] of section.questions.entries() ) {
3214
+ let Multianswer = [];
3215
+ if ( getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage && getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage !='' ) {
3216
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage = await signedUrl( { file_path: decodeURIComponent( getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage ), Bucket: bucket.sop } );
3217
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + getchecklist[0].questionAnswers[secIndex].questions[questionIndex].questionReferenceImage;
3218
+ }
3219
+ for ( let [ ansIndex, answer ] of question.answers.entries() ) {
3220
+ if ( question.answerType == 'multiplechoicemultiple' ) {
3221
+ let checkvalidation = null;
3222
+ if ( answer.validationAnswer && answer.validationAnswer !='' ) {
3223
+ if ( answer.validationType != 'Descriptive Answer' ) {
3224
+ // checkvalidation = await signedUrl( { file_path: decodeURIComponent( answer.validationAnswer ), Bucket: bucket.sop } );
3225
+ checkvalidation = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.validationAnswer;
3226
+ } else {
3227
+ checkvalidation = answer.validationAnswer;
3228
+ }
3229
+ }
3230
+ Multianswer.push( { 'answer': answer.answer, 'no': ansIndex, 'validationAnswer': checkvalidation } );
3231
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].index = ansIndex;
3232
+ }
3233
+ if ( answer.referenceImage != '' ) {
3234
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].referenceImage = await signedUrl( { file_path: decodeURIComponent( answer.referenceImage ), Bucket: bucket.sop } );
3235
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].referenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.referenceImage;
3236
+ }
3237
+ if ( ( answer.validationType == 'Capture Image' || answer.validationType == 'Capture Video' ) && answer.validationAnswer && answer.validationAnswer != '' ) {
3238
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].validationAnswer = await signedUrl( { file_path: decodeURIComponent( answer.validationAnswer ), Bucket: bucket.sop } );
3239
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].answers[ansIndex].validationAnswer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + answer.validationAnswer;
3240
+ }
3241
+ }
3242
+ if ( question?.userAnswer ) {
3243
+ for ( let [ userAnsIndex, userAns ] of question.userAnswer.entries() ) {
3244
+ if ( ( userAns.validationType == 'Capture Image' || userAns.validationType == 'Capture Video' ) && userAns.validationAnswer && userAns.validationAnswer != '' ) {
3245
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].validationAnswer = await signedUrl( { file_path: decodeURIComponent( userAns.validationAnswer ), Bucket: bucket.sop } );
3246
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].validationAnswer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.validationAnswer;
3247
+ }
3248
+ if ( [ 'image', 'descriptiveImage', 'video' ].includes( question.answerType ) && userAns.answer != '' ) {
3249
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].answer = await signedUrl( { file_path: decodeURIComponent( userAns.answer ), Bucket: bucket.sop } );
3250
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].answer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.answer;
3251
+ }
3252
+ if ( userAns.referenceImage != '' ) {
3253
+ // getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].referenceImage = await signedUrl( { file_path: decodeURIComponent( userAns.referenceImage ), Bucket: bucket.sop } );
3254
+ getchecklist[0].questionAnswers[secIndex].questions[questionIndex].userAnswer[userAnsIndex].referenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.referenceImage;
3255
+ }
3256
+ if ( question.answerType == 'multiplechoicemultiple' ) {
3257
+ let ansIndex = Multianswer.findIndex( ( item ) => item.answer == userAns.answer );
3258
+ if ( ansIndex ) {
3259
+ Multianswer[ansIndex].validationAnswer = userAns.validationAnswer;
3260
+ }
3261
+ }
3262
+ if ( question.answerType == 'multipleImage' ) {
3263
+ if ( userAns && userAns.answer && userAns.answer !='' ) {
3264
+ // let manswer = await signedUrl( { file_path: decodeURIComponent( userAns.answer ), Bucket: bucket.sop } );
3265
+ let manswer = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + userAns.answer;
3266
+ Multianswer.push( { 'answer': manswer, 'no': userAnsIndex, 'validationAnswer': '' } );
3267
+ } else {
3268
+
3269
+ }
3270
+ }
3271
+ }
3272
+ }
3273
+ if ( question.answerType == 'multiplechoicemultiple' ) {
3274
+ Multianswer.forEach( ( item ) => {
3275
+ if ( item.validationAnswer == null ) {
3276
+ item.answer = null;
3277
+ }
3278
+ } );
3279
+ if ( Multianswer.length ) {
3280
+ question.Multianswer = Multianswer;
3281
+ } else {
3282
+ question.Multianswer = Multianswer;
3283
+ }
3284
+ }
3285
+ if ( question.answerType == 'multipleImage' ) {
3286
+ if ( Multianswer.length ) {
3287
+ question.Multianswer = Multianswer;
3288
+ } else {
3289
+ Multianswer.push( { 'answer': null, 'no': 0, 'validationAnswer': null } );
3290
+ question.Multianswer = Multianswer;
3291
+ }
3292
+ }
3293
+ }
3294
+ }
3295
+
3296
+ let questions = [];
3297
+ // function processQuestion( question, section, questions, nested=false ) {
3298
+ // let findExists = questions.find( ( item ) => item?.qno && item.qno == question.qno );
3299
+ // if ( findExists && nested ) {
3300
+ // let findIndex = questions.findIndex( ( item ) => item?.qno && item.qno == question.qno );
3301
+ // questions.splice( findIndex, 1 );
3302
+ // questions.push( question );
3303
+ // for ( let answer of question.answers ) {
3304
+ // if ( answer.showLinked && answer?.linkedQuestion != '' ) {
3305
+ // let linkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
3306
+ // if ( linkedQuestion ) {
3307
+ // processQuestion( linkedQuestion, section, questions, true );
3308
+ // }
3309
+ // }
3310
+ // }
3311
+ // }
3312
+ // if ( !findExists ) {
3313
+ // questions.push( question );
3314
+ // for ( let answer of question.answers ) {
3315
+ // if ( answer.showLinked && answer?.linkedQuestion != '' ) {
3316
+ // let linkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
3317
+ // if ( linkedQuestion ) {
3318
+ // processQuestion( linkedQuestion, section, questions, true );
3319
+ // }
3320
+ // }
3321
+ // }
3322
+ // }
3323
+ // }
3324
+
3325
+ for ( let [ index, data ] of getchecklist.entries() ) {
3326
+ for ( let [ secIndex, section ] of data.questionAnswers.entries() ) {
3327
+ questions = [];
3328
+ for ( let [ questionIndex, question ] of section.questions.entries() ) {
3329
+ let linkedQuestions;
3330
+ let uniqueShow = false;
3331
+ if ( !question.linkType ) {
3332
+ if ( !question?.uniqueNo ) {
3333
+ uniqueShow = true;
3334
+ question.uniqueNo = parseInt( getOtp() ) + Date.now() + questionIndex;
3335
+ }
3336
+ questions.push( question );
3337
+ linkedQuestions = [];
3338
+ let questionList;
3339
+ if ( uniqueShow ) {
3340
+ questionList = new Map( section.questions.map( ( ele ) => [ ele.qno, ele ] ) );
3341
+ } else {
3342
+ let uniqueList = question.answers.flatMap( ( ele ) => ele.nestedQuestion );
3343
+ let filteredQuestion = section.questions.filter( ( ele ) => uniqueList.includes( ele?.uniqueNo ) );
3344
+ questionList = new Map( filteredQuestion.map( ( ele ) => [ ele.uniqueNo, ele ] ) );
3345
+ }
3346
+ question.answers.forEach( ( answer ) => {
3347
+ if ( answer.linkedQuestion ) {
3348
+ let nesedQuestion = [];
3349
+ answer.oldNestedQuestion = !uniqueShow ? JSON.parse( JSON.stringify( answer.oldNestedQuestion ) ) : JSON.parse( JSON.stringify( answer.nestedQuestion ) );
3350
+ answer.nestedQuestion.forEach( ( qn, qidx ) => {
3351
+ let getQn = questionList.get( qn );
3352
+ if ( getQn ) {
3353
+ let randomNo;
3354
+ if ( !getQn?.uniqueNo || uniqueShow ) {
3355
+ randomNo = parseInt( getOtp() ) + Date.now() + qidx;
3356
+ } else {
3357
+ randomNo = getQn?.uniqueNo;
3358
+ }
3359
+ getQn = JSON.parse( JSON.stringify( getQn ) );
3360
+ getQn = { ...getQn, uniqueNo: randomNo };
3361
+ nesedQuestion.push( getQn );
3362
+ if ( answer.linkedQuestion == getQn.qno ) {
3363
+ answer.oldLinkedQuestion = answer.linkedQuestion;
3364
+ answer.linkedQuestion = randomNo;
3365
+ }
3366
+ answer.nestedQuestion[qidx] = randomNo;
3367
+ }
3368
+ } );
3369
+ console.log( nesedQuestion );
3370
+ nesedQuestion.forEach( ( ele ) => {
3371
+ let nestedLinkqn = [];
3372
+ ele.answers.forEach( ( ans ) => {
3373
+ if ( ans.linkedQuestion ) {
3374
+ ans.oldNestedQuestion = !uniqueShow ? JSON.parse( JSON.stringify( ans.oldNestedQuestion ) ) : JSON.parse( JSON.stringify( ans.nestedQuestion ) );
3375
+ ans.nestedQuestion.forEach( ( nested, idx ) => {
3376
+ console.log( nested, uniqueShow, 'nested', nestedLinkqn );
3377
+ let findRandom = nesedQuestion.find( ( qn ) => ( uniqueShow ? qn.qno == nested : qn.uniqueNo == nested ) && !nestedLinkqn.includes( qn.uniqueNo ) && !qn.updated );
3378
+ console.log( findRandom );
3379
+ if ( findRandom ) {
3380
+ if ( ans.linkedQuestion == findRandom.qno ) {
3381
+ ans.oldLinkedQuestion = ans.linkedQuestion;
3382
+ ans.linkedQuestion = findRandom.uniqueNo;
3383
+ }
3384
+ ans.nestedQuestion[idx] = findRandom.uniqueNo;
3385
+ nestedLinkqn.push( findRandom.uniqueNo );
3386
+ }
3387
+ } );
3388
+ }
3389
+ } );
3390
+ ele.updated = true;
3391
+ } );
3392
+ linkedQuestions.push( ...nesedQuestion );
3393
+ }
3394
+ } );
3395
+ questions.push( ...linkedQuestions );
3396
+ }
3397
+ // processQuestion( question, section, questions );
3398
+ }
3399
+ getchecklist[index].questionAnswers[secIndex].questions = questions;
3400
+ }
3401
+ }
3402
+
3403
+ await processedchecklist.updateOne( { _id: requestData.processedcheckListId }, { questionAnswers: getchecklist[0].questionAnswers } );
3404
+
3405
+ return res.sendSuccess( getchecklist );
3406
+ }
3407
+ } catch ( e ) {
3408
+ logger.error( { function: 'questionList', error: e, body: req.body } );
3409
+ return res.sendError( e, 500 );
3410
+ }
3411
+ }
3412
+
2515
3413
  export async function questionList( req, res ) {
2516
3414
  try {
2517
3415
  let requestData = req.query;
@@ -3086,10 +3984,12 @@ export async function uploadAnswerImage( req, res ) {
3086
3984
  }
3087
3985
 
3088
3986
  if ( imageUrl != '' ) {
3089
- imageUrl = await signedUrl( { file_path: imageUrl.Key, Bucket: bucket.sop } );
3987
+ // imageUrl = await signedUrl( { file_path: imageUrl.Key, Bucket: bucket.sop } );
3988
+ imageUrl = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN + imageUrl.Key;
3090
3989
 
3091
3990
  return res.sendSuccess( {
3092
3991
  bucketName: bucket.sop,
3992
+ path: imageUrl.Key,
3093
3993
  imageUrl: imageUrl,
3094
3994
  message: 'Image uploaded successfully!',
3095
3995
  } );
@@ -3426,11 +4326,19 @@ export async function updatePlanoStatus( req, res ) {
3426
4326
  if ( !processCheckDetails ) {
3427
4327
  return res.sendError( 'No data found', 204 );
3428
4328
  }
3429
- processCheckDetails.checklistStatus = req.body.status;
3430
- processCheckDetails.save();
4329
+ let storeTimeZone = await storeService.findOne( { storeName: { $regex: processCheckDetails.storeName, $options: 'i' }, clientId: processCheckDetails.client_id }, { 'storeProfile.timeZone': 1 } );
4330
+ let currentDateTime;
4331
+ if ( storeTimeZone?.storeProfile?.timeZone ) {
4332
+ currentDateTime = dayjs().tz( storeTimeZone?.storeProfile?.timeZone );
4333
+ } else {
4334
+ currentDateTime = requestData?.currentTime ? dayjs( requestData.currentTime, 'HH:mm:ss' ) : dayjs();
4335
+ }
4336
+ let submitTimeString = currentDateTime.format( 'hh:mm A, DD MMM YYYY' );
4337
+ await processedchecklist.updateOne( { _id: req.body.id }, { checklistStatus: req.body.status, ...( req.body.status == 'inprogress' ) ? { startTime_string: submitTimeString } : { submitTime_string: submitTimeString } } );
3431
4338
  return res.sendSuccess( 'Status updated Successfully' );
3432
4339
  } catch ( e ) {
3433
4340
  logger.error( { error: e, function: 'updatePlanoStatus' } );
3434
4341
  return res.sendError( e, 500 );
3435
4342
  }
3436
4343
  }
4344
+