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.

match.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. }
  12. const validators = {}
  13. const responseSchemas = {
  14. response: surveyResponseSchema.list,
  15. error: errorSchema.single,
  16. }
  17. module.exports = {
  18. method: 'GET',
  19. path: '/match',
  20. options: {
  21. ...pluginConfig.docs,
  22. tags: ['api'],
  23. /** Protect this route with authentication? */
  24. auth: false,
  25. handler: async function (request, h) {
  26. const { matchService, matchQueueService } =
  27. request.server.services()
  28. const allQueues = await matchQueueService.getAllQueues()
  29. const matched = await matchService.calcMatches(allQueues)
  30. try {
  31. if (!allQueues) {
  32. throw new RangeError('Unable to match profiles')
  33. }
  34. return h
  35. .response({
  36. ok: true,
  37. handler: pluginConfig.handlerType,
  38. data: matched,
  39. })
  40. .code(200)
  41. } catch (err) {
  42. return h
  43. .response({
  44. ok: false,
  45. handler: pluginConfig.handlerType,
  46. data: { error: `${err}` },
  47. })
  48. .code(409)
  49. }
  50. },
  51. /** Validate based on validators object */
  52. validate: {
  53. ...validators,
  54. failAction: 'log',
  55. },
  56. /** Validate the server response */
  57. response: {
  58. status: {
  59. 200: apiSchema.single
  60. .append({
  61. data: responseSchemas.response,
  62. })
  63. .label('response_list_res'),
  64. 409: apiSchema.single
  65. .append({
  66. data: responseSchemas.error,
  67. })
  68. .label('error_single_res'),
  69. },
  70. },
  71. },
  72. }