Skip to content

Commit

Permalink
Resolve display names from OSM API instead of users table
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeorge committed Apr 7, 2022
1 parent ed4dbc7 commit 2c60733
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
6 changes: 1 addition & 5 deletions app/db/migrations/20220302104250_add_user_badges.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ exports.up = async function (knex) {
.references('id')
.inTable('organization_badge')
.onDelete('CASCADE')
table
.integer('user_id')
.references('id')
.inTable('users')
.onDelete('CASCADE')
table.integer('user_id')
table.datetime('assigned_at').defaultTo(knex.fn.now())
table.datetime('valid_until')
table.unique(['badge_id', 'user_id'])
Expand Down
30 changes: 22 additions & 8 deletions app/manage/badges.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const yup = require('yup')
const organization = require('../lib/organization')
const profile = require('../lib/profile')
const { routeWrapper } = require('./utils')
const team = require('../lib/team')

/**
* Get the list of badges of an organization
Expand Down Expand Up @@ -84,10 +85,9 @@ const getBadge = routeWrapper({
.where('id', req.params.badgeId)
.returning('*')

const users = await conn('user_badges')
let users = await conn('user_badges')
.select({
id: 'user_badges.user_id',
profile: 'profile',
assignedAt: 'user_badges.assigned_at',
validUntil: 'user_badges.valid_until'
})
Expand All @@ -96,18 +96,29 @@ const getBadge = routeWrapper({
'user_badges.badge_id',
'organization_badge.id'
)
.leftJoin('users', 'user_badges.user_id', 'users.id')
.where('badge_id', req.params.badgeId)
.returning('*')

reply.send({
...badge,
users: users.map((u) => ({
if (users.length > 0) {
// Get user profiles
const userProfiles = (
await team.resolveMemberNames(users.map((u) => u.id))
).reduce((acc, u) => {
acc[u.id] = u
return acc
}, {})

users = users.map((u) => ({
id: u.id,
assignedAt: u.assignedAt,
validUntil: u.validUntil,
displayName: u.profile.displayName
displayName: userProfiles[u.id] ? userProfiles[u.id].name : ''
}))
}

reply.send({
...badge,
users
})
} catch (err) {
console.log(err)
Expand Down Expand Up @@ -218,10 +229,13 @@ const assignUserBadge = routeWrapper({

reply.send(badge)
} catch (err) {
console.log(err)
if (err.code === '23505') {
return reply.boom.badRequest('User is already assigned to badge.')
} else {
return reply.boom.badRequest('Unexpected error, please try again later.')
return reply.boom.badRequest(
'Unexpected error, please try again later.'
)
}
}
}
Expand Down

0 comments on commit 2c60733

Please sign in to comment.