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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const Schmervice = require('@hapipal/schmervice')
  2. module.exports = class MatchQueueService extends Schmervice.Service {
  3. constructor(...args) {
  4. super(...args)
  5. }
  6. async getPotentials(profileId) {
  7. const { MatchQueue } = this.server.models()
  8. const allPotentials = await MatchQueue.query()
  9. .where('profile_id', profileId)
  10. .andWhere('is_deleted', false)
  11. return allPotentials
  12. }
  13. /**
  14. * Saves Scored Profile Ids to MatchQue IN ORDER
  15. * @param {number} profileId
  16. * @param {array} potentialProfileIds
  17. */
  18. async insertScoredProfilesIntoMatchQueue(profileId, potentialProfileIds) {
  19. const { MatchQueue } = this.server.models()
  20. // returns an array of all matches for the profileId where the profile_id_2 already exists in the potentialProfileIds array
  21. await MatchQueue.query()
  22. .patch({
  23. is_deleted: true,
  24. })
  25. .where('profile_id', profileId)
  26. for (let potentialProfileId of potentialProfileIds) {
  27. await MatchQueue.query().insert({
  28. profile_id: profileId,
  29. target_id: potentialProfileId,
  30. is_deleted: false,
  31. })
  32. }
  33. return await this.getPotentials(profileId)
  34. }
  35. /**
  36. * Set the rows deleted as true, does NOT DELETE from database
  37. * @param {number} profileId
  38. * @param {number} targetId
  39. * @param {boolean} reinsert
  40. * @returns
  41. */
  42. async markAsDeleted(profileId, targetId, reinsert) {
  43. const { MatchQueue } = this.server.models()
  44. await MatchQueue.query()
  45. .patch({
  46. deleted: true,
  47. })
  48. .where('profile_id', profileId)
  49. .andWhere('target_id', targetId)
  50. .first()
  51. if (reinsert) {
  52. await MatchQueue.query().insert({
  53. profile_id: profileId,
  54. target_id: targetId,
  55. deleted: false,
  56. })
  57. }
  58. return await this.getPotentials(profileId)
  59. }
  60. }