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

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