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.2KB

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