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 2.6KB

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