您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

questions.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. }
  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: true,
  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. cors: true,
  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('question_list_res'),
  61. failAction: 'log',
  62. },
  63. },
  64. }