|
|
@@ -22,6 +22,36 @@ const scoreResponses = (seeker, potentialMatch) => {
|
|
22
|
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
|
57
|
* Class to hold our retrieved profile information
|
|
|
@@ -161,24 +191,13 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
161
|
191
|
|
|
162
|
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
|
196
|
* Score a profile
|
|
178
|
197
|
* @param {number} profileId
|
|
179
|
198
|
* @returns {Array} Ordered and scored Profiles
|
|
180
|
199
|
*/
|
|
181
|
|
- async scoreProfilesFor(profileId, maxDistanceMiles, distanceUnit) {
|
|
|
200
|
+ async scoreProfilesFor(profileId, maxDistance, distanceUnit) {
|
|
182
|
201
|
const { Profile } = this.server.models()
|
|
183
|
202
|
|
|
184
|
203
|
// Our User Profile to score for
|
|
|
@@ -188,7 +207,7 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
188
|
207
|
.withGraphFetched('user')
|
|
189
|
208
|
|
|
190
|
209
|
// Move unneeded responses
|
|
191
|
|
- const userZip = this._getZipCodeFromProfile(userProfile)
|
|
|
210
|
+ const userZip = getZipCodeFromProfile(userProfile)
|
|
192
|
211
|
|
|
193
|
212
|
// Find all Profiles that are NOT of our userProfile.type
|
|
194
|
213
|
// ie. If userProfile.type == seeker, then find: poster
|
|
|
@@ -201,7 +220,7 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
201
|
220
|
profileIdsOfOppositeType = profileIdsOfOppositeType.filter(profile => profile.user.is_poster == isPosterOpposite)
|
|
202
|
221
|
|
|
203
|
222
|
const profilePlusDistance = await Promise.all(profileIdsOfOppositeType.map(async profile => {
|
|
204
|
|
- const targetZip = this._getZipCodeFromProfile(profile)
|
|
|
223
|
+ const targetZip = getZipCodeFromProfile(profile)
|
|
205
|
224
|
const distance = await this._compareDistance(userZip, targetZip, distanceUnit)
|
|
206
|
225
|
return {
|
|
207
|
226
|
...profile,
|
|
|
@@ -209,25 +228,12 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
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
|
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
|
/**
|