|
|
@@ -4,7 +4,7 @@ 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
|
|
-const { response } = require('@hapi/hapi/lib/validation')
|
|
|
7
|
+const filter = require('../filter')
|
|
8
|
8
|
|
|
9
|
9
|
module.exports = class ProfileService extends Schmervice.Service {
|
|
10
|
10
|
constructor(...args) {
|
|
|
@@ -243,7 +243,14 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
243
|
243
|
* @param {number} profileId
|
|
244
|
244
|
* @returns {Array} Ordered and scored Profiles
|
|
245
|
245
|
*/
|
|
246
|
|
- async scoreProfilesFor(profileId, maxDistance, distanceUnit) {
|
|
|
246
|
+ async scoreProfilesFor(
|
|
|
247
|
+ profileId,
|
|
|
248
|
+ maxDistance,
|
|
|
249
|
+ distanceUnit,
|
|
|
250
|
+ duration,
|
|
|
251
|
+ presence,
|
|
|
252
|
+ certifications,
|
|
|
253
|
+ ) {
|
|
247
|
254
|
const { Profile } = this.server.models()
|
|
248
|
255
|
|
|
249
|
256
|
await this._setScoreLookup()
|
|
|
@@ -254,29 +261,41 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
254
|
261
|
.withGraphFetched('responses')
|
|
255
|
262
|
.withGraphFetched('user')
|
|
256
|
263
|
|
|
257
|
|
- // Move unneeded responses
|
|
258
|
264
|
const userZip = zipcoder.getZipCodeFromProfile(userProfile)
|
|
259
|
265
|
|
|
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()
|
|
|
266
|
+ // preprocess potential match pool with filter service methods
|
|
|
267
|
+ let matchPool = await Profile.query()
|
|
263
|
268
|
.withGraphFetched('responses')
|
|
264
|
269
|
.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
|
270
|
|
|
276
|
|
- const profilePlusDistance = await Promise.all(
|
|
277
|
|
- profileIdsOfOppositeType.map(async profile => {
|
|
278
|
|
- const targetZip = zipcoder.getZipCodeFromProfile(profile)
|
|
|
271
|
+ matchPool = filter.byProfileType(matchPool, userProfile.user)
|
|
|
272
|
+ matchPool = filter.byNullZip(matchPool)
|
|
|
273
|
+ // attach distance to pool profiles for max distance filter
|
|
|
274
|
+ matchPool = await this.calcProfileDistances(
|
|
|
275
|
+ matchPool,
|
|
|
276
|
+ distanceUnit,
|
|
|
277
|
+ userZip,
|
|
|
278
|
+ )
|
|
|
279
|
+ matchPool = filter.byMaxDistance(matchPool, maxDistance)
|
|
|
280
|
+ matchPool = filter.byDuration(matchPool, duration)
|
|
|
281
|
+ matchPool = filter.byPresence(matchPool, presence)
|
|
|
282
|
+ matchPool = filter.byCertifications(matchPool, certifications)
|
|
279
|
283
|
|
|
|
284
|
+ const scoredProfilesWithDistance = scoring.scoreAll(
|
|
|
285
|
+ matchPool,
|
|
|
286
|
+ userProfile,
|
|
|
287
|
+ this.scoreLookup,
|
|
|
288
|
+ )
|
|
|
289
|
+ // Order by score
|
|
|
290
|
+ return scoredProfilesWithDistance.sort(
|
|
|
291
|
+ (a, b) => b.score.total - a.score.total,
|
|
|
292
|
+ )
|
|
|
293
|
+ }
|
|
|
294
|
+
|
|
|
295
|
+ async calcProfileDistances(matchPool, distanceUnit, userZip) {
|
|
|
296
|
+ await Promise.all(
|
|
|
297
|
+ matchPool.map(async profile => {
|
|
|
298
|
+ const targetZip = zipcoder.getZipCodeFromProfile(profile)
|
|
280
|
299
|
if (!userZip || !targetZip)
|
|
281
|
300
|
return { ...profile, distance: [9999, distanceUnit] }
|
|
282
|
301
|
|
|
|
@@ -291,20 +310,6 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
291
|
310
|
}
|
|
292
|
311
|
}),
|
|
293
|
312
|
)
|
|
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,
|
|
307
|
|
- )
|
|
308
|
313
|
}
|
|
309
|
314
|
|
|
310
|
315
|
/**
|