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

match.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict'
  2. const apiSchema = require('../../schemas/api')
  3. const errorSchema = require('../../schemas/errors')
  4. const surveyResponseSchema = require('../../schemas/responses')
  5. const pluginConfig = {
  6. handlerType: 'match',
  7. docs: {
  8. description: 'matches',
  9. notes: 'Match everyone',
  10. },
  11. opts: {
  12. tags: ['api'],
  13. auth: { strategy: 'default_jwt' },
  14. cors: true,
  15. },
  16. }
  17. const validators = {}
  18. const responseSchemas = {
  19. response: surveyResponseSchema.list,
  20. error: errorSchema.single,
  21. }
  22. module.exports = {
  23. method: 'GET',
  24. path: '/match',
  25. options: {
  26. ...pluginConfig.docs,
  27. ...pluginConfig.opts,
  28. handler: async function (request, h) {
  29. const { matchService, matchQueueService } =
  30. request.server.services()
  31. const allQueues = await matchQueueService.getAllQueues()
  32. const matched = await matchService.calcMatches(allQueues)
  33. try {
  34. if (!allQueues) {
  35. throw new RangeError('Unable to match profiles')
  36. }
  37. return h
  38. .response({
  39. ok: true,
  40. handler: pluginConfig.handlerType,
  41. data: matched,
  42. })
  43. .code(200)
  44. } catch (err) {
  45. return h
  46. .response({
  47. ok: false,
  48. handler: pluginConfig.handlerType,
  49. data: { error: `${err}` },
  50. })
  51. .code(409)
  52. }
  53. },
  54. /** Validate based on validators object */
  55. validate: {
  56. ...validators,
  57. failAction: 'log',
  58. },
  59. /** Validate the server response */
  60. response: {
  61. status: {
  62. 200: apiSchema.single
  63. .append({
  64. data: responseSchemas.response,
  65. })
  66. .label('response_list_res'),
  67. 409: apiSchema.single
  68. .append({
  69. data: responseSchemas.error,
  70. })
  71. .label('error_single_res'),
  72. },
  73. },
  74. },
  75. }