Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

questions.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. response_key_category: Joi.string().required(),
  25. response_key_prompt: Joi.string().required(),
  26. response_key_description: Joi.any(),
  27. }),
  28. }
  29. module.exports = {
  30. method: 'GET',
  31. path: '/questions',
  32. options: {
  33. ...pluginConfig.docs,
  34. tags: ['api'],
  35. /** Protect this route with authentication? */
  36. auth: false,
  37. cors: true,
  38. handler: async function (request, h) {
  39. const { responseService } = request.services()
  40. const responseKeys = await responseService.getResponseKeys()
  41. try {
  42. return {
  43. ok: true,
  44. handler: pluginConfig.handlerType,
  45. data: responseKeys,
  46. }
  47. } catch (err) {
  48. return {
  49. ok: false,
  50. handler: pluginConfig.handlerType,
  51. data: { error: err },
  52. }
  53. }
  54. },
  55. /** Validate based on validators object */
  56. validate: { ...validators, failAction: 'log' },
  57. /** Validate the server response */
  58. response: {
  59. schema: Joi.object({
  60. ok: Joi.bool(),
  61. handler: Joi.string(),
  62. data: Joi.array().items(responseSchemas.responseKeysList),
  63. }),
  64. failAction: 'log',
  65. },
  66. },
  67. }