Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

score.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  14. const validators = {
  15. /** Validate the header (cookie check) */
  16. // headers: true,
  17. /** Validate the route params (/active/{thing}) */
  18. params: params.profileId,
  19. /** Validate the route query (/active/{thing}?limit=10&offset=10) */
  20. query: Joi.object({
  21. max_distance: Joi.number(),
  22. unit: Joi.string(),
  23. }),
  24. /** Validate the incoming payload (POST method) */
  25. // payload: true,
  26. }
  27. const responseSchemas = {
  28. response: Joi.array().items(Joi.object()),
  29. error: errorSchema.single,
  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: apiSchema.single
  87. .append({
  88. data: responseSchemas.response,
  89. })
  90. .label('profile_list_res'),
  91. 409: apiSchema.single
  92. .append({
  93. data: responseSchemas.error,
  94. })
  95. .label('error_single_res'),
  96. },
  97. },
  98. },
  99. }