Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

queue.js 3.2KB

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