|
|
@@ -2,27 +2,114 @@
|
|
2
|
2
|
|
|
3
|
3
|
const Schmervice = require('@hapipal/schmervice');
|
|
4
|
4
|
|
|
|
5
|
+
|
|
5
|
6
|
module.exports = class MembershipService extends Schmervice.Service {
|
|
6
|
|
- constructor(...args) {
|
|
7
|
|
- super(...args)
|
|
|
7
|
+ constructor(...args) { super(...args) }
|
|
|
8
|
+
|
|
|
9
|
+ /**
|
|
|
10
|
+ * Internal method to get list of grouping_ids for this user
|
|
|
11
|
+ * @param {number} userId
|
|
|
12
|
+ * @returns {Array} List of all grouping_ids for user
|
|
|
13
|
+ */
|
|
|
14
|
+ async _getGroupIdsForUserId(userId) {
|
|
|
15
|
+ const { Membership } = this.server.models()
|
|
|
16
|
+
|
|
|
17
|
+ /** Grab every Membership associated with this id */
|
|
|
18
|
+ const allMemberships = await Membership.query()
|
|
|
19
|
+ .where('user_id', userId)
|
|
|
20
|
+
|
|
|
21
|
+ /** Copy a list of the just the Groupings */
|
|
|
22
|
+ const groupingIdsToGrab = allMemberships.map(membership => membership.grouping_id)
|
|
|
23
|
+
|
|
|
24
|
+ /** Uncomment to dedupe the list just in case */
|
|
|
25
|
+ return [...new Set(groupingIdsToGrab)]
|
|
8
|
26
|
}
|
|
9
|
|
- async findGroupingsById(id) {
|
|
10
|
|
- const { Membership, Grouping } = this.server.models()
|
|
11
|
27
|
|
|
12
|
|
- /** Grab every Membership associated with this id */
|
|
13
|
|
- const allMemberships = await Membership.query()
|
|
14
|
|
- .throwIfNotFound()
|
|
15
|
|
- .where('user_id', id)
|
|
|
28
|
+ /**
|
|
|
29
|
+ * Internal method to create a new grouping
|
|
|
30
|
+ * @param {*} groupingToTry
|
|
|
31
|
+ * @param {*} txn
|
|
|
32
|
+ * @returns
|
|
|
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)
|
|
|
41
|
+ .insert(groupingInfo)
|
|
|
42
|
+ }
|
|
16
|
43
|
|
|
17
|
|
- /** Copy a list of the just the Groupings */
|
|
18
|
|
- const groupingIdsToGrab = allMemberships.map(membership => membership.grouping_id)
|
|
|
44
|
+ async findOrCreateGrouping(groupingToTry) {
|
|
|
45
|
+ let idToReturn = groupingToTry.grouping_id
|
|
|
46
|
+ if(!idToReturn) {
|
|
|
47
|
+ /** ?: For some reason this returns the key id */
|
|
|
48
|
+ const grouping = await this._createGrouping(groupingToTry)
|
|
|
49
|
+ idToReturn = grouping.id
|
|
|
50
|
+ }
|
|
|
51
|
+ return idToReturn
|
|
|
52
|
+ }
|
|
19
|
53
|
|
|
20
|
|
- /** Uncomment to dedupe the list just in case */
|
|
21
|
|
- const dedupedGroupings = [...new Set(groupingIdsToGrab)]
|
|
|
54
|
+ /**
|
|
|
55
|
+ * Get a list of groupings for user
|
|
|
56
|
+ * @param {number} userId
|
|
|
57
|
+ * @returns {Array}
|
|
|
58
|
+ */
|
|
|
59
|
+ async findGroupingsById(userId) {
|
|
|
60
|
+ const { Grouping } = this.server.models()
|
|
|
61
|
+
|
|
|
62
|
+ const dedupedGroupings = await this._getGroupIdsForUserId(userId)
|
|
22
|
63
|
|
|
23
|
64
|
/** Grab just the Groupings this id has a Membership for */
|
|
24
|
65
|
return await Grouping.query()
|
|
25
|
66
|
.throwIfNotFound()
|
|
26
|
67
|
.whereIn('grouping_id', dedupedGroupings)
|
|
27
|
68
|
}
|
|
|
69
|
+
|
|
|
70
|
+ /**
|
|
|
71
|
+ * Check for grouping membership then add membership record
|
|
|
72
|
+ * @param {number} userId
|
|
|
73
|
+ * @param {number} groupingId
|
|
|
74
|
+ * @param {string} role
|
|
|
75
|
+ * @returns
|
|
|
76
|
+ */
|
|
|
77
|
+ async joinGrouping(userId, groupingId, role, txn) {
|
|
|
78
|
+ const { Membership } = this.server.models()
|
|
|
79
|
+
|
|
|
80
|
+ const dedupedGroupings = await this._getGroupIdsForUserId(userId)
|
|
|
81
|
+
|
|
|
82
|
+ /** Do NOTHING if already in Grouping */
|
|
|
83
|
+ if(dedupedGroupings.includes(groupingId)) return
|
|
|
84
|
+
|
|
|
85
|
+ console.log(dedupedGroupings)
|
|
|
86
|
+ const membershipInfo = {
|
|
|
87
|
+ user_id: userId,
|
|
|
88
|
+ grouping_id: groupingId,
|
|
|
89
|
+ membership_type: role,
|
|
|
90
|
+ can_edit: false
|
|
|
91
|
+ }
|
|
|
92
|
+ console.log(membershipInfo)
|
|
|
93
|
+ return await Membership.query(txn)
|
|
|
94
|
+ .insert(membershipInfo)
|
|
|
95
|
+ }
|
|
|
96
|
+
|
|
|
97
|
+ /**
|
|
|
98
|
+ * Remove membership record based on grouping_id
|
|
|
99
|
+ * @param {number} userId
|
|
|
100
|
+ * @param {number} groupingId
|
|
|
101
|
+ * @returns
|
|
|
102
|
+ */
|
|
|
103
|
+ async leaveGrouping(userId, groupingId) {
|
|
|
104
|
+ const { Membership } = this.server.models()
|
|
|
105
|
+
|
|
|
106
|
+ const dedupedGroupings = await this._getGroupIdsForUserId(userId)
|
|
|
107
|
+
|
|
|
108
|
+ /** Do NOTHING if NOT in Grouping */
|
|
|
109
|
+ if(!dedupedGroupings.includes(groupingId)) return
|
|
|
110
|
+
|
|
|
111
|
+ return await Membership.query()
|
|
|
112
|
+ .delete()
|
|
|
113
|
+ .where('grouping_id', groupingId)
|
|
|
114
|
+ }
|
|
28
|
115
|
}
|