'use strict' const Joi = require('joi') const apiSchema = require('../../schemas/api') const errorSchema = require('../../schemas/errors') const pluginConfig = { handlerType: 'profile', docs: { description: 'Returns a single profile with tags', notes: 'returns from the Profiles Table', }, } const responseSchemas = { profile: Joi.object({ profile_id: Joi.number(), user_id: Joi.number(), user_name: Joi.string(), responses: Joi.array().items(), tags: Joi.array().items(), user_media: Joi.string(), user_type: Joi.any(), user: Joi.object(), }), error: errorSchema.single, } const validators = { params: Joi.object({ profile_id: Joi.number() }), } 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 { profile_id } = request.params const { profileService } = request.server.services() const res = { ok: true, handler: pluginConfig.handlerType, data: null, } res.data = await profileService.getProfile(profile_id) try { return h.response(res).code(200) } catch (err) { return h .response({ ok: false, handler: pluginConfig.handlerType, data: { error: `${err}` }, }) .code(409) } }, /** Validate based on validators object */ validate: { ...validators, failAction: 'log', }, /** Validate the server response */ response: { status: { 200: apiSchema.single.append({ data: responseSchemas.profile, }), 409: apiSchema.single.append({ data: responseSchemas.error, }), }, }, }, }