|
|
@@ -10,12 +10,12 @@ module.exports = class MembershipService extends Schmervice.Service {
|
|
10
|
10
|
* @param {number} profileId
|
|
11
|
11
|
* @returns {Array} List of all grouping_ids for user
|
|
12
|
12
|
*/
|
|
13
|
|
- async _getGroupIdsForProfileId(profileId, type) {
|
|
|
13
|
+ async _getGroupingIdsForProfileId(profileId, type) {
|
|
14
|
14
|
const { Membership } = this.server.models()
|
|
15
|
15
|
|
|
16
|
16
|
/** Grab every Membership associated with this id */
|
|
17
|
17
|
let allMemberships
|
|
18
|
|
- console.log()
|
|
|
18
|
+
|
|
19
|
19
|
if(type) {
|
|
20
|
20
|
allMemberships = await Membership.query()
|
|
21
|
21
|
.where({ profile_id: profileId })
|
|
|
@@ -26,7 +26,7 @@ module.exports = class MembershipService extends Schmervice.Service {
|
|
26
|
26
|
.where({ profile_id: profileId })
|
|
27
|
27
|
.where({ is_active: true })
|
|
28
|
28
|
}
|
|
29
|
|
-
|
|
|
29
|
+
|
|
30
|
30
|
/** Copy a list of the just the Groupings */
|
|
31
|
31
|
const groupingIdsToGrab = allMemberships.map(
|
|
32
|
32
|
membership => membership.grouping_id,
|
|
|
@@ -38,9 +38,9 @@ module.exports = class MembershipService extends Schmervice.Service {
|
|
38
|
38
|
|
|
39
|
39
|
/**
|
|
40
|
40
|
* Internal method to create a new grouping
|
|
41
|
|
- * @param {*} groupingToTry
|
|
|
41
|
+ * @param {object} groupingToTry from payload data
|
|
42
|
42
|
* @param {*} txn
|
|
43
|
|
- * @returns
|
|
|
43
|
+ * @returns {Grouping} created db record
|
|
44
|
44
|
*/
|
|
45
|
45
|
async _createGrouping(groupingToTry, txn) {
|
|
46
|
46
|
const { Grouping } = this.server.models()
|
|
|
@@ -50,8 +50,13 @@ module.exports = class MembershipService extends Schmervice.Service {
|
|
50
|
50
|
}
|
|
51
|
51
|
return await Grouping.query(txn).insert(groupingInfo)
|
|
52
|
52
|
}
|
|
53
|
|
-
|
|
54
|
|
- async findOrCreateGrouping(groupingToTry) {
|
|
|
53
|
+ /**
|
|
|
54
|
+ * Tries to grab a grouping_id from payload data
|
|
|
55
|
+ * or returns grouping_id from a newly created record
|
|
|
56
|
+ * @param {object} groupingToTry from payload data
|
|
|
57
|
+ * @returns {number} grouping_id from payload or created record
|
|
|
58
|
+ */
|
|
|
59
|
+ async findOrCreateGroupingFromPayload(groupingToTry) {
|
|
55
|
60
|
let idToReturn = groupingToTry.grouping_id
|
|
56
|
61
|
if (!idToReturn) {
|
|
57
|
62
|
/** ?: For some reason this returns the key id */
|
|
|
@@ -66,22 +71,21 @@ module.exports = class MembershipService extends Schmervice.Service {
|
|
66
|
71
|
* @param {number} profileId
|
|
67
|
72
|
* @returns {Array}
|
|
68
|
73
|
*/
|
|
69
|
|
- async findGroupingsById(profileId, type) {
|
|
|
74
|
+ async findGroupingsByProfileId(profileId, type) {
|
|
70
|
75
|
const { Grouping } = this.server.models()
|
|
71
|
76
|
|
|
72
|
|
- const dedupedGroupings = await this._getGroupIdsForProfileId(profileId, type)
|
|
|
77
|
+ const dedupedGroupings = await this._getGroupingIdsForProfileId(profileId, type)
|
|
73
|
78
|
|
|
74
|
79
|
/** Grab just the Groupings this id has a Membership for */
|
|
75
|
80
|
return await Grouping.query()
|
|
76
|
|
- .throwIfNotFound()
|
|
77
|
81
|
.whereIn('grouping_id', dedupedGroupings)
|
|
78
|
82
|
}
|
|
79
|
83
|
|
|
80
|
84
|
async _groupingIdsInCommon(profileId, targetId) {
|
|
81
|
|
- const dedupedUserGroupingIds = await this._getGroupIdsForProfileId(
|
|
|
85
|
+ const dedupedUserGroupingIds = await this._getGroupingIdsForProfileId(
|
|
82
|
86
|
profileId,
|
|
83
|
87
|
)
|
|
84
|
|
- const dedupedTargetGroupingIds = await this._getGroupIdsForProfileId(
|
|
|
88
|
+ const dedupedTargetGroupingIds = await this._getGroupingIdsForProfileId(
|
|
85
|
89
|
targetId,
|
|
86
|
90
|
)
|
|
87
|
91
|
|
|
|
@@ -102,34 +106,6 @@ module.exports = class MembershipService extends Schmervice.Service {
|
|
102
|
106
|
}
|
|
103
|
107
|
}
|
|
104
|
108
|
|
|
105
|
|
- async attemptMatch(profileId, targetId) {
|
|
106
|
|
- const { Membership } = this.server.models()
|
|
107
|
|
-
|
|
108
|
|
- /** If both people have groups in common */
|
|
109
|
|
- const matchingGroupingIds = await this._groupingIdsInCommon(
|
|
110
|
|
- profileId,
|
|
111
|
|
- targetId,
|
|
112
|
|
- )
|
|
113
|
|
-
|
|
114
|
|
- if (matchingGroupingIds.length) {
|
|
115
|
|
- /** Grab all memberships associated with groupingIds */
|
|
116
|
|
- const memberships = await Membership.query().whereIn(
|
|
117
|
|
- 'grouping_id',
|
|
118
|
|
- matchingGroupingIds,
|
|
119
|
|
- )
|
|
120
|
|
-
|
|
121
|
|
- /** Set membership as active only if the user initiates it */
|
|
122
|
|
- await this._patchMembership(memberships, profileId, {
|
|
123
|
|
- is_active: true,
|
|
124
|
|
- })
|
|
125
|
|
-
|
|
126
|
|
- /** Make a new query to get updated information */
|
|
127
|
|
- return await Membership.query().whereIn(
|
|
128
|
|
- 'grouping_id',
|
|
129
|
|
- matchingGroupingIds,
|
|
130
|
|
- )
|
|
131
|
|
- }
|
|
132
|
|
- }
|
|
133
|
109
|
|
|
134
|
110
|
/**
|
|
135
|
111
|
* Check for grouping membership then add membership record and set to active
|
|
|
@@ -168,25 +144,28 @@ module.exports = class MembershipService extends Schmervice.Service {
|
|
168
|
144
|
)
|
|
169
|
145
|
} else {
|
|
170
|
146
|
/**
|
|
171
|
|
- * If both have NO grouping in common create a membership
|
|
172
|
|
- * for both to new group but set membership as inactive for target
|
|
|
147
|
+ * If both have NO grouping in common, create a membership
|
|
|
148
|
+ * for both and add to a new grouping but
|
|
|
149
|
+ * set membership as inactive for target
|
|
173
|
150
|
* */
|
|
174
|
|
- /** Check if the grouping exists and if NOT creat it */
|
|
175
|
|
- const groupingId = await this.findOrCreateGrouping(groupingToWrite)
|
|
176
|
151
|
|
|
177
|
|
- const userMembership = await Membership.query(txn).insert({
|
|
178
|
|
- profile_id: profileId,
|
|
|
152
|
+ /** Check if the grouping exists and if NOT create it */
|
|
|
153
|
+ const groupingId = await this.findOrCreateGroupingFromPayload(groupingToWrite)
|
|
|
154
|
+ const membershipDetailsInCommon = {
|
|
179
|
155
|
grouping_id: groupingId,
|
|
180
|
156
|
membership_type: role,
|
|
181
|
157
|
can_edit: false,
|
|
|
158
|
+ }
|
|
|
159
|
+
|
|
|
160
|
+ const userMembership = await Membership.query(txn).insert({
|
|
|
161
|
+ profile_id: profileId,
|
|
|
162
|
+ ...membershipDetailsInCommon,
|
|
182
|
163
|
is_active: true,
|
|
183
|
164
|
})
|
|
184
|
165
|
|
|
185
|
166
|
const targetMembership = await Membership.query(txn).insert({
|
|
186
|
|
- profile_id: profileId,
|
|
187
|
|
- grouping_id: groupingId,
|
|
188
|
|
- membership_type: role,
|
|
189
|
|
- can_edit: false,
|
|
|
167
|
+ profile_id: targetId,
|
|
|
168
|
+ ...membershipDetailsInCommon,
|
|
190
|
169
|
is_active: false,
|
|
191
|
170
|
})
|
|
192
|
171
|
|
|
|
@@ -203,7 +182,7 @@ module.exports = class MembershipService extends Schmervice.Service {
|
|
203
|
182
|
async leaveGrouping(profileId, groupingId) {
|
|
204
|
183
|
const { Membership } = this.server.models()
|
|
205
|
184
|
|
|
206
|
|
- const dedupedGroupings = await this._getGroupIdsForProfileId(profileId)
|
|
|
185
|
+ const dedupedGroupings = await this._getGroupingIdsForProfileId(profileId)
|
|
207
|
186
|
|
|
208
|
187
|
/** Do NOTHING if NOT in Grouping */
|
|
209
|
188
|
if (!dedupedGroupings.includes(groupingId)) return
|