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 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:
  16. process.env.USE_AUTH != 'true'
  17. ? false
  18. : { strategy: 'default_jwt' },
  19. cors: true,
  20. },
  21. }
  22. const responseSchemas = {
  23. response: Joi.array().items(
  24. Joi.alternatives().try(Joi.number(), profileSchema.single),
  25. ),
  26. error: errorSchema.single,
  27. }
  28. const validators = {
  29. params: params.profileId,
  30. query: params.profileInclude,
  31. }
  32. module.exports = {
  33. method: 'GET',
  34. path: '/{profile_id}/queue',
  35. options: {
  36. ...pluginConfig.docs,
  37. ...pluginConfig.opts,
  38. handler: async function (request, h) {
  39. const { profile_id } = request.params
  40. const { include_profile } = request.query
  41. const { profileService, matchQueueService } =
  42. request.server.services()
  43. const queue = await matchQueueService.getQueue(profile_id)
  44. const queueIds = queue.map(entry => entry.target_id)
  45. // console.log('queueIds', queueIds)
  46. const res = {
  47. ok: true,
  48. handler: pluginConfig.handlerType,
  49. data: queueIds,
  50. }
  51. // HELP: I think there's an issue here
  52. // queueIds spits out the queue profiles in the correct order
  53. // ~However~ when it goes through getProfilesFor
  54. // it comes back in literal database order regardless of is_deleted status
  55. // console.log(
  56. // 'include_profile results',
  57. // await profileService.getProfilesFor(queueIds),
  58. // )
  59. if (include_profile) {
  60. res.data = await profileService.getProfilesFor(
  61. queueIds,
  62. 'participant',
  63. false,
  64. )
  65. }
  66. try {
  67. return h.response(res).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. }