-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4902 bug fix fix api filter for enum (#4909)
- Handle NUMERIC, SELECT, PROBABILITY, RATING FieldMetadataTypes Those filters now works: - http://localhost:3000/rest/opportunities?filter=stage[eq]:MEETING - http://localhost:3000/rest/opportunities?filter=stage[in]:[MEETING,NEW] When providing wrong enum values, the following error messages are returned: - http://localhost:3000/rest/opportunities?filter=stage[eq]:MEETINGG > BadRequestException: 'filter' enum value 'MEETINGG' not available in 'stage' enum. Available enum values are ['NEW', 'SCREENING', 'MEETING', 'PROPOSAL', 'CUSTOMER'] - http://localhost:3000/rest/opportunities?filter=stage[in]:[MEETING,NEWW] > BadRequestException: 'filter' enum value 'NEWW' not available in 'stage' enum. Available enum values are ['NEW', 'SCREENING', 'MEETING', 'PROPOSAL', 'CUSTOMER']
- Loading branch information
Showing
7 changed files
with
122 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...builder/factories/input-factories/filter-utils/__tests__/check-filter-enum-values.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { checkFilterEnumValues } from 'src/engine/api/rest/api-rest-query-builder/factories/input-factories/filter-utils/check-filter-enum-values'; | ||
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity'; | ||
import { | ||
fieldSelectMock, | ||
objectMetadataItemMock, | ||
} from 'src/engine/api/__mocks__/object-metadata-item.mock'; | ||
|
||
describe('checkFilterEnumValues', () => { | ||
it('should check properly', () => { | ||
expect(() => | ||
checkFilterEnumValues( | ||
FieldMetadataType.SELECT, | ||
fieldSelectMock.name, | ||
'OPTION_1', | ||
objectMetadataItemMock, | ||
), | ||
).not.toThrow(); | ||
|
||
expect(() => | ||
checkFilterEnumValues( | ||
FieldMetadataType.SELECT, | ||
fieldSelectMock.name, | ||
'MISSING_OPTION', | ||
objectMetadataItemMock, | ||
), | ||
).toThrow( | ||
`'filter' enum value 'MISSING_OPTION' not available in '${fieldSelectMock.name}' enum. Available enum values are ['OPTION_1', 'OPTION_2']`, | ||
); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
...api-rest-query-builder/factories/input-factories/filter-utils/check-filter-enum-values.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
|
||
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface'; | ||
|
||
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity'; | ||
|
||
export const checkFilterEnumValues = ( | ||
fieldType: FieldMetadataType | undefined, | ||
fieldName: string, | ||
value: string, | ||
objectMetadataItem: ObjectMetadataInterface, | ||
): void => { | ||
if ( | ||
!fieldType || | ||
![FieldMetadataType.MULTI_SELECT, FieldMetadataType.SELECT].includes( | ||
fieldType, | ||
) | ||
) { | ||
return; | ||
} | ||
const field = objectMetadataItem.fields.find( | ||
(field) => field.name === fieldName, | ||
); | ||
|
||
const values = /^\[.*\]$/.test(value) | ||
? value.slice(1, -1).split(',') | ||
: [value]; | ||
const enumValues = field?.options?.map((option) => option.value); | ||
|
||
if (!enumValues) { | ||
return; | ||
} | ||
values.forEach((val) => { | ||
if (!enumValues.includes(val)) { | ||
throw new BadRequestException( | ||
`'filter' enum value '${val}' not available in '${fieldName}' enum. Available enum values are ['${enumValues.join( | ||
"', '", | ||
)}']`, | ||
); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters