'use strict' const Joi = require('joi') const pluginConfig = { handlerType: 'user', 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().integer().greater(0).required(), user_id: Joi.number().integer().greater(0).required(), responses: Joi.array().items( Joi.object({ response_key_id: Joi.number().required(), profile_id: Joi.number().required(), response_id: Joi.number().required(), val: Joi.string().required(), }), ), user_type: Joi.string().required(), }), error: Joi.object({ error: Joi.string(), }), } module.exports = { method: 'GET', path: '/{user_id}/profiles', options: { ...pluginConfig.docs, tags: ['api'], /** Protect this route with authentication? */ auth: false, cors: true, handler: async function (request, h) { const { userService, profileService } = request.server.services() const userId = request.params.user_id const user = await userService.findById(userId) const type = user.is_poster == 1 ? 'poster' : 'seeker' const profiles = await profileService.getCompleteProfilesFor( userId, type, ) 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: { status: { 200: Joi.object({ ok: Joi.bool(), handler: Joi.string(), data: Joi.array().items(responseSchemas.profilesList), }), 500: Joi.object({ ok: Joi.bool(), handler: Joi.string(), data: responseSchemas.error, }), }, }, }, }