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.
@@ -1,4 +1,4 @@
1
- import { signedUrl, fileUpload, chunkArray, logger, sendPushNotification } from 'tango-app-api-middleware';
1
+ import { fileUpload, chunkArray, logger, sendPushNotification } from 'tango-app-api-middleware';
2
2
  import * as checklistService from '../services/checklist.service.js';
3
3
  import * as questionService from '../services/checklistQuestion.service.js';
4
4
  import * as assignedService from '../services/checklistAssign.service.js';
@@ -247,14 +247,15 @@ export const create = async ( req, res ) => {
247
247
  section.questions[qIdx].answers[index].nestedQuestion = [];
248
248
  }
249
249
  if ( answer.showLinked ) {
250
+ // section.questions[qIdx].answers[nestedIndex].nestedQuestion.push( answer.linkedQuestion );
250
251
  if ( nestedIndex != -1 ) {
251
- if ( !section.questions[qIdx].answers[nestedIndex].nestedQuestion.includes( answer.linkedQuestion ) ) {
252
- section.questions[qIdx].answers[nestedIndex].nestedQuestion.push( answer.linkedQuestion );
253
- }
252
+ // if ( !section.questions[qIdx].answers[nestedIndex].nestedQuestion.includes( answer.linkedQuestion ) ) {
253
+ section.questions[qIdx].answers[nestedIndex].nestedQuestion.push( answer.linkedQuestion );
254
+ // }
254
255
  } else {
255
- if ( !section.questions[qIdx].answers[index].nestedQuestion.includes( answer.linkedQuestion ) ) {
256
- section.questions[qIdx].answers[index].nestedQuestion.push( answer.linkedQuestion );
257
- }
256
+ // if ( !section.questions[qIdx].answers[index].nestedQuestion.includes( answer.linkedQuestion ) ) {
257
+ section.questions[qIdx].answers[index].nestedQuestion.push( answer.linkedQuestion );
258
+ // }
258
259
  }
259
260
  let nestedLinkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
260
261
  if ( nestedLinkedQuestion ) {
@@ -271,6 +272,18 @@ export const create = async ( req, res ) => {
271
272
  }
272
273
  }
273
274
  }
275
+ let checkNestedLevel = false;
276
+ section.questions.forEach( ( qns ) => {
277
+ qns.answers.forEach( ( answ ) => {
278
+ if ( answ.nestedQuestion.length > 20 ) {
279
+ checkNestedLevel = true;
280
+ }
281
+ } );
282
+ } );
283
+
284
+ if ( checkNestedLevel ) {
285
+ return res.sendError( { message: 'Unable to create more than 20 linked questions.' }, 400 );
286
+ }
274
287
 
275
288
  let sectionList = {
276
289
  section: section.name,
@@ -286,6 +299,7 @@ export const create = async ( req, res ) => {
286
299
  return res.sendSuccess( { checklistId: checkListId, msg: 'CheckList Created Successfully' } );
287
300
  }
288
301
  } ).catch( ( e ) => {
302
+ console.log( e );
289
303
  return res.sendError( e, 500 );
290
304
  } );
291
305
  }
@@ -295,6 +309,7 @@ export const create = async ( req, res ) => {
295
309
  }
296
310
  }
297
311
  } ).catch( async ( e ) => {
312
+ console.log( e );
298
313
  await checklistService.deleteOne( { _id: checkListId } );
299
314
  return res.sendError( e, 500 );
300
315
  } );
