I have been tasked with refactoring the code below and I have already done so (check the image for reference). However, my supervisor is still not satisfied with the changes, LOL.
const { appTargetId, appUserTargetId, appUserId } = buildIndexKeys(input);
const fromDate = moment(input.requestDate)
.subtract(retention, 'days')
.toISOString();
if (input.targetId && !input.userId) {
// code for target id without user id
let query = this.model
.query('appTargetId')
.eq(appTargetId)
.where('createDate')
.ge(fromDate)
.where('status')
.not()
.eq(NotificationStatus.Delete);
/* istanbul ignore next */
if (input.subApplicationId) {
query = query.filter('subApplicationId').eq(input.subApplicationId);
}
return query.exec();
} else if (input.userId && !input.targetId) {
// code for user id without target id
return this.model
.query('appUserId')
.eq(appUserId)
.where('createDate')
.ge(fromDate)
.where('status')
.not()
.eq(NotificationStatus.Delete)
.exec();
} else {
// code for both user id and target id
return this.model
.query('appUserTargetId')
.eq(appUserTargetId)
.where('createDate')
.ge(fromDate)
.where('status')
.not()
.eq(NotificationStatus.Delete)
.exec();
}
https://i.sstatic.net/JFgJ7.png
Is there another way I could rewrite this code? I've spent countless hours trying to optimize it without much success. Any suggestions for improvement are highly appreciated.