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

respond.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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({
  28. profile_id: Joi.number(),
  29. }),
  30. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  31. query: Joi.object({
  32. response_key_id: Joi.number(),
  33. val: Joi.string(),
  34. }),
  35. /** Validate the incoming payload (POST method) */
  36. // payload: responseSchemas.responses,
  37. }
  38. module.exports = {
  39. method: 'POST',
  40. path: '/{profile_id}/respond',
  41. options: {
  42. ...pluginConfig.docs,
  43. tags: ['api'],
  44. /** Protect this route with authentication? */
  45. auth: false,
  46. cors: true,
  47. handler: async function (request, h) {
  48. const { profileService } = request.services()
  49. const profileId = request.params.profile_id
  50. const responseToSave = {
  51. profile_id: profileId,
  52. response_key_id: request.query.response_key_id,
  53. val: request.query.val,
  54. }
  55. const allResponses = await profileService.saveResponseForProfile(
  56. profileId,
  57. responseToSave,
  58. )
  59. try {
  60. // profileService.saveResponseForProfile() will return null if it exists
  61. if (!allResponses) {
  62. throw new RangeError('Response already exists')
  63. }
  64. return h
  65. .response({
  66. ok: true,
  67. handler: pluginConfig.handlerType,
  68. data: allResponses,
  69. })
  70. .code(201)
  71. } catch (err) {
  72. return h
  73. .response({
  74. ok: false,
  75. handler: pluginConfig.handlerType,
  76. data: { error: `${err}` },
  77. })
  78. .code(409)
  79. }
  80. },
  81. /** Validate based on validators object */
  82. validate: {
  83. ...validators,
  84. failAction: 'log',
  85. },
  86. /** Validate the server response */
  87. response: {
  88. status: {
  89. 201: Joi.object({
  90. ok: Joi.bool(),
  91. handler: Joi.string(),
  92. data: responseSchemas.responses,
  93. }),
  94. 409: Joi.object({
  95. ok: Joi.bool(),
  96. handler: Joi.string(),
  97. data: responseSchemas.error,
  98. }),
  99. },
  100. },
  101. },
  102. }