diff --git a/src/controllers/DevelopmentRequestController.ts b/src/controllers/DevelopmentRequestController.ts index b02b6bf0..ba4e8458 100644 --- a/src/controllers/DevelopmentRequestController.ts +++ b/src/controllers/DevelopmentRequestController.ts @@ -26,6 +26,7 @@ import { ProfileDevelopment } from "../entities/ProfileDevelopment"; import { ProfileDevelopmentHistory } from "../entities/ProfileDevelopmentHistory"; import { setLogDataDiff } from "../interfaces/utils"; import CallAPI from "../interfaces/call-api"; +import { OrgRevision } from "../entities/OrgRevision"; @Route("api/v1/org/profile/development-request") @Tags("DevelopmentRequest") @Security("bearerAuth") @@ -35,6 +36,7 @@ export class DevelopmentRequestController extends Controller { private developmentRequestRepository = AppDataSource.getRepository(DevelopmentRequest); private developmentProjectRepository = AppDataSource.getRepository(DevelopmentProject); private developmentHistoryRepository = AppDataSource.getRepository(ProfileDevelopmentHistory); + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); @Get("user") public async getDevelopmentRequestUser( @@ -109,15 +111,23 @@ export class DevelopmentRequestController extends Controller { @Get("admin") public async getDevelopmentRequestAdmin( - @Request() req: RequestWithUser, + @Request() request: RequestWithUser, @Query("status") status: string, @Query("keyword") keyword: string = "", @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, ) { - // await new permission().PermissionList(req, "SYS_REGISTRY_OFFICER"); + let data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); + const orgRevisionPublish = await this.orgRevisionRepository + .createQueryBuilder("orgRevision") + .where("orgRevision.orgRevisionIsDraft = false") + .andWhere("orgRevision.orgRevisionIsCurrent = true") + .getOne(); const [lists, total] = await AppDataSource.getRepository(DevelopmentRequest) .createQueryBuilder("developmentRequest") + .leftJoinAndSelect("developmentRequest.profile", "profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.orgRevision", "orgRevision") .andWhere( status == undefined || status.trim().toUpperCase() == "ALL" || status == "" ? "1=1" @@ -126,6 +136,59 @@ export class DevelopmentRequestController extends Controller { status: status == undefined || status == null ? "" : status.trim().toUpperCase(), }, ) + .andWhere(orgRevisionPublish ? `current_holders.orgRevisionId = :revisionId` : "1=1", { + revisionId: orgRevisionPublish?.id, + }) + .andWhere( + data.root != undefined && data.root != null + ? data.root[0] != null + ? `current_holders.orgRootId IN (:...root)` + : `current_holders.orgRootId is null` + : "1=1", + { + root: data.root, + }, + ) + .andWhere( + data.child1 != undefined && data.child1 != null + ? data.child1[0] != null + ? `current_holders.orgChild1Id IN (:...child1)` + : `current_holders.orgChild1Id is null` + : "1=1", + { + child1: data.child1, + }, + ) + .andWhere( + data.child2 != undefined && data.child2 != null + ? data.child2[0] != null + ? `current_holders.orgChild2Id IN (:...child2)` + : `current_holders.orgChild2Id is null` + : "1=1", + { + child2: data.child2, + }, + ) + .andWhere( + data.child3 != undefined && data.child3 != null + ? data.child3[0] != null + ? `current_holders.orgChild3Id IN (:...child3)` + : `current_holders.orgChild3Id is null` + : "1=1", + { + child3: data.child3, + }, + ) + .andWhere( + data.child4 != undefined && data.child4 != null + ? data.child4[0] != null + ? `current_holders.orgChild4Id IN (:...child4)` + : `current_holders.orgChild4Id is null` + : "1=1", + { + child4: data.child4, + }, + ) .andWhere( new Brackets((qb) => { qb.where( @@ -163,7 +226,8 @@ export class DevelopmentRequestController extends Controller { { keyword: `%${keyword}%`, }, - ).orWhere( + ) + .orWhere( keyword != null && keyword != "" ? "developmentRequest.createdFullName LIKE :keyword" : "1=1", @@ -177,7 +241,8 @@ export class DevelopmentRequestController extends Controller { .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); - return new HttpSuccess({ data: lists, total }); + const _data = lists.map((item) => ({ ...item, profile: null })); + return new HttpSuccess({ data: _data, total }); } @Get("admin/{id}") @@ -263,7 +328,7 @@ export class DevelopmentRequestController extends Controller { sysName: "REGISTRY_IDP", posLevelName: profile.posLevel.posLevelName, posTypeName: profile.posType.posTypeName, - fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}` + fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, }) .catch((error) => { console.error("Error calling API:", error); diff --git a/src/controllers/ProfileEditController.ts b/src/controllers/ProfileEditController.ts index 605e141f..5d257956 100644 --- a/src/controllers/ProfileEditController.ts +++ b/src/controllers/ProfileEditController.ts @@ -21,6 +21,8 @@ import { CreateProfileEdit, EditProfileEdit, ProfileEdit } from "../entities/Pro import { RequestWithUser } from "../middlewares/user"; import { Brackets } from "typeorm"; import CallAPI from "../interfaces/call-api"; +import permission from "../interfaces/permission"; +import { OrgRevision } from "../entities/OrgRevision"; @Route("api/v1/org/profile/edit") @Tags("ProfileEdit") @@ -28,6 +30,7 @@ import CallAPI from "../interfaces/call-api"; export class ProfileEditController extends Controller { private profileRepo = AppDataSource.getRepository(Profile); private profileEditRepo = AppDataSource.getRepository(ProfileEdit); + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); @Get("user") public async detailProfileEditUser( @@ -55,17 +58,19 @@ export class ProfileEditController extends Controller { new Brackets((qb) => { qb.where(keyword != "" && keyword != null ? "ProfileEdit.topic LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, - }).orWhere( - keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1", - { - keyword: `%${keyword}%`, - }, - ).orWhere( - keyword != "" && keyword != null ? "ProfileEdit.remark LIKE :keyword" : "1=1", - { - keyword: `%${keyword}%`, - }, - ); + }) + .orWhere( + keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != "" && keyword != null ? "ProfileEdit.remark LIKE :keyword" : "1=1", + { + keyword: `%${keyword}%`, + }, + ); }), ) .orderBy("ProfileEdit.createdAt", "DESC") @@ -95,42 +100,107 @@ export class ProfileEditController extends Controller { @Get("admin") public async detailProfileEditAdmin( - @Request() request: { user: Record }, + @Request() request: RequestWithUser, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword: string = "", @Query("status") status: string = "", ) { + let data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); + const orgRevisionPublish = await this.orgRevisionRepository + .createQueryBuilder("orgRevision") + .where("orgRevision.orgRevisionIsDraft = false") + .andWhere("orgRevision.orgRevisionIsCurrent = true") + .getOne(); let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit) .createQueryBuilder("ProfileEdit") .leftJoinAndSelect("ProfileEdit.profile", "profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.orgRevision", "orgRevision") .where((qb) => { if (status != "" && status != null) { qb.andWhere("ProfileEdit.status = :status", { status: status }); } qb.andWhere("ProfileEdit.profileId IS NOT NULL"); }) + .andWhere(orgRevisionPublish ? `current_holders.orgRevisionId = :revisionId` : "1=1", { + revisionId: orgRevisionPublish?.id, + }) + .andWhere( + data.root != undefined && data.root != null + ? data.root[0] != null + ? `current_holders.orgRootId IN (:...root)` + : `current_holders.orgRootId is null` + : "1=1", + { + root: data.root, + }, + ) + .andWhere( + data.child1 != undefined && data.child1 != null + ? data.child1[0] != null + ? `current_holders.orgChild1Id IN (:...child1)` + : `current_holders.orgChild1Id is null` + : "1=1", + { + child1: data.child1, + }, + ) + .andWhere( + data.child2 != undefined && data.child2 != null + ? data.child2[0] != null + ? `current_holders.orgChild2Id IN (:...child2)` + : `current_holders.orgChild2Id is null` + : "1=1", + { + child2: data.child2, + }, + ) + .andWhere( + data.child3 != undefined && data.child3 != null + ? data.child3[0] != null + ? `current_holders.orgChild3Id IN (:...child3)` + : `current_holders.orgChild3Id is null` + : "1=1", + { + child3: data.child3, + }, + ) + .andWhere( + data.child4 != undefined && data.child4 != null + ? data.child4[0] != null + ? `current_holders.orgChild4Id IN (:...child4)` + : `current_holders.orgChild4Id is null` + : "1=1", + { + child4: data.child4, + }, + ) .andWhere( new Brackets((qb) => { qb.where(keyword != "" && keyword != null ? "ProfileEdit.topic LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, - }).orWhere( - keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1", - { - keyword: `%${keyword}%`, - }, - ).orWhere( - keyword != "" && keyword != null ? "ProfileEdit.remark LIKE :keyword" : "1=1", - { - keyword: `%${keyword}%`, - }, - ).orWhere( - keyword != "" && keyword != null ? - "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword" : "1=1", - { - keyword: `%${keyword}%`, - }, - ); + }) + .orWhere( + keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != "" && keyword != null ? "ProfileEdit.remark LIKE :keyword" : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != "" && keyword != null + ? "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ); }), ) .orderBy("ProfileEdit.createdAt", "DESC") @@ -230,7 +300,7 @@ export class ProfileEditController extends Controller { sysName: "REGISTRY_PROFILE", posLevelName: profile.posLevel.posLevelName, posTypeName: profile.posType.posTypeName, - fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}` + fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, }) .catch((error) => { console.error("Error calling API:", error); diff --git a/src/controllers/ProfileEditEmployeeController.ts b/src/controllers/ProfileEditEmployeeController.ts index 64fd1421..03e56021 100644 --- a/src/controllers/ProfileEditEmployeeController.ts +++ b/src/controllers/ProfileEditEmployeeController.ts @@ -24,6 +24,8 @@ import { import { ProfileEmployee } from "../entities/ProfileEmployee"; import { RequestWithUser } from "../middlewares/user"; import { Brackets } from "typeorm"; +import permission from "../interfaces/permission"; +import { OrgRevision } from "../entities/OrgRevision"; @Route("api/v1/org/profile-employee/edit") @Tags("ProfileEmployeeEdit") @@ -31,6 +33,7 @@ import { Brackets } from "typeorm"; export class ProfileEditEmployeeController extends Controller { private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private profileEditRepository = AppDataSource.getRepository(ProfileEdit); + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); @Get("user") public async detailProfileEditUserEmp( @@ -60,17 +63,19 @@ export class ProfileEditEmployeeController extends Controller { new Brackets((qb) => { qb.where(keyword != "" && keyword != null ? "ProfileEdit.topic LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, - }).orWhere( - keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1", - { - keyword: `%${keyword}%`, - }, - ).orWhere( - keyword != "" && keyword != null ? "ProfileEdit.remark LIKE :keyword" : "1=1", - { - keyword: `%${keyword}%`, - }, - ); + }) + .orWhere( + keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != "" && keyword != null ? "ProfileEdit.remark LIKE :keyword" : "1=1", + { + keyword: `%${keyword}%`, + }, + ); }), ) .orderBy("ProfileEdit.createdAt", "DESC") @@ -100,42 +105,107 @@ export class ProfileEditEmployeeController extends Controller { @Get("admin") public async detailProfileEditAdminEmp( - @Request() request: { user: Record }, + @Request() request: RequestWithUser, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword: string = "", @Query("status") status: string = "", ) { + let data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_EMP"); + const orgRevisionPublish = await this.orgRevisionRepository + .createQueryBuilder("orgRevision") + .where("orgRevision.orgRevisionIsDraft = false") + .andWhere("orgRevision.orgRevisionIsCurrent = true") + .getOne(); let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit) .createQueryBuilder("ProfileEdit") .leftJoinAndSelect("ProfileEdit.profileEmployee", "profileEmployee") + .leftJoinAndSelect("profileEmployee.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.orgRevision", "orgRevision") .where((qb) => { if (status != "" && status != null) { qb.andWhere("ProfileEdit.status = :status", { status: status }); } qb.andWhere("ProfileEdit.profileEmployeeId IS NOT NULL"); }) + .andWhere(orgRevisionPublish ? `current_holders.orgRevisionId = :revisionId` : "1=1", { + revisionId: orgRevisionPublish?.id, + }) + .andWhere( + data.root != undefined && data.root != null + ? data.root[0] != null + ? `current_holders.orgRootId IN (:...root)` + : `current_holders.orgRootId is null` + : "1=1", + { + root: data.root, + }, + ) + .andWhere( + data.child1 != undefined && data.child1 != null + ? data.child1[0] != null + ? `current_holders.orgChild1Id IN (:...child1)` + : `current_holders.orgChild1Id is null` + : "1=1", + { + child1: data.child1, + }, + ) + .andWhere( + data.child2 != undefined && data.child2 != null + ? data.child2[0] != null + ? `current_holders.orgChild2Id IN (:...child2)` + : `current_holders.orgChild2Id is null` + : "1=1", + { + child2: data.child2, + }, + ) + .andWhere( + data.child3 != undefined && data.child3 != null + ? data.child3[0] != null + ? `current_holders.orgChild3Id IN (:...child3)` + : `current_holders.orgChild3Id is null` + : "1=1", + { + child3: data.child3, + }, + ) + .andWhere( + data.child4 != undefined && data.child4 != null + ? data.child4[0] != null + ? `current_holders.orgChild4Id IN (:...child4)` + : `current_holders.orgChild4Id is null` + : "1=1", + { + child4: data.child4, + }, + ) .andWhere( new Brackets((qb) => { qb.where(keyword != "" && keyword != null ? "ProfileEdit.topic LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, - }).orWhere( - keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1", - { - keyword: `%${keyword}%`, - }, - ).orWhere( - keyword != "" && keyword != null ? "ProfileEdit.remark LIKE :keyword" : "1=1", - { - keyword: `%${keyword}%`, - }, - ).orWhere( - keyword != "" && keyword != null ? - "CONCAT(profileEmployee.prefix, profileEmployee.firstName, ' ', profileEmployee.lastName) LIKE :keyword" : "1=1", - { - keyword: `%${keyword}%`, - }, - ); + }) + .orWhere( + keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != "" && keyword != null ? "ProfileEdit.remark LIKE :keyword" : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != "" && keyword != null + ? "CONCAT(profileEmployee.prefix, profileEmployee.firstName, ' ', profileEmployee.lastName) LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ); }), ) .orderBy("ProfileEdit.createdAt", "DESC") diff --git a/src/entities/ProfileAbility.ts b/src/entities/ProfileAbility.ts index 1f4c8064..c20fc496 100644 --- a/src/entities/ProfileAbility.ts +++ b/src/entities/ProfileAbility.ts @@ -78,8 +78,7 @@ export class ProfileAbility extends EntityBase { @Column({ nullable: false, - comment: "ข้อมูลจาก Entry", - default: null, + comment: "ข้อมูลจาก Entry", }) isEntry: boolean;