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.

grouping.service.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { db } from '../utils/db.js'
  2. import { Grouping, Profile } from '../entities/index.js'
  3. /**
  4. * Get Memberships associated with a single Profile from the database and
  5. * create a class from the data and
  6. * validate the incoming against the schema
  7. * @param {number} profileId
  8. * @returns {array} instantiated Profile objects (see: /entites/profile)
  9. */
  10. const fetchMembershipsByProfileId = async profileId => {
  11. const validGroupingInstances = []
  12. let memberships
  13. try {
  14. memberships = await db.get(`/membership/${profileId}`)
  15. for (let membership of memberships) {
  16. const grouping = new Grouping(membership)
  17. if (grouping.isValid()) {
  18. // Reformat incoming profile data into Profile entity
  19. grouping.profile = new Profile(grouping.profile)
  20. const targetTags = await db.get(
  21. `/profile/${grouping.profile.profile_id}/tags/${grouping.grouping_id}`,
  22. )
  23. const profileTags = await db.get(
  24. `/profile/${profileId}/tags/${grouping.grouping_id}`,
  25. )
  26. grouping.tags = [...targetTags, ...profileTags]
  27. grouping._loading.value = false
  28. validGroupingInstances.push(grouping)
  29. }
  30. }
  31. } catch (error) {
  32. console.error(`[Grouping Service]: ${error}\ngroupings: ${memberships}`)
  33. }
  34. return validGroupingInstances
  35. }
  36. /**
  37. * Create memberships to a grouping between profileId and targetId
  38. * @param {number} profileId
  39. * @param {number} targetId
  40. * @param {string} groupingType
  41. * @returns {object} the created membership
  42. */
  43. const postMembershipByProfileId = async ({
  44. profileId,
  45. targetId,
  46. groupingType = 'match',
  47. }) => {
  48. const utcDateInSeconds = Date.now() / 1000
  49. const membership = {
  50. target_id: targetId,
  51. grouping_type: groupingType,
  52. grouping_name: `${utcDateInSeconds}_${profileId}_${targetId}`,
  53. }
  54. const membershipMatch = await db.post(
  55. `/membership/${profileId}/join`,
  56. membership,
  57. )
  58. return { membershipMatch, groupingName: membership.grouping_name }
  59. }
  60. const revealProfileInfo = async (membershipId, profileId, tagId) => {
  61. const revealed = await db.post(
  62. `/membership/${membershipId}/reveal?profile=${profileId}&tag=${tagId}`,
  63. )
  64. return revealed
  65. }
  66. export {
  67. fetchMembershipsByProfileId,
  68. postMembershipByProfileId,
  69. revealProfileInfo,
  70. }