tango-app-api-trax 3.4.0-alpha-5 → 3.4.0-beta-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { fileUpload, chunkArray, logger, sendPushNotification } from 'tango-app-api-middleware';
1
+ import { fileUpload, chunkArray, logger, sendPushNotification, insertOpenSearchData } 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';
@@ -289,13 +289,29 @@ export const create = async ( req, res ) => {
289
289
  section: section.name,
290
290
  createdBy: req.user._id,
291
291
  createdByName: req.user.userName,
292
- client_id: req.body.clientId,
292
+ client_id: inputBody.clientId,
293
293
  checkListId: checkListId,
294
294
  question: section.questions,
295
295
  checkList: inputBody.checklistName,
296
296
  };
297
297
  await questionService.create( sectionList ).then( async ( data ) => {
298
298
  if ( i == inputBody.sections.length - 1 ) {
299
+ let logObj = {
300
+ client_id: inputBody.clientId,
301
+ createAt: new Date(),
302
+ sourceCheckList_id: checkListId,
303
+ checkListName: inputBody.checklistName,
304
+ fromCheckListName: '',
305
+ type: 'checklist',
306
+ action: 'created',
307
+ storeName: '',
308
+ store_id: '',
309
+ createdByEmail: req.user.email,
310
+ createdBy: req.user.userName,
311
+ coverage: 'store',
312
+ logDetails: {},
313
+ };
314
+ await insertOpenSearchData( JSON.parse( process.env.OPENSEARCH ).traxActivityLog, logObj );
299
315
  return res.sendSuccess( { checklistId: checkListId, msg: 'CheckList Created Successfully' } );
300
316
  }
301
317
  } ).catch( ( e ) => {
@@ -730,6 +746,8 @@ export const update = async ( req, res ) => {
730
746
  return res.sendError( 'no data found', 204 );
731
747
  }
732
748
 
749
+ let getExistQuestions = await questionService.find( { checkListId: req.params.checklistId, client_id: req.body.clientId } );
750
+
733
751
  inputBody.sections.forEach( async ( element ) => {
734
752
  if ( !element.questions.length && inputBody.submitType == 'configure' ) {
735
753
  return res.sendError( { message: 'Question is Required' }, 400 );
@@ -740,131 +758,227 @@ export const update = async ( req, res ) => {
740
758
  }
741
759
  } );
742
760
 
743
- checkListDetails.checkListName = inputBody.checklistName;
744
- checkListDetails.checkListDescription = inputBody.checklistDescription;
745
- checkListDetails.questionCount = questionCount;
761
+ let params = {
762
+ checkListName: inputBody.checklistName,
763
+ checkListDescription: inputBody.checklistDescription,
764
+ questionCount: questionCount,
765
+ };
746
766
 
747
- checkListDetails.save().then( async () => {
748
- let checkListId = req.params.checklistId;
749
- let logInsertData = {
750
- action: 'checklistUpdate',
751
- checklistId: checkListId,
752
- checkListName: inputBody.checklistName,
753
- createdBy: req.user._id,
754
- createdByName: req.user.userName,
755
- client_id: req.body.clientId,
756
- sections: inputBody?.sections,
757
- createdByEmail: req.user.email,
758
- };
759
- await checklistLogs.create( logInsertData );
760
- if ( inputBody.sections.length ) {
761
- let sectionId =[];
762
- for ( let i = 0; i < inputBody?.sections?.length; i++ ) {
763
- let section = inputBody.sections[i];
764
- section.questions.forEach( ( section ) => {
765
- if ( section.questionReferenceImage && section.questionReferenceImage !='' ) {
766
- let imgUrl = section.questionReferenceImage.split( '?' )[0];
767
+ checkListDetails.updateOne( { _id: req.params.checklistId }, params );
768
+ let checkListId = req.params.checklistId;
769
+ let logInsertData = {
770
+ action: 'checklistUpdate',
771
+ checklistId: checkListId,
772
+ checkListName: inputBody.checklistName,
773
+ createdBy: req.user._id,
774
+ createdByName: req.user.userName,
775
+ client_id: req.body.clientId,
776
+ sections: inputBody?.sections,
777
+ createdByEmail: req.user.email,
778
+ };
779
+ await checklistLogs.create( logInsertData );
780
+ let sectionList =[];
781
+ if ( inputBody.sections.length ) {
782
+ for ( let i = 0; i < inputBody?.sections?.length; i++ ) {
783
+ let section = inputBody.sections[i];
784
+ section.questions.forEach( ( section ) => {
785
+ if ( section.questionReferenceImage && section.questionReferenceImage !='' ) {
786
+ let imgUrl = section.questionReferenceImage.split( '?' )[0];
787
+ let url = imgUrl.split( '/' );
788
+ url.splice( 0, 3 );
789
+ section.questionReferenceImage = url.join( '/' );
790
+ }
791
+ section.answers.forEach( ( answer ) => {
792
+ if ( answer.referenceImage != '' ) {
793
+ let imgUrl = answer.referenceImage.split( '?' )[0];
767
794
  let url = imgUrl.split( '/' );
768
795
  url.splice( 0, 3 );
769
- section.questionReferenceImage = url.join( '/' );
796
+ answer.referenceImage = url.join( '/' );
770
797
  }
771
- // if (['image', 'descriptiveImage'].includes(section.answerType)) {
772
- section.answers.forEach( ( answer ) => {
773
- if ( answer.referenceImage != '' ) {
774
- let imgUrl = answer.referenceImage.split( '?' )[0];
775
- let url = imgUrl.split( '/' );
776
- url.splice( 0, 3 );
777
- answer.referenceImage = url.join( '/' );
778
- }
779
- } );
780
- // }
781
798
  } );
799
+ } );
782
800
 
783
- for ( let [ index, question ] of section.questions.entries() ) {
784
- await processNested( index, question );
785
- }
801
+ for ( let [ index, question ] of section.questions.entries() ) {
802
+ await processNested( index, question );
803
+ }
786
804
 
787
- async function processNested( qIdx, question, nestedIndex=-1 ) {
788
- if ( question?.answers?.length ) {
789
- for ( let [ index, answer ] of question?.answers?.entries() ) {
790
- if ( !section.questions[qIdx].answers[index]?.nestedQuestion && nestedIndex == -1 ) {
791
- section.questions[qIdx].answers[index].nestedQuestion = [];
805
+ async function processNested( qIdx, question, nestedIndex=-1 ) {
806
+ if ( question?.answers?.length ) {
807
+ for ( let [ index, answer ] of question?.answers?.entries() ) {
808
+ if ( !section.questions[qIdx].answers[index]?.nestedQuestion && nestedIndex == -1 ) {
809
+ section.questions[qIdx].answers[index].nestedQuestion = [];
810
+ }
811
+ if ( answer.showLinked ) {
812
+ // section.questions[qIdx].answers[nestedIndex].nestedQuestion.push( answer.linkedQuestion );
813
+ if ( nestedIndex != -1 ) {
814
+ // if ( !section.questions[qIdx].answers[nestedIndex].nestedQuestion.includes( answer.linkedQuestion ) ) {
815
+ section.questions[qIdx].answers[nestedIndex].nestedQuestion.push( answer.linkedQuestion );
816
+ // }
817
+ } else {
818
+ // if ( !section.questions[qIdx].answers[index].nestedQuestion.includes( answer.linkedQuestion ) ) {
819
+ section.questions[qIdx].answers[index].nestedQuestion.push( answer.linkedQuestion );
820
+ // }
792
821
  }
793
- if ( answer.showLinked ) {
794
- if ( nestedIndex != -1 ) {
795
- // if ( !section.questions[qIdx].answers[nestedIndex].nestedQuestion.includes( answer.linkedQuestion ) ) {
796
- section.questions[qIdx].answers[nestedIndex].nestedQuestion.push( answer.linkedQuestion );
797
- // }
798
- } else {
799
- // if ( !section.questions[qIdx].answers[index].nestedQuestion.includes( answer.linkedQuestion ) ) {
800
- section.questions[qIdx].answers[index].nestedQuestion.push( answer.linkedQuestion );
801
- // }
802
- }
803
- let nestedLinkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
804
- if ( nestedLinkedQuestion ) {
805
- let findNestedAnswers = nestedLinkedQuestion.answers.find( ( item ) => item.showLinked );
806
- if ( findNestedAnswers ) {
807
- if ( nestedIndex != -1 ) {
808
- await processNested( qIdx, nestedLinkedQuestion, nestedIndex );
809
- } else {
810
- await processNested( qIdx, nestedLinkedQuestion, index );
811
- }
822
+ let nestedLinkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
823
+ if ( nestedLinkedQuestion ) {
824
+ let findNestedAnswers = nestedLinkedQuestion.answers.find( ( item ) => item.showLinked );
825
+ if ( findNestedAnswers ) {
826
+ if ( nestedIndex != -1 ) {
827
+ await processNested( qIdx, nestedLinkedQuestion, nestedIndex );
828
+ } else {
829
+ await processNested( qIdx, nestedLinkedQuestion, index );
812
830
  }
813
831
  }
814
832
  }
815
833
  }
816
834
  }
817
835
  }
836
+ }
818
837
 
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
- } );
838
+ let checkNestedLevel = false;
839
+ section.questions.forEach( ( qns ) => {
840
+ qns.answers.forEach( ( answ ) => {
841
+ if ( answ.nestedQuestion.length > 20 ) {
842
+ checkNestedLevel = true;
843
+ }
826
844
  } );
845
+ } );
827
846
 
828
- console.log( checkNestedLevel, 'lebej' );
847
+ console.log( checkNestedLevel, 'lebej' );
829
848
 
830
- if ( checkNestedLevel ) {
831
- return res.sendError( { message: 'Unable to create more than 20 linked questions' }, 400 );
832
- }
849
+ if ( checkNestedLevel ) {
850
+ return res.sendError( { message: 'Unable to create more than 20 linked questions' }, 400 );
851
+ }
833
852
 
834
- let sectionList = {
835
- section: section.name,
836
- sectionOldName: section.oldName,
837
- createdBy: req.user._id,
838
- createdByName: req.user.userName,
839
- client_id: req.body.clientId,
840
- checkListId: checkListId,
841
- question: section.questions,
842
- checkList: inputBody.checklistName,
843
- };
844
- await questionService.create( sectionList ).then( async ( data ) => {
845
- sectionId.push( data._id );
846
- if ( i == inputBody.sections.length - 1 ) {
847
- await questionService.deleteMany( { checkListId: checkListId, client_id: req.body.clientId, _id: { $nin: sectionId } } );
848
- await assignedService.updateMany( { checkListId: checkListId }, { checkListName: inputBody.checklistName } );
849
- return res.sendSuccess( { checklistId: checkListId, msg: 'CheckList Created Successfully' } );
850
- }
851
- } ).catch( ( e ) => {
852
- return res.sendError( e, 500 );
853
- } );
853
+ sectionList.push( {
854
+ section: section.name,
855
+ sectionOldName: section.oldName,
856
+ createdBy: req.user._id,
857
+ createdByName: req.user.userName,
858
+ client_id: req.body.clientId,
859
+ checkListId: checkListId,
860
+ question: section.questions,
861
+ checkList: inputBody.checklistName,
862
+ } );
863
+ }
864
+ }
865
+ await questionService.deleteMany( { checkListId: checkListId, client_id: req.body.clientId } );
866
+ if ( sectionList.length ) {
867
+ await questionService.insertMany( sectionList );
868
+ }
869
+
870
+ let questionList = {
871
+ questionAdd: [],
872
+ questionEdit: [],
873
+ questionDelete: [],
874
+ };
875
+ let updateSection = [];
876
+
877
+ function findDifferences( obj1, obj2 ) {
878
+ return Object.keys( obj1 ).reduce( ( diff, key ) => {
879
+ if ( !isEqual( obj1[key], obj2[key] ) ) {
880
+ diff[key] = { previous: obj1[key], new: obj2[key] };
854
881
  }
855
- } else {
856
- if ( inputBody.submitType == 'save' ) {
857
- await questionService.deleteMany( { checkListId: checkListId, client_id: req.body.clientId } );
858
- return res.sendSuccess( { message: 'CheckList Updated Successfully' } );
882
+ return diff;
883
+ }, {} );
884
+ }
885
+
886
+ getExistQuestions.forEach( ( ele ) => {
887
+ let sectionDetails = inputBody.sections.find( ( sec ) => sec.oldName === ele.section );
888
+
889
+ if ( !sectionDetails ) {
890
+ questionList.questionDelete.push( { sectionName: ele.section, questions: ele.question } );
891
+ return;
892
+ }
893
+
894
+ if ( ele.section !== sectionDetails.name ) {
895
+ updateSection.push( { previous: ele.section, new: sectionDetails.name } );
896
+ }
897
+
898
+ ele.question.forEach( ( qn ) => {
899
+ let question = sectionDetails.questions.find( ( qns ) => qns.oldQname === qn.qname );
900
+
901
+ if ( question ) {
902
+ qn.answers.forEach( ( ans ) => {
903
+ delete ans.answeroptionNumber;
904
+ } );
905
+ question.answers.forEach( ( ans ) => {
906
+ delete ans.answeroptionNumber;
907
+ } );
908
+ let compare = findDifferences( qn, question );
909
+ if ( Object.keys( compare ).length && ( compare?.answerType || compare?.answers || compare?.linkType || compare?.qname ) ) {
910
+ questionList.questionEdit.push( { sectionName: ele.section, questions: [ { previous: qn, new: question } ] } );
911
+ }
912
+ } else {
913
+ let sectionIndex = questionList.questionDelete.findIndex( ( sec ) => sec.sectionName === ele.section );
914
+
915
+ if ( sectionIndex !== -1 ) {
916
+ questionList.questionDelete[sectionIndex].questions.push( qn );
917
+ } else {
918
+ questionList.questionDelete.push( { sectionName: ele.section, questions: [ qn ] } );
919
+ }
859
920
  }
921
+ } );
922
+ } );
923
+
924
+ inputBody.sections.forEach( ( ele ) => {
925
+ let sectionDetails = getExistQuestions.find( ( sec ) => sec.section === ele.oldName );
926
+
927
+ if ( !sectionDetails ) {
928
+ questionList.questionAdd.push( { sectionName: ele.name, questions: ele.questions } );
929
+ return;
860
930
  }
861
- } ).catch( ( e ) => {
862
- logger.error( 'update =>', e );
863
- return res.sendError( e, 500 );
931
+
932
+ ele.questions.forEach( ( qn ) => {
933
+ let question = sectionDetails.question.find( ( qns ) => qns.qname === qn.oldQname );
934
+
935
+ if ( !question ) {
936
+ let sectionIndex = questionList.questionAdd.findIndex( ( sec ) => sec.sectionName === ele.name );
937
+
938
+ if ( sectionIndex !== -1 ) {
939
+ questionList.questionAdd[sectionIndex].questions.push( qn );
940
+ } else {
941
+ questionList.questionAdd.push( { sectionName: ele.name, questions: [ qn ] } );
942
+ }
943
+ }
944
+ } );
864
945
  } );
946
+
947
+ if ( updateSection.length || questionList.questionAdd.length || questionList.questionEdit.length || questionList.questionDelete.length || inputBody.checklistName.toLowerCase() != checkListDetails.checkListName.toLowerCase() || inputBody.checklistDescription.toLowerCase() != checkListDetails.checkListDescription.toLowerCase() ) {
948
+ let insertLogData = {
949
+ client_id: req.body.clientId,
950
+ createAt: new Date(),
951
+ sourceCheckList_id: req.params.checklistId,
952
+ checkListName: inputBody.checklistName,
953
+ fromCheckListName: '',
954
+ type: 'checklist',
955
+ action: 'edited',
956
+ storeName: '',
957
+ store_id: '',
958
+ createdByEmail: req.user.email,
959
+ createdBy: req.user.userName,
960
+ coverage: checkListDetails.coverage,
961
+ logDetails: {
962
+ ...( inputBody.checklistName.toLowerCase() == checkListDetails.checkListName.toLowerCase() ) ? { updatedChecklistName: {} } :{ updatedChecklistName: {
963
+ previous: checkListDetails.checkListName,
964
+ new: inputBody.checklistName,
965
+ } },
966
+ ...( inputBody.checklistDescription.toLowerCase() == checkListDetails.checkListDescription.toLowerCase() ) ? { updatedChecklistDescription: {} } :{ updatedChecklistDescription: {
967
+ previous: checkListDetails.checkListDescription,
968
+ new: inputBody.checklistDescription,
969
+ } },
970
+ updatedSectionName: updateSection,
971
+ questionAdd: questionList.questionAdd,
972
+ questionEdit: questionList.questionEdit,
973
+ questionDelete: questionList.questionDelete,
974
+ },
975
+ };
976
+ await insertOpenSearchData( JSON.parse( process.env.OPENSEARCH ).traxActivityLog, insertLogData );
977
+ }
978
+ return res.sendSuccess( { message: 'CheckList Updated Successfully', checklistId: req.params.checklistId } );
865
979
  } catch ( e ) {
866
- logger.error( 'update =>', e );
867
- return res.sendError( e, 500 );
980
+ logger.error( { functionName: 'questionUpdate', error: e } );
981
+ res.sendError( e, 500 );
868
982
  }
869
983
  };
870
984
 
@@ -1643,6 +1757,109 @@ export const updateConfigurev1 =async ( req, res ) => {
1643
1757
  futureDaysDataRemove( currentDate, req.params.checklistId, checklistDetails.checkListName, '333' );
1644
1758
  }
1645
1759
  }
1760
+ checklistDetails = checklistDetails.toObject();
1761
+ checklistDetails.configStartDate = checklistDetails.configStartDate ? dayjs.utc( checklistDetails?.configStartDate ).format( 'YYYY-MM-DD' ) : '';
1762
+ checklistDetails.configEndDate = checklistDetails.configEndDate ? dayjs.utc( checklistDetails?.configEndDate ).format( 'YYYY-MM-DD' ) : '';
1763
+ checklistDetails.scheduleDate = checklistDetails.scheduleDate ? dayjs.utc( checklistDetails?.scheduleDate ).format( 'YYYY-MM-DD' ) : '';
1764
+ checklistDetails.scheduleRepeatedDay = Array.isArray( checklistDetails.scheduleRepeatedDay ) ? checklistDetails.scheduleRepeatedDay[0] : checklistDetails.scheduleRepeatedDay;
1765
+ configDetails.scheduleRepeatedDay = Array.isArray( configDetails.scheduleRepeatedDay ) ? configDetails?.scheduleRepeatedDay[0] : configDetails?.scheduleRepeatedDay;
1766
+ configDetails.scheduleDate = configDetails.scheduleDate ? dayjs( configDetails?.scheduleDate ).format( 'YYYY-MM-DD' ) : '';
1767
+ let removedKeys = [ 'publish', 'publishDate', 'storeCount', 'sections', 'createdAt', 'updatedAt', 'scheduleStartTimeISO', 'scheduleEndTimeISO' ];
1768
+ removedKeys.forEach( ( item ) => {
1769
+ delete configDetails?.[item];
1770
+ delete checklistDetails?.[item];
1771
+ } );
1772
+ let differences = findObjectDifference( checklistDetails, configDetails );
1773
+ if ( Object.keys( differences ).length || req.body.added.length || req.body.removed.length ) {
1774
+ let insertData = {
1775
+ client_id: req.body.clientId,
1776
+ createAt: new Date(),
1777
+ sourceCheckList_id: configDetails._id,
1778
+ checkListName: configDetails.checkListName,
1779
+ fromCheckListName: '',
1780
+ type: 'checklist',
1781
+ action: 'updated',
1782
+ storeName: '',
1783
+ store_id: '',
1784
+ createdByEmail: req.user.email,
1785
+ createdBy: req.user.userName,
1786
+ coverage: configDetails.coverage,
1787
+ logDetails: {
1788
+ schedule: {
1789
+ previous: {
1790
+ ...( differences?.scheduleDate && ( differences?.scheduleDate.previous != '' && differences?.scheduleDate.previous != null ) ) ? { scheduleDate: dayjs( checklistDetails?.scheduleDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1791
+ schedule: differences?.schedule ? checklistDetails.schedule : '',
1792
+ time: ( ( differences?.scheduleEndTime || differences?.scheduleStartTime ) && ( checklistDetails.scheduleStartTime != '' || checklistDetails.scheduleEndTime != '' ) ) ? checklistDetails.scheduleStartTime + ' to ' + checklistDetails.scheduleEndTime : '',
1793
+ scheduleRepeatedDay: differences?.scheduleRepeatedDay ? checklistDetails?.scheduleRepeatedDay : '',
1794
+ scheduleRepeatedType: differences?.scheduleRepeatedType ? checklistDetails?.scheduleRepeatedType : '',
1795
+ ...( differences?.configStartDate && checklistDetails?.configStartDate ) ? { configStartDate: dayjs( checklistDetails?.configStartDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1796
+ ...( differences?.configEndDate && checklistDetails?.configEndDate ) ? { configEndDate: dayjs( checklistDetails?.configEndDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1797
+ scheduleWeekDays: differences?.scheduleWeekDays ? checklistDetails?.scheduleWeekDays : [],
1798
+ scheduleRepeatedMonthWeek: differences?.scheduleRepeatedMonthWeek ? checklistDetails?.scheduleRepeatedMonthWeek : '',
1799
+ specificDate: differences?.specificDate ? checklistDetails?.specificDate : [],
1800
+
1801
+ },
1802
+ new: {
1803
+ ...( differences?.scheduleDate && ( differences?.scheduleDate.new != '' && differences?.scheduleDate.new != null ) ) ? { scheduleDate: dayjs( configDetails?.scheduleDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1804
+ schedule: differences?.schedule ? configDetails.schedule : '',
1805
+ time: ( ( differences?.scheduleEndTime || differences?.scheduleStartTime ) && ( configDetails.scheduleStartTime != '' || configDetails.scheduleEndTime != '' ) ) ? configDetails.scheduleStartTime + ' to ' + configDetails.scheduleEndTime : '',
1806
+ scheduleRepeatedDay: differences?.scheduleRepeatedDay ? configDetails?.scheduleRepeatedDay : '',
1807
+ scheduleRepeatedType: differences?.scheduleRepeatedType ? configDetails?.scheduleRepeatedType : '',
1808
+ ...( differences?.configStartDate && configDetails?.configStartDate ) ? { configStartDate: dayjs( configDetails?.configStartDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1809
+ ...( differences?.configEndDate && configDetails?.configEndDate ) ? { configEndDate: dayjs( configDetails?.configEndDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1810
+ scheduleWeekDays: differences?.scheduleWeekDays ? configDetails?.scheduleWeekDays : [],
1811
+ scheduleRepeatedMonthWeek: differences?.scheduleRepeatedMonthWeek ? configDetails?.scheduleRepeatedMonthWeek : '',
1812
+ specificDate: differences?.specificDate ? configDetails?.specificDate : [],
1813
+ },
1814
+ },
1815
+ response: {
1816
+ previous: [
1817
+ ...( differences?.allowedStoreLocation && checklistDetails.allowedStoreLocation ? [ 'Geo fencing' ] : [] ),
1818
+ ...( differences?.allowedMultiSubmit && checklistDetails.allowedMultiSubmit? [ 'Allow multiple responses' ] : [] ),
1819
+ ...( differences?.allowedOverTime && checklistDetails.allowedOverTime ? [ 'Fill response after configure time.' ] : [] ),
1820
+ ...( differences?.allowOnce && checklistDetails.allowOnce ? [ 'Only one time' ] : [] ),
1821
+ ],
1822
+ new: [
1823
+ ...( differences?.allowedStoreLocation && configDetails.allowedStoreLocation ? [ 'Geo fencing' ] : [] ),
1824
+ ...( differences?.allowedMultiSubmit && configDetails.allowedMultiSubmit? [ 'Allow multiple responses' ] : [] ),
1825
+ ...( differences?.allowedOverTime && configDetails.allowedOverTime ? [ 'Fill response after configure time' ] : [] ),
1826
+ ...( differences?.allowOnce && configDetails.allowOnce ? [ 'Only one time' ] : [] ),
1827
+ ],
1828
+ },
1829
+ ...( differences?.approver ) ? { approver:
1830
+ { previous: differences?.approver?.previous.map( ( item ) => item.name ).toString(),
1831
+ new: differences?.approver?.new.map( ( item ) => item.name ).toString() },
1832
+ } : { approver: {} },
1833
+ ...( differences?.owner ) ? { owner:
1834
+ { previous: differences?.owner?.previous.map( ( item ) => item.name ).toString(),
1835
+ new: differences?.owner?.new.map( ( item ) => item.name ).toString() },
1836
+ } : { owner: {} },
1837
+ ...( req.body.checkListDetails.coverage == 'store' ) ? { storeAdded: req.body.added } :{ storeAdded: [] },
1838
+ ...( req.body.checkListDetails.coverage == 'store' ) ? { storeRemoved: req.body.removed } :{ storeRemoved: [] },
1839
+ ...( req.body.checkListDetails.coverage == 'user' ) ? { userAdded: req.body.added } :{ userAdded: [] },
1840
+ ...( req.body.checkListDetails.coverage == 'user' ) ? { userRemoved: req.body.removed } :{ userRemoved: [] },
1841
+ },
1842
+ };
1843
+ await insertOpenSearchData( JSON.parse( process.env.OPENSEARCH ).traxActivityLog, insertData );
1844
+ }
1845
+
1846
+ let logObj = {
1847
+ client_id: inputBody.checkListDetails.client_id,
1848
+ createAt: new Date(),
1849
+ sourceCheckList_id: inputBody.checkListDetails._id,
1850
+ checkListName: inputBody.checkListDetails.checkListName,
1851
+ fromCheckListName: '',
1852
+ type: 'checklist',
1853
+ action: inputBody.checkListDetails.publish ? 'published' : 'unpublished',
1854
+ storeName: '',
1855
+ store_id: '',
1856
+ createdByEmail: req.user.email,
1857
+ createdBy: req.user.userName,
1858
+ coverage: inputBody.checkListDetails.coverage,
1859
+ logDetails: {},
1860
+ };
1861
+ await insertOpenSearchData( JSON.parse( process.env.OPENSEARCH ).traxActivityLog, logObj );
1862
+
1646
1863
  if ( response?.modifiedCount || response?.matchedCount || response?.upsertedCount ) {
1647
1864
  return res.sendSuccess( { id, message: 'Configured Updated Successfully' } );
1648
1865
  }
@@ -1655,6 +1872,32 @@ export const updateConfigurev1 =async ( req, res ) => {
1655
1872
  }
1656
1873
  };
1657
1874
 
1875
+ function findObjectDifference( oldObj, newObj ) {
1876
+ const isEqual = ( a, b ) => JSON.stringify( a ) === JSON.stringify( b );
1877
+
1878
+ const getArrayDiff = ( oldArr, newArr ) => {
1879
+ const removed = oldArr.filter( ( item ) => !newArr.some( ( newItem ) => isEqual( newItem, item ) ) );
1880
+ const added = newArr.filter( ( item ) => !oldArr.some( ( oldItem ) => isEqual( oldItem, item ) ) );
1881
+ if ( removed.length || added.length ) {
1882
+ return { previous: oldArr, new: newArr };
1883
+ }
1884
+ return [];
1885
+ };
1886
+ const difference = Object.keys( oldObj ).reduce( ( diff, key ) => {
1887
+ oldObj[key] = oldObj[key] == null ? '' : oldObj[key];
1888
+ if ( !isEqual( oldObj[key], newObj[key] ) ) {
1889
+ if ( !Array.isArray( newObj[key] ) ) {
1890
+ diff[key] = { previous: oldObj[key], new: newObj[key] };
1891
+ } else {
1892
+ diff[key] = getArrayDiff( oldObj[key] || [], newObj[key] || [] );
1893
+ }
1894
+ }
1895
+ return diff;
1896
+ }, {} );
1897
+
1898
+ return difference;
1899
+ }
1900
+
1658
1901
  export const updatePublish = async ( req, res ) => {
1659
1902
  try {
1660
1903
  if ( typeof req?.body?.checklistId == 'undefined' && typeof req.body.type == 'undefined' ) {
@@ -1729,6 +1972,22 @@ export const updatePublish = async ( req, res ) => {
1729
1972
 
1730
1973
 
1731
1974
  await checklistService.updateOne( query, getCheckDetails );
1975
+ let logObj = {
1976
+ client_id: req.body.clientId,
1977
+ createAt: new Date(),
1978
+ sourceCheckList_id: req.body.checklistId,
1979
+ checkListName: getCheckDetails.checkListName,
1980
+ fromCheckListName: '',
1981
+ type: 'checklist',
1982
+ action: req.body.publish ? 'published' : 'unpublished',
1983
+ storeName: '',
1984
+ store_id: '',
1985
+ createdByEmail: req.user.email,
1986
+ createdBy: req.user.userName,
1987
+ coverage: getCheckDetails.coverage,
1988
+ logDetails: {},
1989
+ };
1990
+ await insertOpenSearchData( JSON.parse( process.env.OPENSEARCH ).traxActivityLog, logObj );
1732
1991
  if ( getCheckDetails.checkListType == 'custom' ) {
1733
1992
  let currentDate = dayjs.utc().format();
1734
1993
  let updatedscheduleEndTimeISO = dayjs.utc( getCheckDetails.scheduleEndTimeISO ).format( 'HH:mm:ss' );
@@ -3933,6 +4192,44 @@ export async function updateAssign( req, res ) {
3933
4192
  // req.body.assignedGroup = req.body.assignedGroup.map( ( ele ) => {
3934
4193
  // return { id: ele };
3935
4194
  // } );
4195
+ let getAssignedDetails = await assignedService.find( { checkListId: req.body.checkListId } );
4196
+ if ( getAssignedDetails.length ) {
4197
+ if ( req.body.coverage == 'store' ) {
4198
+ getAssignedDetails = [ ...new Set( getAssignedDetails.map( ( ele ) => ele.storeName ) ) ];
4199
+ } else {
4200
+ getAssignedDetails = [ ...new Set( getAssignedDetails.map( ( ele ) => ele.userName ) ) ];
4201
+ }
4202
+ }
4203
+
4204
+ let added = req.body.assignedList.filter( ( item ) => {
4205
+ let storeUsername;
4206
+ if ( req.body.coverage == 'store' ) {
4207
+ storeUsername = item.storeName;
4208
+ } else {
4209
+ storeUsername = item.userName;
4210
+ }
4211
+ if ( !getAssignedDetails.includes( storeUsername ) ) {
4212
+ return item;
4213
+ }
4214
+ } ).map( ( ele ) => {
4215
+ if ( req.body.coverage == 'store' ) {
4216
+ return ele.storeName;
4217
+ } else {
4218
+ return ele.userName;
4219
+ }
4220
+ } );
4221
+ let removed = getAssignedDetails.filter( ( item ) => {
4222
+ let list;
4223
+ if ( req.body.coverage == 'store' ) {
4224
+ list = req.body.assignedList.map( ( item ) => item.storeName );
4225
+ } else {
4226
+ list = req.body.assignedList.map( ( item ) => item.userName ); ;
4227
+ }
4228
+ if ( !list.includes( item ) ) {
4229
+ return item;
4230
+ }
4231
+ } );
4232
+
3936
4233
  await assignedService.deleteMany( { checkListId: req.body.checkListId } );
3937
4234
  let assignedUserList = [];
3938
4235
  let userEmailList = req.body.assignedList.map( ( ele ) => ele.userEmail.toLowerCase() );
@@ -3996,7 +4293,7 @@ export async function updateAssign( req, res ) {
3996
4293
  }
3997
4294
  } ) );
3998
4295
  await assignedService.insertMany( assignedUserList );
3999
- return res.sendSuccess( 'Assign details updated successfully' );
4296
+ return res.sendSuccess( { messgage: 'Assign details updated successfully', added, removed } );
4000
4297
  } catch ( e ) {
4001
4298
  logger.error( { functionName: 'updateAssign', error: e } );
4002
4299
  return res.sendError( e, 500 );