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.6KB

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