Refactor agent profile field processing by removing section-based filtering from agent search and enhancing field selection logic for agent profile updates.

This commit is contained in:
pradeepkumar
2026-03-04 22:11:48 +05:30
parent d4cff1ada8
commit 0defcb24fb

View File

@@ -321,21 +321,12 @@ export class AgentsService {
const filterSlugs = Object.keys(parsedFilters); const filterSlugs = Object.keys(parsedFilters);
if (filterSlugs.length > 0) { if (filterSlugs.length > 0) {
// Get field IDs for the filter slugs, including section info for assignment validation // Get field IDs for the filter slugs
const fields = await this.prisma.profileField.findMany({ const fields = await this.prisma.profileField.findMany({
where: { slug: { in: filterSlugs } }, where: { slug: { in: filterSlugs } },
select: { select: {
id: true, id: true,
slug: true, slug: true,
sectionId: true,
section: {
select: {
isGlobal: true,
agentTypeSections: {
select: { agentTypeId: true },
},
},
},
}, },
}); });
@@ -357,40 +348,25 @@ export class AgentsService {
// Search across ALL fields with this slug (e.g. loan_type in both Professional and Lender sections) // Search across ALL fields with this slug (e.g. loan_type in both Professional and Lender sections)
const allMatchingAgentIds: string[] = []; const allMatchingAgentIds: string[] = [];
const fieldIds = slugFields.map((f) => f.id);
for (const field of slugFields) { const matchingValues = await this.prisma.agentProfileFieldValue.findMany({
const isGlobal = field.section.isGlobal; where: { fieldId: { in: fieldIds } },
const allowedAgentTypeIds = new Set( select: {
field.section.agentTypeSections.map((ats) => ats.agentTypeId), agentProfileId: true,
); jsonValue: true,
},
});
const matchingValues = await this.prisma.agentProfileFieldValue.findMany({ const matchingIds = matchingValues
where: { fieldId: field.id }, .filter((fv) => {
select: { const jsonVal = fv.jsonValue as string[] | null;
agentProfileId: true, if (!jsonVal || !Array.isArray(jsonVal)) return false;
jsonValue: true, return selectedValues.some((sv) => jsonVal.includes(sv));
agentProfile: { })
select: { agentTypeId: true }, .map((fv) => fv.agentProfileId);
},
},
});
const matchingIds = matchingValues allMatchingAgentIds.push(...matchingIds);
.filter((fv) => {
const jsonVal = fv.jsonValue as string[] | null;
if (!jsonVal || !Array.isArray(jsonVal)) return false;
if (!selectedValues.some((sv) => jsonVal.includes(sv))) return false;
const agentTypeId = fv.agentProfile.agentTypeId;
if (!isGlobal && (!agentTypeId || !allowedAgentTypeIds.has(agentTypeId))) {
return false;
}
return true;
})
.map((fv) => fv.agentProfileId);
allMatchingAgentIds.push(...matchingIds);
}
filterConditions.push([...new Set(allMatchingAgentIds)]); filterConditions.push([...new Set(allMatchingAgentIds)]);
} }
@@ -697,17 +673,34 @@ export class AgentsService {
}; };
}> = []; }> = [];
for (const fieldValue of dto.fieldValues) { for (const fieldValue of dto.fieldValues) {
// Find the field by slug // Find the field by slug, preferring the one from a section assigned to this agent's type
const field = await this.prisma.profileField.findFirst({ const candidateFields = await this.prisma.profileField.findMany({
where: { slug: fieldValue.fieldSlug }, where: { slug: fieldValue.fieldSlug },
include: { section: true }, include: {
section: {
include: {
agentTypeSections: { select: { agentTypeId: true } },
},
},
},
}); });
if (!field) { if (candidateFields.length === 0) {
// Skip fields that don't exist // Skip fields that don't exist
continue; continue;
} }
// Pick the field from a section assigned to this agent's type, or global, or fallback to first
const field =
candidateFields.find(
(f) =>
f.section.agentTypeSections.some(
(ats) => ats.agentTypeId === profile.agentTypeId,
),
) ||
candidateFields.find((f) => f.section.isGlobal) ||
candidateFields[0];
// Determine which column to use based on value type and field type // Determine which column to use based on value type and field type
const valueData = this.getValueData(field.fieldType, fieldValue.value); const valueData = this.getValueData(field.fieldType, fieldValue.value);