From 1fce3ced10e4682f2650bf47d28a78f92e806dca Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Sat, 28 Mar 2026 20:09:03 +0530 Subject: [PATCH] fix --- src/agents/agents.service.ts | 78 +++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/src/agents/agents.service.ts b/src/agents/agents.service.ts index f6a8079..3e49e7b 100644 --- a/src/agents/agents.service.ts +++ b/src/agents/agents.service.ts @@ -519,8 +519,84 @@ export class AgentsService { this.prisma.agentProfile.count({ where }), ]); + // Compute match score for each agent based on search context + const agentsWithScore = agents.map((agent: any) => { + let score = 0; + let maxScore = 0; + + // 1. Profile completeness (15 points) + maxScore += 15; + score += Math.round(((agent.profileCompleteness || 0) / 100) * 15); + + // 2. Verification (15 points) + maxScore += 15; + if (agent.isVerified) score += 15; + + // 3. Rating (15 points) - normalized from 0-5 scale + maxScore += 15; + if (agent.averageRating) { + score += Math.round((agent.averageRating / 5) * 15); + } + + // 4. Location match (15 points) + maxScore += 15; + if (city && agent.city?.toLowerCase() === city.toLowerCase()) { + score += 15; + } else if (state && agent.state?.toLowerCase() === state.toLowerCase()) { + score += 10; + } else if (country && agent.country?.toLowerCase() === country.toLowerCase()) { + score += 5; + } else if (location) { + const loc = location.toLowerCase(); + 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) { + try { + const parsedFilters: Record = JSON.parse(filters); + const filterKeys = Object.keys(parsedFilters).filter(k => parsedFilters[k]?.length > 0); + if (filterKeys.length > 0) { + let matchedFilters = 0; + for (const filterSlug of filterKeys) { + const selectedValues = parsedFilters[filterSlug]; + const agentFieldValues = (agent.fieldValues || []) as any[]; + const fieldMatch = agentFieldValues.some((fv: any) => { + if (fv.field?.slug !== filterSlug) return false; + const jsonVal = fv.jsonValue as string[] | null; + if (jsonVal && Array.isArray(jsonVal)) { + return selectedValues.some(sv => jsonVal.includes(sv)); + } + return false; + }); + if (fieldMatch) matchedFilters++; + } + score += Math.round((matchedFilters / filterKeys.length) * 40); + } else { + score += 20; // No specific filters, give base + } + } catch { + score += 20; + } + } else { + score += 20; // No filters applied, give base + } + + // Calculate percentage (minimum 50% for any approved agent) + const rawPercentage = maxScore > 0 ? Math.round((score / maxScore) * 100) : 50; + const matchPercentage = Math.max(50, Math.min(99, rawPercentage)); + + return { ...agent, matchPercentage }; + }); + return { - data: agents, + data: agentsWithScore, meta: { total, page,