'use strict' const Joi = require('joi') const pluginConfig = { handlerType: 'profile', docs: { description: 'Update profile', notes: 'Update profile responses', }, } const responseSchemas = { responses: Joi.array().items( Joi.object({ response_id: Joi.number().required(), profile_id: Joi.number().required(), response_key_id: Joi.number().required(), val: Joi.string().required(), }), ), } const validators = { /** Validate the header (cookie check) */ // headers: true, /** Validate the route params (/active/{thing}) */ params: Joi.object({ profile_id: Joi.number() }), /** Validate the route query (/active/{thing}?limit=10&offset=10) */ // query: true, /** Validate the incoming payload (POST method) */ payload: responseSchemas.responses, } module.exports = { method: 'PATCH', path: '/{profile_id}/update', options: { ...pluginConfig.docs, tags: ['api'], /** Protect this route with authentication? */ auth: false, handler: async function (request) { const { profileService } = request.services() const profileId = request.params.profile_id /** Grab payload info */ const res = request.payload const updatedResponses = await profileService.updateResponsesInProfile(profileId, res) try { return { ok: true, handler: pluginConfig.handlerType, data: updatedResponses, } } 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.responses, }), }, }, }