|
|
@@ -4,7 +4,12 @@ const config = require('../../../db/data-generator/config.json')
|
|
4
|
4
|
const profiler = require('./profiler')
|
|
5
|
5
|
const scoring = require('./scorer')
|
|
6
|
6
|
const zipcoder = require('./zipcoder')
|
|
|
7
|
+<<<<<<< HEAD
|
|
7
|
8
|
const { response } = require('@hapi/hapi/lib/validation')
|
|
|
9
|
+=======
|
|
|
10
|
+const tagger = require('./tagger')
|
|
|
11
|
+const filter = require('./filter')
|
|
|
12
|
+>>>>>>> 154d171 (:construction: new filter service to preprocess a users match pool before scoring)
|
|
8
|
13
|
|
|
9
|
14
|
module.exports = class ProfileService extends Schmervice.Service {
|
|
10
|
15
|
constructor(...args) {
|
|
|
@@ -253,30 +258,36 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
253
|
258
|
.findOne('profile_id', profileId)
|
|
254
|
259
|
.withGraphFetched('responses')
|
|
255
|
260
|
.withGraphFetched('user')
|
|
256
|
|
-
|
|
257
|
|
- // Move unneeded responses
|
|
|
261
|
+
|
|
258
|
262
|
const userZip = zipcoder.getZipCodeFromProfile(userProfile)
|
|
259
|
263
|
|
|
260
|
|
- // Find all Profiles that are NOT of our userProfile.type
|
|
261
|
|
- // ie. If userProfile.type == seeker, then find: poster
|
|
262
|
|
- let profileIdsOfOppositeType = await Profile.query()
|
|
|
264
|
+ // preprocess potential match pool with filter service methods
|
|
|
265
|
+ let matchPool = await Profile.query()
|
|
263
|
266
|
.withGraphFetched('responses')
|
|
264
|
267
|
.withGraphFetched('user')
|
|
265
|
|
- // TODO: Let Objection optimize this
|
|
266
|
|
- const isPosterOpposite = userProfile.user.is_poster == 1 ? 0 : 1
|
|
267
|
|
- profileIdsOfOppositeType = profileIdsOfOppositeType
|
|
268
|
|
- .filter(profile => {
|
|
269
|
|
- return profile.user.is_poster == isPosterOpposite
|
|
270
|
|
- })
|
|
271
|
|
- .filter(profile => {
|
|
272
|
|
- // Only include profiles that included zipcode response
|
|
273
|
|
- return zipcoder.getZipCodeFromProfile(profile) ? true : false
|
|
274
|
|
- })
|
|
275
|
268
|
|
|
276
|
|
- const profilePlusDistance = await Promise.all(
|
|
277
|
|
- profileIdsOfOppositeType.map(async profile => {
|
|
278
|
|
- const targetZip = zipcoder.getZipCodeFromProfile(profile)
|
|
|
269
|
+ matchPool = filter.byProfileType(matchPool, userProfile.user)
|
|
|
270
|
+ matchPool = filter.byNullZip(matchPool)
|
|
|
271
|
+ // attach distance to pool profiles for max distance filter
|
|
|
272
|
+ matchPool = await this.calcProfileDistances(matchPool, distanceUnit, userZip)
|
|
|
273
|
+ // filter with matchPool profiles that have distance
|
|
|
274
|
+ matchPool = filter.byMaxDistance(matchPool, maxDistance)
|
|
|
275
|
+
|
|
|
276
|
+ const scoredProfilesWithDistance = scoring.scoreAll(
|
|
|
277
|
+ matchPool,
|
|
|
278
|
+ userProfile,
|
|
|
279
|
+ this.scoreLookup,
|
|
|
280
|
+ )
|
|
|
281
|
+ // Order by score
|
|
|
282
|
+ return scoredProfilesWithDistance.sort(
|
|
|
283
|
+ (a, b) => b.score.total - a.score.total,
|
|
|
284
|
+ )
|
|
|
285
|
+ }
|
|
279
|
286
|
|
|
|
287
|
+ async calcProfileDistances(matchPool, distanceUnit, userZip){
|
|
|
288
|
+ await Promise.all(
|
|
|
289
|
+ matchPool.map(async profile => {
|
|
|
290
|
+ const targetZip = zipcoder.getZipCodeFromProfile(profile)
|
|
280
|
291
|
if (!userZip || !targetZip)
|
|
281
|
292
|
return { ...profile, distance: [9999, distanceUnit] }
|
|
282
|
293
|
|
|
|
@@ -289,21 +300,7 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
289
|
300
|
...profile,
|
|
290
|
301
|
distance: [distance.toFixed(2), distanceUnit],
|
|
291
|
302
|
}
|
|
292
|
|
- }),
|
|
293
|
|
- )
|
|
294
|
|
-
|
|
295
|
|
- const distanceFilteredProfiles = zipcoder.filterByDistance(
|
|
296
|
|
- profilePlusDistance,
|
|
297
|
|
- maxDistance,
|
|
298
|
|
- )
|
|
299
|
|
- const scoredProfilesWithDistance = scoring.scoreAll(
|
|
300
|
|
- distanceFilteredProfiles,
|
|
301
|
|
- userProfile,
|
|
302
|
|
- this.scoreLookup,
|
|
303
|
|
- )
|
|
304
|
|
- // Order by score
|
|
305
|
|
- return scoredProfilesWithDistance.sort(
|
|
306
|
|
- (a, b) => b.score.total - a.score.total,
|
|
|
303
|
+ })
|
|
307
|
304
|
)
|
|
308
|
305
|
}
|
|
309
|
306
|
|
|
|
@@ -351,8 +348,8 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
351
|
348
|
await this._setTagLookup()
|
|
352
|
349
|
let associations = groupingId
|
|
353
|
350
|
? await TagAssociation.query()
|
|
354
|
|
- .where('grouping_id', groupingId)
|
|
355
|
|
- .andWhere('profile_id', profileId)
|
|
|
351
|
+ .where('grouping_id', groupingId)
|
|
|
352
|
+ .andWhere('profile_id', profileId)
|
|
356
|
353
|
: await TagAssociation.query().andWhere('profile_id', profileId)
|
|
357
|
354
|
return associations
|
|
358
|
355
|
.map(assoc => ({
|