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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 params = require('../../schemas/params')
  7. const pluginConfig = {
  8. handlerType: 'profile',
  9. docs: {
  10. description: 'Returns previously scored profiles',
  11. notes: 'returns from the MatchQueue Table',
  12. },
  13. opts: {
  14. tags: ['api'],
  15. auth: { strategy: 'default_jwt' },
  16. cors: true,
  17. },
  18. }
  19. const responseSchemas = {
  20. response: Joi.array().items(
  21. Joi.alternatives().try(Joi.number(), profileSchema.single),
  22. ),
  23. error: errorSchema.single,
  24. }
  25. const validators = {
  26. params: params.profileId,
  27. query: params.profileInclude,
  28. }
  29. module.exports = {
  30. method: 'GET',
  31. path: '/{profile_id}/queue',
  32. options: {
  33. ...pluginConfig.docs,
  34. ...pluginConfig.opts,
  35. handler: async function (request, h) {
  36. const { profile_id } = request.params
  37. const { include_profile } = request.query
  38. const { profileService, matchQueueService } =
  39. request.server.services()
  40. const queue = await matchQueueService.getQueue(profile_id)
  41. const queueIds = queue.map(entry => entry.target_id)
  42. // console.log('queueIds', queueIds)
  43. const res = {
  44. ok: true,
  45. handler: pluginConfig.handlerType,
  46. data: queueIds,
  47. }
  48. // HELP: I think there's an issue here
  49. // queueIds spits out the queue profiles in the correct order
  50. // ~However~ when it goes through getProfilesFor
  51. // it comes back in literal database order regardless of is_deleted status
  52. // console.log(
  53. // 'include_profile results',
  54. // await profileService.getProfilesFor(queueIds),
  55. // )
  56. if (include_profile) {
  57. res.data = await profileService.getProfilesFor(
  58. queueIds,
  59. 'participant',
  60. false,
  61. )
  62. }
  63. try {
  64. return h.response(res).code(200)
  65. } catch (err) {
  66. return h
  67. .response({
  68. ok: false,
  69. handler: pluginConfig.handlerType,
  70. data: { error: `${err}` },
  71. })
  72. .code(409)
  73. }
  74. },
  75. /** Validate based on validators object */
  76. validate: {
  77. ...validators,
  78. failAction: 'log',
  79. },
  80. /** Validate the server response */
  81. response: {
  82. status: {
  83. 200: apiSchema.single
  84. .append({
  85. data: responseSchemas.response,
  86. })
  87. .label('match_queue_res'),
  88. 409: apiSchema.single
  89. .append({
  90. data: responseSchemas.error,
  91. })
  92. .label('error_single_res'),
  93. },
  94. },
  95. },
  96. }