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.

membership.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. const Schmervice = require('@hapipal/schmervice')
  2. module.exports = class MembershipService extends Schmervice.Service {
  3. constructor(...args) {
  4. super(...args)
  5. }
  6. /**
  7. * Internal method to get list of grouping_ids for this user
  8. * @param {number} profileId
  9. * @returns {Array} List of all grouping_ids for user
  10. */
  11. async _getGroupingIdsForProfileId(profileId) {
  12. const { Membership } = this.server.models()
  13. /** Grab every Membership associated with this id */
  14. const allMemberships = await Membership.query().where({
  15. profile_id: profileId,
  16. })
  17. /** Copy a list of the just the Grouping ids */
  18. const groupingIdsToGrab = allMemberships.map(
  19. membership => membership.grouping_id,
  20. )
  21. /** Uncomment to dedupe the list just in case */
  22. return [...new Set(groupingIdsToGrab)]
  23. }
  24. /**
  25. * Internal method to create a new grouping
  26. * @param {object} groupingToTry from payload data
  27. * @param {*} txn
  28. * @returns {Grouping} created db record
  29. */
  30. async _createGrouping(groupingToTry, txn) {
  31. const { Grouping } = this.server.models()
  32. const groupingInfo = {
  33. grouping_name: groupingToTry.grouping_name,
  34. grouping_type: groupingToTry.grouping_type,
  35. }
  36. return await Grouping.query(txn).insert(groupingInfo)
  37. }
  38. /**
  39. * Tries to grab a grouping_id from payload data
  40. * or returns grouping_id from a newly created record
  41. * @param {object} groupingToTry from payload data
  42. * @returns {number} grouping_id from payload or created record
  43. */
  44. async findOrCreateGroupingFromPayload(groupingToTry) {
  45. let idToReturn = groupingToTry.grouping_id
  46. if (!idToReturn) {
  47. /** ?: For some reason this returns the key id */
  48. const grouping = await this._createGrouping(groupingToTry)
  49. idToReturn = grouping.id
  50. }
  51. return idToReturn
  52. }
  53. /**
  54. * Get a list of groupings for user
  55. * @param {number} profileId
  56. * @returns {Array}
  57. */
  58. async findGroupingsByProfileId(profileId, type) {
  59. const { Grouping } = this.server.models()
  60. const dedupedGroupings = await this._getGroupingIdsForProfileId(
  61. profileId,
  62. type,
  63. )
  64. console.log('dedupedGroupings :>> ', dedupedGroupings)
  65. /** Grab just the Groupings this id has a Membership for */
  66. return await Grouping.query()
  67. .whereIn('grouping_id', dedupedGroupings)
  68. .withGraphFetched('profiles')
  69. }
  70. async _groupingIdsInCommon(profileId, targetId) {
  71. const uids = await this._getGroupingIdsForProfileId(profileId)
  72. const tids = await this._getGroupingIdsForProfileId(targetId)
  73. const common = []
  74. for (let i in uids) {
  75. if (tids.indexOf(uids[i]) !== -1) common.push(uids[i])
  76. }
  77. return common.sort((x, y) => x - y)
  78. }
  79. async _patchMembership(memberships, profileId, patch) {
  80. const { Membership } = this.server.models()
  81. /** Set membership as active only if the user initiates it */
  82. for (let membershipInfo of memberships) {
  83. await Membership.query()
  84. .where('membership_id', membershipInfo.membership_id)
  85. .where('profile_id', profileId)
  86. .patch(patch)
  87. }
  88. }
  89. /**
  90. * Check for grouping membership then add membership record and set to active
  91. * or create a new grouping and add membership record for user and membership record for target
  92. * @param {number} profileId
  93. * @param {number} targetId
  94. * @param {object} groupingToWrite
  95. * @param {string} role
  96. * @returns
  97. */
  98. async joinGrouping(profileId, targetId, groupingToWrite, role, txn) {
  99. const { Membership } = this.server.models()
  100. /** If both people have groups in common */
  101. const matchingGroupingIds = await this._groupingIdsInCommon(
  102. profileId,
  103. targetId,
  104. )
  105. if (matchingGroupingIds.length) {
  106. /** Grab all memberships associated with groupingIds */
  107. const memberships = await Membership.query().whereIn(
  108. 'grouping_id',
  109. matchingGroupingIds,
  110. )
  111. /** Set membership as active only if the user initiates it */
  112. await this._patchMembership(memberships, profileId, {
  113. is_active: true,
  114. })
  115. /** Make a new query to get updated information */
  116. return await Membership.query().whereIn(
  117. 'grouping_id',
  118. matchingGroupingIds,
  119. )
  120. } else {
  121. /**
  122. * If both have NO grouping in common, create a membership
  123. * for both and add to a new grouping but
  124. * set membership as inactive for target
  125. * */
  126. /** Check if the grouping exists and if NOT create it */
  127. const groupingId = await this.findOrCreateGroupingFromPayload(
  128. groupingToWrite,
  129. )
  130. const membershipDetailsInCommon = {
  131. grouping_id: groupingId,
  132. membership_type: role,
  133. can_edit: false,
  134. }
  135. const userMembership = await Membership.query(txn).insert({
  136. profile_id: profileId,
  137. ...membershipDetailsInCommon,
  138. is_active: true,
  139. })
  140. const targetMembership = await Membership.query(txn).insert({
  141. profile_id: targetId,
  142. ...membershipDetailsInCommon,
  143. is_active: false,
  144. })
  145. return [userMembership, targetMembership]
  146. }
  147. }
  148. /**
  149. * Remove membership record based on grouping_id
  150. * @param {number} profileId
  151. * @param {number} groupingId
  152. * @returns
  153. */
  154. async leaveGrouping(profileId, groupingId) {
  155. const { Membership } = this.server.models()
  156. const dedupedGroupings = await this._getGroupingIdsForProfileId(
  157. profileId,
  158. )
  159. /** Do NOTHING if NOT in Grouping */
  160. if (!dedupedGroupings.includes(groupingId)) return
  161. return await Membership.query()
  162. .delete()
  163. .where('grouping_id', groupingId)
  164. }
  165. }