I am getting this error :
30:9 error Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed
Possible solutions I tried is like using if and then else if for the rest of the conditions.
Error will go but my code will not work as expected
How can I refactor the below code?
workItemDetails.value.map((e: any) => {
const existingData = touchPointRequests.filter((t) => +t.ticket_no === e.id);
if (existingData.length && e.fields['System.State']) {
this.updateBrandWebsiteState(existingData, e.fields['System.State']);
}
const existingGoLiveDate = new Date(new Date(existingData[0].go_live_date).toDateString());
const newGoLiveDate = new Date(new Date(e.fields['Custom.GoLiveDate']).toDateString());
const updateData: any = {};
if (
existingData.length &&
e.fields['Custom.GoLiveDate'] &&
newGoLiveDate.getTime() !== existingGoLiveDate.getTime()
) {
const zonedDate = utcToZonedTime(new Date(e.fields['Custom.GoLiveDate']), 'Europe/Berlin');
updateData.goLiveDate = zonedDate;
}
if (
existingData.length &&
e.fields['Microsoft.VSTS.Common.Priority'] &&
AzurePriority[e.fields['Microsoft.VSTS.Common.Priority']] !== existingData[0].priority
) {
updateData.priority = AzurePriority[e.fields['Microsoft.VSTS.Common.Priority']];
}
if (Object.keys(updateData).length) {
this.touchPointRequestRepository.UpdateTouchPointRequestById(existingData[0].tpr_id, updateData);
}
if (
existingData.length &&
e.fields['Custom.PreviewLinks'] != undefined &&
existingData[0].preview_link !== e.fields['Custom.PreviewLinks']
) {
this.brandWebsiteRequestRepository.UpdateBrandWebsiteRequestByTPId(existingData[0].tpr_id, {
previewLink: e.fields['Custom.PreviewLinks'],
});
this.sendPreviewLinkUpdateMail(existingData);
}
if (
existingData.length &&
e.fields['System.State'] === 'In Review' &&
e.fields['Custom.PreviewLinks'] != undefined &&
existingData[0].feedback_and_comments !== e.fields['Custom.PreviewLinks']
) {
this.brandWebsiteRequestRepository.UpdateBrandWebsiteRequestByTPId(existingData[0].tpr_id, {
feedbackAndComments: e.fields['Custom.PreviewLinks'],
});
}
});