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.

queue.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict'
  2. const Joi = require('joi')
  3. const apiSchema = require('../../schemas/api')
  4. const errorSchema = require('../../schemas/errors')
  5. const profileSchema = require('../../schemas/profiles')
  6. const pluginConfig = {
  7. handlerType: 'profile',
  8. docs: {
  9. description: 'Returns previously scored profiles',
  10. notes: 'returns from the MatchQueue Table',
  11. },
  12. }
  13. const responseSchemas = {
  14. response: Joi.array().items(
  15. Joi.alternatives().try(Joi.number(), profileSchema.single),
  16. ),
  17. error: errorSchema.single,
  18. }
  19. const validators = {
  20. params: Joi.object({
  21. profile_id: Joi.string(),
  22. }),
  23. query: Joi.object({
  24. include_profile: Joi.bool(),
  25. limit: Joi.number(),
  26. offset: Joi.number(),
  27. }),
  28. }
  29. module.exports = {
  30. method: 'GET',
  31. path: '/{profile_id}/queue',
  32. options: {
  33. ...pluginConfig.docs,
  34. tags: ['api'],
  35. /** Protect this route with authentication? */
  36. auth: false,
  37. cors: true,
  38. handler: async function (request, h) {
  39. const { profile_id } = request.params
  40. const { limit, offset } = request.query
  41. const { profileService, matchQueueService } =
  42. request.server.services()
  43. const queue = await matchQueueService.getQueue(
  44. profile_id,
  45. limit,
  46. offset,
  47. )
  48. const queueIds = queue.map(entry => entry.target_id)
  49. // HELP: I think there's an issue here
  50. // queueIds spits out the queue profiles in the correct order
  51. // ~However~ when it goes through getProfilesFor
  52. // it comes back in literal database order regardless of is_deleted status
  53. // console.log(
  54. // 'include_profile results',
  55. // await profileService.getProfilesFor(queueIds),
  56. // )
  57. try {
  58. return h
  59. .response({
  60. ok: true,
  61. handler: pluginConfig.handlerType,
  62. data: await profileService.getProfilesFor(
  63. queueIds,
  64. 'participant',
  65. ),
  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('match_queue_res'),
  91. 409: apiSchema.single
  92. .append({
  93. data: responseSchemas.error,
  94. })
  95. .label('error_single_res'),
  96. },
  97. },
  98. },
  99. }