Procházet zdrojové kódy

:recycle: moving around some score functions

tags/0.0.1
J před 4 roky
rodič
revize
75f2e211fa
1 změnil soubory, kde provedl 38 přidání a 32 odebrání
  1. 38
    32
      backend/lib/services/profile.js

+ 38
- 32
backend/lib/services/profile.js Zobrazit soubor

22
         ) * magic,
22
         ) * magic,
23
     )
23
     )
24
 }
24
 }
25
+const filterByDistance = (profileList, max) => {
26
+    return profileList.filter(profile => {
27
+        const profileDistance = Math.floor(parseFloat(profile.distance) * 100)
28
+        const adjustedMaxDistance = Math.floor(parseFloat(max) * 100)
29
+        return profileDistance <= adjustedMaxDistance
30
+    })
31
+}
32
+const scoreAll = (profileList, userProfile) => {
33
+    return profileList.map(profile => {
34
+        return {
35
+            // Uncomment to return the whole profile
36
+            // ...profile,
37
+            profile_id: profile.profile_id,
38
+            score: scoreResponses(userProfile, profile),
39
+            distance: profile.distance
40
+        }
41
+    })
42
+}
43
+/** 
44
+     * Grab the zip code string
45
+    */
46
+ const getZipCodeFromProfile = (profile) => {
47
+    // There should only be one zip code entry per profile
48
+    let zip = profile.responses.filter(response => response.response_key_id == 16)[0]
49
+    const responseIndexForZip = profile.responses.indexOf(zip)
50
+    if(responseIndexForZip >= 0) {
51
+        profile.responses.splice(responseIndexForZip, 1)
52
+    }
53
+    return zip.val
54
+}
25
 
55
 
26
 /**
56
 /**
27
  * Class to hold our retrieved profile information
57
  * Class to hold our retrieved profile information
161
 
191
 
162
         return await Profile.query().delete().where('profile_id', profileId)
192
         return await Profile.query().delete().where('profile_id', profileId)
163
     }
193
     }
164
-    /** 
165
-     * Grab the zip code string
166
-    */
167
-    _getZipCodeFromProfile(profile) {
168
-        // There should only be one zip code entry per profile
169
-        let zip = profile.responses.filter(response => response.response_key_id == 16)[0]
170
-        const responseIndexForZip = profile.responses.indexOf(zip)
171
-        if(responseIndexForZip >= 0) {
172
-            profile.responses.splice(responseIndexForZip, 1)
173
-        }
174
-        return zip.val
175
-    }
194
+    
176
     /**
195
     /**
177
      * Score a profile
196
      * Score a profile
178
      * @param {number} profileId
197
      * @param {number} profileId
179
      * @returns {Array} Ordered and scored Profiles
198
      * @returns {Array} Ordered and scored Profiles
180
      */
199
      */
181
-    async scoreProfilesFor(profileId, maxDistanceMiles, distanceUnit) {
200
+    async scoreProfilesFor(profileId, maxDistance, distanceUnit) {
182
         const { Profile } = this.server.models()
201
         const { Profile } = this.server.models()
183
         
202
         
184
         // Our User Profile to score for
203
         // Our User Profile to score for
188
         .withGraphFetched('user')
207
         .withGraphFetched('user')
189
         
208
         
190
         // Move unneeded responses
209
         // Move unneeded responses
191
-        const userZip = this._getZipCodeFromProfile(userProfile)
210
+        const userZip = getZipCodeFromProfile(userProfile)
192
 
211
 
193
         // Find all Profiles that are NOT of our userProfile.type
212
         // Find all Profiles that are NOT of our userProfile.type
194
         // ie. If userProfile.type == seeker, then find: poster
213
         // ie. If userProfile.type == seeker, then find: poster
201
         profileIdsOfOppositeType = profileIdsOfOppositeType.filter(profile => profile.user.is_poster == isPosterOpposite)
220
         profileIdsOfOppositeType = profileIdsOfOppositeType.filter(profile => profile.user.is_poster == isPosterOpposite)
202
         
221
         
203
         const profilePlusDistance = await Promise.all(profileIdsOfOppositeType.map(async profile => {
222
         const profilePlusDistance = await Promise.all(profileIdsOfOppositeType.map(async profile => {
204
-            const targetZip = this._getZipCodeFromProfile(profile)
223
+            const targetZip = getZipCodeFromProfile(profile)
205
             const distance = await this._compareDistance(userZip, targetZip, distanceUnit)
224
             const distance = await this._compareDistance(userZip, targetZip, distanceUnit)
206
             return {
225
             return {
207
                 ...profile,
226
                 ...profile,
209
             }
228
             }
210
         }))
229
         }))
211
 
230
 
212
-        // Filter by distance
213
-        // TODO: probably do this with a query
214
-        const distanceFiltered = profilePlusDistance.filter(profile => {
215
-            const profileDistance = Math.floor(parseFloat(profile.distance) * 100)
216
-            const adjustedMaxDistance = Math.floor(parseFloat(maxDistanceMiles) * 100)
217
-            return profileDistance <= adjustedMaxDistance
218
-        })
231
+        const distanceFilteredProfiles = filterByDistance(profilePlusDistance, maxDistance)
232
+
233
+        const scoredProfilesWithDistance = scoreAll(distanceFilteredProfiles, userProfile)
219
 
234
 
220
-        const scored = distanceFiltered.map(profile => {
221
-            return {
222
-                // Uncomment to return the whole profile
223
-                // ...profile,
224
-                profile_id: profile.profile_id,
225
-                score: scoreResponses(userProfile, profile),
226
-                distance: profile.distance
227
-            }
228
-        })
229
         // Order by score
235
         // Order by score
230
-        return scored.sort((a, b) => a.score - b.score)
236
+        return scoredProfilesWithDistance.sort((a, b) => a.score - b.score)
231
     }
237
     }
232
     
238
     
233
     /**
239
     /**

Načítá se…
Zrušit
Uložit