diff --git a/src/controllers/ExRetirementController.ts b/src/controllers/ExRetirementController.ts index 880a8477..c8174e6f 100644 --- a/src/controllers/ExRetirementController.ts +++ b/src/controllers/ExRetirementController.ts @@ -168,3 +168,65 @@ async function getToken(ClientID: string, ClientSecret: string): Promise return Promise.reject({ message: "Error occurred", error }); } } + +// function post retire data to exprofile system +export async function PostRetireToExprofile( + citizenID: string, + prefix: string, + firstName: string, + lastName: string, + retireYear: string, + positionName: string, + positionTypeName: string, + positionLevelName: string, + retireDate: Date, + organizeName: string, // child4Name child3Name child2Name child1Name rootName + retireTypeName: string, // เช่น เกษียณ, ขอโอนออก, ลาออก, ปลดออก, ไล่ออก, ... +) { + 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 = "importOfficerRetireData"; + const body = { + scope: scope, + data: { + citizenID: citizenID, + prenameTH: prefix, + firstNameTH: firstName, + lastNameTH: lastName, + retireYear, + positionNameTH: positionName, + positionTypeNameTH: positionTypeName, + positionLevelNameTH: positionLevelName, + retireDate, + organizeNameTH: organizeName, + retireTypeNameTH: retireTypeName, + }, + }; + + const res = await axios.post(API_URL_BANGKOK + "/importData", body, { + headers: { + Authorization: `Bearer ${token}`, + "Content-Type": "application/json", + }, + }); + + 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 ได้"); + } + } +}