Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

matchqueue.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const Schmervice = require('@hapipal/schmervice')
  2. module.exports = class MatchQueueService extends Schmervice.Service {
  3. constructor(...args) {
  4. super(...args)
  5. }
  6. /**
  7. * Gets a list of queue entries for profileId
  8. * @param {number} profileId
  9. * @returns {array} MatchQueue
  10. */
  11. async getQueue(profileId, limit, offset = 0) {
  12. const { MatchQueue } = this.server.models()
  13. if (typeof limit === 'undefined') {
  14. return await MatchQueue.query()
  15. .where('profile_id', profileId)
  16. .where('is_deleted', 0)
  17. }
  18. return await MatchQueue.query()
  19. .where('profile_id', profileId)
  20. .where('is_deleted', 0)
  21. .limit(limit)
  22. .offset(offset)
  23. }
  24. /**
  25. * Returns queues by profile id by user type
  26. * @returns {object}
  27. */
  28. async getAllQueues() {
  29. const { MatchQueue } = this.server.models()
  30. const queueEntries = await MatchQueue.query()
  31. .andWhere('is_deleted', false)
  32. .withGraphFetched('user')
  33. const queueByProfileId = {
  34. poster: {},
  35. seeker: {},
  36. }
  37. queueEntries.forEach(entry => {
  38. const type = entry.user.is_poster == 1 ? 'poster' : 'seeker'
  39. if (!queueByProfileId[type][entry.profile_id]) {
  40. queueByProfileId[type][entry.profile_id] = []
  41. }
  42. queueByProfileId[type][entry.profile_id].push(entry.target_id)
  43. })
  44. return queueByProfileId
  45. }
  46. /**
  47. * Saves Scored Profile Ids to MatchQue IN ORDER
  48. * @param {number} profileId
  49. * @param {array} targetIds
  50. */
  51. async saveMatchQueue(profileId, targetIds) {
  52. const { MatchQueue } = this.server.models()
  53. // returns an array of all matches for the profileId where the target_id already exists in the targetIds array
  54. await MatchQueue.query()
  55. .patch({
  56. is_deleted: true,
  57. })
  58. .where('profile_id', profileId)
  59. for (let id of targetIds) {
  60. await MatchQueue.query().insert({
  61. profile_id: profileId,
  62. target_id: id,
  63. is_deleted: false,
  64. })
  65. }
  66. return await this.getQueue(profileId)
  67. }
  68. /**
  69. * Set the rows deleted as true, does NOT DELETE from database
  70. * @param {number} profileId
  71. * @param {number} targetId
  72. * @param {boolean} reinsert
  73. * @returns
  74. */
  75. async markAsDeleted(profileId, targetId, reinsert) {
  76. const { MatchQueue } = this.server.models()
  77. /** Always set row to deleted */
  78. await MatchQueue.query()
  79. .where('profile_id', profileId)
  80. .andWhere('target_id', targetId)
  81. .patch({
  82. is_deleted: true,
  83. })
  84. .first()
  85. if (reinsert) {
  86. await MatchQueue.query().insert({
  87. profile_id: profileId,
  88. target_id: targetId,
  89. is_deleted: false,
  90. })
  91. }
  92. return await this.getQueue(profileId)
  93. }
  94. }