diff --git a/app/db/migrations/20220302104250_add_user_badges.js b/app/db/migrations/20220302104250_add_user_badges.js index 026d2b00..bff7b27e 100644 --- a/app/db/migrations/20220302104250_add_user_badges.js +++ b/app/db/migrations/20220302104250_add_user_badges.js @@ -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']) diff --git a/app/manage/badges.js b/app/manage/badges.js index e258e77e..191b4a6b 100644 --- a/app/manage/badges.js +++ b/app/manage/badges.js @@ -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 @@ -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' }) @@ -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) @@ -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.' + ) } } }