'use strict' const Joi = require('joi') const apiSchema = require('../../schemas/api') const errorSchema = require('../../schemas/errors') const surveyResponseSchema = require('../../schemas/responses') const params = require('../../schemas/params') const pluginConfig = { handlerType: 'profile', docs: { description: 'Insert responses', notes: 'Insert new responses', }, } const responseSchemas = { response: surveyResponseSchema.single, error: errorSchema.single, } module.exports = { method: 'POST', path: '/{profile_id}/insert/{response_key_id?}', options: { ...pluginConfig.docs, tags: ['api'], /** Protect this route with authentication? */ auth: false, cors: true, handler: async function (request, h) { const { profileService } = request.services() /** Grab payload info */ const res = request.payload try { // TODO: Currently passwords are stored in plain text, big no no... const insertedResponse = await profileService.insertSingleResponseForProfile(res) if (!insertedResponse) { throw new Error('Response not inserted') } return h .response({ ok: true, handler: pluginConfig.handlerType, data: insertedResponse, }) .code(200) } catch (err) { return h .response({ ok: false, handler: pluginConfig.handlerType, data: { error: `${err}` }, }) .code(409) } }, /** Validate based on validators object */ validate: { failAction: 'log', }, /** Validate the server response */ response: { status: { 200: apiSchema.single .append({ data: responseSchemas.response, }) .label('response_list_res'), 409: apiSchema.single .append({ data: responseSchemas.error, }) .label('error_single_res'), }, }, }, }