Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

questions.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict'
  2. const Joi = require('joi')
  3. const apiSchema = require('../../schemas/api')
  4. const surveyResponseSchema = require('../../schemas/responses')
  5. const pluginConfig = {
  6. handlerType: 'survey',
  7. docs: {
  8. description: 'Get survey questions',
  9. notes: 'Returns a list of all possible survey questions in the form of response_keys',
  10. },
  11. opts: {
  12. tags: ['api'],
  13. auth: false,
  14. cors: true,
  15. },
  16. }
  17. /** Validator functions by request method */
  18. const validators = {
  19. /** Validate the header (cookie check) */
  20. // headers: true,
  21. /** Validate the route params (/active/{thing}) */
  22. // params: true,
  23. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  24. // query: true,
  25. /** Validate the incoming payload (POST method) */
  26. // payload: true,
  27. }
  28. const responseSchemas = {
  29. response: surveyResponseSchema.keys,
  30. }
  31. module.exports = {
  32. method: 'GET',
  33. path: '/questions',
  34. options: {
  35. ...pluginConfig.docs,
  36. ...pluginConfig.opts,
  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: apiSchema.single
  59. .append({
  60. data: responseSchemas.response,
  61. })
  62. .label('question_list_res'),
  63. failAction: 'log',
  64. },
  65. },
  66. }