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.

score.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'score',
  5. docs: {
  6. description: 'scores',
  7. notes: 'A list of profile scores',
  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({
  15. profile_id: Joi.number(),
  16. }),
  17. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  18. query: Joi.object({
  19. max_distance: Joi.number(),
  20. unit: Joi.string(),
  21. }),
  22. /** Validate the incoming payload (POST method) */
  23. // payload: true,
  24. }
  25. const responseSchemas = {
  26. response: Joi.array().items(),
  27. error: Joi.object({
  28. error: Joi.string(),
  29. }),
  30. }
  31. module.exports = {
  32. method: 'GET',
  33. path: '/{profile_id}/score',
  34. options: {
  35. ...pluginConfig.docs,
  36. tags: ['api'],
  37. /** Protect this route with authentication? */
  38. auth: false,
  39. cors: true,
  40. handler: async function (request, h) {
  41. const { profileService, matchQueueService } =
  42. request.server.services()
  43. const profileId = request.params.profile_id
  44. const maxDistanceMiles = request.query.max_distance
  45. const distanceUnit = request.query.unit
  46. ? request.query.unit
  47. : 'mile'
  48. const scoredProfiles = await profileService.scoreProfilesFor(
  49. profileId,
  50. maxDistanceMiles,
  51. distanceUnit,
  52. )
  53. try {
  54. if (!scoredProfiles) {
  55. throw new RangeError('Unable to score profiles')
  56. }
  57. await matchQueueService.saveMatchQueue(
  58. profileId,
  59. scoredProfiles.map(profile => profile.profile_id),
  60. )
  61. return h
  62. .response({
  63. ok: true,
  64. handler: pluginConfig.handlerType,
  65. data: scoredProfiles,
  66. })
  67. .code(200)
  68. } catch (err) {
  69. return h
  70. .response({
  71. ok: false,
  72. handler: pluginConfig.handlerType,
  73. data: { error: `${err}` },
  74. })
  75. .code(409)
  76. }
  77. },
  78. /** Validate based on validators object */
  79. validate: {
  80. ...validators,
  81. failAction: 'log',
  82. },
  83. /** Validate the server response */
  84. response: {
  85. status: {
  86. 200: Joi.object({
  87. ok: Joi.bool(),
  88. handler: Joi.string(),
  89. data: responseSchemas.response,
  90. }),
  91. 409: Joi.object({
  92. ok: Joi.bool(),
  93. handler: Joi.string(),
  94. data: responseSchemas.error,
  95. }),
  96. },
  97. },
  98. },
  99. }