This commit is contained in:
Bright 2025-03-05 18:26:19 +07:00
parent 935ec5dae2
commit c5c8ccd8c0
4 changed files with 296 additions and 172 deletions

View file

@ -19,6 +19,7 @@ import HttpError from "../interfaces/http-error";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { In, LessThan, MoreThan } from "typeorm";
import permission from "../interfaces/permission";
import { setLogDataDiff } from "../interfaces/utils";
@ -31,57 +32,195 @@ import { TenureLevelEmployee } from "../entities/TenureLevelEmployee";
@Security("bearerAuth")
export class ProfileSalaryController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee);
private salaryRepo = AppDataSource.getRepository(ProfileSalary);
private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory);
private positionOfficerRepo = AppDataSource.getRepository(TenurePositionOfficer);
private positionEmployeeRepo = AppDataSource.getRepository(TenurePositionEmployee);
private levelOfficerRepo = AppDataSource.getRepository(TenureLevelOfficer);
// async cronjobTenurePositionOfficer() {
@Get("XXX")
private levelEmployeeRepo = AppDataSource.getRepository(TenureLevelEmployee);
@Get("TenurePositionOfficer")
public async cronjobTenurePositionOfficer() {
let data: any = [];
const profile = await this.profileRepo.find({
where: { id: "09e89026-c420-4136-bd9e-7e408f530b69" },
});
await Promise.all(
profile.map(async (x) => {
const sql_mode = await AppDataSource.query(
"SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));",
);
const positionOfficer = await this.positionOfficerRepo.find();
await this.positionOfficerRepo.remove(positionOfficer);
const profile = await this.profileRepo.find();
for await (const x of profile) {
const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?)", [x.id]);
const _position = position.length > 0 ? position[0] : [];
const mapPosition =
_position.length > 1
? _position.slice(1).map((curr: any, index: number) => ({
? _position.slice(1)
.map((curr: any, index: number) => ({
days_diff: curr.days_diff,
positionName: _position[index]?.positionName,
}))
}))
: [];
const calDayDiff = mapPosition
.filter((curr: any) => curr.positionName == x.position)
.reduce(
(acc: any, curr: any) => {
acc.days_diff += Number(curr.days_diff) || 0;
acc.positionName = curr.positionName
return acc;
},
{ days_diff: 0 },
{ days_diff: 0,positionName: null}
);
console.log("==========> ", calDayDiff);
const _mapData = {
const mapData:any = {
profileId: x.id,
positionName: x.position,
days_diff: null,
Years: null,
Months: null,
Days: null,
};
data.push(_mapData);
}),
positionName: calDayDiff.positionName,
days_diff: calDayDiff.days_diff,
Years: (calDayDiff.days_diff / 365.2524).toFixed(4),
Months: (calDayDiff.days_diff / 30.4375 %12).toFixed(4),
Days: (calDayDiff.days_diff % 30.4375).toFixed(4),
}
// data.push(_mapData);
await this.positionOfficerRepo.save(mapData);
}
// await this.positionOfficerRepo.save(data);
);
return new HttpSuccess();
}
@Get("TenurePositionEmployee")
public async cronjobTenurePositionEmployee() {
let data: any = [];
const positionEmployee = await this.positionEmployeeRepo.find();
await this.positionEmployeeRepo.remove(positionEmployee);
const profile = await this.profileEmployeeRepo.find();
for await (const x of profile) {
const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?)", [x.id]);
const _position = position.length > 0 ? position[0] : [];
const mapPosition =
_position.length > 1
? _position.slice(1)
.map((curr: any, index: number) => ({
days_diff: curr.days_diff,
positionName: _position[index]?.positionName,
}))
: [];
const calDayDiff = mapPosition
.filter((curr: any) => curr.positionName == x.position)
.reduce(
(acc: any, curr: any) => {
acc.days_diff += Number(curr.days_diff) || 0;
acc.positionName = curr.positionName
return acc;
},
{ days_diff: 0,positionName: null}
);
const mapData:any = {
profileEmployeeId: x.id,
positionName: calDayDiff.positionName,
days_diff: calDayDiff.days_diff,
Years: (calDayDiff.days_diff / 365.2524).toFixed(4),
Months: (calDayDiff.days_diff / 30.4375 %12).toFixed(4),
Days: (calDayDiff.days_diff % 30.4375).toFixed(4),
}
// data.push(_mapData);
await this.positionEmployeeRepo.save(mapData);
}
// await this.positionEmployeeRepo.save(data);
return new HttpSuccess();
}
@Get("TenureLevelOfficer")
public async cronjobTenureLevelOfficer() {
let data: any = [];
const positionOfficer = await this.levelOfficerRepo.find();
await this.levelOfficerRepo.remove(positionOfficer);
const profile = await this.profileRepo.find({relations:["posLevel"]});
for await (const x of profile) {
const positionLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?)", [x.id]);
const _positionLevel = positionLevel.length > 0 ? positionLevel[0] : [];
const mapPositionLevel =
_positionLevel.length > 1
? _positionLevel.slice(1)
.map((curr: any, index: number) => ({
days_diff: curr.days_diff,
positionType: _positionLevel[index]?.positionType,
positionLevel: _positionLevel[index]?.positionLevel,
positionCee: _positionLevel[index]?.positionCee,
}))
: [];
const calDayDiff = mapPositionLevel
.filter((curr: any) => curr.positionLevel == (x.posLevel?.posLevelName ??null))
.reduce(
(acc: any, curr: any) => {
acc.days_diff += Number(curr.days_diff) || 0;
acc.positionType = curr.positionType
acc.positionLevel = curr.positionLevel
acc.positionCee = curr.positionCee
return acc;
},
{ days_diff: 0,positionType:null,positionLevel:null,positionCee:null}
);
const mapData:any = {
profileId: x.id,
positionType: calDayDiff.positionType,
positionLevel: calDayDiff.positionLevel,
positionCee: calDayDiff.positionCee,
days_diff: calDayDiff.days_diff,
Years: x.posLevel == null? 0:((calDayDiff.days_diff / 365.2524).toFixed(4)),
Months: x.posLevel == null? 0:((calDayDiff.days_diff / 30.4375 %12).toFixed(4)),
Days: x.posLevel == null? 0:((calDayDiff.days_diff % 30.4375).toFixed(4)),
}
// data.push(_mapData);
await this.levelOfficerRepo.save(mapData);
}
// await this.levelOfficerRepo.save(data);
return new HttpSuccess();
}
@Get("TenureLevelEmployee")
public async cronjobTenureLevelEmployee() {
let data: any = [];
const positionEmployee = await this.levelEmployeeRepo.find();
await this.levelEmployeeRepo.remove(positionEmployee);
const profile = await this.profileEmployeeRepo.find({relations:["posLevel"]});
for await (const x of profile) {
const positionLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?)", [x.id]);
const _positionLevel = positionLevel.length > 0 ? positionLevel[0] : [];
const mapPositionLevel =
_positionLevel.length > 1
? _positionLevel.slice(1)
.map((curr: any, index: number) => ({
days_diff: curr.days_diff,
positionType: _positionLevel[index]?.positionType,
positionLevel: _positionLevel[index]?.positionLevel,
positionCee: _positionLevel[index]?.positionCee,
}))
: [];
const calDayDiff = mapPositionLevel
.filter((curr: any) => curr.positionLevel == (x.posLevel?.posLevelName ??null))
.reduce(
(acc: any, curr: any) => {
acc.days_diff += Number(curr.days_diff) || 0;
acc.positionType = curr.positionType
acc.positionLevel = curr.positionLevel
acc.positionCee = curr.positionCee
return acc;
},
{ days_diff: 0,positionType:null,positionLevel:null,positionCee:null}
);
const mapData:any = {
profileEmployeeId: x.id,
positionType: calDayDiff.positionType,
positionLevel: calDayDiff.positionLevel,
positionCee: calDayDiff.positionCee,
days_diff: calDayDiff.days_diff,
Years: x.posLevel == null? 0:((calDayDiff.days_diff / 365.2524).toFixed(4)),
Months: x.posLevel == null? 0:((calDayDiff.days_diff / 30.4375 %12).toFixed(4)),
Days: x.posLevel == null? 0:((calDayDiff.days_diff % 30.4375).toFixed(4)),
}
// data.push(_mapData);
await this.levelEmployeeRepo.save(mapData);
}
// await this.levelEmployeeRepo.save(data);
return new HttpSuccess();
}
@Get("user")
public async getSalaryUser(@Request() request: { user: Record<string, any> }) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });