refactor: streamline SVG handling and improve error messages in user and email controllers

This commit is contained in:
Ari Yeger
2025-07-23 16:24:47 -04:00
parent 8eaa0c3d06
commit 9add60735e
7 changed files with 72 additions and 72 deletions

View File

@ -144,9 +144,5 @@ async function withTimeout(promise, ms) {
}
export default {
withTimeout,
transporter,
router
};
export default router;

View File

@ -41,12 +41,12 @@ router.get("/",
*/
async (req, res, next) => {
if (!req.body || !req.body.username || !req.body.password) {
return res.status(400).json({data: null, error: 'Username and password are required'});
return res.status(400).json({data: null, message: 'Username and password are required'});
}
await users.addNewUser(req.body.username, req.body.password)
.then((user) => {
if (!user) {
return res.status(500).json({data: null, error: 'User not found after insertion'});
return res.status(500).json({data: null, message: 'User not found after insertion'});
}
res.status(200).json({data: user, message: 'User added successfully'});
})
@ -64,21 +64,21 @@ router.get("/:identifier",
*/
async (req, res, next) => {
const userIdentifier = req.params.identifier;
if (!userIdentifier) return res.status(400).json({data: null, error: 'User identifier is required'});
if (!userIdentifier) return res.status(400).json({data: null, message: 'User identifier is required'});
await users.getUser(userIdentifier)
.then(user => {
if (!user) return res.status(404).json({data: null, error: 'User not found'});
if (!user) return res.status(404).json({data: null, message: 'User not found'});
res.status(200).json({data: user});
})
.catch((err) => {
if (err instanceof Error) {
if (err.message === 'User not found') {
return res.status(404).json({data: null, error: 'User not found'});
return res.status(404).json({data: null, error: err});
}
if (err.message === 'Multiple users found with the same identifier, something has gone wrong') {
res.status(500).json({
data: null,
error: 'Multiple users found with the same identifier, something has gone wrong'
error: err
});
} else next(err);
} else {
@ -100,16 +100,16 @@ router.post("/login",
const {username, password} = req.body;
if (!username || !password) return res.status(400).json({
data: null,
error: 'Username and password are required'
message: 'Username and password are required'
});
await users.login(username, password).then(data => {
if (!data) return res.status(401).json({data: null, error: 'Invalid username or password'});
if (!data) return res.status(401).json({data: null, message: 'Invalid username or password'});
res.status(200).json({data: data, message: 'Login successful'});
})
.catch((err) => {
if (err instanceof Error) {
if (err.message === 'Invalid username or password') {
res.status(401).json({data: null, error: 'Invalid username or password'});
res.status(401).json({data: null, error: err});
} else next(err);
} else {
next(new Error('An unhandled error occurred while login'));
@ -127,12 +127,10 @@ router.patch("/:identifier", authHandler.authenticateUser,
*/
async (req, res, next) => {
const userIdentifier = req.params.id;
if (!userIdentifier) {
return res.status(400).json({data: null, error: 'User Identifier is required'});
}
if (!userIdentifier) return res.status(400).json({data: null, message: 'User Identifier is required'});
await users.updateUser(req.body)
.then(user => {
if (!user) res.status(404).json({data: null, error: 'User not found'});
if (!user) res.status(404).json({data: null, message: 'User not found'});
else res.status(200).json({data: user, message: 'User updated successfully'});
})
.catch((err) => next(err))
@ -150,10 +148,10 @@ router.delete("/:identifier", authHandler.authenticateUser,
*/
async (req, res, next) => {
const userIdentifier = req.params.identifier;
if (!userIdentifier) return res.status(400).json({data: null, error: 'User identifier is required'});
if (!userIdentifier) return res.status(400).json({data: null, message: 'User identifier is required'});
await users.deleteUser(userIdentifier)
.then(user => {
if (!user) return res.status(404).json({data: null, error: 'User not found'});
if (!user) return res.status(404).json({data: null, message: 'User not found'});
res.status(200).json({data: user, message: 'User deleted successfully'});
})
.catch((err) => next(err));