api run corn job retire

This commit is contained in:
mamoss 2025-03-25 14:08:17 +07:00
parent 9e1ebf6de1
commit cd2cd1645b

View file

@ -1249,6 +1249,101 @@ export class CommandController extends Controller {
return new HttpSuccess();
}
/**
* API tab3
*
* @summary API tab3
*
* @param {string} id Id
*/
@Get("run/cronjob/retirement")
async CreateCronjobUpdateRetirementStatus(@Request() request: RequestWithUser) {
let body = {
client_id: "gettoken",
client_secret: process.env.AUTH_ACCOUNT_SECRET,
grant_type: "client_credentials",
};
const postData = querystring.stringify(body);
const response = await axios.post(
`${process.env.KC_URL}/realms/${process.env.KC_REALMS}/protocol/openid-connect/token`,
postData,
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
api_key: process.env.API_KEY,
},
},
);
const adminToken = response.data.access_token;
const today = new Date();
today.setUTCHours(0, 0, 0, 0);
let type: string = "OFFICER";
try {
const response_ = await axios.get(
process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`,
{
headers: {
Authorization: `Bearer ${adminToken}`,
"Content-Type": "application/json",
api_key: process.env.API_KEY,
},
},
);
if (response && response_.data.result.length > 0) {
let profiles: Profile[] = [];
await Promise.all(
response_.data.result.map(async (x: any) => {
const _profile = await this.profileRepository.findOneBy({ id: x.profileId });
if (_profile) {
_profile.isRetirement = true;
_profile.isLeave = true;
_profile.leaveType = "RETIRE";
_profile.leaveDate = new Date();
_profile.dateLeave = new Date();
_profile.lastUpdatedAt = new Date();
profiles.push(_profile);
}
}),
);
await this.profileRepository.save(profiles);
}
} catch {}
type = "EMPLOYEE";
try {
const response_ = await axios.get(
process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`,
{
headers: {
Authorization: `Bearer ${adminToken}`,
"Content-Type": "application/json",
api_key: process.env.API_KEY,
},
},
);
if (response && response_.data.result.length > 0) {
let profiles: ProfileEmployee[] = [];
await Promise.all(
response_.data.result.map(async (x: any) => {
const _profileEmp = await this.profileEmployeeRepository.findOneBy({ id: x.profileId });
if (_profileEmp) {
_profileEmp.isRetirement = true;
_profileEmp.isLeave = true;
_profileEmp.leaveType = "RETIRE";
_profileEmp.leaveDate = new Date();
_profileEmp.dateLeave = new Date();
_profileEmp.lastUpdatedAt = new Date();
profiles.push(_profileEmp);
}
}),
);
await this.profileEmployeeRepository.save(profiles);
}
} catch {}
return new HttpSuccess();
}
async cronjobUpdateRetirementStatus() {
let body = {
client_id: "gettoken",