This commit is contained in:
pradeepkumar
2026-03-28 20:09:03 +05:30
parent a71145f6a6
commit 1fce3ced10

View File

@@ -519,8 +519,84 @@ export class AgentsService {
this.prisma.agentProfile.count({ where }), 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<string, string[]> = 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 { return {
data: agents, data: agentsWithScore,
meta: { meta: {
total, total,
page, page,