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

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