53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from "aws-lambda";
|
|
import { prismaClient } from "../../../common/database/prisma.lambda.service";
|
|
import { verifyMinglarAdminHostToken } from "../../../common/middlewares/jwt/authForMinglarAdminHost";
|
|
import { safeHandler } from "../../../common/utils/handlers/safeHandler";
|
|
import ApiError from "../../../common/utils/helper/ApiError";
|
|
import { PrePopulateService } from "../services/prepopulate.service";
|
|
|
|
const prePopulateService = new PrePopulateService(prismaClient);
|
|
|
|
export const handler = safeHandler(async (
|
|
event: APIGatewayProxyEvent,
|
|
context?: Context
|
|
): Promise<APIGatewayProxyResult> => {
|
|
|
|
// 1) Token check
|
|
const token =
|
|
event.headers["x-auth-token"] ||
|
|
event.headers["X-Auth-Token"];
|
|
|
|
if (!token) {
|
|
throw new ApiError(
|
|
400,
|
|
"This is a protected route. Please provide a valid token."
|
|
);
|
|
}
|
|
|
|
// 2) Authenticate user
|
|
await verifyMinglarAdminHostToken(token);
|
|
|
|
// 3) Get bankXid from query params
|
|
const bankXid = Number(event.queryStringParameters?.bankXid);
|
|
|
|
if (!bankXid || isNaN(bankXid)) {
|
|
throw new ApiError(400, "Valid bankXid is required in query params.");
|
|
}
|
|
|
|
// 4) Fetch branches for the bank
|
|
const branches = await prePopulateService.getBankBranchesByBankId(bankXid);
|
|
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Access-Control-Allow-Origin": "*",
|
|
},
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message: "Bank branches retrieved successfully",
|
|
data: branches
|
|
}),
|
|
};
|
|
});
|