Exclude null compatibility scores when ordering profiles by compatibility. Update compute-scores logic to delete invalid score rows instead of setting them to null. Add corresponding test coverage.

This commit is contained in:
MartinBraquet
2026-07-26 12:40:08 +02:00
parent 680431ed75
commit 833b82feea
3 changed files with 24 additions and 3 deletions

View File

@@ -659,6 +659,10 @@ export const loadProfiles = async (props: profileQueryType, db?: SupabaseDirectC
: '90 days',
}),
// Precomputed scores can be nulled out (e.g. after the user deletes their last shared
// prompt) instead of the row being removed — don't surface those as matches.
orderByParam === 'compatibility_score' && where('cs.score IS NOT NULL'),
// Exclude profiles that the requester has chosen to hide
userId &&
where(

View File

@@ -319,4 +319,20 @@ describe('loadProfiles', () => {
expect(profilesModule.loadProfiles(props)).rejects.toThrowError('Incompatible with user ID')
})
})
describe('when ordering by compatibility score', () => {
it('excludes profiles whose precomputed score was nulled out', async () => {
;(mockPg.map as jest.Mock).mockResolvedValue([])
;(mockPg.one as jest.Mock).mockResolvedValue(0)
await profilesModule.loadProfiles({
orderBy: 'compatibility_score',
compatibleWithUserId: 'user-1',
})
const [query] = mockPg.map.mock.calls[0]
expect(query).toContain('cs.score IS NOT NULL')
})
})
})

View File

@@ -29,11 +29,12 @@ export async function recomputeCompatibilityScoresForUser(
// Load all answers for the target user
const answersSelf = await getAnswersForUser(userId)
// If the user has no answered questions, set the score to null
// If the user has no answered questions, there's no valid score to keep for any pair
// involving them — remove the rows rather than nulling them out, so a row's existence
// always means "we have a valid score for this pair".
if (!hasAnsweredQuestions(answersSelf)) {
await pg.none(
`update compatibility_scores
set score = null
`delete from compatibility_scores
where user_id_1 = $1
or user_id_2 = $1`,
[userId],