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

Feature/edit company details #308

Merged
merged 32 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f289e85
Added route to edit company details
Feb 5, 2023
d0a408f
Changed request from patch to put in route
Feb 5, 2023
6b848c3
Added company editing details implementation to services/company.js
Feb 5, 2023
094720f
Fixed small mistake in route
Feb 5, 2023
e4d10be
Added function sendCompanyEditedNotification to services and added CO…
Feb 5, 2023
c2ff584
Added 'edit' validator
Feb 5, 2023
074ef1e
Fixed some white space trouble and import trouble
Feb 5, 2023
829a94d
Added some tests that should fail
Feb 9, 2023
a966f2b
Added tests that should pass
Feb 9, 2023
acecde9
Fixed all tests, removed unnecessary validators and emails previously…
Feb 14, 2023
77c753f
removed changes to 'CanManageAccountSettings' validator
Feb 27, 2023
7a94454
removed changes in validators/company.js that were accidentally left …
Feb 27, 2023
181ab39
removed trailing spaces
Feb 28, 2023
74e01cd
removed unused imports
Feb 28, 2023
cacaef5
Added more relevant tests such as fail for no authentication and fail…
Feb 28, 2023
fae9d39
Added service to update all offers from a company (based on company ID)
Feb 28, 2023
ca83681
Changed edit company route to update details in changed company offers
Feb 28, 2023
91ca4ed
Added test to see if offers reflect changes in company details
Feb 28, 2023
8377f3f
Used previously defined function 'changeAttributes' to edit company d…
Feb 28, 2023
cf4450d
removed unnecessary comment
Feb 28, 2023
07ba8e3
fixed 'updateAllOffersByCompanyId' redundancies
Feb 28, 2023
b9d2247
changed function call in route to new name (changeAttributes)
Feb 28, 2023
b03dc26
wrote validator support for editing a company
Feb 28, 2023
3267589
Added validators.edit verification
Feb 28, 2023
44ae945
updateAllOffersByCompanyId now returns all offers updated
Feb 28, 2023
9f8e028
Added test to check if offers reflect company changes
Feb 28, 2023
8cc776f
fixed describe test names
Mar 7, 2023
309de07
refactored my tests, removing duplicated code and creating everything…
Mar 7, 2023
33a248f
Fixed test asserts to correct methods
Mar 9, 2023
0c9e127
Removed redundant testing on tests that return bad HTTPstatus (and th…
Mar 9, 2023
ccf60f7
Changed small detail in services/offer.js
Mar 9, 2023
f04634a
Refactored my tests to create accounts and companies using shorter code
Mar 9, 2023
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
23 changes: 13 additions & 10 deletions src/api/middleware/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,18 @@ export const isNotDisabled = (owner) => async (req, res, next) => {
};

export const canManageAccountSettings = (companyId) => async (req, res, next) => {
const company = await (new CompanyService()).findById(companyId, true);

// only god or the same company can change account settings
if (!req.hasAdminPrivileges && company._id.toString() !== req.targetOwner) {
return next(new APIError(
HTTPStatus.FORBIDDEN,
ErrorTypes.FORBIDDEN,
ValidationReasons.INSUFFICIENT_PERMISSIONS_COMPANY_SETTINGS
));
try {
Naapperas marked this conversation as resolved.
Show resolved Hide resolved
const company = await (new CompanyService()).findById(companyId, true);
if (!req.hasAdminPrivileges && company._id.toString() !== req.targetOwner) {
return next(new APIError(
HTTPStatus.FORBIDDEN,
ErrorTypes.FORBIDDEN,
ValidationReasons.INSUFFICIENT_PERMISSIONS_COMPANY_SETTINGS
));
}
return next();
} catch (err) {
console.error(err);
return next(err);
}
return next();
};
26 changes: 25 additions & 1 deletion src/api/middleware/validators/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export const deleteCompany = useExpressValidators([
existingCompanyParamValidator,
]);


export const getOffers = useExpressValidators([
existingCompanyParamValidator,
]);
Expand Down Expand Up @@ -106,3 +105,28 @@ export const setDefaultValuesConcurrent = (req, res, next) => {
}
return next();
};

export const edit = useExpressValidators([
existingCompanyParamValidator,
body("name", ValidationReasons.DEFAULT)
.optional()
.isString().withMessage(ValidationReasons.STRING).bail()
.isLength({ min: CompanyConstants.companyName.min_length })
.withMessage(ValidationReasons.TOO_SHORT(CompanyConstants.companyName.min_length))
.isLength({ max: CompanyConstants.companyName.max_length })
.withMessage(ValidationReasons.TOO_LONG(CompanyConstants.companyName.max_length)),
body("bio", ValidationReasons.DEFAULT)
.optional()
.isString().withMessage(ValidationReasons.STRING).bail()
.isLength({ max: CompanyConstants.bio.max_length })
.withMessage(ValidationReasons.TOO_LONG(CompanyConstants.bio.max_length)),
body("contacts", ValidationReasons.DEFAULT)
.optional()
.customSanitizer(ensureArray)
.isArray({ min: CompanyConstants.contacts.min_length, max: CompanyConstants.contacts.max_length })
.withMessage(ValidationReasons.ARRAY_SIZE(CompanyConstants.contacts.min_length, CompanyConstants.contacts.max_length)),
body("logo", ValidationReasons.DEFAULT)
.optional()
.isString().withMessage(ValidationReasons.STRING).bail()
.isURL().withMessage(ValidationReasons.URL),
]);
29 changes: 29 additions & 0 deletions src/api/routes/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,33 @@ export default (app) => {
return next(err);
}
});


