| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- '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: 'Update profile',
- notes: 'Update profile responses',
- },
- }
-
- const responseSchemas = {
- response: surveyResponseSchema.list,
- error: errorSchema.single,
- }
-
- const validators = {
- /** Validate the header (cookie check) */
- // headers: true,
-
- /** Validate the route params (/active/{thing}) */
- params: params.profileId,
-
- /** 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: apiSchema.single
- .append({
- data: responseSchemas.response,
- })
- .label('response_list_res'),
- 409: apiSchema.single
- .append({
- data: responseSchemas.error,
- })
- .label('error_single_res'),
- },
- },
- },
- }
|