| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- '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(),
- }),
- ),
- error: Joi.object({
- error: Joi.string(),
- }),
- }
-
- 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/{response_id?}',
- options: {
- ...pluginConfig.docs,
- tags: ['api'],
- /** Protect this route with authentication? */
- auth: false,
-
- handler: async function (request, h) {
- const { profileService } = request.services()
- const profileId = request.params.profile_id
-
- /** Grab payload info */
- const res = request.payload
- try {
- const updatedResponses =
- await profileService.updateResponsesInProfile(
- profileId,
- res,
- )
-
- if (!updatedResponses) {
- throw new RangeError('Response not updated')
- }
- return h
- .response({
- ok: true,
- handler: pluginConfig.handlerType,
- data: updatedResponses,
- })
- .code(200)
- } 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: {
- 200: Joi.object({
- ok: Joi.bool(),
- handler: Joi.string(),
- data: responseSchemas.responses,
- }),
- 409: Joi.object({
- ok: Joi.bool(),
- handler: Joi.string(),
- data: responseSchemas.error,
- }),
- },
- },
- },
- }
|