fix: use new structure

This commit is contained in:
cihatata authored and cihat committed 2025-02-11 07:43:38 +03:00
1 parent a98a3dd7a3
commit 88df88891c
22 files changed
+193 -146

No files matched your search

+7 -5
View File
@@ -1,8 +1,10 @@
import logger from "../utils/logger.js";
import { errorMessages } from "../utils/messages.js";
import ServiceRegistry from "../service/serviceRegistry.js";
import StringService from "../service/stringService.js";
const SERVICE_NAME = "verifyOwnership";
const verifyOwnership = (Model, paramName) => {
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
return async (req, res, next) => {
const userId = req.user._id;
const documentId = req.params[paramName];
@@ -11,11 +13,11 @@ const verifyOwnership = (Model, paramName) => {
//If the document is not found, return a 404 error
if (!doc) {
logger.error({
message: errorMessages.VERIFY_OWNER_NOT_FOUND,
message: stringService.verifyOwnerNotFound,
service: SERVICE_NAME,
method: "verifyOwnership",
});
const error = new Error(errorMessages.VERIFY_OWNER_NOT_FOUND(req.language));
const error = new Error(stringService.verifyOwnerNotFound);
error.status = 404;
throw error;
}
@@ -23,7 +25,7 @@ const verifyOwnership = (Model, paramName) => {
// Special case for User model, as it will not have a `userId` field as other docs will
if (Model.modelName === "User") {
if (userId.toString() !== doc._id.toString()) {
const error = new Error(errorMessages.VERIFY_OWNER_UNAUTHORIZED(req.language));
const error = new Error(stringService.verifyOwnerUnauthorized);
error.status = 403;
throw error;
}
@@ -33,7 +35,7 @@ const verifyOwnership = (Model, paramName) => {
// If the userID does not match the document's userID, return a 403 error
if (userId.toString() !== doc.userId.toString()) {
const error = new Error(errorMessages.VERIFY_OWNER_UNAUTHORIZED(req.language));
const error = new Error(stringService.verifyOwnerUnauthorized);
error.status = 403;
throw error;
}