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);
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({
where: { slug: { in: filterSlugs } },
select: {
id: true,
slug: true,
sectionId: true,
section: {
select: {
isGlobal: true,
agentTypeSections: {
select: { agentTypeId: true },
},
},
},
},
});
@@ -357,21 +348,13 @@ export class AgentsService {
// Search across ALL fields with this slug (e.g. loan_type in both Professional and Lender sections)
const allMatchingAgentIds: string[] = [];
for (const field of slugFields) {
const isGlobal = field.section.isGlobal;
const allowedAgentTypeIds = new Set(
field.section.agentTypeSections.map((ats) => ats.agentTypeId),
);
const fieldIds = slugFields.map((f) => f.id);
const matchingValues = await this.prisma.agentProfileFieldValue.findMany({
where: { fieldId: field.id },
where: { fieldId: { in: fieldIds } },
select: {
agentProfileId: true,
jsonValue: true,
agentProfile: {
select: { agentTypeId: true },
},
},
});
@@ -379,18 +362,11 @@ export class AgentsService {
.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;
return selectedValues.some((sv) => jsonVal.includes(sv));
})
.map((fv) => fv.agentProfileId);
allMatchingAgentIds.push(...matchingIds);
}
filterConditions.push([...new Set(allMatchingAgentIds)]);
}
@@ -697,17 +673,34 @@ export class AgentsService {
};
}> = [];
for (const fieldValue of dto.fieldValues) {
// Find the field by slug
const field = await this.prisma.profileField.findFirst({
// Find the field by slug, preferring the one from a section assigned to this agent's type
const candidateFields = await this.prisma.profileField.findMany({
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
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
const valueData = this.getValueData(field.fieldType, fieldValue.value);