add transaction #224
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m10s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m10s
This commit is contained in:
parent
ecd0388eb0
commit
832c5d2cb3
10 changed files with 2322 additions and 1991 deletions
|
|
@ -4,7 +4,7 @@ import { PosMaster } from "../entities/PosMaster";
|
|||
import { Position } from "../entities/Position";
|
||||
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
|
||||
import { EmployeePosition } from "../entities/EmployeePosition";
|
||||
import { In, IsNull, MoreThan, Not } from "typeorm";
|
||||
import { EntityManager, In, IsNull, MoreThan, Not } from "typeorm";
|
||||
import { RequestWithUser } from "../middlewares/user";
|
||||
import { Command } from "../entities/Command";
|
||||
import { ProfileSalary } from "../entities/ProfileSalary";
|
||||
|
|
@ -254,14 +254,23 @@ export function calculateRetireYear(birthDate: Date) {
|
|||
|
||||
return yy + 61;
|
||||
}
|
||||
export async function removeProfileInOrganize(profileId: string, type: string) {
|
||||
const currentRevision = await AppDataSource.getRepository(OrgRevision)
|
||||
export async function removeProfileInOrganize(
|
||||
profileId: string,
|
||||
type: string,
|
||||
manager?: EntityManager,
|
||||
) {
|
||||
// ถ้าส่ง manager เข้ามา → ทุก query/update อยู่ใน transaction ของ caller (all-or-nothing)
|
||||
// ถ้าไม่ส่ง → ใช้ global DataSource เหมือนเดิม (backward compatible)
|
||||
const ds = manager ?? AppDataSource;
|
||||
const currentRevision = await ds
|
||||
.getRepository(OrgRevision)
|
||||
.createQueryBuilder("orgRevision")
|
||||
.where("orgRevision.orgRevisionIsDraft = false")
|
||||
.andWhere("orgRevision.orgRevisionIsCurrent = true")
|
||||
.getOne();
|
||||
|
||||
const draftRevision = await AppDataSource.getRepository(OrgRevision)
|
||||
const draftRevision = await ds
|
||||
.getRepository(OrgRevision)
|
||||
.createQueryBuilder("orgRevision")
|
||||
.where("orgRevision.orgRevisionIsDraft = true")
|
||||
.andWhere("orgRevision.orgRevisionIsCurrent = false")
|
||||
|
|
@ -271,26 +280,30 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
|
|||
return;
|
||||
}
|
||||
if (type === "OFFICER") {
|
||||
const findProfileInposMaster = await AppDataSource.getRepository(PosMaster)
|
||||
const findProfileInposMaster = await ds
|
||||
.getRepository(PosMaster)
|
||||
.createQueryBuilder("posMaster")
|
||||
.where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id })
|
||||
.andWhere("posMaster.current_holderId = :profileId", { profileId })
|
||||
.getOne();
|
||||
|
||||
await AppDataSource.getRepository(PosMaster)
|
||||
await ds
|
||||
.getRepository(PosMaster)
|
||||
.createQueryBuilder()
|
||||
.update(PosMaster)
|
||||
.set({ current_holderId: null, isSit: false })
|
||||
.where("id = :id", { id: findProfileInposMaster?.id })
|
||||
.execute();
|
||||
|
||||
const findProfileInposMasterDraft = await AppDataSource.getRepository(PosMaster)
|
||||
const findProfileInposMasterDraft = await ds
|
||||
.getRepository(PosMaster)
|
||||
.createQueryBuilder("posMaster")
|
||||
.where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: draftRevision?.id })
|
||||
.andWhere("posMaster.next_holderId = :profileId", { profileId })
|
||||
.getOne();
|
||||
|
||||
await AppDataSource.getRepository(PosMaster)
|
||||
await ds
|
||||
.getRepository(PosMaster)
|
||||
.createQueryBuilder()
|
||||
.update(PosMaster)
|
||||
.set({ next_holderId: null, isSit: false })
|
||||
|
|
@ -300,7 +313,8 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
|
|||
if (!findProfileInposMaster && !findProfileInposMasterDraft) {
|
||||
return;
|
||||
}
|
||||
const findPosition = await AppDataSource.getRepository(Position)
|
||||
const findPosition = await ds
|
||||
.getRepository(Position)
|
||||
.createQueryBuilder("position")
|
||||
.where("position.posMasterId = :posMasterId", { posMasterId: findProfileInposMaster?.id })
|
||||
.getMany();
|
||||
|
|
@ -308,7 +322,8 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
|
|||
if (!findPosition) {
|
||||
return;
|
||||
}
|
||||
await AppDataSource.getRepository(Position)
|
||||
await ds
|
||||
.getRepository(Position)
|
||||
.createQueryBuilder()
|
||||
.update(Position)
|
||||
.set({ positionIsSelected: false })
|
||||
|
|
@ -316,14 +331,16 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
|
|||
.execute();
|
||||
}
|
||||
if (type === "EMPLOYEE") {
|
||||
const findProfileInEmpPosMaster = await AppDataSource.getRepository(EmployeePosMaster)
|
||||
const findProfileInEmpPosMaster = await ds
|
||||
.getRepository(EmployeePosMaster)
|
||||
.createQueryBuilder("employeePosMaster")
|
||||
.where("employeePosMaster.orgRevisionId = :orgRevisionId", {
|
||||
orgRevisionId: currentRevision?.id,
|
||||
})
|
||||
.andWhere("employeePosMaster.current_holderId = :profileId", { profileId })
|
||||
.getOne();
|
||||
await AppDataSource.getRepository(EmployeePosMaster)
|
||||
await ds
|
||||
.getRepository(EmployeePosMaster)
|
||||
.createQueryBuilder()
|
||||
.update(EmployeePosMaster)
|
||||
.set({ current_holderId: null, isSit: false })
|
||||
|
|
@ -333,7 +350,8 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
|
|||
if (!findProfileInEmpPosMaster) {
|
||||
return;
|
||||
}
|
||||
const findEmpPosition = await AppDataSource.getRepository(EmployeePosition)
|
||||
const findEmpPosition = await ds
|
||||
.getRepository(EmployeePosition)
|
||||
.createQueryBuilder("employeePosition")
|
||||
.where("employeePosition.posMasterId = :posMasterId", {
|
||||
posMasterId: findProfileInEmpPosMaster?.id,
|
||||
|
|
@ -344,7 +362,8 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
|
|||
return;
|
||||
}
|
||||
|
||||
await AppDataSource.getRepository(EmployeePosition)
|
||||
await ds
|
||||
.getRepository(EmployeePosition)
|
||||
.createQueryBuilder()
|
||||
.update(EmployeePosition)
|
||||
.set({ positionIsSelected: false })
|
||||
|
|
@ -353,8 +372,10 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function removePostMasterAct(profileId: string) {
|
||||
const currentRevision = await AppDataSource.getRepository(OrgRevision)
|
||||
export async function removePostMasterAct(profileId: string, manager?: EntityManager) {
|
||||
const ds = manager ?? AppDataSource;
|
||||
const currentRevision = await ds
|
||||
.getRepository(OrgRevision)
|
||||
.createQueryBuilder("orgRevision")
|
||||
.where("orgRevision.orgRevisionIsDraft = false")
|
||||
.andWhere("orgRevision.orgRevisionIsCurrent = true")
|
||||
|
|
@ -364,7 +385,8 @@ export async function removePostMasterAct(profileId: string) {
|
|||
return;
|
||||
}
|
||||
|
||||
const findProfileInposMaster = await AppDataSource.getRepository(PosMaster)
|
||||
const findProfileInposMaster = await ds
|
||||
.getRepository(PosMaster)
|
||||
.createQueryBuilder("posMaster")
|
||||
.where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id })
|
||||
.andWhere("posMaster.current_holderId = :profileId", { profileId })
|
||||
|
|
@ -374,11 +396,12 @@ export async function removePostMasterAct(profileId: string) {
|
|||
return;
|
||||
}
|
||||
|
||||
const posMasterAct = await AppDataSource.getRepository(PosMasterAct)
|
||||
const posMasterAct = await ds
|
||||
.getRepository(PosMasterAct)
|
||||
.createQueryBuilder("posMasterAct")
|
||||
.where("posMasterAct.posMasterChildId = :posMasterChildId", { posMasterChildId: findProfileInposMaster.id })
|
||||
.getMany();
|
||||
await AppDataSource.getRepository(PosMasterAct).remove(posMasterAct);
|
||||
await ds.getRepository(PosMasterAct).remove(posMasterAct);
|
||||
}
|
||||
|
||||
export async function checkReturnCommandType(commandId: string) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue