|
|
@@ -5,7 +5,7 @@ const magic = 1000
|
|
5
|
5
|
const scoreResponses = (seeker, potentialMatch) => {
|
|
6
|
6
|
if (seeker.responses.length != potentialMatch.responses.length)
|
|
7
|
7
|
return {
|
|
8
|
|
- error: `complete responses for profile: ${seeker.profile_id} unqeual to profile: ${potentialMatch.profile_id}`,
|
|
|
8
|
+ error: `complete responses for profile: ${seeker.profile_id} unqeual to profile: ${potentialMatch.profile_id} | ${seeker.responses.length}:${potentialMatch.responses.length}`,
|
|
9
|
9
|
}
|
|
10
|
10
|
|
|
11
|
11
|
const checkValCb = res => {
|
|
|
@@ -19,10 +19,6 @@ const scoreResponses = (seeker, potentialMatch) => {
|
|
19
|
19
|
) * magic,
|
|
20
|
20
|
)
|
|
21
|
21
|
}
|
|
22
|
|
-const userTypes = {
|
|
23
|
|
- seeker: 'seeker',
|
|
24
|
|
- poster: 'poster',
|
|
25
|
|
-}
|
|
26
|
22
|
|
|
27
|
23
|
/**
|
|
28
|
24
|
* Class to hold our retrieved profile information
|
|
|
@@ -150,26 +146,31 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
150
|
146
|
* @returns {Array} Ordered and scored Profiles
|
|
151
|
147
|
*/
|
|
152
|
148
|
async scoreProfilesFor(userId) {
|
|
153
|
|
- const { User } = this.server.models(['user'])
|
|
154
|
149
|
const { Profile } = this.server.models()
|
|
155
|
150
|
|
|
156
|
|
- const user = await User.query().findOne('user_id', userId)
|
|
|
151
|
+ // Our User Profile to score for
|
|
157
|
152
|
const userProfile = await Profile.query()
|
|
158
|
153
|
.findOne('user_id', userId)
|
|
159
|
154
|
.withGraphFetched('responses')
|
|
160
|
|
- const isPosterOpposite = user.is_poster == 1 ? 0 : 1
|
|
161
|
|
- const userType = Object.keys(userTypes)[isPosterOpposite]
|
|
|
155
|
+ .withGraphFetched('user')
|
|
|
156
|
+
|
|
|
157
|
+ const isPosterOpposite = userProfile.user.is_poster == 1 ? 0 : 1
|
|
|
158
|
+
|
|
|
159
|
+ // Find all Profiles that are NOT of our userProfile.type
|
|
|
160
|
+ // ie. If userProfile.type == seeker, then find: poster
|
|
|
161
|
+ let profileIdsOfOppositeType = await Profile.query()
|
|
|
162
|
+ .withGraphFetched('responses')
|
|
|
163
|
+ .withGraphFetched('user')
|
|
162
|
164
|
|
|
163
|
|
- const profileIdsOfOppositeType = await Profile.query().withGraphFetched(
|
|
164
|
|
- 'responses',
|
|
|
165
|
+ // TODO: Let Objection optimize this
|
|
|
166
|
+ profileIdsOfOppositeType = profileIdsOfOppositeType.filter(
|
|
|
167
|
+ profile => profile.user.is_poster == isPosterOpposite,
|
|
165
|
168
|
)
|
|
166
|
169
|
|
|
167
|
|
- return profileIdsOfOppositeType.map(profile => ({
|
|
|
170
|
+ const scored = profileIdsOfOppositeType.map(profile => ({
|
|
168
|
171
|
profile_id: profile.profile_id,
|
|
169
|
|
- score: scoreResponses(
|
|
170
|
|
- userProfile,
|
|
171
|
|
- new CompleteProfile(profile, userType),
|
|
172
|
|
- ),
|
|
|
172
|
+ score: scoreResponses(userProfile, profile),
|
|
173
|
173
|
}))
|
|
|
174
|
+ return scored.sort((a, b) => a.score - b.score)
|
|
174
|
175
|
}
|
|
175
|
176
|
}
|