Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

score.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 duration = request.query.duration
  49. const presence = request.query.presence
  50. const certifications = request.query.certifications
  51. const scoredProfiles = await profileService.scoreProfilesFor(
  52. profileId,
  53. maxDistanceMiles,
  54. distanceUnit,
  55. duration,
  56. presence,
  57. certifications,
  58. )
  59. try {
  60. if (!scoredProfiles) {
  61. throw new RangeError('Unable to score profiles')
  62. }
  63. await matchQueueService.saveMatchQueue(
  64. profileId,
  65. scoredProfiles.map(profile => profile.profile_id),
  66. )
  67. return h
  68. .response({
  69. ok: true,
  70. handler: pluginConfig.handlerType,
  71. data: scoredProfiles,
  72. })
  73. .code(200)
  74. } catch (err) {
  75. return h
  76. .response({
  77. ok: false,
  78. handler: pluginConfig.handlerType,
  79. data: { error: `${err}` },
  80. })
  81. .code(409)
  82. }
  83. },
  84. /** Validate based on validators object */
  85. validate: {
  86. ...validators,
  87. failAction: 'log',
  88. },
  89. /** Validate the server response */
  90. response: {
  91. status: {
  92. 200: apiSchema.single
  93. .append({
  94. data: responseSchemas.response,
  95. })
  96. .label('profile_list_res'),
  97. 409: apiSchema.single
  98. .append({
  99. data: responseSchemas.error,
  100. })
  101. .label('error_single_res'),
  102. },
  103. },
  104. },
  105. }