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

update vendor route added #28

Merged
merged 1 commit into from
Nov 16, 2023
Merged
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
56 changes: 55 additions & 1 deletion backend/controllers/VendorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,58 @@ const createVendor = async (req, res, next) => {
next();
};

module.exports = {getVendor, getVendors, createVendor, getVendorById, authenticateVendor};
const updateVendor = async (req, res, next) => {
const {vendorId} = req.params;
const {
name,
email,
phone_number,
password,
website,
} = req.body;

// Checks if the required fields are present
if (!password || !email || !name) {
console.log(req.body);
return res.status(400).json({error: 'Missing required fields'});
}

// Hashes the password using bcrypt
let passwordHash;
try {
const salt = await genSalt(10);
passwordHash = await hash(password, salt);
} catch (err) {
console.log(err);
res.status(500).json({error: err});
return;
}

try {
await db.none(
'UPDATE Vendors SET \
name = $1, \
email = $2, \
phone_number = $3, \
password = $4, \
website = $5 \
WHERE vendor_id = $6',
[name, email, phone_number, passwordHash, website, vendorId],
);
} catch (err) {
// Duplicate emails are not allowed
if (err.code === '23505') {
res.status(400).json({error: 'A vendor with that email already exists'});
return;
}

// Other internal error
console.log(err);
res.status(500).json({error: err});
return;
}

next();
};

module.exports = {getVendor, getVendors, createVendor, getVendorById, authenticateVendor, updateVendor};
3 changes: 3 additions & 0 deletions backend/routes/VendorRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
createVendor,
getVendorById,
authenticateVendor,
updateVendor,
} = require('../controllers/VendorController');
const sendSuccessResponse = require('../middleware/successResponse');

Expand All @@ -23,4 +24,6 @@
res.status(200).json({status: 'success'});
});

router.put('/:vendorId', updateVendor, sendSuccessResponse);

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a database access
, but is not rate-limited.

module.exports = router;
Loading