|
|
@@ -67,31 +67,97 @@ module.exports = class MembershipService extends Schmervice.Service {
|
|
67
|
67
|
.whereIn('grouping_id', dedupedGroupings)
|
|
68
|
68
|
}
|
|
69
|
69
|
|
|
|
70
|
+ async _groupingIdsInCommon(userId, targetId) {
|
|
|
71
|
+ const dedupedUserGroupingIds = await this._getGroupIdsForUserId(userId)
|
|
|
72
|
+ const dedupedTargetGroupingIds = await this._getGroupIdsForUserId(targetId)
|
|
|
73
|
+
|
|
|
74
|
+ /** Return true if both people have a group in common */
|
|
|
75
|
+ return dedupedUserGroupingIds.filter(groupingId => dedupedTargetGroupingIds.includes(groupingId))
|
|
|
76
|
+ }
|
|
|
77
|
+ async _patchMembership(memberships, userId, patch) {
|
|
|
78
|
+ const { Membership } = this.server.models()
|
|
|
79
|
+
|
|
|
80
|
+ /** Set membership as active only if the user initiates it */
|
|
|
81
|
+ for(let membershipInfo of memberships) {
|
|
|
82
|
+ await Membership.query()
|
|
|
83
|
+ .where('membership_id', membershipInfo.membership_id)
|
|
|
84
|
+ .where('user_id', userId)
|
|
|
85
|
+ .patch(patch)
|
|
|
86
|
+ }
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ async attemptMatch(userId, targetId, txn) {
|
|
|
90
|
+ const { Membership } = this.server.models()
|
|
|
91
|
+
|
|
|
92
|
+ /** If both people have groups in common */
|
|
|
93
|
+ const matchingGroupingIds = await this._groupingIdsInCommon(userId, targetId)
|
|
|
94
|
+
|
|
|
95
|
+ if(matchingGroupingIds.length) {
|
|
|
96
|
+
|
|
|
97
|
+ /** Grab all memberships associated with groupingIds */
|
|
|
98
|
+ const memberships = await Membership.query().whereIn('grouping_id', matchingGroupingIds)
|
|
|
99
|
+
|
|
|
100
|
+ /** Set membership as active only if the user initiates it */
|
|
|
101
|
+ await this._patchMembership(memberships, userId, { is_active: true })
|
|
|
102
|
+
|
|
|
103
|
+ /** Make a new query to get updated information */
|
|
|
104
|
+ return await Membership.query().whereIn('grouping_id', matchingGroupingIds)
|
|
|
105
|
+ }
|
|
|
106
|
+ }
|
|
|
107
|
+
|
|
70
|
108
|
/**
|
|
71
|
|
- * Check for grouping membership then add membership record
|
|
|
109
|
+ * Check for grouping membership then add membership record and set to active
|
|
|
110
|
+ * or create a new grouping and add membership record for user and membership record for target
|
|
72
|
111
|
* @param {number} userId
|
|
73
|
|
- * @param {number} groupingId
|
|
|
112
|
+ * @param {number} targetId
|
|
|
113
|
+ * @param {object} groupingToWrite
|
|
74
|
114
|
* @param {string} role
|
|
75
|
115
|
* @returns
|
|
76
|
116
|
*/
|
|
77
|
|
- async joinGrouping(userId, groupingId, role, txn) {
|
|
|
117
|
+ async joinGrouping(userId, targetId, groupingToWrite, role, txn) {
|
|
78
|
118
|
const { Membership } = this.server.models()
|
|
79
|
119
|
|
|
80
|
|
- const dedupedGroupings = await this._getGroupIdsForUserId(userId)
|
|
|
120
|
+ /** If both people have groups in common */
|
|
|
121
|
+ const matchingGroupingIds = await this._groupingIdsInCommon(userId, targetId)
|
|
81
|
122
|
|
|
82
|
|
- /** Do NOTHING if already in Grouping */
|
|
83
|
|
- if(dedupedGroupings.includes(groupingId)) return
|
|
|
123
|
+ if(matchingGroupingIds.length) {
|
|
84
|
124
|
|
|
85
|
|
- console.log(dedupedGroupings)
|
|
86
|
|
- const membershipInfo = {
|
|
87
|
|
- user_id: userId,
|
|
88
|
|
- grouping_id: groupingId,
|
|
89
|
|
- membership_type: role,
|
|
90
|
|
- can_edit: false
|
|
|
125
|
+ /** Grab all memberships associated with groupingIds */
|
|
|
126
|
+ const memberships = await Membership.query().whereIn('grouping_id', matchingGroupingIds)
|
|
|
127
|
+
|
|
|
128
|
+ /** Set membership as active only if the user initiates it */
|
|
|
129
|
+ await this._patchMembership(memberships, userId, { is_active: true })
|
|
|
130
|
+
|
|
|
131
|
+ /** Make a new query to get updated information */
|
|
|
132
|
+ return await Membership.query().whereIn('grouping_id', matchingGroupingIds)
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ /**
|
|
|
136
|
+ * If both have NO grouping in common create a membership
|
|
|
137
|
+ * for both to new group but set membership as inactive for target
|
|
|
138
|
+ * */
|
|
|
139
|
+ else {
|
|
|
140
|
+ /** Check if the grouping exists and if NOT creat it */
|
|
|
141
|
+ const groupingId = await this.findOrCreateGrouping(groupingToWrite)
|
|
|
142
|
+
|
|
|
143
|
+ const userMembership = await Membership.query(txn).insert({
|
|
|
144
|
+ user_id: userId,
|
|
|
145
|
+ grouping_id: groupingId,
|
|
|
146
|
+ membership_type: role,
|
|
|
147
|
+ can_edit: false,
|
|
|
148
|
+ is_active: true
|
|
|
149
|
+ })
|
|
|
150
|
+
|
|
|
151
|
+ const targetMembership = await Membership.query(txn).insert({
|
|
|
152
|
+ user_id: targetId,
|
|
|
153
|
+ grouping_id: groupingId,
|
|
|
154
|
+ membership_type: role,
|
|
|
155
|
+ can_edit: false,
|
|
|
156
|
+ is_active: false
|
|
|
157
|
+ })
|
|
|
158
|
+
|
|
|
159
|
+ return [userMembership, targetMembership]
|
|
91
|
160
|
}
|
|
92
|
|
- console.log(membershipInfo)
|
|
93
|
|
- return await Membership.query(txn)
|
|
94
|
|
- .insert(membershipInfo)
|
|
95
|
161
|
}
|
|
96
|
162
|
|
|
97
|
163
|
/**
|