Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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