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

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