Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

update.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'profile',
  5. docs: {
  6. description: 'Update profile',
  7. notes: 'Update profile responses',
  8. },
  9. }
  10. const responseSchemas = {
  11. responses: Joi.array().items(
  12. Joi.object({
  13. response_id: Joi.number().required(),
  14. profile_id: Joi.number().required(),
  15. response_key_id: Joi.number().required(),
  16. val: Joi.string().required(),
  17. }),
  18. ),
  19. error: Joi.object({
  20. error: Joi.string(),
  21. }),
  22. }
  23. const validators = {
  24. /** Validate the header (cookie check) */
  25. // headers: true,
  26. /** Validate the route params (/active/{thing}) */
  27. params: Joi.object({ profile_id: Joi.number() }),
  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. tags: ['api'],
  39. /** Protect this route with authentication? */
  40. auth: false,
  41. handler: async function (request, h) {
  42. const { profileService } = request.services()
  43. const profileId = request.params.profile_id
  44. /** Grab payload info */
  45. const res = request.payload
  46. try {
  47. const updatedResponses =
  48. await profileService.updateResponsesInProfile(
  49. profileId,
  50. res,
  51. )
  52. if (!updatedResponses) {
  53. throw new RangeError('Response not updated')
  54. }
  55. return h
  56. .response({
  57. ok: true,
  58. handler: pluginConfig.handlerType,
  59. data: updatedResponses,
  60. })
  61. .code(200)
  62. } catch (err) {
  63. return h
  64. .response({
  65. ok: false,
  66. handler: pluginConfig.handlerType,
  67. data: { error: `${err}` },
  68. })
  69. .code(409)
  70. }
  71. },
  72. /** Validate based on validators object */
  73. validate: {
  74. ...validators,
  75. failAction: 'log',
  76. },
  77. /** Validate the server response */
  78. response: {
  79. status: {
  80. 200: Joi.object({
  81. ok: Joi.bool(),
  82. handler: Joi.string(),
  83. data: responseSchemas.responses,
  84. }),
  85. 409: Joi.object({
  86. ok: Joi.bool(),
  87. handler: Joi.string(),
  88. data: responseSchemas.error,
  89. }),
  90. },
  91. },
  92. },
  93. }