Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

respond.js 3.1KB

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