Przeglądaj źródła

:recycle: adding in user relation | simplifying scoring service

tags/0.0.1
j 4 lat temu
rodzic
commit
0df012daa2
2 zmienionych plików z 26 dodań i 16 usunięć
  1. 9
    0
      backend/lib/models/profile.js
  2. 17
    16
      backend/lib/services/profile.js

+ 9
- 0
backend/lib/models/profile.js Wyświetl plik

@@ -1,6 +1,7 @@
1 1
 const Schwifty = require('@hapipal/schwifty')
2 2
 const Joi = require('joi')
3 3
 const Response = require('./response')
4
+const User = require('./user')
4 5
 
5 6
 module.exports = class Profile extends Schwifty.Model {
6 7
     static get tableName() {
@@ -16,6 +17,14 @@ module.exports = class Profile extends Schwifty.Model {
16 17
                     to: 'profiles.profile_id',
17 18
                 },
18 19
             },
20
+            user: {
21
+                relation: Schwifty.Model.BelongsToOneRelation,
22
+                modelClass: User,
23
+                join: {
24
+                    from: 'users.user_id',
25
+                    to: 'profiles.user_id',
26
+                },
27
+            },
19 28
         }
20 29
     }
21 30
     static get joiSchema() {

+ 17
- 16
backend/lib/services/profile.js Wyświetl plik

@@ -5,7 +5,7 @@ const magic = 1000
5 5
 const scoreResponses = (seeker, potentialMatch) => {
6 6
     if (seeker.responses.length != potentialMatch.responses.length)
7 7
         return {
8
-            error: `complete responses for profile: ${seeker.profile_id} unqeual to profile: ${potentialMatch.profile_id}`,
8
+            error: `complete responses for profile: ${seeker.profile_id} unqeual to profile: ${potentialMatch.profile_id} | ${seeker.responses.length}:${potentialMatch.responses.length}`,
9 9
         }
10 10
 
11 11
     const checkValCb = res => {
@@ -19,10 +19,6 @@ const scoreResponses = (seeker, potentialMatch) => {
19 19
         ) * magic,
20 20
     )
21 21
 }
22
-const userTypes = {
23
-    seeker: 'seeker',
24
-    poster: 'poster',
25
-}
26 22
 
27 23
 /**
28 24
  * Class to hold our retrieved profile information
@@ -150,26 +146,31 @@ module.exports = class ProfileService extends Schmervice.Service {
150 146
      * @returns {Array} Ordered and scored Profiles
151 147
      */
152 148
     async scoreProfilesFor(userId) {
153
-        const { User } = this.server.models(['user'])
154 149
         const { Profile } = this.server.models()
155 150
 
156
-        const user = await User.query().findOne('user_id', userId)
151
+        // Our User Profile to score for
157 152
         const userProfile = await Profile.query()
158 153
             .findOne('user_id', userId)
159 154
             .withGraphFetched('responses')
160
-        const isPosterOpposite = user.is_poster == 1 ? 0 : 1
161
-        const userType = Object.keys(userTypes)[isPosterOpposite]
155
+            .withGraphFetched('user')
156
+
157
+        const isPosterOpposite = userProfile.user.is_poster == 1 ? 0 : 1
158
+
159
+        // Find all Profiles that are NOT of our userProfile.type
160
+        // ie. If userProfile.type == seeker, then find: poster
161
+        let profileIdsOfOppositeType = await Profile.query()
162
+            .withGraphFetched('responses')
163
+            .withGraphFetched('user')
162 164
 
163
-        const profileIdsOfOppositeType = await Profile.query().withGraphFetched(
164
-            'responses',
165
+        // TODO: Let Objection optimize this
166
+        profileIdsOfOppositeType = profileIdsOfOppositeType.filter(
167
+            profile => profile.user.is_poster == isPosterOpposite,
165 168
         )
166 169
 
167
-        return profileIdsOfOppositeType.map(profile => ({
170
+        const scored = profileIdsOfOppositeType.map(profile => ({
168 171
             profile_id: profile.profile_id,
169
-            score: scoreResponses(
170
-                userProfile,
171
-                new CompleteProfile(profile, userType),
172
-            ),
172
+            score: scoreResponses(userProfile, profile),
173 173
         }))
174
+        return scored.sort((a, b) => a.score - b.score)
174 175
     }
175 176
 }

Ładowanie…
Anuluj
Zapisz