/**
* Edit company details.
* Company or admin can edit.
*/

router.put("/:companyId/edit",
or([
authMiddleware.isCompany,
authMiddleware.isAdmin,
authMiddleware.isGod
], { status_code: HTTPStatus.UNAUTHORIZED, error_code: ErrorTypes.FORBIDDEN, msg: ValidationReasons.INSUFFICIENT_PERMISSIONS }),
validators.edit,
(req, res, next) => companyMiddleware.canManageAccountSettings(req.params.companyId)(req, res, next),
(req, res, next) => companyMiddleware.isNotBlocked(req.params.companyId)(req, res, next),
(req, res, next) => companyMiddleware.isNotDisabled(req.params.companyId)(req, res, next),
async (req, res, next) => {
try {
const companyService = new CompanyService();
const offerService = new OfferService();
const company = await companyService.changeAttributes(req.params.companyId, req.body);
await offerService.updateAllOffersByCompanyId(company);
return res.json(company);
} catch (err) {
return next(err);
}
}
);
};
29 changes: 18 additions & 11 deletions src/services/company.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { COMPANY_BLOCKED_NOTIFICATION,
import {
COMPANY_BLOCKED_NOTIFICATION,
COMPANY_UNBLOCKED_NOTIFICATION,
COMPANY_DISABLED_NOTIFICATION,
COMPANY_ENABLED_NOTIFICATION,
COMPANY_DELETED_NOTIFICATION
COMPANY_DELETED_NOTIFICATION,
} from "../email-templates/companyManagement.js";
import EmailService from "../lib/emailService.js";
import Account from "../models/Account.js";
Expand Down Expand Up @@ -60,14 +61,14 @@ class CompanyService {
if (!showHidden) query.withoutBlocked().withoutDisabled();
if (!showAdminReason) query.hideAdminReason();
const company = await query;
return company;
return company;
}

/**
* @param {@param} companyId Id of the company
*/
async block(companyId, adminReason) {
const company = await Company.findByIdAndUpdate(
const company = await Company.findByIdAndUpdate(
companyId,
{
isBlocked: true,
Expand Down Expand Up @@ -96,12 +97,19 @@ class CompanyService {
* @param {*} company_id id of the company
* @param {*} attributes object containing the attributes to change in company
*/
async changeAttributes(company_id, attributes) {
const company = await Company.findOneAndUpdate(
{ _id: company_id },
attributes,
{ new: true });
return company;
async changeAttributes(companyId, companyDetails) {
try {
const company = await Company.findOneAndUpdate(
{ _id: companyId },
companyDetails,
{ new: true }
);

return company;
} catch (err) {
console.error(err);
throw err;
}
}

/**
Expand Down Expand Up @@ -188,7 +196,6 @@ class CompanyService {
throw err;
}
}

}

export default CompanyService;
9 changes: 9 additions & 0 deletions src/services/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,15 @@ class OfferService {
await Offer.deleteMany({ owner: companyId });
}

async updateAllOffersByCompanyId(company) {
const changes = {
ownerName: company.name,
ownerLogo: company.logo,
contacts: company.contacts,
};
const offers = await Offer.updateMany({ owner: company._id }, changes, { new: true });
return offers;
}
}

export default OfferService;
Loading