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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict'
  2. const Joi = require('joi')
  3. const profile = require('../../plugins/profile')
  4. const pluginConfig = {
  5. handlerType: 'profile',
  6. docs: {
  7. description: 'Update profile',
  8. notes: 'Update profile responses',
  9. },
  10. }
  11. const responseSchemas = {
  12. responses: Joi.array().items(
  13. Joi.object({
  14. response_id: Joi.number().required(),
  15. profile_id: Joi.number().required(),
  16. response_key_id: Joi.number().required(),
  17. val: Joi.string().required(),
  18. }),
  19. ),
  20. error: Joi.object({
  21. error: Joi.string(),
  22. }),
  23. }
  24. const validators = {
  25. /** Validate the header (cookie check) */
  26. // headers: true,
  27. /** Validate the route params (/active/{thing}) */
  28. params: Joi.object({
  29. profile_id: Joi.number()
  30. }),
  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. tags: ['api'],
  45. /** Protect this route with authentication? */
  46. auth: false,
  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(profileId, responseToSave)
  56. try {
  57. // profileService.saveResponseForProfile() will return null if it exists
  58. if(!allResponses){
  59. throw new RangeError('Response already exists')
  60. }
  61. return h.response({
  62. ok: true,
  63. handler: pluginConfig.handlerType,
  64. data: allResponses,
  65. }).code(201)
  66. } catch (err) {
  67. return h.response({
  68. ok: false,
  69. handler: pluginConfig.handlerType,
  70. data: { error: `${err}` },
  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: Joi.object({
  83. ok: Joi.bool(),
  84. handler: Joi.string(),
  85. data: responseSchemas.responses,
  86. }),
  87. 409: Joi.object({
  88. ok: Joi.bool(),
  89. handler: Joi.string(),
  90. data: responseSchemas.error,
  91. }),
  92. },
  93. },
  94. },
  95. }