created: script active act position

This commit is contained in:
Warunee Tamkoo 2026-02-02 16:18:32 +07:00
parent e92321d360
commit 9507040f75

View file

@ -20,6 +20,7 @@ import { PosMaster } from "../entities/PosMaster";
import { Brackets, LessThan, MoreThan } from "typeorm";
import { OrgRevision } from "../entities/OrgRevision";
import Extension from "../interfaces/extension";
import { ProfileActposition } from "../entities/ProfileActposition";
@Route("api/v1/org/pos/act")
@Tags("PosMasterAct")
@ -32,6 +33,7 @@ export class PosMasterActController extends Controller {
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
private posMasterActRepository = AppDataSource.getRepository(PosMasterAct);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private actpositionRepository = AppDataSource.getRepository(ProfileActposition);
/**
* API
@ -535,4 +537,109 @@ export class PosMasterActController extends Controller {
return new HttpSuccess(_posMaster);
}
/**
* API active
* @summary active (SUPER ADMIN)
* @param {string} id Id
*/
@Post("{id}")
async activePosMasterAct(@Path() id: string, @Request() req: { user: Record<string, any> }) {
const posMasterActs = await this.posMasterActRepository
.createQueryBuilder("posMasterAct")
.leftJoinAndSelect("posMasterAct.posMaster", "posMaster")
.leftJoinAndSelect("posMaster.orgRoot", "orgRoot")
.leftJoinAndSelect("posMaster.orgChild1", "orgChild1")
.leftJoinAndSelect("posMaster.orgChild2", "orgChild2")
.leftJoinAndSelect("posMaster.orgChild3", "orgChild3")
.leftJoinAndSelect("posMaster.orgChild4", "orgChild4")
.leftJoinAndSelect("posMaster.current_holder", "current_holder")
.leftJoinAndSelect("posMasterAct.posMasterChild", "posMasterChild")
.where("posMaster.orgRootId = :orgRootId", { orgRootId: id })
.andWhere("posMasterAct.statusReport = :statusReport", { statusReport: "PENDING" })
.select([
"posMasterAct.id",
"posMasterAct.statusReport",
"posMaster.posMasterNo",
"orgRoot.orgRootShortName",
"orgChild1.orgChild1ShortName",
"orgChild2.orgChild2ShortName",
"orgChild3.orgChild3ShortName",
"orgChild4.orgChild4ShortName",
"current_holder.position",
"posMasterChild.current_holderId",
])
.getMany();
if (posMasterActs.length === 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรักษาการในตำแหน่งของหน่วยงานนี้");
}
await Promise.all(
posMasterActs.map(async (posMasterAct) => {
const orgShortName =
[
posMasterAct.posMaster?.orgChild4?.orgChild4ShortName,
posMasterAct.posMaster?.orgChild3?.orgChild3ShortName,
posMasterAct.posMaster?.orgChild2?.orgChild2ShortName,
posMasterAct.posMaster?.orgChild1?.orgChild1ShortName,
posMasterAct.posMaster?.orgRoot?.orgRootShortName,
].find(Boolean) ?? "";
const profileId = posMasterAct.posMasterChild?.current_holderId;
if (profileId) {
const existingActivePositions = await this.actpositionRepository.find({
select: [
"id",
"status",
"lastUpdateUserId",
"lastUpdateFullName",
"lastUpdatedAt",
"dateEnd",
],
where: { profileId, status: true },
});
if (existingActivePositions.length > 0) {
await Promise.all(
existingActivePositions.map(async (pos) => {
Object.assign(pos, {
status: false,
lastUpdateUserId: req.user?.sub ?? null,
lastUpdateFullName: req.user?.name ?? null,
lastUpdatedAt: new Date(),
dateEnd: new Date(),
});
await this.actpositionRepository.save(pos);
}),
);
}
}
const dataAct = new ProfileActposition();
Object.assign(dataAct, {
profileId: profileId ?? null,
dateStart: new Date(),
posNo:
orgShortName && posMasterAct.posMaster?.posMasterNo
? `${orgShortName} ${posMasterAct.posMaster.posMasterNo}`
: posMasterAct.posMaster?.posMasterNo ?? "-",
position: posMasterAct.posMaster?.current_holder?.position ?? null,
posNoAbb: orgShortName,
status: true,
createdUserId: req.user?.sub ?? null,
createdFullName: req.user?.name ?? null,
lastUpdateUserId: req.user?.sub ?? null,
lastUpdateFullName: req.user?.name ?? null,
});
await this.actpositionRepository.save(dataAct);
posMasterAct.statusReport = "DONE";
await this.posMasterActRepository.save(posMasterAct);
}),
);
return new HttpSuccess();
}
}