Enhance getCityByState handler to support optional search term with validation and update service method to filter cities accordingly.
This commit is contained in:
@@ -27,15 +27,21 @@ export const handler = safeHandler(async (
|
||||
// 2) Authenticate user
|
||||
await verifyMinglarAdminHostToken(token);
|
||||
|
||||
// 3) Get bankXid from query params
|
||||
// 3) Get stateXid and optional search term from query params
|
||||
const stateXid = Number(event.queryStringParameters?.stateXid);
|
||||
const search = event.queryStringParameters?.search?.trim();
|
||||
|
||||
if (!stateXid || isNaN(stateXid)) {
|
||||
throw new ApiError(400, "Valid stateXid is required in query params.");
|
||||
}
|
||||
|
||||
// 4) Fetch branches for the bank
|
||||
const branches = await prePopulateService.getCityByStateId(stateXid);
|
||||
// If search is provided, enforce minimum 3 characters
|
||||
if (search && search.length < 3) {
|
||||
throw new ApiError(400, "Search term must be at least 3 characters long.");
|
||||
}
|
||||
|
||||
// 4) Fetch cities for the state (optionally filtered by search)
|
||||
const branches = await prePopulateService.getCityByStateId(stateXid, search);
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
|
||||
@@ -39,12 +39,20 @@ export class PrePopulateService {
|
||||
}
|
||||
|
||||
|
||||
async getCityByStateId(stateXid: number) {
|
||||
async getCityByStateId(stateXid: number, search?: string) {
|
||||
return await this.prisma.cities.findMany({
|
||||
where: {
|
||||
stateXid,
|
||||
isActive: true,
|
||||
deletedAt: null
|
||||
deletedAt: null,
|
||||
...(search && search.length >= 3
|
||||
? {
|
||||
cityName: {
|
||||
contains: search,
|
||||
mode: 'insensitive',
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
|
||||
Reference in New Issue
Block a user