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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. opts: {
  13. tags: ['api'],
  14. auth:
  15. process.env.USE_AUTH != 'true'
  16. ? false
  17. : { strategy: 'default_jwt' },
  18. cors: true,
  19. },
  20. }
  21. /** Validator functions by request method */
  22. const validators = {
  23. /** Validate the header (cookie check) */
  24. // headers: true,
  25. /** Validate the route params (/active/{thing}) */
  26. params: params.userId,
  27. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  28. // query: true,
  29. /** Validate the incoming payload (POST method) */
  30. // payload: true,
  31. }
  32. const responseSchemas = {
  33. response: surveyResponseSchema.keys,
  34. }
  35. module.exports = {
  36. method: 'GET',
  37. path: '/questions',
  38. options: {
  39. ...pluginConfig.docs,
  40. ...pluginConfig.opts,
  41. handler: async function (request, h) {
  42. const { responseService } = request.services()
  43. const responseKeys = await responseService.getResponseKeys()
  44. try {
  45. return {
  46. ok: true,
  47. handler: pluginConfig.handlerType,
  48. data: responseKeys,
  49. }
  50. } catch (err) {
  51. return {
  52. ok: false,
  53. handler: pluginConfig.handlerType,
  54. data: { error: err },
  55. }
  56. }
  57. },
  58. /** Validate based on validators object */
  59. validate: { ...validators, failAction: 'log' },
  60. /** Validate the server response */
  61. response: {
  62. schema: apiSchema.single
  63. .append({
  64. data: responseSchemas.response,
  65. })
  66. .label('response_list_res'),
  67. failAction: 'log',
  68. },
  69. },
  70. }