Skip to content

Commit

Permalink
fix: adjust user update logic in api
Browse files Browse the repository at this point in the history
  • Loading branch information
johackim committed Apr 14, 2024
1 parent a737e46 commit 0554429
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pages/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ const getQuery = (_, res, user) => res.status(200).send({ user });
const putQuery = async (body, res, user) => {
const { firstName = '', lastName = '', password } = body || {};

if (!firstName && !lastName && !password) {
return res.status(400).send({ message: 'Missing required parameters' });
if (firstName && lastName) {
await user.update({ firstName, lastName });
await upsertCustomer(user.email, user.id, `${firstName} ${lastName}`);
return res.status(200).send({ message: 'User updated' });
}

if (password) {
await isValidPassword(password).catch((err) => res.status(401).send({ message: err.message }));
const hashPassword = await bcrypt.hash(password, 10);
await user.update({ password: hashPassword });
return res.status(200).send({ message: 'Password updated' });
}

await user.update({ firstName, lastName });
await upsertCustomer(user.email, user.id, `${firstName} ${lastName}`);

return res.status(200).send({ message: 'User updated' });
return res.status(400).send({ message: 'Missing required parameters' });
};

const deleteQuery = async (_, res, user) => {
Expand Down

0 comments on commit 0554429

Please sign in to comment.