refactor: disable agent match scoring and sorting when no search filters are active and strip privacy preferences from user objects

This commit is contained in:
pradeepkumar
2026-04-13 16:51:47 +05:30
parent e1a364dc5b
commit 7be1b03383

View File

@@ -605,8 +605,20 @@ export class AgentsService {
return true; // 'public' or no setting
});
// Compute match score for each agent based on search context
// Determine if any search/filter criteria is active
const hasActiveSearch = !!(search || location || city || state || country || agentTypeId || filters);
// Compute match score only when user has applied search/filter criteria
const agentsWithScore = visibleAgents.map((agent: any) => {
// Strip privacyPreferences from response
const { user, ...agentWithoutUser } = agent;
const { privacyPreferences, ...userWithoutPrefs } = user || {};
// No scoring when browsing without filters
if (!hasActiveSearch) {
return { ...agentWithoutUser, user: userWithoutPrefs, matchPercentage: null };
}
let score = 0;
let maxScore = 0;
@@ -637,14 +649,11 @@ export class AgentsService {
if (agent.city?.toLowerCase().includes(loc) || agent.state?.toLowerCase().includes(loc)) {
score += 10;
}
} else {
// No location filter — give base points
score += 8;
}
// 5. Filter match (40 points) - how well dynamic field values match search filters
maxScore += 40;
if (filters) {
maxScore += 40;
try {
const parsedFilters: Record<string, string[]> = JSON.parse(filters);
const filterKeys = Object.keys(parsedFilters).filter(k => parsedFilters[k]?.length > 0);
@@ -664,28 +673,23 @@ export class AgentsService {
if (fieldMatch) matchedFilters++;
}
score += Math.round((matchedFilters / filterKeys.length) * 40);
} else {
score += 20; // No specific filters, give base
}
} catch {
score += 20;
// Invalid JSON, skip filter scoring
}
} else {
score += 20; // No filters applied, give base
}
// Calculate percentage (minimum 50% for any approved agent)
// Calculate percentage (minimum 50% for any matched agent)
const rawPercentage = maxScore > 0 ? Math.round((score / maxScore) * 100) : 50;
const matchPercentage = Math.max(50, Math.min(99, rawPercentage));
// Strip privacyPreferences from response
const { user, ...agentWithoutUser } = agent;
const { privacyPreferences, ...userWithoutPrefs } = user || {};
return { ...agentWithoutUser, user: userWithoutPrefs, matchPercentage };
});
// Sort by match percentage (highest first)
agentsWithScore.sort((a: any, b: any) => b.matchPercentage - a.matchPercentage);
// Sort by match percentage (highest first) only when scoring is active
if (hasActiveSearch) {
agentsWithScore.sort((a: any, b: any) => (b.matchPercentage || 0) - (a.matchPercentage || 0));
}
return {
data: agentsWithScore,