diff --git a/src/api/middleware/company.js b/src/api/middleware/company.js index 9cfc51ca..6542b20c 100644 --- a/src/api/middleware/company.js +++ b/src/api/middleware/company.js @@ -6,6 +6,7 @@ import CompanyConstants from "../../models/constants/Company.js"; import Offer from "../../models/Offer.js"; import CompanyService from "../../services/company.js"; import OfferService from "../../services/offer.js"; +import { existingModel } from "./utils.js"; export const verifyMaxConcurrentOffers = (owner, publishDate, publishEndDate, offerId) => async (req, res, next) => { @@ -122,3 +123,15 @@ export const canManageAccountSettings = (companyId) => async (req, res, next) => } return next(); }; + +export const existingCompany = existingModel( + (param, req) => + new CompanyService().findById( + param, + req.hasAdminPrivileges, + req.hasAdminPrivileges + ), + "companyId", + "company", + ValidationReasons.COMPANY_NOT_FOUND +); diff --git a/src/api/middleware/offer.js b/src/api/middleware/offer.js index 0fcd512b..a01eae22 100644 --- a/src/api/middleware/offer.js +++ b/src/api/middleware/offer.js @@ -1,6 +1,7 @@ import OfferService from "../../services/offer.js"; import * as companyMiddleware from "./company.js"; -import { when } from "./utils.js"; +import { when, existingModel } from "./utils.js"; +import ValidationReasons from "./validators/validationReasons.js"; export const isOwnerNotDisabled = async (req, res, next) => { const offer = await (new OfferService()).getOfferById(req.params.offerId, req.targetOwner, true); @@ -24,3 +25,16 @@ export const setTargetOwner = (req, res, next) => { return next(); }; + +export const existingOffer = existingModel( + (param, req) => + new OfferService().getOfferById( + param, + req.targetOwner, + req.hasAdminPrivileges, + req.hasAdminPrivileges + ), + "offerId", + "offer", + ValidationReasons.OFFER_NOT_FOUND +); diff --git a/src/api/middleware/utils.js b/src/api/middleware/utils.js index 8d8522cc..8e22aa86 100644 --- a/src/api/middleware/utils.js +++ b/src/api/middleware/utils.js @@ -94,3 +94,17 @@ export const storeInLocals = (req, obj) => { ...obj, }; }; + +export const existingModel = (fn, param, variable, error) => async (req, res, next) => { + const model = await fn(req.params[param], req); + + if (!model) return next(new APIError( + HTTPStatus.NOT_FOUND, + ErrorTypes.FORBIDDEN, + error(req.params[param]) + )); + + req[variable] = model; + + return next(); +}; diff --git a/test/end-to-end/company.js b/test/end-to-end/company.js index 32c10d9c..6a8a3865 100644 --- a/test/end-to-end/company.js +++ b/test/end-to-end/company.js @@ -571,7 +571,7 @@ describe("Company endpoint", () => { const res = await test_agent .put(`/company/${id}/block`) .send({ adminReason }) - .expect(HTTPStatus.UNPROCESSABLE_ENTITY); + .expect(HTTPStatus.NOT_FOUND); expect(res.body).toHaveProperty("error_code", ErrorTypes.VALIDATION_ERROR); expect(res.body).toHaveProperty("errors"); expect(res.body.errors[0]).toHaveProperty("param", "companyId"); @@ -812,7 +812,7 @@ describe("Company endpoint", () => { const id = "111111111111111111111111"; const res = await test_agent .put(`/company/${id}/unblock`) - .expect(HTTPStatus.UNPROCESSABLE_ENTITY); + .expect(HTTPStatus.NOT_FOUND); expect(res.body).toHaveProperty("error_code", ErrorTypes.VALIDATION_ERROR); expect(res.body).toHaveProperty("errors"); expect(res.body.errors[0]).toHaveProperty("param", "companyId"); @@ -1123,7 +1123,7 @@ describe("Company endpoint", () => { const res = await test_agent .put(`/company/${id}/enable`) .send(withGodToken()) - .expect(HTTPStatus.UNPROCESSABLE_ENTITY); + .expect(HTTPStatus.NOT_FOUND); expect(res.body).toHaveProperty("error_code", ErrorTypes.VALIDATION_ERROR); expect(res.body).toHaveProperty("errors"); @@ -1368,7 +1368,7 @@ describe("Company endpoint", () => { const res = await test_agent .put(`/company/${id}/disable`) .send(withGodToken()) - .expect(HTTPStatus.UNPROCESSABLE_ENTITY); + .expect(HTTPStatus.NOT_FOUND); expect(res.body).toHaveProperty("error_code", ErrorTypes.VALIDATION_ERROR); expect(res.body).toHaveProperty("errors"); @@ -1567,7 +1567,7 @@ describe("Company endpoint", () => { const res = await test_agent .post(`/company/${id}/delete`) .send(withGodToken()) - .expect(HTTPStatus.UNPROCESSABLE_ENTITY); + .expect(HTTPStatus.NOT_FOUND); expect(res.body).toHaveProperty("error_code", ErrorTypes.VALIDATION_ERROR); expect(res.body).toHaveProperty("errors"); @@ -1766,7 +1766,7 @@ describe("Company endpoint", () => { const res = await test_agent .get(`/company/${id}/hasReachedMaxConcurrentOffersBetweenDates`) .send(withGodToken({ publishDate, publishEndDate })) - .expect(HTTPStatus.UNPROCESSABLE_ENTITY); + .expect(HTTPStatus.NOT_FOUND); expect(res.body).toHaveProperty("error_code", ErrorTypes.VALIDATION_ERROR); expect(res.body).toHaveProperty("errors"); diff --git a/test/end-to-end/offer.js b/test/end-to-end/offer.js index 4fa37786..eada31ae 100644 --- a/test/end-to-end/offer.js +++ b/test/end-to-end/offer.js @@ -3025,7 +3025,7 @@ describe("Offer endpoint tests", () => { const res = await test_agent .post(`/offers/edit/${_id}`) .send(withGodToken()) - .expect(HTTPStatus.UNPROCESSABLE_ENTITY); + .expect(HTTPStatus.NOT_FOUND); expect(res.body.errors[0]).toHaveProperty("param", "offerId"); expect(res.body.errors[0]).toHaveProperty("msg", ValidationReasons.OFFER_NOT_FOUND(_id)); }); @@ -4501,7 +4501,7 @@ describe("Offer endpoint tests", () => { const res = await test_agent .put(`/offers/${_id}/archive`) .send(withGodToken()) - .expect(HTTPStatus.UNPROCESSABLE_ENTITY); + .expect(HTTPStatus.NOT_FOUND); expect(res.body.errors[0]).toHaveProperty("param", "offerId"); expect(res.body.errors[0]).toHaveProperty("msg", ValidationReasons.OFFER_NOT_FOUND(_id));