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.

questions.js 2.0KB

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. handler: async function (request, h) {
  37. const { responseService } = request.services()
  38. const responseKeys = await responseService.getResponseKeys()
  39. try {
  40. return {
  41. ok: true,
  42. handler: pluginConfig.handlerType,
  43. data: responseKeys,
  44. }
  45. } catch (err) {
  46. return {
  47. ok: false,
  48. handler: pluginConfig.handlerType,
  49. data: { error: err },
  50. }
  51. }
  52. },
  53. /** Validate based on validators object */
  54. validate: { ...validators, failAction: 'log' },
  55. /** Validate the server response */
  56. response: {
  57. schema: Joi.object({
  58. ok: Joi.bool(),
  59. handler: Joi.string(),
  60. data: Joi.array().items(responseSchemas.responseKeysList),
  61. }),
  62. failAction: 'log',
  63. },
  64. },
  65. }