您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

queue.js 3.1KB

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