You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict'
  2. const Joi = require('joi')
  3. const apiSchema = require('../../schemas/api')
  4. const errorSchema = require('../../schemas/errors')
  5. const surveyResponseSchema = require('../../schemas/responses')
  6. const params = require('../../schemas/params')
  7. const pluginConfig = {
  8. handlerType: 'profile',
  9. docs: {
  10. description: 'Update profile',
  11. notes: 'Update profile responses',
  12. },
  13. }
  14. const responseSchemas = {
  15. response: surveyResponseSchema.list,
  16. error: errorSchema.single,
  17. }
  18. const validators = {
  19. /** Validate the header (cookie check) */
  20. // headers: true,
  21. /** Validate the route params (/active/{thing}) */
  22. params: params.profileId,
  23. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  24. // query: true,
  25. /** Validate the incoming payload (POST method) */
  26. payload: responseSchemas.responses,
  27. }
  28. module.exports = {
  29. method: 'PATCH',
  30. path: '/{profile_id}/update/{response_id?}',
  31. options: {
  32. ...pluginConfig.docs,
  33. tags: ['api'],
  34. /** Protect this route with authentication? */
  35. auth: false,
  36. handler: async function (request, h) {
  37. const { profileService } = request.services()
  38. const profileId = request.params.profile_id
  39. /** Grab payload info */
  40. const res = request.payload
  41. try {
  42. const updatedResponses =
  43. await profileService.updateResponsesInProfile(
  44. profileId,
  45. res,
  46. )
  47. if (!updatedResponses) {
  48. throw new RangeError('Response not updated')
  49. }
  50. return h
  51. .response({
  52. ok: true,
  53. handler: pluginConfig.handlerType,
  54. data: updatedResponses,
  55. })
  56. .code(200)
  57. } catch (err) {
  58. return h
  59. .response({
  60. ok: false,
  61. handler: pluginConfig.handlerType,
  62. data: { error: `${err}` },
  63. })
  64. .code(409)
  65. }
  66. },
  67. /** Validate based on validators object */
  68. validate: {
  69. ...validators,
  70. failAction: 'log',
  71. },
  72. /** Validate the server response */
  73. response: {
  74. status: {
  75. 200: apiSchema.single
  76. .append({
  77. data: responseSchemas.response,
  78. })
  79. .label('response_list_res'),
  80. 409: apiSchema.single
  81. .append({
  82. data: responseSchemas.error,
  83. })
  84. .label('error_single_res'),
  85. },
  86. },
  87. },
  88. }