From 9d82c27819c8b2275697192fe9049197fd5efe86 Mon Sep 17 00:00:00 2001 From: Vitor George Date: Mon, 14 Mar 2022 11:24:14 +0000 Subject: [PATCH] Move route wrapper to utils file, add documentation header --- app/manage/badges.js | 475 ++++++++++++++++++++++--------------------- app/manage/utils.js | 30 ++- 2 files changed, 277 insertions(+), 228 deletions(-) diff --git a/app/manage/badges.js b/app/manage/badges.js index fa11579c..47eee591 100644 --- a/app/manage/badges.js +++ b/app/manage/badges.js @@ -2,247 +2,268 @@ const db = require('../db') const yup = require('yup') const organization = require('../lib/organization') const profile = require('../lib/profile') +const { routeWrapper } = require('./utils') -function route ({ validate, handler }) { - return async (req, reply) => { +/** + * Get the list of badges of an organization + */ +const listBadges = routeWrapper({ + validate: { + params: yup + .object({ + id: yup.number().required().positive().integer() + }) + .required() + }, + handler: async function (req, reply) { try { - if (validate.params) { - req.params = await validate.params.validate(req.params) - } - - if (validate.body) { - req.body = await validate.body.validate(req.body) - } - } catch (error) { - console.log(error) - reply.boom.badRequest(error) + const conn = await db() + const badges = await conn('organization_badge') + .select('*') + .where('organization_id', req.params.id) + reply.send(badges) + } catch (err) { + console.log(err) + return reply.boom.badRequest(err.message) } - await handler(req, reply) } -} +}) -module.exports = { - listBadges: route({ - validate: { - params: yup - .object({ - id: yup.number().required().positive().integer() - }) - .required() - }, - handler: async function (req, reply) { - try { - const conn = await db() - const badges = await conn('organization_badge') - .select('*') - .where('organization_id', req.params.id) - reply.send(badges) - } catch (err) { - console.log(err) - return reply.boom.badRequest(err.message) - } - } - }), - createBadge: route({ - validate: { - params: yup - .object({ - id: yup.number().required().positive().integer() - }) - .required(), - body: yup - .object({ - name: yup.string().required(), - color: yup.string().required() +/** + * Create organization badge + */ +const createBadge = routeWrapper({ + validate: { + params: yup + .object({ + id: yup.number().required().positive().integer() + }) + .required(), + body: yup + .object({ + name: yup.string().required(), + color: yup.string().required() + }) + .required() + }, + handler: async function (req, reply) { + try { + const conn = await db() + const [badge] = await conn('organization_badge') + .insert({ + organization_id: req.params.id, + ...req.body }) - .required() - }, - handler: async function (req, reply) { - try { - const conn = await db() - const [badge] = await conn('organization_badge') - .insert({ - organization_id: req.params.id, - ...req.body - }) - .returning('*') - reply.send(badge) - } catch (err) { - console.log(err) - return reply.boom.badRequest(err.message) - } + .returning('*') + reply.send(badge) + } catch (err) { + console.log(err) + return reply.boom.badRequest(err.message) } - }), - patchBadge: route({ - validate: { - params: yup - .object({ - id: yup.number().required().positive().integer(), - badgeId: yup.number().required().positive().integer() - }) - .required(), - body: yup - .object({ - name: yup.string().optional(), - color: yup.string().optional() - }) - .required() - }, - handler: async function (req, reply) { - try { - const conn = await db() - const [badge] = await conn('organization_badge') - .update(req.body) - .where('id', req.params.badgeId) - .returning('*') - reply.send(badge) - } catch (err) { - console.log(err) - return reply.boom.badRequest(err.message) - } + } +}) + +/** + * Edit organization badge + */ +const patchBadge = routeWrapper({ + validate: { + params: yup + .object({ + id: yup.number().required().positive().integer(), + badgeId: yup.number().required().positive().integer() + }) + .required(), + body: yup + .object({ + name: yup.string().optional(), + color: yup.string().optional() + }) + .required() + }, + handler: async function (req, reply) { + try { + const conn = await db() + const [badge] = await conn('organization_badge') + .update(req.body) + .where('id', req.params.badgeId) + .returning('*') + reply.send(badge) + } catch (err) { + console.log(err) + return reply.boom.badRequest(err.message) } - }), - deleteBadge: route({ - validate: { - params: yup - .object({ - badgeId: yup.number().required().positive().integer() - }) - .required() - }, - handler: async function (req, reply) { - try { - const conn = await db() - await conn('organization_badge') - .delete() - .where('id', req.params.badgeId) - return reply.sendStatus(200) - } catch (err) { - console.log(err) - return reply.boom.badRequest(err.message) - } + } +}) + +/** + * Delete organization badge + */ +const deleteBadge = routeWrapper({ + validate: { + params: yup + .object({ + badgeId: yup.number().required().positive().integer() + }) + .required() + }, + handler: async function (req, reply) { + try { + const conn = await db() + await conn('organization_badge') + .delete() + .where('id', req.params.badgeId) + return reply.sendStatus(200) + } catch (err) { + console.log(err) + return reply.boom.badRequest(err.message) } - }), - assignUserBadge: route({ - validate: { - params: yup - .object({ - id: yup.number().required().positive().integer(), - badgeId: yup.number().required().positive().integer(), - userId: yup.number().required().positive().integer() - }) - .required(), - body: yup - .object({ - assigned_at: yup.date().optional(), - valid_until: yup.date().optional() + } +}) + +/** + * Assign organization badge to an user + */ +const assignUserBadge = routeWrapper({ + validate: { + params: yup + .object({ + id: yup.number().required().positive().integer(), + badgeId: yup.number().required().positive().integer(), + userId: yup.number().required().positive().integer() + }) + .required(), + body: yup + .object({ + assigned_at: yup.date().optional(), + valid_until: yup.date().optional() + }) + }, + handler: async function (req, reply) { + try { + const conn = await db() + + // user is member + await organization.isMember(req.params.id, req.params.userId) + + // assign badge + const [badge] = await conn('user_badge') + .insert({ + user_id: req.params.userId, + badge_id: req.params.badgeId, + assigned_at: req.body.assigned_at, + valid_until: req.body.valid_until }) - }, - handler: async function (req, reply) { - try { - const conn = await db() - - // user is member - await organization.isMember(req.params.id, req.params.userId) - - // assign badge - const [badge] = await conn('user_badge') - .insert({ - user_id: req.params.userId, - badge_id: req.params.badgeId, - assigned_at: req.body.assigned_at, - valid_until: req.body.valid_until - }) - .returning('*') - - reply.send(badge) - } catch (err) { - console.log(err) - return reply.boom.badRequest(err.message) - } + .returning('*') + + reply.send(badge) + } catch (err) { + console.log(err) + return reply.boom.badRequest(err.message) } - }), - listUserBadges: route({ - validate: { - params: yup - .object({ - userId: yup.number().required().positive().integer() - }) - .required() - }, - handler: async function (req, reply) { - try { - const badges = await profile.getUserBadges(req.params.userId) - reply.send({ badges }) - } catch (err) { - console.log(err) - return reply.boom.badRequest(err.message) - } + } +}) + +/** + * List badges of an user + */ +const listUserBadges = routeWrapper({ + validate: { + params: yup + .object({ + userId: yup.number().required().positive().integer() + }) + .required() + }, + handler: async function (req, reply) { + try { + const badges = await profile.getUserBadges(req.params.userId) + reply.send({ badges }) + } catch (err) { + console.log(err) + return reply.boom.badRequest(err.message) } - }), - updateUserBadge: route({ - validate: { - params: yup - .object({ - badgeId: yup.number().required().positive().integer(), - userId: yup.number().required().positive().integer() + } +}) + +/** + * Update a badge assigned to an user + */ +const updateUserBadge = routeWrapper({ + validate: { + params: yup + .object({ + badgeId: yup.number().required().positive().integer(), + userId: yup.number().required().positive().integer() + }) + .required(), + body: yup + .object({ + assigned_at: yup.date().optional(), + valid_until: yup.date().optional() + }) + }, + handler: async function (req, reply) { + try { + const conn = await db() + + // assign badge + const [badge] = await conn('user_badge') + .update({ + assigned_at: req.body.assigned_at, + valid_until: req.body.valid_until }) - .required(), - body: yup - .object({ - assigned_at: yup.date().optional(), - valid_until: yup.date().optional() + .where({ + user_id: req.params.userId, + badge_id: req.params.badgeId }) - }, - handler: async function (req, reply) { - try { - const conn = await db() - - // assign badge - const [badge] = await conn('user_badge') - .update({ - assigned_at: req.body.assigned_at, - valid_until: req.body.valid_until - }) - .where({ - user_id: req.params.userId, - badge_id: req.params.badgeId - }) - .returning('*') - - reply.send(badge) - } catch (err) { - console.log(err) - return reply.boom.badRequest(err.message) - } + .returning('*') + + reply.send(badge) + } catch (err) { + console.log(err) + 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) - } + } +}) + +/** + * Remove badge assign to an user + */ +const removeUserBadge = routeWrapper({ + 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) } - }) + } +}) + +module.exports = { + listBadges, + createBadge, + patchBadge, + deleteBadge, + assignUserBadge, + listUserBadges, + updateUserBadge, + removeUserBadge } diff --git a/app/manage/utils.js b/app/manage/utils.js index 28eab9b1..6f144bf0 100644 --- a/app/manage/utils.js +++ b/app/manage/utils.js @@ -28,6 +28,34 @@ async function teamsMembersModeratorsHelper (teamsData) { }) } +/** + * Route wrapper to perform validation before processing + * the request. + * @param {function} config.validate Yup validation schema + * @param {function} config.handler Handler to execute if validation pass + * + * @returns {function} Route middleware function + */ +function routeWrapper (config) { + const { validate, handler } = config + return async (req, reply) => { + try { + if (validate.params) { + req.params = await validate.params.validate(req.params) + } + + if (validate.body) { + req.body = await validate.body.validate(req.body) + } + } catch (error) { + console.log(error) + reply.boom.badRequest(error) + } + await handler(req, reply) + } +} + module.exports = { - teamsMembersModeratorsHelper + teamsMembersModeratorsHelper, + routeWrapper }