@@ -358,23 +373,23 @@ export const getConfigDetails = async ( req, res ) => {
358
373
  let questionDetails = await questionService.find( query );
359
374
  if ( questionDetails.length ) {
360
375
  let sections = [];
361
- let bucket = JSON.parse( process.env.BUCKET );
376
+ // let bucket = JSON.parse( process.env.BUCKET );
362
377
  questionDetails.forEach( ( item ) => {
363
378
  item.question.forEach( async ( question ) => {
364
379
  if ( question.questionReferenceImage && question.questionReferenceImage !='' ) {
365
- let inputData = {
366
- Bucket: bucket.sop,
367
- file_path: decodeURIComponent( question.questionReferenceImage ),
368
- };
369
- question.questionReferenceImage = await signedUrl( inputData );
380
+ // let inputData = {
381
+ // Bucket: bucket.sop,
382
+ // file_path: decodeURIComponent( question.questionReferenceImage ),
383
+ // };
384
+ question.questionReferenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN+question.questionReferenceImage;
370
385
  }
371
386
  question.answers.forEach( async ( answer ) => {
372
387
  if ( answer.referenceImage != '' ) {
373
- let inputData = {
374
- Bucket: bucket.sop,
375
- file_path: decodeURIComponent( answer.referenceImage ),
376
- };
377
- answer.referenceImage = await signedUrl( inputData );
388
+ // let inputData = {
389
+ // Bucket: bucket.sop,
390
+ // file_path: decodeURIComponent( answer.referenceImage ),
391
+ // };
392
+ answer.referenceImage = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN+answer.referenceImage;
378
393
  }
379
394
  } );
380
395
  } );
@@ -777,13 +792,13 @@ export const update = async ( req, res ) => {
777
792
  }
778
793
  if ( answer.showLinked ) {
779
794
  if ( nestedIndex != -1 ) {
780
- if ( !section.questions[qIdx].answers[nestedIndex].nestedQuestion.includes( answer.linkedQuestion ) ) {
781
- section.questions[qIdx].answers[nestedIndex].nestedQuestion.push( answer.linkedQuestion );
782
- }
795
+ // if ( !section.questions[qIdx].answers[nestedIndex].nestedQuestion.includes( answer.linkedQuestion ) ) {
796
+ section.questions[qIdx].answers[nestedIndex].nestedQuestion.push( answer.linkedQuestion );
797
+ // }
783
798
  } else {
784
- if ( !section.questions[qIdx].answers[index].nestedQuestion.includes( answer.linkedQuestion ) ) {
785
- section.questions[qIdx].answers[index].nestedQuestion.push( answer.linkedQuestion );
786
- }
799
+ // if ( !section.questions[qIdx].answers[index].nestedQuestion.includes( answer.linkedQuestion ) ) {
800
+ section.questions[qIdx].answers[index].nestedQuestion.push( answer.linkedQuestion );
801
+ // }
787
802
  }
788
803
  let nestedLinkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
789
804
  if ( nestedLinkedQuestion ) {
@@ -801,6 +816,21 @@ export const update = async ( req, res ) => {
801
816
  }
802
817
  }
803
818
 
819
+ let checkNestedLevel = false;
820
+ section.questions.forEach( ( qns ) => {
821
+ qns.answers.forEach( ( answ ) => {
822
+ if ( answ.nestedQuestion.length > 20 ) {
823
+ checkNestedLevel = true;
824
+ }
825
+ } );
826
+ } );
827
+
828
+ console.log( checkNestedLevel, 'lebej' );
829
+
830
+ if ( checkNestedLevel ) {
831
+ return res.sendError( { message: 'Unable to create more than 20 linked questions' }, 400 );
832
+ }
833
+
804
834
  let sectionList = {
805
835
  section: section.name,
806
836
  sectionOldName: section.oldName,
@@ -859,11 +889,11 @@ export const uploadImage = async ( req, res ) => {
859
889
  };
860
890
  imgUrl = await fileUpload( params );
861
891
 
862
- let inputData = {
863
- Bucket: bucket.sop,
864
- file_path: imgUrl.Key,
865
- };
866
- imgUrl = await signedUrl( inputData );
892
+ // let inputData = {
893
+ // Bucket: bucket.sop,
894
+ // file_path: imgUrl.Key,
895
+ // };
896
+ imgUrl = JSON.parse( process.env.CDNURL )?.TraxAnswerCDN+imgUrl.Key;
867
897
  if ( !imgUrl ) {
868
898
  return res.sendError( { message: 'Something went Wrong' }, 500 );
869
899
  }
@@ -1465,9 +1495,12 @@ export const updateConfigurev1 =async ( req, res ) => {
1465
1495
  if ( !checklistDetails ) {
1466
1496
  return res.sendError( 'no data found', 204 );
1467
1497
  }
1468
-
1469
- let currentDate = dayjs().format( 'HH:mm:ss' );
1470
- currentDate = dayjs.utc( currentDate, 'HH:mm:ss' ).format();
1498
+ let currentDate;
1499
+ if ( inputBody.timeZone ) {
1500
+ currentDate = dayjs().tz( inputBody.timeZone ).format();
1501
+ } else {
1502
+ currentDate = dayjs().format();
1503
+ }
1471
1504
  let updatedscheduleEndTimeISO = dayjs.utc( inputBody?.checkListDetails?.scheduleEndTime, 'hh:mm A' ).format( 'HH:mm:ss' );
1472
1505
  let newUpdatedDate = dayjs.utc( updatedscheduleEndTimeISO, 'HH:mm:ss' ).format();
1473
1506
 
@@ -3142,7 +3175,6 @@ async function insertPCBulkV4( getCLconfig, checklistId, currentdate, updatedche
3142
3175
  }
3143
3176
  if ( getsubmitDetails ) {
3144
3177
  getsubmitDetails = [ getsubmitDetails ];
3145
- console.log( submittedDetails, getsubmitDetails );
3146
3178
  }
3147
3179
  function findDifferences( obj1, obj2 ) {
3148
3180
  return Object.keys( obj1 ).reduce( ( diff, key ) => {
@@ -3178,6 +3210,7 @@ async function insertPCBulkV4( getCLconfig, checklistId, currentdate, updatedche
3178
3210
  delete data.parentanswer;
3179
3211
  delete data.remarks;
3180
3212
  delete data.linkquestionenabled;
3213
+ delete data.uniqueNo;
3181
3214
  if ( data.descriptivetype == null ) {
3182
3215
  delete data.descriptivetype;
3183
3216
  }
@@ -3189,29 +3222,101 @@ async function insertPCBulkV4( getCLconfig, checklistId, currentdate, updatedche
3189
3222
  delete ans.index;
3190
3223
  delete ans.validationAnswer;
3191
3224
  delete ans.answeroptionNumber;
3225
+ ans.nestedQuestion = ans?.oldNestedQuestion ? ans.oldNestedQuestion : ans.nestedQuestion;
3226
+ ans.linkedQuestion = ans?.oldLinkedQuestion ? ans.oldLinkedQuestion : ans.linkedQuestion;
3227
+ delete ans.oldNestedQuestion;
3228
+ delete ans.oldLinkedQuestion;
3192
3229
  } );
3193
3230
  const compare = findDifferences( data, qns );
3194
3231
  if ( compare?.answerType || compare?.answers || compare?.linkType ) {
3195
3232
  logger.info( 'compare =>', compare );
3233
+ logger.info( 'compareSection =>', { section: section.sectionName } );
3234
+ logger.info( 'qno =>', { qno: data.qno } );
3196
3235
  modifiedCount++;
3197
- question.push( qns );
3236
+ question.push( { question: qns, type: 'qnEdit' } );
3198
3237
  } else {
3199
- getsubmitDetails[0].questionAnswers[index].questions[findQuestion].qname = qns.qname;
3200
- question.push( getsubmitDetails[0].questionAnswers[index].questions[findQuestion] );
3238
+ if ( getsubmitDetails[0].questionAnswers[index].questions[findQuestion].qname != qns.qname ) {
3239
+ question.push( { type: 'nameChange', question: qns } );
3240
+ }
3241
+ // getsubmitDetails[0].questionAnswers[index].questions[findQuestion].qname = qns.qname;
3201
3242
  }
3202
3243
  } else {
3203
3244
  modifiedCount++;
3204
- question.push( qns );
3245
+ question.push( { question: qns, type: 'add' } );
3205
3246
  }
3206
3247
  } );
3207
- getsubmitDetails[0].questionAnswers[index].questions = question;
3208
- sectionList.push( getsubmitDetails[0].questionAnswers[index] );
3248
+ // getsubmitDetails[0].questionAnswers[index].questions = question;
3249
+ sectionList.push( { sectionName: section.sectionName, oldName: section.sectionOldName, question: question, type: 'edit' } );
3209
3250
  } else {
3210
3251
  modifiedCount++;
3211
- sectionList.push( section );
3252
+ sectionList.push( { section: section, type: 'add' } );
3212
3253
  }
3213
3254
  }
3214
- getsubmitDetails[0].questionAnswers = sectionList;
3255
+ // getsubmitDetails[0].questionAnswers = sectionList;
3256
+ sectionList.forEach( ( sec ) => {
3257
+ if ( sec.type == 'add' ) {
3258
+ getsubmitDetails[0].questionAnswers.push( sec.section );
3259
+ } else {
3260
+ sec.question.forEach( ( qn ) => {
3261
+ let sectionDetails = getsubmitDetails[0].questionAnswers.findIndex( ( section ) => section.sectionName == sec?.oldName || section.sectionName == sec.sectionName );
3262
+ if ( qn.type == 'add' ) {
3263
+ getsubmitDetails[0].questionAnswers[sectionDetails].questions.push( qn.question );
3264
+ } else {
3265
+ let questions = getsubmitDetails[0].questionAnswers[sectionDetails].questions.reduce( ( acc, ele, idx ) => {
3266
+ if ( ele.qname.trim() == qn?.question?.oldQname?.trim() || ele.qname.trim() == qn.question.qname.trim() ) {
3267
+ acc.push( idx );
3268
+ }
3269
+ return acc;
3270
+ }, [] );
3271
+ if ( questions.length && [ 'qnEdit', 'nameChange' ].includes( qn.type ) ) {
3272
+ if ( qn.type == 'qnEdit' ) {
3273
+ let checkLinkType = getsubmitDetails[0].questionAnswers[sectionDetails].questions[questions[0]].linkType;
3274
+ if ( checkLinkType && !qn.question.linkType ) {
3275
+ questions.forEach( ( qnIdx, index ) => {
3276
+ if ( index != 0 ) {
3277
+ getsubmitDetails[0].questionAnswers[sectionDetails].questions.splice( qnIdx, 1 );
3278
+ }
3279
+ } );
3280
+ questions = [ questions[0] ];
3281
+ }
3282
+ }
3283
+ questions.forEach( ( qnIdx ) => {
3284
+ if ( qn.type == 'qnEdit' ) {
3285
+ getsubmitDetails[0].questionAnswers[sectionDetails].questions[qnIdx].answers.forEach( ( ele ) => {
3286
+ console.log( ele?.nestedQuestion );
3287
+ if ( ele?.nestedQuestion.length ) {
3288
+ ele?.nestedQuestion.forEach( ( nested ) => {
3289
+ let findIndex = getsubmitDetails[0].questionAnswers[sectionDetails].questions.findIndex( ( qn ) => qn.uniqueNo == nested );
3290
+ if ( findIndex != -1 ) {
3291
+ delete getsubmitDetails[0].questionAnswers[sectionDetails].questions[findIndex].userAnswer;
3292
+ let questionDetails = getsubmitDetails[0].questionAnswers[sectionDetails].questions[findIndex];
3293
+ let checkQuestionExists = getsubmitDetails[0].questionAnswers[sectionDetails].questions.filter( ( qnDetails ) => qnDetails.qname == questionDetails.qname );
3294
+ if ( checkQuestionExists.length > 1 ) {
3295
+ getsubmitDetails[0].questionAnswers[sectionDetails].questions.splice( findIndex, 1 );
3296
+ } else {
3297
+ delete getsubmitDetails[0].questionAnswers[sectionDetails].questions[findIndex].uniqueNo;
3298
+ getsubmitDetails[0].questionAnswers[sectionDetails].questions[findIndex].linkquestionenabled = false;
3299
+ getsubmitDetails[0].questionAnswers[sectionDetails].questions[findIndex].answers.forEach( ( ele ) => {
3300
+ if ( ele?.oldNestedQuestion?.length ) {
3301
+ ele.nestedQuestion = ele.oldNestedQuestion;
3302
+ ele.linkedQuestion = ele.oldLinkedQuestion;
3303
+ }
3304
+ } );
3305
+ }
3306
+ }
3307
+ } );
3308
+ }
3309
+ } );
3310
+ getsubmitDetails[0].questionAnswers[sectionDetails].questions[qnIdx] = qn.question;
3311
+ } else {
3312
+ getsubmitDetails[0].questionAnswers[sectionDetails].questions[qnIdx].qname = qn.question.qname;
3313
+ }
3314
+ } );
3315
+ }
3316
+ }
3317
+ } );
3318
+ }
3319
+ } );
3215
3320
  if ( modifiedCount ) {
3216
3321
  getsubmitDetails[0].checklistStatus = 'inprogress';
3217
3322
  getsubmitDetails[0].date_string = dayjs( currentdate ).format( 'YYYY-MM-DD' );
@@ -3619,7 +3724,12 @@ export const selectAssign = async ( req, res ) => {
3619
3724
  // //Select Store and cluster
3620
3725
  if ( requestData.assignType == 'store' ) {
3621
3726
  let storeQuery = [
3622
- { $match: { clientId: requestData.clientId, status: 'active' } },
3727
+ {
3728
+ $match: {
3729
+ ...( req.user.userType==='client'&&req.user.role!='superadmin' ) ? { storeId: { $in: requestData?.assignedStores } } :{ clientId: requestData.clientId },
3730
+ status: 'active',
3731
+ },
3732
+ },
3623
3733
  {
3624
3734
  $project: {
3625
3735
  storeName: 1,
@@ -3809,7 +3809,8 @@ export async function sendAlert( req, res ) {
3809
3809
  }
3810
3810
  const fcmToken = item.token;
3811
3811
  try {
3812
- await sendPushNotification( title, description, fcmToken );
3812
+ let response = await sendPushNotification( title, description, fcmToken );
3813
+ console.log( response, 'response' );
3813
3814
  } catch ( e ) {
3814
3815
  logger.error( { message: 'send alert', error: e, details: data } );
3815
3816
  }