'use strict' const Joi = require('joi') const pluginConfig = { handlerType: 'profile', docs: { description: 'Create profile', notes: 'Create a profile 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: 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(), }), } module.exports = { method: 'POST', path: '/{user_id}/create', options: { ...pluginConfig.docs, tags: ['api'], /** Protect this route with authentication? */ auth: false, handler: async function (request) { const { profileService } = request.services() const userId = request.params.user_id /** Grab payload info */ const res = request.payload const profile = await profileService.saveResponsesCreateProfileFor( userId, res, ) try { return { ok: true, handler: pluginConfig.handlerType, data: profile, } } 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: responseSchemas.response, }), }, }, }