Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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. handler: async function (request, h) {
  40. const { profileService, matchQueueService } =
  41. request.server.services()
  42. const profileId = request.params.profile_id
  43. const maxDistanceMiles = request.query.max_distance
  44. const distanceUnit = request.query.unit
  45. ? request.query.unit
  46. : 'mile'
  47. const scoredProfiles = await profileService.scoreProfilesFor(
  48. profileId,
  49. maxDistanceMiles,
  50. distanceUnit,
  51. )
  52. try {
  53. if (!scoredProfiles) {
  54. throw new RangeError('Unable to score profiles')
  55. }
  56. await matchQueueService.saveMatchQueue(
  57. profileId,
  58. scoredProfiles.map(profile => profile.profile_id),
  59. )
  60. return h
  61. .response({
  62. ok: true,
  63. handler: pluginConfig.handlerType,
  64. data: scoredProfiles,
  65. })
  66. .code(200)
  67. } catch (err) {
  68. return h
  69. .response({
  70. ok: false,
  71. handler: pluginConfig.handlerType,
  72. data: { error: `${err}` },
  73. })
  74. .code(409)
  75. }
  76. },
  77. /** Validate based on validators object */
  78. validate: {
  79. ...validators,
  80. failAction: 'log',
  81. },
  82. /** Validate the server response */
  83. response: {
  84. status: {
  85. 200: Joi.object({
  86. ok: Joi.bool(),
  87. handler: Joi.string(),
  88. data: responseSchemas.response,
  89. }),
  90. 409: Joi.object({
  91. ok: Joi.bool(),
  92. handler: Joi.string(),
  93. data: responseSchemas.error,
  94. }),
  95. },
  96. },
  97. },
  98. }