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.

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