feat: add optional agentTypeId query parameter to filter fields by agent type scope
This commit is contained in:
@@ -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()
|
||||||
|
|||||||
@@ -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
|
||||||
isActive: true,
|
// any agent type (previous behavior).
|
||||||
fieldType: { in: filterableTypes },
|
const sectionFilter = agentTypeId
|
||||||
options: { not: Prisma.JsonNull },
|
? {
|
||||||
// Only include fields from sections that are global or assigned to at least one agent type
|
isActive: true,
|
||||||
section: {
|
OR: [
|
||||||
|
{ isGlobal: true },
|
||||||
|
{ agentTypeSections: { some: { agentTypeId } } },
|
||||||
|
],
|
||||||
|
}
|
||||||
|
: {
|
||||||
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 },
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user