Skip to content

Commit ce2a571

Browse files
committed
Refactor company profile endpoint and middleware
1 parent bb130ae commit ce2a571

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

src/api/middleware/company.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,45 +85,41 @@ export const profileComplete = async (req, res, next) => {
8585
return next();
8686
};
8787

88-
export const restrictedAccess = (owner) => async (req, res, next) => {
89-
const company = await (new CompanyService()).findById(owner, true);
90-
let error = {};
91-
92-
if (req.params?.companyId === req.user.company) {
93-
let reason = ValidationReasons.UNKNOWN;
88+
export const canAccessProfile = (companyId) => async (req, res, next) => {
89+
const company = await new CompanyService().findById(companyId, true);
9490

95-
if (company.isBlocked)
96-
reason = ValidationReasons.COMPANY_BLOCKED;
97-
else if (company.isDisabled)
98-
reason = ValidationReasons.COMPANY_DISABLED;
99-
100-
error = new APIError(
101-
HTTPStatus.OK,
91+
const notFound = () =>
92+
new APIError(
93+
HTTPStatus.UNPROCESSABLE_ENTITY,
10294
ErrorTypes.VALIDATION_ERROR,
103-
reason,
104-
{ company: company }
95+
[
96+
{
97+
value: companyId,
98+
msg: ValidationReasons.COMPANY_NOT_FOUND(companyId),
99+
param: "companyId",
100+
location: "params",
101+
},
102+
]
105103
);
106-
} else {
107-
error = new APIError(
108-
HTTPStatus.FORBIDDEN,
109-
ErrorTypes.FORBIDDEN,
110-
ValidationReasons.NOT_FOUND
104+
105+
const errorOrNotFound = (reason) =>
106+
companyId === req.user?.company?.toString() || req.hasAdminPrivileges
107+
? new APIError(HTTPStatus.FORBIDDEN, ErrorTypes.FORBIDDEN, reason)
108+
: notFound();
109+
110+
if (!company.hasFinishedRegistration)
111+
return next(
112+
errorOrNotFound(ValidationReasons.REGISTRATION_NOT_FINISHED)
111113
);
112-
}
113114

114-
return next(error);
115-
};
115+
if (req.hasAdminPrivileges)
116+
return next();
116117

117-
export const registrationStatus = (owner) => async (req, res, next) => {
118-
const company = await (new CompanyService()).findById(owner, true);
118+
if (company.isBlocked)
119+
return next(errorOrNotFound(ValidationReasons.COMPANY_BLOCKED));
119120

120-
if (!company.hasFinishedRegistration) {
121-
return next(new APIError(
122-
HTTPStatus.FORBIDDEN,
123-
ErrorTypes.FORBIDDEN,
124-
(req.params?.companyId !== req.user.company) ? ValidationReasons.NOT_FOUND : ValidationReasons.REGISTRATION_NOT_FINISHED
125-
));
126-
}
121+
if (company.isDisabled && companyId !== req.user?.company?.toString())
122+
return next(notFound());
127123

128124
return next();
129125
};

src/api/routes/company.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,23 @@ export default (app) => {
7575

7676
router.get("/:companyId",
7777
validators.profile,
78-
(req, res, next) => companyMiddleware.restrictedAccess(req.params.companyId)(req, res, next),
79-
(req, res, next) => companyMiddleware.registrationStatus(req.params.companyId)(req, res, next),
78+
(req, res, next) => companyMiddleware.canAccessProfile(req.params.companyId)(req, res, next),
8079
async (req, res) => {
81-
const company = await new CompanyService().findById(req.params.companyId, req.hasAdminPrivileges, req.hasAdminPrivileges);
82-
const offers = (await new OfferService()
83-
.getOffersByCompanyId(req.params.companyId, req.targetOwner, req.hasAdminPrivileges, {
84-
sort: { publishDate: "desc" }, limit: CompanyConstants.offers.max_profile_visible
85-
})
80+
const company = await new CompanyService().findById(
81+
req.params.companyId,
82+
// Can be safely set to true, as the middleware takes
83+
// care of validation for us
84+
true,
85+
req.hasAdminPrivileges
86+
);
87+
const offers = await new OfferService().getOffersByCompanyId(
88+
req.params.companyId,
89+
req.targetOwner,
90+
req.hasAdminPrivileges,
91+
{
92+
sort: { publishDate: "desc" },
93+
limit: CompanyConstants.offers.max_profile_visible,
94+
}
8695
);
8796
return res.json({ company, offers });
8897
}

0 commit comments

Comments
 (0)