Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup middlewares/validators #309

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/api/middleware/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {

Expand Down Expand Up @@ -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
);
16 changes: 15 additions & 1 deletion src/api/middleware/offer.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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
);
14 changes: 14 additions & 0 deletions src/api/middleware/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
12 changes: 6 additions & 6 deletions test/end-to-end/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions test/end-to-end/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand Down Expand Up @@ -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));
Expand Down