diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index ca701323..7315ac5c 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -5976,6 +5976,322 @@ export class ProfileController extends Controller { return new HttpSuccess({ data: data, total }); } + /** + * API รายการทะเบียนประวัติที่เมนูระบบพัฒนา + * + * @summary รายการทะเบียนประวัติที่เมนูระบบพัฒนา + * + */ + @Get("development") + @Example({ + status: 200, + message: "สำเร็จ", + result: { + data: [ + { + id: "ecb0b34c-037e-41f2-b95e-7e19f88b42ae", + createdAt: "2024-03-24T12:39:12.105Z", + createdUserId: "00000000-0000-0000-0000-000000000000", + lastUpdatedAt: "2024-03-24T12:41:43.164Z", + lastUpdateUserId: "00000000-0000-0000-0000-000000000000", + createdFullName: "string", + lastUpdateFullName: "string", + rank: null, + prefix: null, + firstName: "Methapon", + lastName: "Metanipat", + citizenId: null, + position: null, + posLevelId: null, + posTypeId: null, + email: null, + phone: null, + keycloak: null, + isProbation: false, + dateRetire: null, + birthDate: null, + ethnicity: null, + telephoneNumber: null, + gender: null, + relationship: null, + bloodGroup: null, + posLevel: null, + posType: null, + org: null, + }, + ], + total: 1, + }, + }) + // ...existing code... + async listProfileDev( + @Request() request: RequestWithUser, + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query() + searchField?: "firstName" | "lastName" | "fullName" | "citizenId" | "position" | "posNo", + @Query() searchKeyword: string = "", + @Query() posType?: string, + @Query() posLevel?: string, + @Query() yearLeave?: number, + @Query() isProbation?: boolean, + @Query() isRetire?: boolean, + @Query() node?: number, + @Query() nodeId?: string, + @Query() isAll?: boolean, + @Query() retireType?: string, + // @Query() sortBy: string = "current_holders.posMasterNo", + @Query() sort: "ASC" | "DESC" = "ASC", + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, + ) { + let _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); + let queryLike = + "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword"; + if (searchField == "citizenId") { + queryLike = "profile.citizenId LIKE :keyword"; + } else if (searchField == "position") { + queryLike = "profile.position LIKE :keyword"; + } else if (searchField == "posNo") { + queryLike = ` + CASE + WHEN current_holders.orgChild4Id IS NOT NULL THEN CONCAT(orgChild4.orgChild4ShortName, " ", current_holders.posMasterNo) + WHEN current_holders.orgChild3Id IS NOT NULL THEN CONCAT(orgChild3.orgChild3ShortName, " ", current_holders.posMasterNo) + WHEN current_holders.orgChild2Id IS NOT NULL THEN CONCAT(orgChild2.orgChild2ShortName, " ", current_holders.posMasterNo) + WHEN current_holders.orgChild1Id IS NOT NULL THEN CONCAT(orgChild1.orgChild1ShortName, " ", current_holders.posMasterNo) + ELSE CONCAT(orgRoot.orgRootShortName, " ", current_holders.posMasterNo) + END LIKE :keyword + `; + } + let nodeCondition = "1=1"; + let nodeAll = ""; + if (node === 0 && nodeId) { + nodeCondition = "current_holders.orgRootId = :nodeId"; + if (isAll == false) nodeAll = " AND current_holders.orgChild1Id IS NULL"; + } else if (node === 1 && nodeId) { + nodeCondition = "current_holders.orgChild1Id = :nodeId"; + if (isAll == false) nodeAll = " AND current_holders.orgChild2Id IS NULL"; + } else if (node === 2 && nodeId) { + nodeCondition = "current_holders.orgChild2Id = :nodeId"; + if (isAll == false) nodeAll = " AND current_holders.orgChild3Id IS NULL"; + } else if (node === 3 && nodeId) { + nodeCondition = "current_holders.orgChild3Id = :nodeId"; + if (isAll == false) nodeAll = " AND current_holders.orgChild4Id IS NULL"; + } else if (node === 4 && nodeId) { + nodeCondition = "current_holders.orgChild4Id = :nodeId"; + } + nodeCondition = nodeCondition + nodeAll; + // เลือกเฉพาะฟิลด์ที่จำเป็น + let query = await this.profileRepo + .createQueryBuilder("profile") + .leftJoin("profile.posLevel", "posLevel") + .leftJoin("profile.posType", "posType") + .leftJoin("profile.current_holders", "current_holders") + .leftJoin("current_holders.positions", "positions") + .leftJoin("positions.posExecutive", "posExecutive") + .leftJoin("current_holders.orgRoot", "orgRoot") + .leftJoin("current_holders.orgChild1", "orgChild1") + .leftJoin("current_holders.orgChild2", "orgChild2") + .leftJoin("current_holders.orgChild3", "orgChild3") + .leftJoin("current_holders.orgChild4", "orgChild4") + .select([ + "profile.id", + "profile.prefix", + "profile.rank", + "profile.firstName", + "profile.lastName", + "profile.citizenId", + "profile.position", + "posLevel.id", + "posLevel.posLevelName", + "posType.id", + "posType.posTypeName", + "current_holders.orgRevisionId", + "current_holders.posMasterNo", + "orgRoot.id", + "orgRoot.ancestorDNA", + "orgRoot.orgRootName", + "orgRoot.orgRootShortName", + "orgRoot.orgRootOrder", + "orgChild1.orgChild1Name", + "orgChild1.orgChild1ShortName", + "orgChild1.orgChild1Order", + "orgChild2.orgChild2Name", + "orgChild2.orgChild2ShortName", + "orgChild2.orgChild2Order", + "orgChild3.orgChild3Name", + "orgChild3.orgChild3ShortName", + "orgChild3.orgChild3Order", + "orgChild4.orgChild4Name", + "orgChild4.orgChild4ShortName", + "orgChild4.orgChild4Order", + "positions.id", + "posExecutive.id", + "posExecutive.posExecutiveName" + ]) + .where("current_holders.orgRevisionId = :orgRevisionId", { + orgRevisionId: ( + await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true } }) + )?.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(posType ? "posType.posTypeName LIKE :keyword1" : "1=1", { keyword1: `${posType}` }) + .andWhere(posLevel ? "posLevel.posLevelName LIKE :keyword2" : "1=1", { + keyword2: `${posLevel}`, + }) + .andWhere( + isProbation !== undefined && isProbation !== null + ? `profile.isProbation = ${isProbation}` + : "1=1", + ) + .andWhere( + isRetire !== undefined && isRetire !== null + ? isRetire == false + ? `profile.isLeave IS FALSE` + : isRetire == true && retireType + ? `profile.isLeave IS TRUE AND profile.leaveType = '${retireType}'` + : `profile.isLeave IS TRUE` + : "1=1", + ) + .andWhere(nodeCondition, { nodeId: nodeId }) + .andWhere( + new Brackets((qb) => { + qb.orWhere(searchKeyword ? queryLike : "1=1", { keyword: `%${searchKeyword}%` }); + }), + ) + .addSelect("CASE WHEN current_holders.posMasterNo IS NULL THEN 1 ELSE 0 END", "sort_order") + + if (sortBy) { + if(sortBy === "posLevel"){ + query = query.orderBy( + `posLevel.posLevelName`, + descending ? "DESC" : "ASC" + ); + }else if(sortBy === "posType"){ + query = query.orderBy( + `posType.posTypeName`, + descending ? "DESC" : "ASC" + ); + }else if(sortBy === "posExecutive"){ + query = query.orderBy( + `posExecutive.posExecutiveName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `profile.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + }else{ + query = query.orderBy("sort_order", "ASC") + .addOrderBy("orgRoot.orgRootOrder", sort) + .addOrderBy("orgChild1.orgChild1Order", sort) + .addOrderBy("orgChild2.orgChild2Order", sort) + .addOrderBy("orgChild3.orgChild3Order", sort) + .addOrderBy("orgChild4.orgChild4Order", sort) + .addOrderBy("current_holders.posMasterNo", sort) + } + + const [record, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + + // map ข้อมูลแบบเร็วขึ้น + const data = record.map((_data) => { + const holder = _data.current_holders?.[0]; + const org = [ + holder?.orgChild4?.orgChild4Name, + holder?.orgChild3?.orgChild3Name, + holder?.orgChild2?.orgChild2Name, + holder?.orgChild1?.orgChild1Name, + holder?.orgRoot?.orgRootName, + ] + .filter(Boolean) + .join("\n"); + + const shortName = !holder + ? null + : holder.orgChild4 != null + ? `${holder.orgChild4.orgChild4ShortName} ${holder.posMasterNo}` + : holder.orgChild3 != null + ? `${holder.orgChild3.orgChild3ShortName} ${holder.posMasterNo}` + : holder.orgChild2 != null + ? `${holder.orgChild2.orgChild2ShortName} ${holder.posMasterNo}` + : holder.orgChild1 != null + ? `${holder.orgChild1.orgChild1ShortName} ${holder.posMasterNo}` + : holder.orgRoot != null + ? `${holder.orgRoot.orgRootShortName} ${holder.posMasterNo}` + : null; + + return { + id: _data.id, + avatar: _data.avatar, + avatarName: _data.avatarName, + prefix: _data.prefix, + rank: _data.rank, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + posLevel: _data.posLevel?.posLevelName ?? null, + posType: _data.posType?.posTypeName ?? null, + posLevelId: _data.posLevel?.id ?? null, + posTypeId: _data.posType?.id ?? null, + position: _data.position, + posExecutive: _data.current_holders[0]?.positions[0]?.posExecutive?.posExecutiveName ?? null, + posNo: shortName ?? null, + rootId: holder?.orgRoot?.id ?? null, + root: holder?.orgRoot?.orgRootName ?? null, + rootDnaId: holder?.orgRoot == null ? null : holder?.orgRoot?.ancestorDNA, + orgRootShortName: holder?.orgRoot?.orgRootShortName ?? null, + orgRevisionId: holder?.orgRoot?.orgRevisionId ?? null, + org, + }; + }); + + return new HttpSuccess({ data, total }); + } + /** * API รายละเอียดรายการทะเบียนประวัติ * @@ -6114,10 +6430,8 @@ export class ProfileController extends Controller { @Query() nodeId?: string, @Query() isAll?: boolean, @Query() retireType?: string, - // @Query() sortBy: string = "current_holders.posMasterNo", + @Query() sortBy: string = "current_holders.posMasterNo", @Query() sort: "ASC" | "DESC" = "ASC", - @Query("sortBy") sortBy?: string, - @Query("descending") descending?: boolean, ) { let _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); let queryLike = @@ -6157,7 +6471,7 @@ export class ProfileController extends Controller { nodeCondition = nodeCondition + nodeAll; // เลือกเฉพาะฟิลด์ที่จำเป็น - let query = await this.profileRepo + const [record,total] = await this.profileRepo .createQueryBuilder("profile") .leftJoin("profile.posLevel", "posLevel") .leftJoin("profile.posType", "posType") @@ -6282,30 +6596,14 @@ export class ProfileController extends Controller { qb.orWhere(searchKeyword ? queryLike : "1=1", { keyword: `%${searchKeyword}%` }); }), ) - .addSelect("CASE WHEN current_holders.posMasterNo IS NULL THEN 1 ELSE 0 END", "sort_order"); - - if (sortBy) { - if (sortBy === "posLevel") { - query = query.orderBy(`posLevel.posLevelName`, descending ? "DESC" : "ASC"); - } else if (sortBy === "posType") { - query = query.orderBy(`posType.posTypeName`, descending ? "DESC" : "ASC"); - } else if (sortBy === "posExecutive") { - query = query.orderBy(`posExecutive.posExecutiveName`, descending ? "DESC" : "ASC"); - } else { - query = query.orderBy(`profile.${sortBy}`, descending ? "DESC" : "ASC"); - } - } else { - query = query - .orderBy("sort_order", "ASC") - .addOrderBy("orgRoot.orgRootOrder", sort) - .addOrderBy("orgChild1.orgChild1Order", sort) - .addOrderBy("orgChild2.orgChild2Order", sort) - .addOrderBy("orgChild3.orgChild3Order", sort) - .addOrderBy("orgChild4.orgChild4Order", sort) - .addOrderBy("current_holders.posMasterNo", sort); - } - - const [record, total] = await query + .addSelect("CASE WHEN current_holders.posMasterNo IS NULL THEN 1 ELSE 0 END", "sort_order") + .orderBy("sort_order", "ASC") + .addOrderBy("orgRoot.orgRootOrder", sort) + .addOrderBy("orgChild1.orgChild1Order", sort) + .addOrderBy("orgChild2.orgChild2Order", sort) + .addOrderBy("orgChild3.orgChild3Order", sort) + .addOrderBy("orgChild4.orgChild4Order", sort) + .addOrderBy("current_holders.posMasterNo", sort) .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); @@ -6367,6 +6665,7 @@ export class ProfileController extends Controller { return new HttpSuccess({ data, total }); } + /** * API ค้นหารายชื่อไปครองตำแหน่ง *