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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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) {
  12. const { MatchQueue } = this.server.models()
  13. return await MatchQueue.query()
  14. .where('profile_id', profileId)
  15. .where('is_deleted', 0)
  16. }
  17. /**
  18. * Returns queues by profile id by user type
  19. * @returns {object}
  20. */
  21. async getAllQueues() {
  22. const { MatchQueue } = this.server.models()
  23. const queueEntries = await MatchQueue.query()
  24. .andWhere('is_deleted', false)
  25. .withGraphFetched('user')
  26. const queueByProfileId = {
  27. poster: {},
  28. seeker: {},
  29. }
  30. queueEntries.forEach(entry => {
  31. const type = entry.user.is_poster == 1 ? 'poster' : 'seeker'
  32. if (!queueByProfileId[type][entry.profile_id]) {
  33. queueByProfileId[type][entry.profile_id] = []
  34. }
  35. queueByProfileId[type][entry.profile_id].push(entry.target_id)
  36. })
  37. return queueByProfileId
  38. }
  39. /**
  40. * Saves Scored Profile Ids to MatchQue IN ORDER
  41. * @param {number} profileId
  42. * @param {array} targetIds
  43. */
  44. async saveMatchQueue(profileId, targetIds) {
  45. const { MatchQueue } = this.server.models()
  46. // returns an array of all matches for the profileId where the target_id already exists in the targetIds array
  47. await MatchQueue.query()
  48. .patch({
  49. is_deleted: true,
  50. })
  51. .where('profile_id', profileId)
  52. for (let id of targetIds) {
  53. await MatchQueue.query().insert({
  54. profile_id: profileId,
  55. target_id: id,
  56. is_deleted: false,
  57. })
  58. }
  59. return await this.getQueue(profileId)
  60. }
  61. /**
  62. * Set the rows deleted as true, does NOT DELETE from database
  63. * @param {number} profileId
  64. * @param {number} targetId
  65. * @param {boolean} reinsert
  66. * @returns
  67. */
  68. async markAsDeleted(profileId, targetId, reinsert) {
  69. const { MatchQueue } = this.server.models()
  70. await MatchQueue.query()
  71. .patch({
  72. is_deleted: true,
  73. })
  74. .where('profile_id', profileId)
  75. .andWhere('target_id', targetId)
  76. .first()
  77. if (reinsert) {
  78. await MatchQueue.query().insert({
  79. profile_id: profileId,
  80. target_id: targetId,
  81. is_deleted: false,
  82. })
  83. }
  84. return await this.getQueue(profileId)
  85. }
  86. }