From c6fabbcf166a9af459c6cd2e2885b3d01246c2fd Mon Sep 17 00:00:00 2001 From: kittapath <> Date: Fri, 12 Dec 2025 01:37:44 +0700 Subject: [PATCH] add permission brother --- src/controllers/DevelopmentController.ts | 35 +++++------ src/interfaces/permission.ts | 78 +++++++++++++++--------- 2 files changed, 64 insertions(+), 49 deletions(-) diff --git a/src/controllers/DevelopmentController.ts b/src/controllers/DevelopmentController.ts index 6871cd8..5522f9c 100644 --- a/src/controllers/DevelopmentController.ts +++ b/src/controllers/DevelopmentController.ts @@ -1551,7 +1551,7 @@ export class DevelopmentController extends Controller { if (!development) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรมนี้"); } - Object.assign(development, { ...requestBody}); + Object.assign(development, { ...requestBody }); development.lastUpdateUserId = request.user.sub; development.lastUpdateFullName = request.user.name; development.lastUpdatedAt = new Date(); @@ -2085,7 +2085,7 @@ export class DevelopmentController extends Controller { _data.child1 != undefined && _data.child1 != null ? _data.child1[0] != null ? `development.child1DnaId IN (:...child1)` - : `development.child1DnaId is null` + : `development.child1DnaId is ${_data.privilege == "PARENT" ? "not null" : "null"}` : "1=1", { child1: _data.child1, @@ -2130,19 +2130,15 @@ export class DevelopmentController extends Controller { "development.child2", "development.child3", "development.child4", - ]) + ]); - if (sortBy) { - query = query.orderBy( - `development.${sortBy}`, - descending ? "DESC" : "ASC" - ); - }else{ - query = query.orderBy("development.year", "DESC") - .addOrderBy("development.createdAt", "DESC") - } + if (sortBy) { + query = query.orderBy(`development.${sortBy}`, descending ? "DESC" : "ASC"); + } else { + query = query.orderBy("development.year", "DESC").addOrderBy("development.createdAt", "DESC"); + } - const [development, total] = await query + const [development, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); @@ -2632,12 +2628,13 @@ export class DevelopmentController extends Controller { }); const developmentAddresss = await this.developmentAddresssRepository.find({ - where: { developmentId: id} - }) - + where: { developmentId: id }, + }); + const places = developmentAddresss - .map(addr => - `- ${addr.address}, ${addr.addressType === "IN_COUNTRY" ? addr.provinceName : addr.country} (${addr.addressType === "IN_COUNTRY" ? "ภายในประเทศ" : "ภายนอกประเทศ"})` + .map( + (addr) => + `- ${addr.address}, ${addr.addressType === "IN_COUNTRY" ? addr.provinceName : addr.country} (${addr.addressType === "IN_COUNTRY" ? "ภายในประเทศ" : "ภายนอกประเทศ"})`, ) .filter(Boolean) .join("\n"); @@ -2768,7 +2765,7 @@ export class DevelopmentController extends Controller { techniqueActuals.length > 0 && dev20Lists.some((item) => techniqueActuals.includes(item)); isDevelopment10 = techniqueActuals.length > 0 && dev10Lists.some((item) => techniqueActuals.includes(item)); - + if (x.type == "OFFICER") { await new CallAPI() .PostData(request, "/org/profile/development", { diff --git a/src/interfaces/permission.ts b/src/interfaces/permission.ts index 6ff8977..a47c923 100644 --- a/src/interfaces/permission.ts +++ b/src/interfaces/permission.ts @@ -94,6 +94,15 @@ class CheckAuth { child4: null, privilege: "ROOT", }; + } else if (privilege == "PARENT") { + data = { + root: [x.orgRootId], + child1: [null], + child2: null, + child3: null, + child4: null, + privilege: "PARENT", + }; } else if (privilege == "CHILD") { data = { root: node >= 0 ? [x.orgRootId] : null, @@ -103,6 +112,15 @@ class CheckAuth { child4: node >= 4 ? [x.orgChild4Id] : null, privilege: "CHILD", }; + } else if (privilege == "BROTHER") { + data = { + // root: node >= 0 ? [x.orgRootId] : null, + root: node >= 0 ? [x.orgRootId] : null, + child1: node >= 2 ? [x.orgChild1Id] : null, + child2: node >= 3 ? [x.orgChild2Id] : null, + child3: node >= 4 ? [x.orgChild3Id] : null, + privilege: "BROTHER", + }; } else if (privilege == "NORMAL") { data = { root: [x.orgRootId], @@ -185,38 +203,38 @@ class CheckAuth { } public async checkOrg(token: any, keycloakId: string) { const redisClient = await this.redis.createClient({ - host: process.env.REDIS_HOST, - port: process.env.REDIS_PORT, - }) - const getAsync = promisify(redisClient.get).bind(redisClient) - try { - let reply = await getAsync("org_" + keycloakId) - if (reply != null) { - reply = JSON.parse(reply) - } else { - if (!keycloakId) throw new Error("No KeycloakId provided") - const x = await new CallAPI().GetData( - { - headers: { authorization: token }, - }, - `/org/permission/checkOrg/${keycloakId}`, - false - ) + host: process.env.REDIS_HOST, + port: process.env.REDIS_PORT, + }); + const getAsync = promisify(redisClient.get).bind(redisClient); + try { + let reply = await getAsync("org_" + keycloakId); + if (reply != null) { + reply = JSON.parse(reply); + } else { + if (!keycloakId) throw new Error("No KeycloakId provided"); + const x = await new CallAPI().GetData( + { + headers: { authorization: token }, + }, + `/org/permission/checkOrg/${keycloakId}`, + false, + ); - const data = { - orgRootId: x.orgRootId, - orgChild1Id: x.orgChild1Id, - orgChild2Id: x.orgChild2Id, - orgChild3Id: x.orgChild3Id, - orgChild4Id: x.orgChild4Id, - } + const data = { + orgRootId: x.orgRootId, + orgChild1Id: x.orgChild1Id, + orgChild2Id: x.orgChild2Id, + orgChild3Id: x.orgChild3Id, + orgChild4Id: x.orgChild4Id, + }; - return data - } - } catch (error) { - console.error("Error calling API:", error) - throw error - } + return data; + } + } catch (error) { + console.error("Error calling API:", error); + throw error; + } } public async PermissionCreate(req: RequestWithUser, system: string) { return await this.Permission(req, system, "CREATE");