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

175 lines
4.9 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";
import { In } from "typeorm";
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
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-07-25 16:20:48 +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;
}
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();
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();
}
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();
}
}