diff --git a/src/profile-fields/profile-fields.controller.ts b/src/profile-fields/profile-fields.controller.ts index 21c3821..803ffa0 100644 --- a/src/profile-fields/profile-fields.controller.ts +++ b/src/profile-fields/profile-fields.controller.ts @@ -27,8 +27,9 @@ export class ProfileFieldsController { @Public() @Get('filterable') @ApiOperation({ summary: 'Get all filterable fields with options (Public)' }) - getFilterableFields() { - return this.fieldsService.getFilterableFields(); + @ApiQuery({ name: 'agentTypeId', required: false, description: 'Narrow filters to a specific agent type — excludes sections from other agent types' }) + getFilterableFields(@Query('agentTypeId') agentTypeId?: string) { + return this.fieldsService.getFilterableFields(agentTypeId); } @Post() diff --git a/src/profile-fields/profile-fields.service.ts b/src/profile-fields/profile-fields.service.ts index 04ab87a..c17d02a 100644 --- a/src/profile-fields/profile-fields.service.ts +++ b/src/profile-fields/profile-fields.service.ts @@ -235,7 +235,7 @@ export class ProfileFieldsService { * Only includes fields from sections that are global or assigned to at least one agent type * De-duplicates by both slug AND name to handle duplicate fields with different slugs */ - async getFilterableFields() { + async getFilterableFields(agentTypeId?: string) { const filterableTypes: FieldType[] = [ FieldType.CHECKBOX_GROUP, FieldType.SELECT, @@ -243,19 +243,31 @@ export class ProfileFieldsService { FieldType.RADIO, // experience-style single-choice fields (years_in_business, contracts_completed) ]; - const fields = await this.prisma.profileField.findMany({ - where: { - isActive: true, - fieldType: { in: filterableTypes }, - options: { not: Prisma.JsonNull }, - // Only include fields from sections that are global or assigned to at least one agent type - section: { + // When agentTypeId is provided, scope to sections that are global OR assigned to that + // specific agent type. Without it, fall back to sections that are global OR assigned to + // any agent type (previous behavior). + const sectionFilter = agentTypeId + ? { + isActive: true, + OR: [ + { isGlobal: true }, + { agentTypeSections: { some: { agentTypeId } } }, + ], + } + : { isActive: true, OR: [ { isGlobal: true }, { agentTypeSections: { some: {} } }, ], - }, + }; + + const fields = await this.prisma.profileField.findMany({ + where: { + isActive: true, + fieldType: { in: filterableTypes }, + options: { not: Prisma.JsonNull }, + section: sectionFilter, }, select: { id: true, @@ -346,13 +358,7 @@ export class ProfileFieldsService { slug: 'expertise_areas', isActive: true, fieldType: FieldType.TAG_INPUT, - section: { - isActive: true, - OR: [ - { isGlobal: true }, - { agentTypeSections: { some: {} } }, - ], - }, + section: sectionFilter, }, select: { id: true, name: true, slug: true, fieldType: true }, });