|
|
@@ -1,7 +1,7 @@
|
|
1
|
1
|
const Schmervice = require('@hapipal/schmervice')
|
|
2
|
2
|
const cosineSimilarity = require('compute-cosine-similarity')
|
|
3
|
3
|
const haversine = require('haversine')
|
|
4
|
|
-
|
|
|
4
|
+const zipcodeKey = 7
|
|
5
|
5
|
const magic = 1000
|
|
6
|
6
|
const scoreResponses = (seeker, potentialMatch) => {
|
|
7
|
7
|
if (seeker.responses.length != potentialMatch.responses.length)
|
|
|
@@ -44,14 +44,16 @@ const scoreAll = (profileList, userProfile) => {
|
|
44
|
44
|
*/
|
|
45
|
45
|
const getZipCodeFromProfile = profile => {
|
|
46
|
46
|
// There should only be one zip code entry per profile
|
|
47
|
|
- let zip = profile.responses.filter(
|
|
48
|
|
- response => response.response_key_id == 16,
|
|
|
47
|
+ let zipRes = profile.responses.filter(
|
|
|
48
|
+ // Whatever the zipcode questions
|
|
|
49
|
+ response => response.response_key_id == zipcodeKey,
|
|
49
|
50
|
)[0]
|
|
50
|
|
- const responseIndexForZip = profile.responses.indexOf(zip)
|
|
|
51
|
+ const responseIndexForZip = profile.responses.indexOf(zipRes)
|
|
51
|
52
|
if (responseIndexForZip >= 0) {
|
|
52
|
53
|
profile.responses.splice(responseIndexForZip, 1)
|
|
53
|
54
|
}
|
|
54
|
|
- return zip.val
|
|
|
55
|
+ // console.log(zipRes)
|
|
|
56
|
+ return zipRes.val
|
|
55
|
57
|
}
|
|
56
|
58
|
|
|
57
|
59
|
/**
|
|
|
@@ -63,8 +65,11 @@ class CompleteProfile {
|
|
63
|
65
|
constructor(profile, type) {
|
|
64
|
66
|
this.user_id = profile.user_id // int user_id
|
|
65
|
67
|
this.profile_id = profile.profile_id // int profile_id
|
|
|
68
|
+ this.profile_image = profile.user_media // int profile_id
|
|
66
|
69
|
this.responses = profile.responses // [] of all responses
|
|
67
|
70
|
this.user_type = type
|
|
|
71
|
+
|
|
|
72
|
+ this.user = profile.user
|
|
68
|
73
|
}
|
|
69
|
74
|
}
|
|
70
|
75
|
|
|
|
@@ -112,6 +117,7 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
112
|
117
|
const profilesEntries = await Profile.query()
|
|
113
|
118
|
.whereIn('profile_id', profileIdArray)
|
|
114
|
119
|
.withGraphFetched('responses')
|
|
|
120
|
+ .withGraphFetched('user')
|
|
115
|
121
|
|
|
116
|
122
|
return profilesEntries.map(profile => {
|
|
117
|
123
|
return new CompleteProfile(profile, type)
|
|
|
@@ -232,16 +238,25 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
232
|
238
|
let profileIdsOfOppositeType = await Profile.query()
|
|
233
|
239
|
.withGraphFetched('responses')
|
|
234
|
240
|
.withGraphFetched('user')
|
|
235
|
|
-
|
|
|
241
|
+
|
|
236
|
242
|
// TODO: Let Objection optimize this
|
|
237
|
243
|
const isPosterOpposite = userProfile.user.is_poster == 1 ? 0 : 1
|
|
238
|
244
|
profileIdsOfOppositeType = profileIdsOfOppositeType.filter(
|
|
239
|
245
|
profile => profile.user.is_poster == isPosterOpposite,
|
|
240
|
246
|
)
|
|
241
|
247
|
|
|
|
248
|
+ // Only include profiles that included zipcode response
|
|
|
249
|
+ profileIdsOfOppositeType = profileIdsOfOppositeType.filter(profile => {
|
|
|
250
|
+ const zipcodeResponses = profile.responses.filter(response => response.response_key_id == zipcodeKey)
|
|
|
251
|
+ return zipcodeResponses.length > 0
|
|
|
252
|
+ })
|
|
|
253
|
+
|
|
242
|
254
|
const profilePlusDistance = await Promise.all(
|
|
243
|
255
|
profileIdsOfOppositeType.map(async profile => {
|
|
244
|
256
|
const targetZip = getZipCodeFromProfile(profile)
|
|
|
257
|
+
|
|
|
258
|
+ if(!userZip || !targetZip) return { ...profile, distance: [9999, distanceUnit] }
|
|
|
259
|
+
|
|
245
|
260
|
const distance = await this._compareDistance(
|
|
246
|
261
|
userZip,
|
|
247
|
262
|
targetZip,
|
|
|
@@ -281,7 +296,7 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
281
|
296
|
parseInt(zipCode),
|
|
282
|
297
|
)
|
|
283
|
298
|
if (!zipInfo) {
|
|
284
|
|
- console.log(zipCode)
|
|
|
299
|
+ console.log('zip:', zipCode)
|
|
285
|
300
|
}
|
|
286
|
301
|
|
|
287
|
302
|
return {
|
|
|
@@ -297,7 +312,7 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
297
|
312
|
* @param {number} distance in miles
|
|
298
|
313
|
*/
|
|
299
|
314
|
async _compareDistance(start_zip, end_zip, distanceUnit) {
|
|
300
|
|
- if (!start_zip || !end_zip) return
|
|
|
315
|
+ if (!start_zip || !end_zip || isNaN(start_zip) || isNaN(end_zip)) return
|
|
301
|
316
|
|
|
302
|
317
|
const start = await this._latLonForZip(start_zip)
|
|
303
|
318
|
const end = await this._latLonForZip(end_zip)
|