'use strict' const Joi = require('joi') const errorSchema = require('../../schemas/errors') const profileSchema = require('../../schemas/profiles') const params = require('../../schemas/params') 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: params.userId, /** Validate the route query (/active/{thing}?limit=10&offset=10) */ // query: true, /** Validate the incoming payload (POST method) */ // payload: true, } const responseSchemas = { profilesList: profileSchema.list, error: errorSchema.single, } module.exports = { method: 'GET', path: '/{user_id}/profiles', options: { ...pluginConfig.docs, tags: ['api'], auth: 'default_jwt', 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: responseSchemas.profilesList, }).label('list_profiles_res'), 500: Joi.object({ ok: Joi.bool(), handler: Joi.string(), data: responseSchemas.error, }).label('error_single_res'), }, }, }, }