浏览代码

:sparkles: joining group checks for matches

master
j 5 年前
父节点
当前提交
dd21aa4e17
共有 3 个文件被更改,包括 86 次插入20 次删除
  1. 1
    0
      backend/lib/models/membership.js
  2. 4
    5
      backend/lib/routes/membership/join.js
  3. 81
    15
      backend/lib/services/membership.js

+ 1
- 0
backend/lib/models/membership.js 查看文件

@@ -10,6 +10,7 @@ module.exports = class Membership extends Schwifty.Model {
10 10
             grouping_id: Joi.number().allow(null),
11 11
             membership_type: Joi.string().required(),
12 12
             can_edit: Joi.boolean().required(),
13
+            is_active: Joi.boolean().required(),
13 14
         })
14 15
     }
15 16
 }

+ 4
- 5
backend/lib/routes/membership/join.js 查看文件

@@ -14,6 +14,7 @@ const validators = {
14 14
     join: {
15 15
         payload: Joi.object({
16 16
             user_id: Joi.number().required(),
17
+            target_id: Joi.number().allow(null),
17 18
             grouping_id: Joi.number().allow(null),
18 19
             grouping_name: Joi.string().allow(null),
19 20
             grouping_type: Joi.string().allow(null),
@@ -47,19 +48,17 @@ module.exports = {
47 48
                     grouping_name: res.grouping_name,
48 49
                     grouping_type: res.grouping_type
49 50
                 }
50
-                /** Check if the grouping exists and if NOT creat it */
51
-                const groupingId = await membershipService.findOrCreateGrouping(groupingToWrite)
52
-
51
+               
53 52
                 /** Default to participant role */
54 53
                 const role = res.role ? res.role : 'participant'
55 54
 
56 55
                 /** User membership service method to create membership */
57
-                const membership = await membershipService.joinGrouping(res.user_id, groupingId, role)
56
+                const memberships = await membershipService.joinGrouping(res.user_id, res.target_id, groupingToWrite, role)
58 57
 
59 58
                 return {
60 59
                     ok: true,
61 60
                     handler: pluginConfig.handlerType,
62
-                    data: { membership }
61
+                    data: { memberships, hasMatch: memberships.every(membership => membership.is_active == true) }
63 62
                 }
64 63
             }
65 64
             catch(err) {

+ 81
- 15
backend/lib/services/membership.js 查看文件

@@ -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
     /**

正在加载...
取消
保存