From 7be1b03383665a245421c6392275d279f5897928 Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Mon, 13 Apr 2026 16:51:47 +0530 Subject: [PATCH] refactor: disable agent match scoring and sorting when no search filters are active and strip privacy preferences from user objects --- src/agents/agents.service.ts | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/agents/agents.service.ts b/src/agents/agents.service.ts index 09fa43d..59d6f6c 100644 --- a/src/agents/agents.service.ts +++ b/src/agents/agents.service.ts @@ -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 = 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,