hrms-api-org/src/interfaces/utils.ts

536 lines
16 KiB
TypeScript
Raw Normal View History

import { OrgRevision } from "../entities/OrgRevision";
import { AppDataSource } from "../database/data-source";
import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
import { EmployeePosition } from "../entities/EmployeePosition";
2024-10-29 14:05:52 +07:00
import { In, IsNull, MoreThan, Not } from "typeorm";
2024-08-09 10:17:39 +07:00
import { RequestWithUser } from "../middlewares/user";
import { Command } from "../entities/Command";
2024-10-29 14:05:52 +07:00
import { ProfileSalary } from "../entities/ProfileSalary";
2024-12-11 17:13:03 +07:00
import { Profile } from "../entities/Profile";
2024-12-20 13:46:51 +07:00
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { CommandRecive } from "../entities/CommandRecive";
export function calculateAge(start: Date, end = new Date()) {
if (start.getTime() > end.getTime()) return null;
let year = end.getFullYear() - start.getFullYear();
let month = end.getMonth() - start.getMonth();
let day = end.getDate() - start.getDate();
2024-07-01 17:27:01 +07:00
// if (month < 0) {
// month += 12;
// year -= 1;
// }
// if (day < 0) {
// month -= 1;
// day =
// new Date(start.getFullYear(), start.getMonth(), 0).getDate() -
// start.getDate() +
// end.getDate();
// }
2024-07-03 00:36:23 +07:00
if (day < 0) {
month -= 1;
day += new Date(end.getFullYear(), end.getMonth(), 0).getDate();
}
2024-07-01 17:27:01 +07:00
2024-07-03 00:36:23 +07:00
if (month < 0) {
month += 12;
year -= 1;
}
return { year, month, day };
}
2024-03-08 17:20:39 +07:00
2024-11-08 16:34:03 +07:00
export async function calculateGovAge(profileId: string, type: string) {
// type = OFFICER , EMPLOYEE
const isEmployee = type === "EMPLOYEE";
2024-11-08 16:34:03 +07:00
const records = await AppDataSource.getRepository(ProfileSalary).find({
where: {
[isEmployee ? "profileEmployeeId" : "profileId"]: profileId,
},
2024-10-29 14:05:52 +07:00
select: ["date", "dateGovernment", "isGovernment"],
order: { order: "ASC" },
});
2024-10-29 14:05:52 +07:00
if (!records || records.length === 0) {
return null;
}
let endDateFristRec: any = null;
2024-11-08 16:34:03 +07:00
endDateFristRec = await AppDataSource.getRepository(ProfileSalary).findOne({
2024-10-29 14:05:52 +07:00
where: {
2024-11-08 16:34:03 +07:00
[isEmployee ? "profileEmployeeId" : "profileId"]: profileId,
2024-10-29 14:05:52 +07:00
dateGovernment: Not(IsNull()),
isGovernment: false,
},
select: ["dateGovernment", "order"],
2024-10-29 14:05:52 +07:00
order: { order: "ASC" },
});
2024-11-08 16:34:03 +07:00
const calculateDuration = (startDate: any, endDate: any) => {
let years = endDate.getFullYear() - startDate.getFullYear();
let months = endDate.getMonth() - startDate.getMonth();
let days = endDate.getDate() - startDate.getDate();
2024-10-29 14:05:52 +07:00
if (days < 0) {
months--;
const lastMonthDays = new Date(endDate.getFullYear(), endDate.getMonth(), 0).getDate();
days += lastMonthDays;
}
if (months < 0) {
months += 12;
years--;
}
return { years, months, days };
};
2024-12-20 13:46:51 +07:00
let profile:any;
profile = await AppDataSource.getRepository(Profile).findOne({
2024-12-11 17:13:03 +07:00
where:{
id:profileId
}
})
2024-12-20 13:46:51 +07:00
if(isEmployee){
profile = await AppDataSource.getRepository(ProfileEmployee).findOne({
where:{
id:profileId
}
})
}
2024-12-11 17:13:03 +07:00
// const firstStartDate = new Date(records[0].date);
const firstStartDate = profile?.dateAppoint ? profile?.dateAppoint : new Date();
const firstEndDate = endDateFristRec ? new Date(endDateFristRec.dateGovernment) : new Date();
2024-12-11 17:13:03 +07:00
2024-11-08 16:34:03 +07:00
const {
years: totalYears1,
months: totalMonths1,
days: totalDays1,
} = calculateDuration(firstStartDate, firstEndDate);
2024-12-20 13:46:51 +07:00
2024-10-29 14:05:52 +07:00
const records_middle = await AppDataSource.getRepository(ProfileSalary).find({
where: {
2024-11-08 16:34:03 +07:00
[isEmployee ? "profileEmployeeId" : "profileId"]: profileId,
2024-10-29 14:05:52 +07:00
order: MoreThan(endDateFristRec?.order),
dateGovernment: Not(IsNull()),
},
select: ["dateGovernment", "isGovernment"],
2024-10-29 14:05:52 +07:00
order: { order: "ASC" },
});
if (!records_middle || records_middle.length === 0) {
return { year: totalYears1, month: totalMonths1, day: totalDays1 };
2024-10-29 14:05:52 +07:00
}
2024-11-08 16:34:03 +07:00
let totalYears2 = 0,
totalMonths2 = 0,
totalDays2 = 0;
2024-10-29 14:05:52 +07:00
for (let i = 0; i < records_middle.length; i++) {
const startDate = new Date(records_middle[i].dateGovernment);
2024-11-08 16:34:03 +07:00
const endDate =
i < records_middle.length - 1 ? new Date(records_middle[i + 1].dateGovernment) : new Date();
2024-10-29 14:05:52 +07:00
2024-11-08 16:34:03 +07:00
if (
records_middle[i].isGovernment === false &&
(i === records_middle.length - 1 || records_middle[i + 1].isGovernment === false)
) {
break;
2024-10-29 14:05:52 +07:00
}
2024-11-08 16:34:03 +07:00
const {
years: years_mid,
months: months_mid,
days: days_mid,
} = calculateDuration(startDate, endDate);
2024-10-29 14:05:52 +07:00
totalYears2 += years_mid;
totalMonths2 += months_mid;
totalDays2 += days_mid;
}
const adjustTotal = (years: any, months: any, days: any) => {
if (days >= new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate()) {
2024-11-08 16:34:03 +07:00
months += Math.floor(
days / new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate(),
);
days %= new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate();
}
2024-10-29 14:05:52 +07:00
if (months >= 12) {
years += Math.floor(months / 12);
months %= 12;
}
2024-10-29 14:05:52 +07:00
return { years, months, days };
};
2024-10-29 14:05:52 +07:00
const adjustedTotal = adjustTotal(totalYears2, totalMonths2, totalDays2);
2024-11-08 16:34:03 +07:00
let sumYears = totalYears1 + adjustedTotal.years;
let sumMonths = totalMonths1 + adjustedTotal.months;
let sumDays = totalDays1 + adjustedTotal.days;
2024-11-08 16:34:03 +07:00
2024-10-29 14:05:52 +07:00
if (sumMonths >= 12) {
sumYears += Math.floor(sumMonths / 12);
sumMonths %= 12;
2024-10-29 14:05:52 +07:00
}
2024-11-08 16:34:03 +07:00
return { year: sumYears, month: sumMonths, day: sumDays };
2024-10-29 14:05:52 +07:00
}
2024-03-08 17:20:39 +07:00
export function calculateRetireDate(birthDate: Date) {
2024-07-03 00:36:23 +07:00
// let _birthDate = birthDate;
// _birthDate.setFullYear(_birthDate.getFullYear() + 60); var year = d.getFullYear();
var d = birthDate;
var year = d.getFullYear();
var month = d.getMonth();
var day = d.getDate();
2024-10-07 14:53:27 +07:00
var c = new Date(year + 60, month, day - 1);
2024-07-03 00:36:23 +07:00
return c;
}
export function calculateRetireLaw(birthDate: Date) {
2024-03-08 17:20:39 +07:00
let dd = birthDate.getDate();
let mm = birthDate.getMonth();
let yy = birthDate.getFullYear();
let flag = true;
switch (mm) {
2024-07-03 00:36:23 +07:00
case 9:
2024-03-08 17:20:39 +07:00
if (dd >= 2) flag = false;
break;
2024-07-03 00:36:23 +07:00
case 10:
flag = false;
2024-03-26 09:47:17 +07:00
break;
2024-07-03 00:36:23 +07:00
case 11:
flag = false;
2024-03-08 17:20:39 +07:00
break;
}
2024-07-03 00:36:23 +07:00
if (flag == true) return new Date(`${yy + 60}-09-30T00:00:00.000+07:00`);
2024-03-08 17:20:39 +07:00
2024-03-11 10:45:40 +07:00
return new Date(`${yy + 61}-09-30T00:00:00.000+07:00`);
2024-03-08 17:20:39 +07:00
}
2024-03-26 09:47:17 +07:00
export function calculateRetireYear(birthDate: Date) {
let dd = birthDate.getDate();
let mm = birthDate.getMonth();
let yy = birthDate.getFullYear();
let flag = true;
switch (mm) {
case 10:
if (dd >= 2) flag = false;
break;
case 11:
break;
case 12:
break;
}
if (flag) return yy + 60;
return yy + 61;
}
2024-10-07 14:53:27 +07:00
export async function removeProfileInOrganize(profileId: string, type: string) {
const currentRevision = await AppDataSource.getRepository(OrgRevision)
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
if (!currentRevision) {
2024-08-02 10:45:11 +07:00
return;
}
if (type === "OFFICER") {
const findProfileInposMaster = await AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster")
.where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id })
.andWhere("posMaster.current_holderId = :profileId", { profileId })
.getOne();
2024-10-07 14:53:27 +07:00
await AppDataSource.getRepository(PosMaster)
.createQueryBuilder()
.update(PosMaster)
.set({ current_holderId: null })
.where("id = :id", { id: findProfileInposMaster?.id })
.execute();
if (!findProfileInposMaster) {
2024-08-02 10:45:11 +07:00
return;
}
const findPosition = await AppDataSource.getRepository(Position)
.createQueryBuilder("position")
.where("position.posMasterId = :posMasterId", { posMasterId: findProfileInposMaster?.id })
.getMany();
if (!findPosition) {
2024-08-02 10:45:11 +07:00
return;
}
await AppDataSource.getRepository(Position)
.createQueryBuilder()
.update(Position)
.set({ positionIsSelected: false })
.where("id IN (:...ids)", { ids: findPosition.map((item) => item.id) })
.execute();
}
2024-10-07 14:53:27 +07:00
if (type === "EMPLOYEE") {
const findProfileInEmpPosMaster = await AppDataSource.getRepository(EmployeePosMaster)
.createQueryBuilder("employeePosMaster")
.where("employeePosMaster.orgRevisionId = :orgRevisionId", {
orgRevisionId: currentRevision?.id,
})
.andWhere("employeePosMaster.current_holderId = :profileId", { profileId })
.getOne();
await AppDataSource.getRepository(EmployeePosMaster)
.createQueryBuilder()
.update(EmployeePosMaster)
.set({ current_holderId: null })
.where("id = :id", { id: findProfileInEmpPosMaster?.id })
.execute();
if (!findProfileInEmpPosMaster) {
2024-08-02 10:45:11 +07:00
return;
}
const findEmpPosition = await AppDataSource.getRepository(EmployeePosition)
.createQueryBuilder("employeePosition")
.where("employeePosition.posMasterId = :posMasterId", {
posMasterId: findProfileInEmpPosMaster?.id,
})
.getMany();
if (!findEmpPosition) {
2024-08-02 10:45:11 +07:00
return;
}
await AppDataSource.getRepository(EmployeePosition)
.createQueryBuilder()
.update(EmployeePosition)
.set({ positionIsSelected: false })
.where("id IN (:...ids)", { ids: findEmpPosition.map((item) => item.id) })
.execute();
}
}
export async function checkReturnCommandType(commandId: string) {
const commandRepository = AppDataSource.getRepository(Command);
const _type = await commandRepository.findOne({
where: {
2024-11-08 16:34:03 +07:00
id: commandId,
},
relations: ["commandType"],
});
if (!["C-PM-08", "C-PM-09"].includes(String(_type?.commandType.code))) {
return false;
}
return true;
}
export async function checkExceptCommandType(commandId: string) {
const commandRepository = AppDataSource.getRepository(Command);
const _type = await commandRepository.findOne({
where: {
2024-11-08 16:34:03 +07:00
id: commandId,
},
relations: ["commandType"],
});
if (!["C-PM-25", "C-PM-26"].includes(String(_type?.commandType.code))) {
return false;
}
return true;
}
export async function checkCommandType(commandId: string) {
const commandRepository = AppDataSource.getRepository(Command);
const commandReciveRepository = AppDataSource.getRepository(CommandRecive)
const _type = await commandRepository.findOne({
where: {
2024-11-08 16:34:03 +07:00
id: commandId,
},
relations: ["commandType", "commandRecives"],
});
2024-11-08 16:34:03 +07:00
if (
!["C-PM-12", "C-PM-13", "C-PM-17", "C-PM-18", "C-PM-23", "C-PM-19", "C-PM-20"].includes(
String(_type?.commandType.code),
)
) {
// return false;
return { status: false, LeaveType: null, leaveRemark: null};
}
// return true;
const _commandRecive = await commandReciveRepository.findOne({
where: { commandId: commandId }
});
let _leaveType: string =""
switch(String(_type?.commandType.code)){
case "C-PM-12" : {
_leaveType = "PROBATION_REPORT";
break;
}
case "C-PM-13" : {
_leaveType = "PLACEMENT_TRANSFER";
break;
}
case "C-PM-17" : {
_leaveType = "RETIRE_RESIGN";
break;
}
case "C-PM-18" : {
_leaveType = "RETIRE_OUT";
break;
}
case "C-PM-19" : {
_leaveType = "DISCIPLINE_RESULT_REMOVE";
break;
}
case "C-PM-20" : {
_leaveType = "DISCIPLINE_RESULT_DISMISS";
break;
}
case "C-PM-23" : {
_leaveType = "RETIRE_RESIGN_EMP";
break;
}
default: {
_leaveType = ""
}
}
return { status: true, LeaveType: _leaveType, leaveRemark: _commandRecive ? _commandRecive.remarkVertical : null };
}
2024-08-09 10:17:39 +07:00
//logs
export type DataDiff = {
before: any;
after: any;
};
export type LogSequence = {
action: string;
status: "success" | "error";
description: string;
query?: any;
request?: {
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
url?: string;
payload?: string;
response?: string;
};
};
export function setLogDataDiff(req: RequestWithUser, data: DataDiff) {
req.app.locals.logData.dataDiff = {
before: JSON.stringify(data.before),
after: JSON.stringify(data.after),
};
}
export function addLogSequence(req: RequestWithUser, data: LogSequence) {
if (!req?.app?.locals?.logData?.sequence) {
req.app.locals.logData.sequence = [];
}
req.app.locals.logData.sequence = req.app.locals.logData.sequence.concat(data);
}
export function editLogSequence(req: RequestWithUser, index: number, data: LogSequence) {
req.app.locals.logData.sequence[index] = data;
}
2024-10-11 11:05:31 +07:00
export function commandTypePath(commandCode: string): string | null {
switch (commandCode) {
case "C-PM-01":
2024-10-18 17:35:04 +07:00
return "/placement/recruit/report"; //
2024-10-11 11:05:31 +07:00
case "C-PM-02":
2024-10-18 17:35:04 +07:00
return "/placement/candidate/report"; //
2024-10-11 11:05:31 +07:00
case "C-PM-03":
return "/placement/appoint/report";
case "C-PM-04":
return "/placement/move/report";
case "C-PM-05":
return "/placement/appointment/appoint/report";
case "C-PM-06":
2024-10-18 17:35:04 +07:00
return "/placement/slip/report";
2024-10-11 11:05:31 +07:00
case "C-PM-07":
return "/placement/appointment/move/report";
case "C-PM-08":
return "/retirement/other/appoint/report";
case "C-PM-09":
return "/retirement/other/out/report";
case "C-PM-10":
2024-10-18 17:35:04 +07:00
return "/probation/report/command10/officer/report";
2024-10-11 11:05:31 +07:00
case "C-PM-11":
2024-11-08 16:34:03 +07:00
return "/probation/report/command11/officer/report"; //PROBATION
2024-10-11 11:05:31 +07:00
case "C-PM-12":
2024-11-08 16:34:03 +07:00
return "/probation/report/command12/officer/report"; //PROBATION
2024-10-11 11:05:31 +07:00
case "C-PM-13":
return "/placement/transfer/command/report";
case "C-PM-14":
2024-10-18 17:35:04 +07:00
return "/placement/receive/command/report";
2024-10-11 11:05:31 +07:00
case "C-PM-15":
return "/placement/officer/command/report";
case "C-PM-16":
return "/placement/repatriation/command/report";
case "C-PM-17":
return "/retirement/resign/command/report";
case "C-PM-18":
return "/retirement/out/command/report";
case "C-PM-19":
return "/discipline/result/command19/report";
case "C-PM-20":
return "/discipline/result/command20/report";
case "C-PM-21":
2024-11-08 16:34:03 +07:00
return "/org/command/command21/employee/report"; //ORG
2024-10-11 11:05:31 +07:00
case "C-PM-22":
return "/placement/appointment/employee-appoint/report";
case "C-PM-23":
2024-11-08 16:34:03 +07:00
return "/retirement/resign-employee/command/report";
2024-10-11 11:05:31 +07:00
case "C-PM-24":
return "/placement/appointment/employee-move/report";
case "C-PM-25":
return "/discipline/result/command25/report";
case "C-PM-26":
return "/discipline/result/command26/report";
case "C-PM-27":
return "/discipline/result/command27/report";
case "C-PM-28":
return "/discipline/result/command28/report";
case "C-PM-29":
return "/discipline/result/command29/report";
case "C-PM-30":
return "/discipline/result/command30/report";
case "C-PM-31":
return "/discipline/result/command31/report";
case "C-PM-32":
return "/discipline/result/command32/report";
case "C-PM-33":
2024-11-08 16:34:03 +07:00
return "/salary/report/command/officer/report"; //SALARY
2024-10-11 11:05:31 +07:00
case "C-PM-34":
2024-11-08 16:34:03 +07:00
return "/salary/report/command/officer/report"; //SALARY
2024-10-11 11:05:31 +07:00
case "C-PM-35":
2024-11-08 16:34:03 +07:00
return "/salary/report/command/officer/report"; //SALARY
2024-10-11 11:05:31 +07:00
case "C-PM-36":
2024-11-08 16:34:03 +07:00
return "/salary/report/command/employee/report"; //SALARY
2024-10-11 11:05:31 +07:00
case "C-PM-37":
2024-11-08 16:34:03 +07:00
return "/salary/report/command/employee/report"; //SALARY
2024-10-11 11:05:31 +07:00
case "C-PM-38":
2024-11-08 16:34:03 +07:00
return "/org/command/command38/officer/report"; //ORG
2024-10-11 11:05:31 +07:00
case "C-PM-39":
2024-10-18 17:35:04 +07:00
return "/placement/appointment/slip/report";
2024-10-11 11:05:31 +07:00
case "C-PM-40":
2024-11-08 16:34:03 +07:00
return "/org/command/command40/officer/report"; //ORG
2024-10-11 11:05:31 +07:00
case "C-PM-41":
return "/retirement/resign/leave-cancel/report";
2024-11-08 16:34:03 +07:00
case "C-PM-42":
return "/retirement/resign-employee/leave-cancel/report";
2024-10-11 11:05:31 +07:00
default:
return null;
}
}