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.

responses.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'survey',
  5. docs: {
  6. description: 'Get responses to questions',
  7. notes: 'Returns a list of all survey responses for a user',
  8. },
  9. }
  10. /** Validator functions by request method */
  11. const validators = {
  12. /** Validate the header (cookie check) */
  13. // headers: true,
  14. /** Validate the route params (/active/{thing}) */
  15. params: Joi.object({ user_id: Joi.number() }),
  16. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  17. // query: true,
  18. /** Validate the incoming payload (POST method) */
  19. // payload: true,
  20. }
  21. const responseSchemas = {
  22. responseKeysList: Joi.object({
  23. response_key_id: Joi.number().required(),
  24. }),
  25. }
  26. module.exports = {
  27. method: 'GET',
  28. path: '/questions',
  29. options: {
  30. ...pluginConfig.docs,
  31. tags: ['api'],
  32. /** Protect this route with authentication? */
  33. auth: false,
  34. handler: async function (request, h) {
  35. const { responseService } = request.services()
  36. const responseKeys = await responseService.getResponseKeys()
  37. try {
  38. return {
  39. ok: true,
  40. handler: pluginConfig.handlerType,
  41. data: responseKeys,
  42. }
  43. } catch (err) {
  44. return {
  45. ok: false,
  46. handler: pluginConfig.handlerType,
  47. data: { error: err },
  48. }
  49. }
  50. },
  51. /** Validate based on validators object */
  52. validate: { ...validators, failAction: 'log' },
  53. /** Validate the server response */
  54. response: {
  55. schema: Joi.object({
  56. ok: Joi.bool(),
  57. handler: Joi.string(),
  58. data: Joi.array().items(responseSchemas.responseKeysList),
  59. }),
  60. failAction: 'log',
  61. },
  62. },
  63. }