feat: add optional agentTypeId query parameter to filter fields by agent type scope

This commit is contained in:
pradeepkumar
2026-04-16 16:45:57 +05:30
parent d7a8da539a
commit 4b36591812
2 changed files with 25 additions and 18 deletions

View File

@@ -27,8 +27,9 @@ export class ProfileFieldsController {
@Public() @Public()
@Get('filterable') @Get('filterable')
@ApiOperation({ summary: 'Get all filterable fields with options (Public)' }) @ApiOperation({ summary: 'Get all filterable fields with options (Public)' })
getFilterableFields() { @ApiQuery({ name: 'agentTypeId', required: false, description: 'Narrow filters to a specific agent type — excludes sections from other agent types' })
return this.fieldsService.getFilterableFields(); getFilterableFields(@Query('agentTypeId') agentTypeId?: string) {
return this.fieldsService.getFilterableFields(agentTypeId);
} }
@Post() @Post()

View File

@@ -235,7 +235,7 @@ export class ProfileFieldsService {
* Only includes fields from sections that are global or assigned to at least one agent type * 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 * De-duplicates by both slug AND name to handle duplicate fields with different slugs
*/ */
async getFilterableFields() { async getFilterableFields(agentTypeId?: string) {
const filterableTypes: FieldType[] = [ const filterableTypes: FieldType[] = [
FieldType.CHECKBOX_GROUP, FieldType.CHECKBOX_GROUP,
FieldType.SELECT, FieldType.SELECT,
@@ -243,19 +243,31 @@ export class ProfileFieldsService {
FieldType.RADIO, // experience-style single-choice fields (years_in_business, contracts_completed) FieldType.RADIO, // experience-style single-choice fields (years_in_business, contracts_completed)
]; ];
const fields = await this.prisma.profileField.findMany({ // When agentTypeId is provided, scope to sections that are global OR assigned to that
where: { // 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, isActive: true,
fieldType: { in: filterableTypes }, OR: [
options: { not: Prisma.JsonNull }, { isGlobal: true },
// Only include fields from sections that are global or assigned to at least one agent type { agentTypeSections: { some: { agentTypeId } } },
section: { ],
}
: {
isActive: true, isActive: true,
OR: [ OR: [
{ isGlobal: true }, { isGlobal: true },
{ agentTypeSections: { some: {} } }, { agentTypeSections: { some: {} } },
], ],
}, };
const fields = await this.prisma.profileField.findMany({
where: {
isActive: true,
fieldType: { in: filterableTypes },
options: { not: Prisma.JsonNull },
section: sectionFilter,
}, },
select: { select: {
id: true, id: true,
@@ -346,13 +358,7 @@ export class ProfileFieldsService {
slug: 'expertise_areas', slug: 'expertise_areas',
isActive: true, isActive: true,
fieldType: FieldType.TAG_INPUT, fieldType: FieldType.TAG_INPUT,
section: { section: sectionFilter,
isActive: true,
OR: [
{ isGlobal: true },
{ agentTypeSections: { some: {} } },
],
},
}, },
select: { id: true, name: true, slug: true, fieldType: true }, select: { id: true, name: true, slug: true, fieldType: true },
}); });