'use strict' const Joi = require('joi') const pluginConfig = { handlerType: 'profile', docs: { description: 'profiles', notes: 'A list of profiles associated with this user', }, } 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 = { profilesList: Joi.object({ profile_id: Joi.number().required(), user_id: Joi.number().required(), responses: Joi.array().items( Joi.object({ response_id: Joi.number().required(), profile_id: Joi.number().required(), response_key_id: Joi.number().required(), val: Joi.string().required(), }), ), response_keys: Joi.array(), }), } module.exports = { method: 'GET', path: '/{user_id}', options: { ...pluginConfig.docs, tags: ['api'], /** Protect this route with authentication? */ auth: false, handler: async function (request, h) { const { profileService } = request.services() const userId = request.params.user_id const profiles = await profileService.getCompleteProfilesFor(userId) try { return { ok: true, handler: pluginConfig.handlerType, data: profiles, } } 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.profilesList), }), }, }, }