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.

responses.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 responses to questions',
  9. notes: 'Returns a list of all survey responses for a user',
  10. },
  11. }
  12. /** Validator functions by request method */
  13. const validators = {
  14. /** Validate the header (cookie check) */
  15. // headers: true,
  16. /** Validate the route params (/active/{thing}) */
  17. params: Joi.object({ user_id: Joi.number() }),
  18. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  19. // query: true,
  20. /** Validate the incoming payload (POST method) */
  21. // payload: true,
  22. }
  23. const responseSchemas = {
  24. response: surveyResponseSchema.keys
  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: apiSchema.single.append({
  56. data: responseSchemas.response
  57. }),
  58. failAction: 'log',
  59. },
  60. },
  61. }