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.

update.js 2.9KB

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