Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

responses.js 2.0KB

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