'use strict' const Joi = require('joi') const apiSchema = require('../../schemas/api') const errorSchema = require('../../schemas/errors') const groupingSchema = require('../../schemas/groupings') 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({ profile_id: Joi.number() }), /** Validate the route query (/active/{thing}?limit=10&offset=10) */ query: Joi.object({ type: Joi.string().lowercase().min(6).max(11) }), /** Validate the incoming payload (POST method) */ // payload: true, } const responseSchemas = { single: groupingSchema.single, list: groupingSchema.list, error: errorSchema.single } module.exports = { method: 'GET', path: '/{profile_id}', options: { ...pluginConfig.docs, tags: ['api'], /** Protect this route with authentication? */ auth: false, cors: true, handler: async function (request, h) { const { membershipService } = request.services() const membershipType = request.query.type const profileId = request.params.profile_id const groupings = await membershipService.findGroupingsByProfileId( profileId, membershipType ) 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: apiSchema.single.append({ data: responseSchemas.list }) }, }, }