refactor(PermissionController): getPermission

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2026-05-07 13:27:27 +07:00
parent c1a4df63e5
commit fe1ebaa1cf
3 changed files with 230 additions and 13 deletions

View file

@ -25,6 +25,9 @@ import { ProfileActposition } from "../entities/ProfileActposition";
import { RequestWithUser } from "../middlewares/user";
import { escape } from "querystring";
const REDIS_HOST = process.env.REDIS_HOST;
const REDIS_PORT = process.env.REDIS_PORT;
@Route("api/v1/org/pos/act")
@Tags("PosMasterAct")
@Security("bearerAuth")
@ -37,6 +40,23 @@ export class PosMasterActController extends Controller {
private posMasterActRepository = AppDataSource.getRepository(PosMasterAct);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private actpositionRepository = AppDataSource.getRepository(ProfileActposition);
private redis = require("redis");
/**
* Helper function cache acting position
*/
private async invalidatePermissionCache(profileIds: string[]) {
const redisClient = await this.redis.createClient({
host: REDIS_HOST,
port: REDIS_PORT,
});
for (const profileId of profileIds) {
redisClient.del(`role_${profileId}_acting`);
redisClient.del(`role_${profileId}_normal`);
redisClient.del(`menu_${profileId}`);
}
}
/**
* API
@ -89,6 +109,12 @@ export class PosMasterActController extends Controller {
posMasterAct.createdAt = new Date();
posMasterAct.lastUpdatedAt = new Date();
await this.posMasterActRepository.save(posMasterAct);
// ลบ cache ของผู้ถูกรักษาการ (current_holder ของ posMasterChild)
if (posMasterChild.current_holderId) {
await this.invalidatePermissionCache([posMasterChild.current_holderId]);
}
return new HttpSuccess(posMasterAct);
}
@ -295,6 +321,7 @@ export class PosMasterActController extends Controller {
where: {
id: id,
},
relations: ["posMasterChild"],
});
try {
result = await this.posMasterActRepository.delete({ id: id });
@ -318,6 +345,11 @@ export class PosMasterActController extends Controller {
p.posMasterOrder = i + 1;
await this.posMasterActRepository.save(p);
});
// ลบ cache ของผู้ถูกรักษาการ
if (posMasterAct.posMasterChild?.current_holderId) {
await this.invalidatePermissionCache([posMasterAct.posMasterChild.current_holderId]);
}
}
return new HttpSuccess();
}
@ -768,6 +800,9 @@ export class PosMasterActController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรักษาการในตำแหน่งของหน่วยงานนี้");
}
// เก็บ profileIds ที่ได้รับผลกระทบเพื่อลบ cache
const affectedProfileIds: string[] = [];
await Promise.all(
posMasterActs.map(async (posMasterAct) => {
const orgShortName =
@ -782,6 +817,8 @@ export class PosMasterActController extends Controller {
const profileId = posMasterAct.posMasterChild?.current_holderId;
if (profileId) {
affectedProfileIds.push(profileId);
const existingActivePositions = await this.actpositionRepository.find({
select: [
"id",
@ -834,6 +871,11 @@ export class PosMasterActController extends Controller {
}),
);
// ลบ cache ของผู้ถูกรักษาการทั้งหมด
if (affectedProfileIds.length > 0) {
await this.invalidatePermissionCache(affectedProfileIds);
}
return new HttpSuccess();
}
}