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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'survey',
  5. docs: {
  6. description: 'Get survey questions',
  7. notes: 'Returns a list of all possible survey questions in the form of response_keys',
  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: true,
  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. profile_id: Joi.number().required(),
  25. val: Joi.string().required(),
  26. }),
  27. }
  28. module.exports = {
  29. method: 'GET',
  30. path: '/questions',
  31. options: {
  32. ...pluginConfig.docs,
  33. tags: ['api'],
  34. /** Protect this route with authentication? */
  35. auth: false,
  36. cors: true,
  37. handler: async function (request, h) {
  38. const { responseService } = request.services()
  39. const responseKeys = await responseService.getResponseKeys()
  40. try {
  41. return {
  42. ok: true,
  43. handler: pluginConfig.handlerType,
  44. data: responseKeys,
  45. }
  46. } catch (err) {
  47. return {
  48. ok: false,
  49. handler: pluginConfig.handlerType,
  50. data: { error: err },
  51. }
  52. }
  53. },
  54. /** Validate based on validators object */
  55. validate: { ...validators, failAction: 'log' },
  56. /** Validate the server response */
  57. response: {
  58. schema: Joi.object({
  59. ok: Joi.bool(),
  60. handler: Joi.string(),
  61. data: Joi.array().items(responseSchemas.responseKeysList),
  62. }),
  63. failAction: 'log',
  64. },
  65. },
  66. }