126 lines
3.7 KiB
TypeScript
126 lines
3.7 KiB
TypeScript
import axios from "axios";
|
|
import {
|
|
Controller,
|
|
Post,
|
|
Delete,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
Body,
|
|
Path,
|
|
Request,
|
|
Response,
|
|
Get,
|
|
} from "tsoa";
|
|
import HttpError from "../interfaces/http-error";
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
|
|
interface CachedToken {
|
|
token: string;
|
|
expiry: Date;
|
|
}
|
|
const API_URL_BANGKOK = "https://exprofile.bangkok.go.th/API";
|
|
const clientId = "e5f6ad6ce374177eef023bf5d0c018b6";
|
|
const clientSecret = "5EhOvN5DwHOKakupqT9FmCk7MOwpT3zLqLPkPh4ZhJpxBN2nMG@2022";
|
|
|
|
class TokenCache {
|
|
private static cache: Map<string, CachedToken> = new Map();
|
|
|
|
static get(key: string): string | null {
|
|
const cached = this.cache.get(key);
|
|
if (!cached) return null;
|
|
return cached.token;
|
|
}
|
|
|
|
static set(key: string, token: string): void {
|
|
this.cache.set(key, { token, expiry: new Date(Date.now() + 50 * 60 * 1000) });
|
|
}
|
|
|
|
static delete(key: string): void {
|
|
this.cache.delete(key);
|
|
}
|
|
}
|
|
|
|
@Route("api/v1/org/ex/retirement")
|
|
@Tags("ExRetirement")
|
|
@Security("bearerAuth")
|
|
export class ExRetirementController extends Controller {
|
|
@Post()
|
|
async getExRetirement(
|
|
@Body()
|
|
requestBody: {
|
|
type: string; //ประเภท
|
|
retireYear: string; //ปีที่เกษียณ
|
|
citizenID: string; //เลขบัตรประชาชน
|
|
firstNameTH: string; //ชื่อ
|
|
lastNameTH: string; //นามสกุล
|
|
page: number; //หน้า
|
|
},
|
|
) {
|
|
let retryCount = 0;
|
|
const maxRetries = 2;
|
|
|
|
while (retryCount < maxRetries) {
|
|
try {
|
|
const token = await getToken(clientId, clientSecret);
|
|
|
|
if (!token) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถขอ Token ได้");
|
|
}
|
|
|
|
const scope = requestBody.type === "officer" ? "getOfficerRetireData" : "";
|
|
const startRecord = requestBody.page !== 1 ? (requestBody.page - 1) * 25 : 0;
|
|
|
|
const formData = new FormData();
|
|
formData.append("scope", scope);
|
|
formData.append("startRecord", startRecord.toString());
|
|
formData.append("retireYear", requestBody.retireYear);
|
|
formData.append("citizenID", requestBody.citizenID);
|
|
formData.append("firstNameTH", requestBody.firstNameTH);
|
|
formData.append("lastNameTH", requestBody.lastNameTH);
|
|
|
|
const res = await axios.post(API_URL_BANGKOK + "/getData", formData, {
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
});
|
|
|
|
return res.data;
|
|
} catch (error: any) {
|
|
if (error.response?.status === 500 && retryCount < maxRetries - 1) {
|
|
TokenCache.delete(`${clientId}:${clientSecret}`);
|
|
retryCount++;
|
|
continue;
|
|
}
|
|
throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ไม่สามารถติดต่อ API ได้");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async function getToken(ClientID: string, ClientSecret: string): Promise<string> {
|
|
const cacheKey = `${ClientID}:${ClientSecret}`;
|
|
|
|
// ลองหา token ใน cache ก่อน
|
|
const cachedToken = TokenCache.get(cacheKey);
|
|
if (cachedToken) {
|
|
return cachedToken;
|
|
}
|
|
|
|
// ถ้าไม่มีใน cache ให้ขอใหม่
|
|
try {
|
|
const formData = new FormData();
|
|
formData.append("ClientID", ClientID);
|
|
formData.append("ClientSecret", ClientSecret);
|
|
const res = await axios.post(API_URL_BANGKOK + "/authorize", formData, {
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
const token = res.data.token;
|
|
TokenCache.set(cacheKey, token);
|
|
return token;
|
|
} catch (error) {
|
|
return Promise.reject({ message: "Error occurred", error });
|
|
}
|
|
}
|