Merge branch 'develop' of github.com:Frappet/hrms-api-org into develop

This commit is contained in:
kittapath 2025-01-30 14:59:03 +07:00
commit c9e83aebc9
3 changed files with 151 additions and 54 deletions

View file

@ -57,6 +57,16 @@ async function main() {
} }
}); });
const cronTime_Oct = "0 0 1 10 *";
cron.schedule(cronTime_Oct, async () => {
try {
const commandController = new CommandController();
await commandController.cronjobUpdateRetirementStatus();
} catch (error) {
console.error("Error executing function from controller:", error);
}
});
// app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`)); // app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`));
app.listen( app.listen(
APP_PORT, APP_PORT,

View file

@ -1329,6 +1329,93 @@ export class CommandController extends Controller {
return new HttpSuccess(); return new HttpSuccess();
} }
async cronjobUpdateRetirementStatus() {
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();
}
/** /**
* API tab4 * API tab4
* *

View file

@ -1160,59 +1160,59 @@ export class OrganizationUnauthorizeController extends Controller {
return new HttpSuccess("Email verified successfully."); return new HttpSuccess("Email verified successfully.");
} }
@Patch("retirement") // @Patch("retirement")
public async updateStatusRetirement( // public async updateStatusRetirement(
@Body() // @Body()
body: { // body: {
data: { // data: {
profileId: string; // profileId: string;
}[]; // }[];
}, // },
) { // ) {
let profiles: Profile[] = []; // let profiles: Profile[] = [];
let _null: any = null; // let _null: any = null;
await Promise.all( // await Promise.all(
body.data.map(async (item) => { // body.data.map(async (item) => {
const _profile = await this.profileRepo.findOneBy({ id: item.profileId }); // const _profile = await this.profileRepo.findOneBy({ id: item.profileId });
if (!_profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); // if (!_profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
_profile.isRetirement = true; // _profile.isRetirement = true;
_profile.isLeave = true; // _profile.isLeave = true;
_profile.leaveType = "RETIRE"; // _profile.leaveType = "RETIRE";
_profile.leaveDate = new Date(); // _profile.leaveDate = new Date();
_profile.dateLeave = new Date(); // _profile.dateLeave = new Date();
_profile.lastUpdatedAt = new Date(); // _profile.lastUpdatedAt = new Date();
profiles.push(_profile); // profiles.push(_profile);
}) // })
); // );
await this.profileRepo.save(profiles); // await this.profileRepo.save(profiles);
return new HttpSuccess(); // return new HttpSuccess();
} // }
@Patch("retirement-employee") // @Patch("retirement-employee")
public async updateStatusRetirementEmp( // public async updateStatusRetirementEmp(
@Body() // @Body()
body: { // body: {
data: { // data: {
profileId: string; // profileId: string;
}[]; // }[];
}, // },
) { // ) {
let profiles: ProfileEmployee[] = []; // let profiles: ProfileEmployee[] = [];
let _null: any = null; // let _null: any = null;
await Promise.all( // await Promise.all(
body.data.map(async (item) => { // body.data.map(async (item) => {
const _profile = await this.profileEmpRepo.findOneBy({ id: item.profileId }); // const _profile = await this.profileEmpRepo.findOneBy({ id: item.profileId });
if (!_profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); // if (!_profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
_profile.isRetirement = true; // _profile.isRetirement = true;
_profile.isLeave = true; // _profile.isLeave = true;
_profile.leaveType = "RETIRE"; // _profile.leaveType = "RETIRE";
_profile.leaveDate = new Date(); // _profile.leaveDate = new Date();
_profile.dateLeave = new Date(); // _profile.dateLeave = new Date();
_profile.lastUpdatedAt = new Date(); // _profile.lastUpdatedAt = new Date();
profiles.push(_profile); // profiles.push(_profile);
}) // })
); // );
await this.profileEmpRepo.save(profiles); // await this.profileEmpRepo.save(profiles);
return new HttpSuccess(); // return new HttpSuccess();
} // }
} }