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.service.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { db } from '../utils/db.js'
  2. import { Profile } from '../entities/index.js'
  3. /**
  4. * Get a match queue of profiles
  5. * @param {number} profileId
  6. * @param {number} limit
  7. * @param {number} offset
  8. * @returns {array} profiles
  9. */
  10. const fetchQueueByProfileId = async (profileId, offset = 0, limit = 5) => {
  11. let queue
  12. try {
  13. queue = await db.get(
  14. `/profile/${profileId}/queue?include_profile=true&limit=${limit}&offset=${offset}`,
  15. )
  16. if (!queue?.length) {
  17. throw '[Queue Service]: Could not retrieve match queue.\nHave you taken the survey and scored this profile?'
  18. }
  19. } catch (err) {
  20. console.error(err)
  21. }
  22. return queue
  23. ? queue.map(profileData => {
  24. return new Profile({ email: 'fixme@gmail.com', ...profileData })
  25. })
  26. : []
  27. }
  28. /**
  29. * Remove or reinsert a profile in match queue
  30. * @param {number} profileId profile viewing the queue
  31. * @param {number} targetId
  32. * @param {boolean} reinsert FALSE if profileId accepted targetId; TRUE to reinsert into match queue
  33. * @returns {array} profiles
  34. */
  35. const updateQueueByProfileId = async (profileId, targetId, reinsert) => {
  36. let updateQueue
  37. try {
  38. updateQueue = await db.patch(
  39. `/profile/${profileId}/queue/${targetId}/delete?include_profile=true&reinsert=${reinsert}`,
  40. )
  41. if (!updateQueue?.length) {
  42. throw '[Queue Service]: Could not update match queue.'
  43. }
  44. } catch (err) {
  45. console.error(err)
  46. }
  47. return updateQueue
  48. ? updateQueue.map(profileData => {
  49. return new Profile({ email: 'fixme@gmail.com', ...profileData })
  50. })
  51. : []
  52. }
  53. export { fetchQueueByProfileId, updateQueueByProfileId }