diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 8046056a..4e5f898e 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -160,6 +160,8 @@ export class CommandController extends Controller { @Get("list") async GetResult( @Request() request: RequestWithUser, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query() keyword: string = "", @@ -262,14 +264,9 @@ export class CommandController extends Controller { yearKeyword = match[1].trim(); } let yearInBC = yearKeyword ? parseInt(yearKeyword) - 543 : null; - // - - // console.log("k>>",keyword); - // console.log("bk>>",baseKeyword); - // console.log("yk>>",yearKeyword); - // console.log("yi>>",yearInBC); - - const [commands, total] = await this.commandRepository + + // const [commands, total] = await this.commandRepository + let query = await this.commandRepository .createQueryBuilder("command") .leftJoinAndSelect("command.commandType", "commandType") .andWhere( @@ -339,6 +336,15 @@ export class CommandController extends Controller { }), ) .orderBy("command.createdAt", "DESC") + + if (sortBy) { + query = query.orderBy( + `command.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + + let [commands, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); @@ -3012,6 +3018,38 @@ export class CommandController extends Controller { return new HttpSuccess(_data); } + /** + * API อัพเดทสถานะ CANCEL คำสั่งยกเลิกการลาออก, คำสั่งยกเลิกการลาออกลูกจ้าง + * + * @summary API อัพเดทสถานะ CANCEL คำสั่งยกเลิกการลาออก, คำสั่งยกเลิกการลาออกลูกจ้าง + * + */ + @Post("cancel-resign") + public async command41Excecute( + @Request() req: RequestWithUser, + @Body() + body: { + resignId: string[]; + }, + ) { + const _refId = Array.from(new Set(body.resignId)); + // 1. ดึง commandRecive ที่ refId ตรงกับ resignId + const commandRecives = await this.commandReciveRepository.find({ + select: ["commandId"], + where: { refId: In(_refId) }, + }); + // 2. ดึง commandId ที่ไม่ซ้ำ + const commandIds = Array.from(new Set(commandRecives.map(x => x.commandId).filter(Boolean))); + // 3. อัปเดต status ของ command + if (commandIds.length > 0) { + await this.commandRepository.update( + { id: In(commandIds) }, + { status: "CANCEL" } + ); + } + return new HttpSuccess(); + } + @Post("excexute/salary-current") public async newSalaryAndUpdateCurrent( @Request() req: RequestWithUser, @@ -3443,6 +3481,7 @@ export class CommandController extends Controller { orgChild2New?: string | null; orgChild3New?: string | null; orgChild4New?: string | null; + resignId?: string | null; }[]; }, ) { @@ -3507,6 +3546,19 @@ export class CommandController extends Controller { if (code && ["C-PM-08", "C-PM-17", "C-PM-18"].includes(code)) { removePostMasterAct(profile.id); } + //ออกคำสั่งยกเลิกลาออกต้องอัพเดทสถานะคำสั่งลาออกเป็น CANCEL + else if (item.resignId && code && ["C-PM-41"].includes(code)) { + const commandRecive = await this.commandReciveRepository.findOne({ + select: ["commandId"], + where: { refId: item.resignId }, + }); + if (commandRecive && commandRecive.commandId) { + await this.commandRepository.update( + { id: commandRecive?.commandId }, + { status: "CANCEL" } + ); + } + } let _commandYear = item.commandYear; if (item.commandYear) { _commandYear = item.commandYear > 2500 ? item.commandYear : item.commandYear + 543; @@ -3773,6 +3825,7 @@ export class CommandController extends Controller { commandCode?: string | null; commandName?: string | null; remark: string | null; + resignId: string | null; }[]; }, ) { @@ -3828,6 +3881,20 @@ export class CommandController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + const code = _command?.commandType?.code; + //ออกคำสั่งยกเลิกลาออกต้องอัพเดทสถานะคำสั่งลาออกเป็น CANCEL + if (item.resignId && code && ["C-PM-42"].includes(code)) { + const commandRecive = await this.commandReciveRepository.findOne({ + select: ["commandId"], + where: { refId: item.resignId }, + }); + if (commandRecive && commandRecive.commandId) { + await this.commandRepository.update( + { id: commandRecive?.commandId }, + { status: "CANCEL" } + ); + } + } let _commandYear = item.commandYear; if (item.commandYear) { _commandYear = item.commandYear > 2500 ? item.commandYear : item.commandYear + 543; diff --git a/src/controllers/DevelopmentRequestController.ts b/src/controllers/DevelopmentRequestController.ts index 752ae293..7c646230 100644 --- a/src/controllers/DevelopmentRequestController.ts +++ b/src/controllers/DevelopmentRequestController.ts @@ -127,6 +127,8 @@ export class DevelopmentRequestController extends Controller { @Query("keyword") keyword: string = "", @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); const orgRevisionPublish = await this.orgRevisionRepository @@ -134,7 +136,7 @@ export class DevelopmentRequestController extends Controller { .where("orgRevision.orgRevisionIsDraft = false") .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); - const [lists, total] = await AppDataSource.getRepository(DevelopmentRequest) + let query = await AppDataSource.getRepository(DevelopmentRequest) .createQueryBuilder("developmentRequest") .leftJoinAndSelect("developmentRequest.profile", "profile") .leftJoinAndSelect("profile.current_holders", "current_holders") @@ -249,9 +251,20 @@ export class DevelopmentRequestController extends Controller { }), ) .orderBy("developmentRequest.createdAt", "DESC") - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + + + if (sortBy) { + query = query.orderBy( + `developmentRequest.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + + const [lists, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + const _data = lists.map((item) => ({ ...item, profile: null })); return new HttpSuccess({ data: _data, total }); } diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 20511811..5ad7c11c 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -30,6 +30,7 @@ import { checkQueueInProgress, setLogDataDiff } from "../interfaces/utils"; import { sendToQueueOrg, sendToQueueOrgDraft } from "../services/rabbitmq"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; +import { PermissionOrg } from "../entities/PermissionOrg"; @Route("api/v1/org") @Tags("Organization") @@ -49,6 +50,7 @@ export class OrganizationController extends Controller { private profileRepo = AppDataSource.getRepository(Profile); private posTypeRepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); + private permissionOrgRepository = AppDataSource.getRepository(PermissionOrg); /** * API ล้างข้อมูล @@ -7869,4 +7871,48 @@ export class OrganizationController extends Controller { posLevelNameOrder: posLevel.map((x) => x.posLevelName), }); } + + /** + * API เพิ่มสิทธิ์โครงสร้าง + * + * @summary - เพิ่มสิทธิ์โครงสร้าง (ADMIN) + * + */ + @Get("root/add/permission/{child1Id}") + async addRootPermission(@Path() child1Id: string, @Request() request: RequestWithUser) { + const profiles = await this.profileRepo.find({ + where: { + keycloak: Not(IsNull()), + current_holders: { + orgChild1Id: child1Id, + }, + }, + }); + const orgRoots = await this.orgRootRepository.find({ + where: { + orgRevision: { + orgRevisionIsDraft: true, + orgRevisionIsCurrent: false, + }, + }, + }); + + for await (const root of orgRoots) { + const _permissionOrg = profiles.map((profile) => { + const permission = new PermissionOrg(); + permission.orgRootId = root.id; + permission.profileId = profile.id; + permission.createdUserId = request.user.sub; + permission.createdFullName = request.user.name; + permission.lastUpdateUserId = request.user.sub; + permission.lastUpdateFullName = request.user.name; + permission.createdAt = new Date(); + permission.lastUpdatedAt = new Date(); + return permission; + }); + await this.permissionOrgRepository.save(_permissionOrg); + } + + return new HttpSuccess(); + } } diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 81781adf..33d33227 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -2585,6 +2585,8 @@ export class ProfileController extends Controller { keyword: string; page: number; pageSize: number; + sortBy?: string; + descending?: boolean; }, ) { let posMaster = await this.posMasterRepo.findOne({ @@ -2672,7 +2674,7 @@ export class ProfileController extends Controller { // condition.isDirector = true; // // conditionNow.isDirector = true; // } - const [lists, total] = await AppDataSource.getRepository(viewDirectorActing) + let query = await AppDataSource.getRepository(viewDirectorActing) .createQueryBuilder("viewDirectorActing") // .andWhere(condition) .andWhere( @@ -2724,9 +2726,19 @@ export class ProfileController extends Controller { ); }), ) - .skip((body.page - 1) * body.pageSize) - .take(body.pageSize) - .getManyAndCount(); + + if (body.sortBy) { + query = query.orderBy( + `viewDirectorActing.${body.sortBy}`, + body.descending ? "DESC" : "ASC" + ); + } + + const [lists, total] = await query + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); + return new HttpSuccess({ data: lists, total }); } else { // const posMaster = await this.posMasterRepo.findOne({ @@ -2764,7 +2776,7 @@ export class ProfileController extends Controller { // condition.isDirector = true; // // conditionNow.isDirector = true; // } - const [lists, total] = await AppDataSource.getRepository(viewDirector) + let query = await AppDataSource.getRepository(viewDirector) .createQueryBuilder("viewDirector") // .andWhere(condition) .andWhere( @@ -2816,9 +2828,19 @@ export class ProfileController extends Controller { ); }), ) + + if (body.sortBy) { + query = query.orderBy( + `viewDirector.${body.sortBy}`, + body.descending ? "DESC" : "ASC" + ); + } + + const [lists, total] = await query .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); + return new HttpSuccess({ data: lists, total }); } } @@ -8545,6 +8567,8 @@ export class ProfileController extends Controller { keyword?: string; system?: string; }, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean ) { // ค้นหารายชื่อถ้าไม่ส่ง system มาให้ default ตามทะเบียนประวัติ let _system: string = "SYS_REGISTRY_OFFICER"; @@ -8616,7 +8640,7 @@ export class ProfileController extends Controller { break; } - const [findProfile, total] = await this.profileRepo + let query = await this.profileRepo .createQueryBuilder("profile") .leftJoinAndSelect("profile.posType", "posType") .leftJoinAndSelect("profile.posLevel", "posLevel") @@ -8672,6 +8696,27 @@ export class ProfileController extends Controller { qb.orWhere(body.keyword ? queryLike : "1=1", { keyword: `%${body.keyword}%` }); }), ) + + if (sortBy) { + if(sortBy === "name"){ + query = query + .orderBy(`profile.prefix`,descending ? "DESC" : "ASC") + .addOrderBy(`profile.firstName`,descending ? "DESC" : "ASC") + .addOrderBy(`profile.lastName`,descending ? "DESC" : "ASC") + }else if(sortBy === "organization"){ + query = query.orderBy( + `orgRoot.orgRootName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `profile.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + } + + const [findProfile, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); @@ -9103,6 +9148,8 @@ export class ProfileController extends Controller { page: number; pageSize: number; keyword?: string; + sortBy?: string; + descending?: boolean; }, ) { let _data: any = { @@ -9116,7 +9163,7 @@ export class ProfileController extends Controller { if (!request.user.role.includes("SUPER_ADMIN")) { _data = await new permission().PermissionOrgCreate(request, "SYS_PROBATION"); } - const [findProfile, total] = await AppDataSource.getRepository(Profile) + let query = await AppDataSource.getRepository(Profile) .createQueryBuilder("profile") .leftJoinAndSelect("profile.profileSalary", "profileSalary") .leftJoinAndSelect("profile.posLevel", "posLevel") @@ -9230,7 +9277,39 @@ export class ProfileController extends Controller { ); }), ) - .orderBy("profile.citizenId", "ASC") + + if (body.sortBy) { + if(body.sortBy === "posLevelName"){ + query = query.orderBy( + `posLevel.posLevelName`, + body.descending ? "DESC" : "ASC" + ); + }else if(body.sortBy === "posTypeName"){ + query = query.orderBy( + `posType.posTypeName`, + body.descending ? "DESC" : "ASC" + ); + }else if(body.sortBy === "commandNo"){ + query = query.orderBy( + `profileSalary.commandNo`, + body.descending ? "DESC" : "ASC" + ); + }else if(body.sortBy === "orgRootName"){ + query = query.orderBy( + `orgRoot.orgRootName`, + body.descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `profile.${body.sortBy}`, + body.descending ? "DESC" : "ASC" + ); + } + }else{ + query = query.orderBy("profile.citizenId", "ASC") + } + + const [findProfile, total] = await query .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); diff --git a/src/controllers/ProfileDevelopmentController.ts b/src/controllers/ProfileDevelopmentController.ts index 7982fe7d..3a615a5a 100644 --- a/src/controllers/ProfileDevelopmentController.ts +++ b/src/controllers/ProfileDevelopmentController.ts @@ -58,11 +58,13 @@ export class ProfileDevelopmentController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query() searchKeyword: string = "", + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_OFFICER"); if (_workflow == false) await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId); - const [profileDevelopment, total] = await AppDataSource.getRepository(ProfileDevelopment) + let query = await AppDataSource.getRepository(ProfileDevelopment) .createQueryBuilder("profileDevelopment") .where({ profileId: profileId }) .andWhere( @@ -102,9 +104,19 @@ export class ProfileDevelopmentController extends Controller { }), ) .orderBy("profileDevelopment.createdAt", "ASC") + + if (sortBy) { + query = query.orderBy( + `profileDevelopment.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + + const [profileDevelopment, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); + return new HttpSuccess({ data: profileDevelopment, total }); } diff --git a/src/controllers/ProfileDevelopmentEmployeeController.ts b/src/controllers/ProfileDevelopmentEmployeeController.ts index c7099378..76260a1b 100644 --- a/src/controllers/ProfileDevelopmentEmployeeController.ts +++ b/src/controllers/ProfileDevelopmentEmployeeController.ts @@ -56,11 +56,14 @@ export class ProfileDevelopmentEmployeeController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query() searchKeyword: string = "", + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_EMP"); if (_workflow == false) await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_EMP", profileId); - const [profileDevelopment, total] = await AppDataSource.getRepository(ProfileDevelopment) + + let query = await AppDataSource.getRepository(ProfileDevelopment) .createQueryBuilder("profileDevelopment") .where({ profileEmployeeId: profileId }) .andWhere( @@ -100,9 +103,19 @@ export class ProfileDevelopmentEmployeeController extends Controller { }), ) .orderBy("profileDevelopment.createdAt", "ASC") + + if (sortBy) { + query = query.orderBy( + `profileDevelopment.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + + const [profileDevelopment, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); + return new HttpSuccess({ data: profileDevelopment, total }); } diff --git a/src/controllers/ProfileEditController.ts b/src/controllers/ProfileEditController.ts index 9e90d294..c7d66ee0 100644 --- a/src/controllers/ProfileEditController.ts +++ b/src/controllers/ProfileEditController.ts @@ -116,6 +116,8 @@ export class ProfileEditController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword: string = "", @Query("status") status: string = "", + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); const orgRevisionPublish = await this.orgRevisionRepository @@ -123,7 +125,7 @@ export class ProfileEditController extends Controller { .where("orgRevision.orgRevisionIsDraft = false") .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); - let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit) + let query = await AppDataSource.getRepository(ProfileEdit) .createQueryBuilder("ProfileEdit") .leftJoinAndSelect("ProfileEdit.profile", "profile") .leftJoinAndSelect("profile.current_holders", "current_holders") @@ -214,10 +216,27 @@ export class ProfileEditController extends Controller { ); }), ) - .orderBy("ProfileEdit.createdAt", "DESC") - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + .orderBy("ProfileEdit.createdAt", "DESC") + + if (sortBy) { + if(sortBy == "fullname"){ + query = query.orderBy( + `profile.firstName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `ProfileEdit.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + } + + const [getProfileEdit, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + const _data = getProfileEdit.map((item) => ({ id: item.id, idcard: item.profile.citizenId, diff --git a/src/controllers/ProfileEditEmployeeController.ts b/src/controllers/ProfileEditEmployeeController.ts index 03e56021..75fa1672 100644 --- a/src/controllers/ProfileEditEmployeeController.ts +++ b/src/controllers/ProfileEditEmployeeController.ts @@ -110,6 +110,8 @@ export class ProfileEditEmployeeController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword: string = "", @Query("status") status: string = "", + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_EMP"); const orgRevisionPublish = await this.orgRevisionRepository @@ -117,7 +119,8 @@ export class ProfileEditEmployeeController extends Controller { .where("orgRevision.orgRevisionIsDraft = false") .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); - let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit) + + let query = await AppDataSource.getRepository(ProfileEdit) .createQueryBuilder("ProfileEdit") .leftJoinAndSelect("ProfileEdit.profileEmployee", "profileEmployee") .leftJoinAndSelect("profileEmployee.current_holders", "current_holders") @@ -209,9 +212,25 @@ export class ProfileEditEmployeeController extends Controller { }), ) .orderBy("ProfileEdit.createdAt", "DESC") - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + + if (sortBy) { + if(sortBy == "fullname"){ + query = query.orderBy( + `profileEmployee.firstName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `ProfileEdit.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + } + + let [getProfileEdit, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); const _data = getProfileEdit.map((item) => ({ id: item.id, diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index b00197a5..e5d03e4a 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -1504,6 +1504,8 @@ export class ProfileEmployeeTempController extends Controller { @Query() isProbation?: boolean, @Query() isRetire?: boolean, @Query() type?: string, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_TEMP"); let queryLike = @@ -1525,7 +1527,8 @@ export class ProfileEmployeeTempController extends Controller { if (!findRevision) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } - const [record, total] = await this.profileRepo + + let query = await this.profileRepo .createQueryBuilder("profileEmployee") .leftJoinAndSelect("profileEmployee.posLevel", "posLevel") .leftJoinAndSelect("profileEmployee.posType", "posType") @@ -1638,9 +1641,36 @@ export class ProfileEmployeeTempController extends Controller { } }), ) - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + + 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 == "govAge"){ + query = query.orderBy( + `profileEmployee.dateAppoint`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `profileEmployee.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + } + + const [record, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + const data = await Promise.all( record.map((_data) => { const shortName = diff --git a/src/controllers/ProfileSalaryTempController.ts b/src/controllers/ProfileSalaryTempController.ts index 81cc1751..b160f98d 100644 --- a/src/controllers/ProfileSalaryTempController.ts +++ b/src/controllers/ProfileSalaryTempController.ts @@ -64,6 +64,8 @@ export class ProfileSalaryTempController extends Controller { @Query() searchKeyword: string = "", @Query() statusCheckEdit?: string | null, @Query() rootId?: string, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { if (type.trim().toUpperCase() == "OFFICER") { let _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); @@ -78,7 +80,7 @@ export class ProfileSalaryTempController extends Controller { if (!findRevision) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } - const [record, total] = await this.profileRepo + let query = await this.profileRepo .createQueryBuilder("profile") .leftJoinAndSelect("profile.posLevel", "posLevel") .leftJoinAndSelect("profile.posType", "posType") @@ -321,17 +323,51 @@ export class ProfileSalaryTempController extends Controller { rootId: rootId, }) .addSelect("CASE WHEN current_holders.posMasterNo IS NULL THEN 1 ELSE 0 END", "sort_order") - .orderBy("sort_order", "ASC") + // .orderBy(`${sortBy}`, sort) + + 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 if(sortBy == "posNo"){ + query = query.orderBy("orgChild4.orgChild4ShortName",descending ? "DESC" : "ASC") + .addOrderBy("orgChild3.orgChild3ShortName",descending ? "DESC" : "ASC") + .addOrderBy("orgChild2.orgChild2ShortName",descending ? "DESC" : "ASC") + .addOrderBy("orgChild1.orgChild1ShortName",descending ? "DESC" : "ASC") + .addOrderBy("orgRoot.orgRootShortName",descending ? "DESC" : "ASC") + .addOrderBy("current_holders.posMasterNo",descending ? "DESC" : "ASC") + }else{ + query = query.orderBy( + `profile.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + }else{ + query = query.orderBy("sort_order", "ASC") .addOrderBy("orgRoot.orgRootOrder", "ASC") .addOrderBy("orgChild1.orgChild1Order", "ASC") .addOrderBy("orgChild2.orgChild2Order", "ASC") .addOrderBy("orgChild3.orgChild3Order", "ASC") .addOrderBy("orgChild4.orgChild4Order", "ASC") .addOrderBy("current_holders.posMasterNo", "ASC") - // .orderBy(`${sortBy}`, sort) - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + } + + const [record, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); const data = await Promise.all( record.map((_data) => { @@ -465,7 +501,7 @@ export class ProfileSalaryTempController extends Controller { if (!findRevision) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } - const [record, total] = await this.profileEmployeeRepo + let query = await this.profileEmployeeRepo .createQueryBuilder("profileEmployee") .leftJoinAndSelect("profileEmployee.posLevel", "posLevel") .leftJoinAndSelect("profileEmployee.posType", "posType") @@ -721,9 +757,35 @@ export class ProfileSalaryTempController extends Controller { }) .orderBy("current_holders.posMasterNo", "ASC") // .orderBy(`${sortBy}`, sort) - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + + 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 == "posNo"){ + query = query.orderBy( + `orgRoot.orgRootShortName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `profileEmployee.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + } + const [record, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + const data = await Promise.all( record.map((_data) => { const shortName = diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 811a2c8b..d98800b0 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -4592,11 +4592,6 @@ export class ReportController extends Controller { (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); - if (positionMasterOld && positionMasterOld.positions) { - profilePositionName = [ - ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), - ]; - } if (positionMasterOld && positionMasterOld.positions) { profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), @@ -4684,6 +4679,11 @@ export class ReportController extends Controller { } } } + if (positionMasterProfileOld == null && posMaster.next_holder != null + && posMaster.next_holder.current_holders == null + ) { + positionMasterProfileOld = positionMasterOld + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -4783,17 +4783,23 @@ export class ReportController extends Controller { ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posType.posTypeName, + // : posMaster.next_holder.posType.posTypeName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posType?.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posLevel.posLevelName, + // : posMaster.next_holder.posLevel.posLevelName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posLevel?.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null @@ -4811,12 +4817,18 @@ export class ReportController extends Controller { if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + profileFullname: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgName.toString()) + : Extension.ToThaiNumber(node.orgTreeName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosMasterNo: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgShortName.toString()) + : Extension.ToThaiNumber(node.orgTreeShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", @@ -5087,7 +5099,11 @@ export class ReportController extends Controller { } } } - + if (positionMasterProfileOld == null && posMaster.next_holder != null + && posMaster.next_holder.current_holders == null + ) { + positionMasterProfileOld = positionMasterOld + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -5188,17 +5204,23 @@ export class ReportController extends Controller { ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posType.posTypeName, + // : posMaster.next_holder.posType.posTypeName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posType?.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posLevel.posLevelName, + // : posMaster.next_holder.posLevel.posLevelName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posLevel?.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null @@ -5216,12 +5238,18 @@ export class ReportController extends Controller { if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + profileFullname: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgName.toString()) + : Extension.ToThaiNumber(node.orgTreeName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosMasterNo: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgShortName.toString()) + : Extension.ToThaiNumber(node.orgTreeShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", @@ -5499,7 +5527,11 @@ export class ReportController extends Controller { } } } - + if (positionMasterProfileOld == null && posMaster.next_holder != null + && posMaster.next_holder.current_holders == null + ) { + positionMasterProfileOld = positionMasterOld + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -5600,17 +5632,23 @@ export class ReportController extends Controller { ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posType.posTypeName, + // : posMaster.next_holder.posType.posTypeName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posType?.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posLevel.posLevelName, + // : posMaster.next_holder.posLevel.posLevelName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posLevel?.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null @@ -5628,12 +5666,18 @@ export class ReportController extends Controller { if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + profileFullname: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgName.toString()) + : Extension.ToThaiNumber(node.orgTreeName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosMasterNo: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgShortName.toString()) + : Extension.ToThaiNumber(node.orgTreeShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", @@ -5912,7 +5956,11 @@ export class ReportController extends Controller { } } } - + if (positionMasterProfileOld == null && posMaster.next_holder != null + && posMaster.next_holder.current_holders == null + ) { + positionMasterProfileOld = positionMasterOld + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -6013,17 +6061,23 @@ export class ReportController extends Controller { ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posType.posTypeName, + // : posMaster.next_holder.posType.posTypeName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posType?.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posLevel.posLevelName, + // : posMaster.next_holder.posLevel.posLevelName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posLevel?.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null @@ -6041,12 +6095,18 @@ export class ReportController extends Controller { if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + profileFullname: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgName.toString()) + : Extension.ToThaiNumber(node.orgTreeName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosMasterNo: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgShortName.toString()) + : Extension.ToThaiNumber(node.orgTreeShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", @@ -6332,7 +6392,11 @@ export class ReportController extends Controller { } } } - + if (positionMasterProfileOld == null && posMaster.next_holder != null + && posMaster.next_holder.current_holders == null + ) { + positionMasterProfileOld = positionMasterOld + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -6433,17 +6497,23 @@ export class ReportController extends Controller { ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posType.posTypeName, + // : posMaster.next_holder.posType.posTypeName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posType?.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posLevel.posLevelName, + // : posMaster.next_holder.posLevel.posLevelName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posLevel?.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null @@ -6461,12 +6531,18 @@ export class ReportController extends Controller { if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + profileFullname: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgName.toString()) + : Extension.ToThaiNumber(node.orgTreeName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosMasterNo: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgShortName.toString()) + : Extension.ToThaiNumber(node.orgTreeShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index e808a2be..8d77b1ec 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -852,10 +852,15 @@ export class KeycloakController extends Controller { // _item.birthDate.toISOString().slice(5, 7) + // gregorianYear; // password = formattedDate; - const _date = new Date(_item.birthDate.toDateString()).getDate().toString().padStart(2, "0"); - const _month = (new Date(_item.birthDate.toDateString()).getMonth()+1).toString().padStart(2, "0"); - const _year = (new Date(_item.birthDate.toDateString()).getFullYear()+543); - password = `${_date}${_month}${_year}` + const _date = new Date(_item.birthDate.toDateString()) + .getDate() + .toString() + .padStart(2, "0"); + const _month = (new Date(_item.birthDate.toDateString()).getMonth() + 1) + .toString() + .padStart(2, "0"); + const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543; + password = `${_date}${_month}${_year}`; } const checkUser = await getUserByUsername(_item.citizenId); let userId: any = ""; @@ -918,10 +923,15 @@ export class KeycloakController extends Controller { // _item.birthDate.toISOString().slice(5, 7) + // gregorianYear; // password = formattedDate; - const _date = new Date(_item.birthDate.toDateString()).getDate().toString().padStart(2, "0"); - const _month = (new Date(_item.birthDate.toDateString()).getMonth()+1).toString().padStart(2, "0"); - const _year = (new Date(_item.birthDate.toDateString()).getFullYear()+543); - password = `${_date}${_month}${_year}` + const _date = new Date(_item.birthDate.toDateString()) + .getDate() + .toString() + .padStart(2, "0"); + const _month = (new Date(_item.birthDate.toDateString()).getMonth() + 1) + .toString() + .padStart(2, "0"); + const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543; + password = `${_date}${_month}${_year}`; } const checkUser = await getUserByUsername(_item.citizenId); let userId: any = ""; @@ -990,4 +1000,83 @@ export class KeycloakController extends Controller { } return; } + + @Post("add-role-staff/user/{child1Id}") + @Security("bearerAuth", ["system", "admin"]) + async addroleStaffToUser( + @Path() child1Id: string, + @Request() request: { user: { sub: string; preferred_username: string } }, + ) { + const profiles = await this.profileRepo.find({ + where: { + keycloak: Not(IsNull()), + current_holders: { + orgChild1Id: child1Id, + }, + }, + relations: ["roleKeycloaks"], + }); + // return profiles.length; + + for await (const _item of profiles) { + let password = _item.citizenId; + if (_item.birthDate != null) { + const _date = new Date(_item.birthDate.toDateString()) + .getDate() + .toString() + .padStart(2, "0"); + const _month = (new Date(_item.birthDate.toDateString()).getMonth() + 1) + .toString() + .padStart(2, "0"); + const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543; + password = `${_date}${_month}${_year}`; + } + const checkUser = await getUserByUsername(_item.citizenId); + let userId: any = ""; + if (checkUser.length == 0) { + userId = await createUser(_item.citizenId, password, { + firstName: _item.firstName, + lastName: _item.lastName, + }); + if (typeof userId !== "string") { + throw new Error(userId.errorMessage); + } + } else { + userId = checkUser[0].id; + } + + const list = await getRoles(); + if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); + const resultUser = await addUserRoles( + userId, + list.filter((v) => v.id == "8a1a0dc9-304c-4e5b-a90a-65f841048212"), + ); + const resultStaff = await addUserRoles( + userId, + list.filter((v) => v.id == "f1fff8db-0795-47c1-9952-f3c18d5b6172"), + ); + + // if (!resultUser) { + // throw new Error("Failed. Cannot set user's role."); + // } + + // if (!resultStaff) { + // throw new Error("Failed. Cannot set staff's role."); + // } + if (typeof userId === "string") { + _item.keycloak = userId; + } + const roleKeycloakUser = await this.roleKeycloakRepo.find({ + where: { id: "8a1a0dc9-304c-4e5b-a90a-65f841048212" }, + }); + const roleKeycloakStaff = await this.roleKeycloakRepo.find({ + where: { id: "f1fff8db-0795-47c1-9952-f3c18d5b6172" }, + }); + if (_item) { + _item.roleKeycloaks = Array.from(new Set([...roleKeycloakUser, ...roleKeycloakStaff])); + this.profileRepo.save(_item); + } + } + return ""; + } }