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()
@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()

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
* 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: {
// 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,
fieldType: { in: filterableTypes },
options: { not: Prisma.JsonNull },
// Only include fields from sections that are global or assigned to at least one agent type
section: {
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 },
});