Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

membership.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. /** Grab just the Groupings this id has a Membership for */
  69. return await Grouping.query()
  70. .whereIn('grouping_id', dedupedGroupings)
  71. .withGraphFetched('profiles')
  72. }
  73. async _groupingIdsInCommon(profileId, targetId) {
  74. const uids = await this._getGroupingIdsForProfileId(profileId)
  75. const tids = await this._getGroupingIdsForProfileId(targetId)
  76. const common = []
  77. for (let i in uids) {
  78. if (tids.indexOf(uids[i]) !== -1) common.push(uids[i])
  79. }
  80. return common.sort((x, y) => x - y)
  81. }
  82. async _patchMembership(memberships, profileId, patch) {
  83. const { Membership } = this.server.models()
  84. /** Set membership as active only if the user initiates it */
  85. for (let membershipInfo of memberships) {
  86. await Membership.query()
  87. .where('membership_id', membershipInfo.membership_id)
  88. .where('profile_id', profileId)
  89. .patch(patch)
  90. }
  91. }
  92. /**
  93. * Check for grouping membership then add membership record and set to active
  94. * or create a new grouping and add membership record for user and membership record for target
  95. * @param {number} profileId
  96. * @param {number} targetId
  97. * @param {object} groupingToWrite
  98. * @param {string} role
  99. * @returns
  100. */
  101. async joinGrouping(profileId, targetId, groupingToWrite, role, txn) {
  102. const { Membership } = this.server.models()
  103. /** If both people have groups in common */
  104. const matchingGroupingIds = await this._groupingIdsInCommon(
  105. profileId,
  106. targetId,
  107. )
  108. if (matchingGroupingIds.length) {
  109. /** Grab all memberships associated with groupingIds */
  110. let memberships = await Membership.query().whereIn(
  111. 'grouping_id',
  112. matchingGroupingIds,
  113. )
  114. /** Set membership as active only if the user initiates it */
  115. await this._patchMembership(memberships, profileId, {
  116. is_active: true,
  117. })
  118. /** Make a new query to get updated information */
  119. memberships = await Membership.query().whereIn(
  120. 'grouping_id',
  121. matchingGroupingIds,
  122. )
  123. const groupings = await this._getGroupings(matchingGroupingIds, txn)
  124. return { memberships, groupings }
  125. } else {
  126. /**
  127. * If both have NO grouping in common, create a membership
  128. * for both and add to a new grouping but
  129. * set membership as inactive for target
  130. * */
  131. /** Check if the grouping exists and if NOT create it */
  132. const groupingId = await this.findOrCreateGroupingFromPayload(
  133. groupingToWrite,
  134. )
  135. const membershipDetailsInCommon = {
  136. grouping_id: groupingId,
  137. membership_type: role,
  138. can_edit: false,
  139. }
  140. const userMembership = await Membership.query(txn).insert({
  141. profile_id: profileId,
  142. ...membershipDetailsInCommon,
  143. is_active: true,
  144. })
  145. const targetMembership = await Membership.query(txn).insert({
  146. profile_id: targetId,
  147. ...membershipDetailsInCommon,
  148. is_active: false,
  149. })
  150. return {
  151. memberships: [userMembership, targetMembership],
  152. groupings: [],
  153. }
  154. }
  155. }
  156. /**
  157. * Remove membership record based on grouping_id
  158. * @param {number} profileId
  159. * @param {number} groupingId
  160. * @returns
  161. */
  162. async leaveGrouping(profileId, groupingId) {
  163. const { Membership } = this.server.models()
  164. const dedupedGroupings = await this._getGroupingIdsForProfileId(
  165. profileId,
  166. )
  167. /** Do NOTHING if NOT in Grouping */
  168. if (!dedupedGroupings.includes(groupingId)) return
  169. return await Membership.query()
  170. .delete()
  171. .where('grouping_id', groupingId)
  172. }
  173. }