Created
August 2, 2024 06:00
-
-
Save montasim/9646e90957cc607768d4cb91787d4f5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import errorResponse from '../utilities/errorResponse.js'; | |
import httpStatus from '../constant/httpStatus.constants.js'; | |
import sendResponse from '../utilities/sendResponse.js'; | |
import loggerService from '../service/logger.service.js'; | |
import toSentenceCase from '../utilities/toSentenceCase.js'; | |
const getResourceById = async (model, populateMethod, resourceId, resourceType) => { | |
try { | |
const resource = await populateMethod(model.findById(resourceId)); | |
if (!resource) { | |
return errorResponse(`${toSentenceCase(resourceType)} not found.`, httpStatus.NOT_FOUND); | |
} | |
return sendResponse( | |
resource, | |
`${toSentenceCase(resourceType)} fetched successfully.`, | |
httpStatus.OK | |
); | |
} catch (error) { | |
loggerService.error(`Failed to get ${resourceType}: ${error}`); | |
return errorResponse( | |
error.message || `Failed to get ${resourceType}.`, | |
httpStatus.INTERNAL_SERVER_ERROR | |
); | |
} | |
}; | |
const service = { | |
getResourceById | |
}; | |
export default service; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment