Skip to content

Commit

Permalink
unitaryfund/metriq-app#386: Fix update route
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Feb 14, 2022
1 parent 676aafd commit e6bc04e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions metriq-api/api-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ router.route('/user/password')
.post(accountController.update_password)
router.route('/user')
.get(userController.read)
.post(userController.update)
.delete(userController.delete)
router.route('/user/submission/:page')
.get(userController.readSubmissions)
Expand Down
6 changes: 6 additions & 0 deletions metriq-api/controller/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ exports.read = async function (req, res) {
'Successfully retrieved user profile.')
}

exports.update = async function (req, res) {
routeWrapper(res,
async () => await userService.update(req.body),
'Successfully retrieved user profile.')
}

// Validate the delete request and delete the user.
exports.delete = async function (req, res) {
routeWrapper(res,
Expand Down
12 changes: 12 additions & 0 deletions metriq-api/service/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ class UserService extends ModelService {
return { success: true, body: await this.sanitize(user) }
}

async update (reqBody) {
const user = await this.getByPk(reqBody.id)
if (!user) {
return { success: false, error: 'User not found.' }
}

user.affiliation = reqBody.affiliation
await user.save()

return { success: true, body: await this.sanitize(user) }
}

async login (reqBody) {
const user = await this.getByUsernameOrEmail(reqBody.username)
if (!user) {
Expand Down
2 changes: 1 addition & 1 deletion metriq-app

0 comments on commit e6bc04e

Please sign in to comment.