Skip to content

Commit

Permalink
Remove user badge
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeorge committed Mar 8, 2022
1 parent 85aca1e commit 4f670d3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 11 deletions.
28 changes: 28 additions & 0 deletions app/manage/badges.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,33 @@ module.exports = {
return reply.boom.badRequest(err.message)
}
}
}),
removeUserBadge: route({
validate: {
params: yup
.object({
badgeId: yup.number().required().positive().integer(),
userId: yup.number().required().positive().integer()
})
.required()
},
handler: async function (req, reply) {
try {
const conn = await db()

// delete user badge
await conn('user_badge')
.delete()
.where({
user_id: req.params.userId,
badge_id: req.params.badgeId
})

reply.sendStatus(200)
} catch (err) {
console.log(err)
return reply.boom.badRequest(err.message)
}
}
})
}
18 changes: 7 additions & 11 deletions app/manage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const {
listBadges,
assignUserBadge,
listUserBadges,
updateUserBadge
updateUserBadge,
removeUserBadge
} = require('./badges')

const {
Expand Down Expand Up @@ -181,16 +182,11 @@ function manageRouter (nextApp) {
updateUserBadge
)

// router.patch(
// '/api/organizations/:id/badges/:badgeId/assign/:userId',
// can('organization:edit'),
// updateUserBadge
// )
// router.delete(
// '/api/organizations/:id/badges/:badgeId/remove/:userId',
// can('organization:edit'),
// removeUserBadge
// )
router.delete(
`/api/organizations/:id/member/:userId/badge/:badgeId`,
can('organization:edit'),
removeUserBadge
)

/**
* List, Create, Read, Update, Delete operations on profiles
Expand Down
57 changes: 57 additions & 0 deletions app/tests/api/badges-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,21 @@ test('List user badges', async (t) => {
test('Update badge', async (t) => {
const updateBadgeRoute = `/api/organizations/${org1.id}/member/${orgTeamMember.id}/badge/${badge2.id}`

// Disallow managers
await orgManager.agent
.patch(updateBadgeRoute)
.expect(401)

// Disallow org team Members
await orgTeamMember.agent
.patch(updateBadgeRoute)
.expect(401)

// Disallow non-members
await notOrgMember.agent
.patch(updateBadgeRoute)
.expect(401)

// Allow owners
const badgeAssignment = (await orgOwner.agent
.patch(updateBadgeRoute)
Expand All @@ -374,3 +389,45 @@ test('Update badge', async (t) => {
valid_until: '2021-01-01T00:00:00.000Z'
})
})

/**
* REMOVE BADGE
*/
test('Remove badge', async (t) => {
const removeBadgeRoute = `/api/organizations/${org1.id}/member/${orgTeamMember.id}/badge/${badge2.id}`

// Disallow managers
await orgManager.agent
.delete(removeBadgeRoute)
.expect(401)

// Disallow org team Members
await orgTeamMember.agent
.delete(removeBadgeRoute)
.expect(401)

// Disallow non-members
await notOrgMember.agent
.delete(removeBadgeRoute)
.expect(401)

// Allow owners
await orgOwner.agent
.delete(removeBadgeRoute)
.expect(200)

const badges = (await orgManager.agent
.get(`/api/user/${orgTeamMember.id}/badges`)
.expect(200)).body

t.like(badges, { badges: [
{
id: 3,
organization_id: 1,
name: 'badge number 3',
color: 'yellow',
assigned_at: '2020-07-07T00:00:00.000Z',
valid_until: '2020-08-08T00:00:00.000Z'
}
] })
})

0 comments on commit 4f670d3

Please sign in to comment.