From aff6200368592e5447b67d7260712e83f6cfe480 Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Thu, 7 May 2026 17:01:03 +0700 Subject: [PATCH] refactor(PosMasterActController): redisClient.del role_ menu_ --- src/controllers/PosMasterActController.ts | 66 +++++++++++++++-------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/src/controllers/PosMasterActController.ts b/src/controllers/PosMasterActController.ts index dd4acd1b..72aac19b 100644 --- a/src/controllers/PosMasterActController.ts +++ b/src/controllers/PosMasterActController.ts @@ -24,6 +24,10 @@ import Extension from "../interfaces/extension"; import { ProfileActposition } from "../entities/ProfileActposition"; import { RequestWithUser } from "../middlewares/user"; import { escape } from "querystring"; +import { promisify } from "util"; + +const REDIS_HOST = process.env.REDIS_HOST; +const REDIS_PORT = process.env.REDIS_PORT; @Route("api/v1/org/pos/act") @Tags("PosMasterAct") @@ -37,6 +41,7 @@ export class PosMasterActController extends Controller { private posMasterActRepository = AppDataSource.getRepository(PosMasterAct); private posMasterRepository = AppDataSource.getRepository(PosMaster); private actpositionRepository = AppDataSource.getRepository(ProfileActposition); + private redis = require("redis"); /** * API เพิ่มรักษาการในตำแหน่ง @@ -92,7 +97,6 @@ export class PosMasterActController extends Controller { return new HttpSuccess(posMasterAct); } - /** * API ค้นหาตำแหน่งในระบบสมัครสอบ ขรก. * @@ -125,9 +129,7 @@ export class PosMasterActController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } - let posId: any[] = posMasterMain.posMasterActs.map( - (x) => x.posMasterChildId - ); + let posId: any[] = posMasterMain.posMasterActs.map((x) => x.posMasterChildId); posId.push(body.posmasterId); const query = await AppDataSource.getRepository(PosMaster) @@ -172,31 +174,31 @@ export class PosMasterActController extends Controller { posMasterMain.orgRootId == null ? "posMaster.orgRootId IS NULL" : "posMaster.orgRootId = :orgRootId", - { orgRootId: posMasterMain.orgRootId } + { orgRootId: posMasterMain.orgRootId }, ) .andWhere( posMasterMain.orgChild1Id == null ? "posMaster.orgChild1Id IS NULL" : "posMaster.orgChild1Id = :orgChild1Id", - { orgChild1Id: posMasterMain.orgChild1Id } + { orgChild1Id: posMasterMain.orgChild1Id }, ) .andWhere( posMasterMain.orgChild2Id == null ? "posMaster.orgChild2Id IS NULL" : "posMaster.orgChild2Id = :orgChild2Id", - { orgChild2Id: posMasterMain.orgChild2Id } + { orgChild2Id: posMasterMain.orgChild2Id }, ) .andWhere( posMasterMain.orgChild3Id == null ? "posMaster.orgChild3Id IS NULL" : "posMaster.orgChild3Id = :orgChild3Id", - { orgChild3Id: posMasterMain.orgChild3Id } + { orgChild3Id: posMasterMain.orgChild3Id }, ) .andWhere( posMasterMain.orgChild4Id == null ? "posMaster.orgChild4Id IS NULL" : "posMaster.orgChild4Id = :orgChild4Id", - { orgChild4Id: posMasterMain.orgChild4Id } + { orgChild4Id: posMasterMain.orgChild4Id }, ); } } else { @@ -210,7 +212,7 @@ export class PosMasterActController extends Controller { new Brackets((qb) => { qb.where( `CONCAT(current_holder.prefix, current_holder.firstName, ' ', current_holder.lastName) LIKE :keyword`, - { keyword: `%${keyword}%` } + { keyword: `%${keyword}%` }, ) .orWhere(`current_holder.citizenId LIKE :keyword`, { keyword: `%${keyword}%`, @@ -228,7 +230,7 @@ export class PosMasterActController extends Controller { ' ', posMaster.posMasterNo ) LIKE :keyword`, - { keyword: `%${keyword}%` } + { keyword: `%${keyword}%` }, ) .orWhere(`posLevel.posLevelName LIKE :keyword`, { keyword: `%${keyword}%`, @@ -238,8 +240,8 @@ export class PosMasterActController extends Controller { }) .orWhere(`current_holder.position LIKE :keyword`, { keyword: `%${keyword}%`, - }) - }) + }); + }), ); } @@ -280,7 +282,6 @@ export class PosMasterActController extends Controller { return new HttpSuccess({ data: data, total }); } - /** * API ลบรักษาการในตำแหน่ง * @@ -690,12 +691,12 @@ export class PosMasterActController extends Controller { x.posMasterChild?.orgRoot?.orgRootShortName, ].find((name) => !!name) && x.posMasterChild?.posMasterNo ? `${[ - x.posMasterChild?.orgChild4?.orgChild4ShortName, - x.posMasterChild?.orgChild3?.orgChild3ShortName, - x.posMasterChild?.orgChild2?.orgChild2ShortName, - x.posMasterChild?.orgChild1?.orgChild1ShortName, - x.posMasterChild?.orgRoot?.orgRootShortName, - ].find((name) => !!name)} ${x.posMasterChild.posMasterNo}` + x.posMasterChild?.orgChild4?.orgChild4ShortName, + x.posMasterChild?.orgChild3?.orgChild3ShortName, + x.posMasterChild?.orgChild2?.orgChild2ShortName, + x.posMasterChild?.orgChild1?.orgChild1ShortName, + x.posMasterChild?.orgRoot?.orgRootShortName, + ].find((name) => !!name)} ${x.posMasterChild.posMasterNo}` : x.posMasterChild?.posMasterNo || null; const orgShortNameAct = [ @@ -768,6 +769,9 @@ export class PosMasterActController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรักษาการในตำแหน่งของหน่วยงานนี้"); } + // เก็บรวบรวม profileIds ทั้งหมดเพื่อ clear cache หลังจากบันทึกเสร็จ + const profileIdsToClearCache = new Set(); + await Promise.all( posMasterActs.map(async (posMasterAct) => { const orgShortName = @@ -782,6 +786,8 @@ export class PosMasterActController extends Controller { const profileId = posMasterAct.posMasterChild?.current_holderId; if (profileId) { + profileIdsToClearCache.add(profileId); + const existingActivePositions = await this.actpositionRepository.find({ select: [ "id", @@ -790,7 +796,7 @@ export class PosMasterActController extends Controller { "lastUpdateFullName", "lastUpdatedAt", "dateEnd", - "isDeleted" + "isDeleted", ], where: { profileId, status: true, isDeleted: false }, }); @@ -834,6 +840,24 @@ export class PosMasterActController extends Controller { }), ); + // Clear Redis cache หลังจากบันทึกข้อมูลเสร็จแล้ว + // ทำงานนอก loop เพื่อ clear รอบเดียว ไม่ใช่ทุก iteration + if (profileIdsToClearCache.size > 0) { + await Promise.all( + Array.from(profileIdsToClearCache).map(async (profileId) => { + const redisClient = await this.redis.createClient({ + host: REDIS_HOST, + port: REDIS_PORT, + }); + + const delAsync = promisify(redisClient.del).bind(redisClient); + await delAsync("role_" + profileId); + await delAsync("menu_" + profileId); + + redisClient.quit(); + }), + ); + } return new HttpSuccess(); } }