diff --git a/src/agents/agents.service.ts b/src/agents/agents.service.ts index ba3dda2..66c2c40 100644 --- a/src/agents/agents.service.ts +++ b/src/agents/agents.service.ts @@ -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,40 +348,25 @@ export class AgentsService { // Search across ALL fields with this slug (e.g. loan_type in both Professional and Lender sections) const allMatchingAgentIds: string[] = []; + const fieldIds = slugFields.map((f) => f.id); - for (const field of slugFields) { - const isGlobal = field.section.isGlobal; - const allowedAgentTypeIds = new Set( - field.section.agentTypeSections.map((ats) => ats.agentTypeId), - ); + const matchingValues = await this.prisma.agentProfileFieldValue.findMany({ + where: { fieldId: { in: fieldIds } }, + select: { + agentProfileId: true, + jsonValue: true, + }, + }); - const matchingValues = await this.prisma.agentProfileFieldValue.findMany({ - where: { fieldId: field.id }, - select: { - agentProfileId: true, - jsonValue: true, - agentProfile: { - select: { agentTypeId: true }, - }, - }, - }); + const matchingIds = matchingValues + .filter((fv) => { + const jsonVal = fv.jsonValue as string[] | null; + if (!jsonVal || !Array.isArray(jsonVal)) return false; + return selectedValues.some((sv) => jsonVal.includes(sv)); + }) + .map((fv) => fv.agentProfileId); - const matchingIds = matchingValues - .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); - } + 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);