You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

respond.js 3.1KB

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