'use strict' const Joi = require('joi') const pluginConfig = { handlerType: 'grouping', docs: { description: 'active memberships', notes: 'A list of groupings with active membership', }, } const validators = { /** Validate the header (cookie check) */ // headers: true, /** Validate the route params (/active/{thing}) */ params: Joi.object({ user_id: Joi.number() }), /** Validate the route query (/active/{thing}?limit=10&offset=10) */ // query: true, /** Validate the incoming payload (POST method) */ // payload: true, } const responseSchemas = { groupingsList: Joi.object({ grouping_id: Joi.number(), grouping_name: Joi.string(), grouping_type: Joi.string(), }), } module.exports = { method: 'GET', path: '/active/{user_id}', options: { ...pluginConfig.docs, tags: ['api'], /** Protect this route with authentication? */ auth: false, handler: async function (request, h) { const { membershipService } = request.services() const userId = request.params.user_id const groupings = await membershipService.findGroupingsById(userId) try { return { ok: true, handler: pluginConfig.handlerType, data: groupings, } } catch (err) { return { ok: false, handler: pluginConfig.handlerType, data: { error: `${err}` }, } } }, /** Validate based on validators object */ validate: { ...validators, failAction: 'log', }, /** Validate the server response */ response: { schema: Joi.object({ ok: Joi.bool(), handler: Joi.string(), data: Joi.array().items(responseSchemas.groupingsList), }), }, }, }