Browse Source

:recycle: adding in user relation | simplifying scoring service

tags/0.0.1
j 4 years ago
parent
commit
0df012daa2
2 changed files with 26 additions and 16 deletions
  1. 9
    0
      backend/lib/models/profile.js
  2. 17
    16
      backend/lib/services/profile.js

+ 9
- 0
backend/lib/models/profile.js View File

1
 const Schwifty = require('@hapipal/schwifty')
1
 const Schwifty = require('@hapipal/schwifty')
2
 const Joi = require('joi')
2
 const Joi = require('joi')
3
 const Response = require('./response')
3
 const Response = require('./response')
4
+const User = require('./user')
4
 
5
 
5
 module.exports = class Profile extends Schwifty.Model {
6
 module.exports = class Profile extends Schwifty.Model {
6
     static get tableName() {
7
     static get tableName() {
16
                     to: 'profiles.profile_id',
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
     static get joiSchema() {
30
     static get joiSchema() {

+ 17
- 16
backend/lib/services/profile.js View File

5
 const scoreResponses = (seeker, potentialMatch) => {
5
 const scoreResponses = (seeker, potentialMatch) => {
6
     if (seeker.responses.length != potentialMatch.responses.length)
6
     if (seeker.responses.length != potentialMatch.responses.length)
7
         return {
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
     const checkValCb = res => {
11
     const checkValCb = res => {
19
         ) * magic,
19
         ) * magic,
20
     )
20
     )
21
 }
21
 }
22
-const userTypes = {
23
-    seeker: 'seeker',
24
-    poster: 'poster',
25
-}
26
 
22
 
27
 /**
23
 /**
28
  * Class to hold our retrieved profile information
24
  * Class to hold our retrieved profile information
150
      * @returns {Array} Ordered and scored Profiles
146
      * @returns {Array} Ordered and scored Profiles
151
      */
147
      */
152
     async scoreProfilesFor(userId) {
148
     async scoreProfilesFor(userId) {
153
-        const { User } = this.server.models(['user'])
154
         const { Profile } = this.server.models()
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
         const userProfile = await Profile.query()
152
         const userProfile = await Profile.query()
158
             .findOne('user_id', userId)
153
             .findOne('user_id', userId)
159
             .withGraphFetched('responses')
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
             profile_id: profile.profile_id,
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
 }

Loading…
Cancel
Save