Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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