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.

responses.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict'
  2. const Joi = require('joi')
  3. const apiSchema = require('../../schemas/api')
  4. const surveyResponseSchema = require('../../schemas/responses')
  5. const params = require('../../schemas/params')
  6. const pluginConfig = {
  7. handlerType: 'survey',
  8. docs: {
  9. description: 'Get responses to questions',
  10. notes: 'Returns a list of all survey responses for a user',
  11. },
  12. }
  13. /** Validator functions by request method */
  14. const validators = {
  15. /** Validate the header (cookie check) */
  16. // headers: true,
  17. /** Validate the route params (/active/{thing}) */
  18. params: params.userId,
  19. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  20. // query: true,
  21. /** Validate the incoming payload (POST method) */
  22. // payload: true,
  23. }
  24. const responseSchemas = {
  25. response: surveyResponseSchema.keys,
  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: apiSchema.single
  57. .append({
  58. data: responseSchemas.response,
  59. })
  60. .label('response_list_res'),
  61. failAction: 'log',
  62. },
  63. },
  64. }