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.

matchqueue.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'matchque',
  5. docs: {
  6. description: 'inserts scored matches',
  7. notes: 'Waits for Scoring Service, and inserts any that does not exist in MatchQue Table',
  8. },
  9. }
  10. const validators = {
  11. // /** Validate the header (cookie check) */
  12. // // headers: true,
  13. // /** Validate the route params (/active/{thing}) */
  14. params: Joi.object({ profile_id: Joi.number() }),
  15. // /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  16. // // query: true,
  17. // /** Validate the incoming payload (POST method) */
  18. payload: Joi.object({
  19. maxDistance: Joi.number().required(),
  20. distanceUnit: Joi.string().required(),
  21. }),
  22. }
  23. module.exports = {
  24. method: 'POST',
  25. path: '/{profile_id}/matches',
  26. options: {
  27. ...pluginConfig.docs,
  28. tags: ['api'],
  29. /** Protect this route with authentication? */
  30. auth: false,
  31. handler: async function (request, h) {
  32. const { profile_id } = request.params
  33. const { maxDistance, distanceUnit } = request.payload
  34. const { matchQueService, profileService } =
  35. request.server.services()
  36. // get results back from profileServices.scoreProfilesFor(profile_id, maxDistance, distanceUnit)
  37. const potentials = await profileService.scoreProfilesFor(
  38. profile_id,
  39. maxDistance,
  40. distanceUnit,
  41. )
  42. let potentialProfileIds = potentials.map(
  43. potential => potential.profile_id,
  44. )
  45. potentialProfileIds = [...new Set(potentialProfileIds)]
  46. await matchQueService.insertScoredProfilesIntoMatchQue(
  47. profile_id,
  48. potentialProfileIds,
  49. )
  50. return matchQueService.getPotentials(profile_id)
  51. },
  52. /** Validate based on validators object */
  53. validate: {
  54. ...validators,
  55. failAction: 'log',
  56. },
  57. // couldn't get validate server response working...
  58. /** Validate the server response */
  59. // response: {
  60. // schema: Joi.object({
  61. // ok: Joi.bool(),
  62. // handler: Joi.string(),
  63. // data: Joi.object(),
  64. // }),
  65. // },
  66. },
  67. }