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 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { db } from '../utils/db'
  2. import { Grouping, Profile } from '../entities'
  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 membershipsForProfileId = await db.get(`/membership/${profileId}`)
  12. const validGroupingInstances = []
  13. for (let membership of membershipsForProfileId) {
  14. const grouping = new Grouping(membership)
  15. if (grouping.isValid()) {
  16. // Reformat incoming profile data into Profile entity
  17. grouping.profile = new Profile(grouping.profile)
  18. validGroupingInstances.push(grouping)
  19. }
  20. }
  21. return validGroupingInstances
  22. }
  23. /**
  24. * Create memberships to a grouping between profileId and targetId
  25. * @param {number} profileId
  26. * @param {number} targetId
  27. * @param {string} groupingType
  28. * @returns {object} the created membership
  29. */
  30. const postMembershipByProfileId = async ({
  31. profileId,
  32. targetId,
  33. groupingType = 'match',
  34. }) => {
  35. const utcDateInSeconds = Date.now() / 1000
  36. const membership = {
  37. target_id: targetId,
  38. grouping_type: groupingType,
  39. grouping_name: `${utcDateInSeconds}_${profileId}_${targetId}`,
  40. }
  41. const membershipMatch = await db.post(
  42. `/membership/${profileId}/join`,
  43. membership,
  44. )
  45. return { membershipMatch, groupingName: membership.grouping_name }
  46. }
  47. export { fetchMembershipsByProfileId, postMembershipByProfileId }