Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.append({
  76. data: responseSchemas.response,
  77. }).label('response_list_res'),
  78. 409: apiSchema.single.append({
  79. data: responseSchemas.error,
  80. }).label('error_single_res')
  81. },
  82. },
  83. },
  84. }