diff --git a/src/controllers/PosMasterActController.ts b/src/controllers/PosMasterActController.ts index 4d449d21..25595045 100644 --- a/src/controllers/PosMasterActController.ts +++ b/src/controllers/PosMasterActController.ts @@ -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 }) { + 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(); + } }