'use strict' const Joi = require('joi') const errorSchema = require('../../schemas/errors') const params = require('../../schemas/params') const pluginConfig = { handlerType: 'user', docs: { description: 'Create profile for user', notes: 'Create a profile 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: Joi.array().items( Joi.object({ response_key_id: Joi.number().required(), val: Joi.string().required(), }), ), } const responseSchemas = { response: Joi.object({ profile_id: Joi.number(), user_id: Joi.number(), user_name: Joi.string(), }).label('created_profile'), error: errorSchema.single, } module.exports = { method: 'POST', path: '/{user_id}/profile', 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 { if (type === 'seeker' && profiles.length > 0) { throw new RangeError( 'Job seekers may only have ONE profile', ) } /** Grab payload info */ const res = request.payload const profile = await profileService.saveResponsesCreateProfileFor( userId, res, ) return h .response({ ok: true, handler: pluginConfig.handlerType, data: profile, }) .code(201) } 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: { 201: Joi.object({ ok: Joi.bool(), handler: Joi.string(), data: responseSchemas.response, }).label('created_profile_res'), 409: Joi.object({ ok: Joi.bool(), handler: Joi.string(), data: responseSchemas.error, }).label('error_single_res'), }, }, }, }