Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

respond.js 3.2KB

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