diff --git a/src/controllers/ChangePositionController.ts b/src/controllers/ChangePositionController.ts index 273e336a..9ce7281a 100644 --- a/src/controllers/ChangePositionController.ts +++ b/src/controllers/ChangePositionController.ts @@ -22,8 +22,22 @@ import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { Equal, ILike, In, IsNull, Like, Not, Brackets, MoreThan } from "typeorm"; import { RequestWithUser } from "../middlewares/user"; -import { ChangePosition, CreateChangePosition, UpdateChangePosition } from "../entities/ChangePosition"; -import { ProfileChangePosition, CreateProfileChangePosition } from "../entities/ProfileChangePosition"; +import { + ChangePosition, + CreateChangePosition, + UpdateChangePosition +} from "../entities/ChangePosition"; +import { + ProfileChangePosition, + CreateProfileChangePosition, + UpdateProfileChangePosition, + SelectProfileChangePosition +} from "../entities/ProfileChangePosition"; +import { OrgRoot } from "../entities/OrgRoot"; +import { OrgChild1 } from "../entities/OrgChild1"; +import { OrgChild2 } from "../entities/OrgChild2"; +import { OrgChild3 } from "../entities/OrgChild3"; +import { OrgChild4 } from "../entities/OrgChild4"; @Route("api/v1/org/placement/change-position") @Tags("Placement") @@ -34,8 +48,13 @@ import { ProfileChangePosition, CreateProfileChangePosition } from "../entities/ ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class ChangePositionController extends Controller { - private ChangePositionRepository = AppDataSource.getRepository(ChangePosition); - private ProfileChangePositionRepository = AppDataSource.getRepository(ProfileChangePosition); + private changePositionRepository = AppDataSource.getRepository(ChangePosition); + private profileChangePositionRepository = AppDataSource.getRepository(ProfileChangePosition); + private orgRootRepository = AppDataSource.getRepository(OrgRoot); + private child1Repository = AppDataSource.getRepository(OrgChild1); + private child2Repository = AppDataSource.getRepository(OrgChild2); + private child3Repository = AppDataSource.getRepository(OrgChild3); + private child4Repository = AppDataSource.getRepository(OrgChild4); /** * API เพิ่มรอบย้ายสับเปลี่ยนตำแหน่ง @@ -48,7 +67,7 @@ export class ChangePositionController extends Controller { @Body() body: CreateChangePosition, @Request() request: RequestWithUser, ) { - const _changePosition = await this.ChangePositionRepository.findOne({ + const _changePosition = await this.changePositionRepository.findOne({ where: { name: body.name }, }); if(_changePosition){ @@ -66,42 +85,7 @@ export class ChangePositionController extends Controller { changePosition.createdFullName = request.user.name; changePosition.lastUpdateUserId = request.user.sub; changePosition.lastUpdateFullName = request.user.name; - await this.ChangePositionRepository.save(changePosition); - return new HttpSuccess(); - } - - /** - * API เพิ่มเพิ่มรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง - * - * @summary เพิ่มเพิ่มรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง (ADMIN) - * - * @param {string} id Id รอบย้ายสับเปลี่ยนตำแหน่ง - */ - @Post("profile/{id}") - async CreateProfileChangePosition( - @Path() id: string, - @Body() body: CreateProfileChangePosition, - @Request() request: RequestWithUser, - ) { - const changePosition = await this.ChangePositionRepository.findOneBy({ id }); - if (!changePosition) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบย้ายสับเปลี่ยนตำแหน่ง"); - - const profileChangePositions: ProfileChangePosition[] = []; - const profiles = new ProfileChangePosition(); - for (const data of body.profiles) { - Object.assign(profiles, data); - let positionOld = data.positionOld ? `${data.positionOld}` : ""; - let rootOld = data.rootOld ? data.positionOld ? `/${data.rootOld}` : `${data.rootOld}` : ""; - profiles.changePositionId = id; - profiles.organizationPositionOld = `${positionOld}${rootOld}`, - profiles.status = "WAITTING", - profiles.createdUserId = request.user.sub; - profiles.createdFullName = request.user.name; - profiles.lastUpdateUserId = request.user.sub; - profiles.lastUpdateFullName = request.user.name; - profileChangePositions.push(profiles); - } - await this.ProfileChangePositionRepository.save(profileChangePositions); + await this.changePositionRepository.save(changePosition); return new HttpSuccess(); } @@ -116,7 +100,7 @@ export class ChangePositionController extends Controller { async DeleteChangePosition(@Path() id: string) { let result: any; try { - result = await this.ChangePositionRepository.delete({ id: id }); + result = await this.changePositionRepository.delete({ id: id }); } catch { throw new HttpError( HttpStatusCode.NOT_FOUND, @@ -143,10 +127,10 @@ export class ChangePositionController extends Controller { @Body() body: UpdateChangePosition, ) { - const changePosition = await this.ChangePositionRepository.findOneBy({ id }); + const changePosition = await this.changePositionRepository.findOneBy({ id }); if (!changePosition) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบย้ายสับเปลี่ยนตำแหน่ง"); - const checkDuplicate = await this.ChangePositionRepository.find({ + const checkDuplicate = await this.changePositionRepository.find({ where: { id: Not(id), name: body.name @@ -161,7 +145,7 @@ export class ChangePositionController extends Controller { Object.assign(changePosition, body); changePosition.lastUpdateUserId = request.user.sub; changePosition.lastUpdateFullName = request.user.name; - await this.ChangePositionRepository.save(changePosition); + await this.changePositionRepository.save(changePosition); return new HttpSuccess(); } @@ -172,9 +156,25 @@ export class ChangePositionController extends Controller { * */ @Get() - async GetChangePositionLists() { - const data = await this.ChangePositionRepository.find(); - return new HttpSuccess(data); + async GetChangePositionLists( + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query() searchKeyword: string = "", + ) { + const [changePosition, total] = await AppDataSource.getRepository(ChangePosition) + .createQueryBuilder("changePosition") + .where( + searchKeyword ? + "changePosition.name LIKE :keyword OR changePosition.date LIKE :keyword OR changePosition.status LIKE :keyword" + : "1=1", + { keyword: `%${searchKeyword}%` } + ) + .orderBy("changePosition.date", "ASC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + + return new HttpSuccess({ data: changePosition, total }); } /** @@ -187,9 +187,283 @@ export class ChangePositionController extends Controller { @Get("{id}") async GetChangePositionById( @Path() id: string ) { - const data = await this.ChangePositionRepository.findOne({ where: { id: id }}); + const data = await this.changePositionRepository.findOne({ where: { id: id }}); if (!data) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบย้ายสับเปลี่ยนตำแหน่ง"); return new HttpSuccess(data); } + /** + * API เพิ่มเพิ่มรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง + * + * @summary เพิ่มเพิ่มรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง (ADMIN) + * + * @param {string} changePositionId changePositionId รอบย้ายสับเปลี่ยนตำแหน่ง + */ + @Post("profile/{changePositionId}") + async CreateProfileChangePosition( + @Path() changePositionId: string, + @Body() body: CreateProfileChangePosition, + @Request() request: RequestWithUser, + ) { + const changePosition = await this.changePositionRepository.findOneBy({ id: changePositionId }); + if (!changePosition) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบย้ายสับเปลี่ยนตำแหน่ง"); + + const profileChangePositions: ProfileChangePosition[] = []; + const profiles = new ProfileChangePosition(); + for (const data of body.profiles) { + Object.assign(profiles, data); + let positionOld = data.positionOld ? `${data.positionOld}` : ""; + let rootOld = data.rootOld ? data.positionOld ? `/${data.rootOld}` : `${data.rootOld}` : ""; + profiles.changePositionId = changePositionId; + profiles.organizationPositionOld = `${positionOld}${rootOld}`, + profiles.status = "WAITTING", + profiles.createdUserId = request.user.sub; + profiles.createdFullName = request.user.name; + profiles.lastUpdateUserId = request.user.sub; + profiles.lastUpdateFullName = request.user.name; + profileChangePositions.push(profiles); + } + await this.profileChangePositionRepository.save(profileChangePositions); + return new HttpSuccess(); + } + + /** + * API ลบรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง + * + * @summary ลบรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง (ADMIN) + * + * @param {string} id Id รายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง + */ + @Delete("profile/{id}") + async DeleteProfileChangePosition(@Path() id: string) { + const result = await this.profileChangePositionRepository.delete({ id: id }); + if (result.affected == undefined || result.affected <= 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + return new HttpSuccess(); + } + + /** + * API รายการรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง + * + * @summary API รายการรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง (ADMIN) + * + * @param {string} changePositionId Id รอบย้ายสับเปลี่ยนตำแหน่ง + */ + @Get("profile-all/{changePositionId}") + async GetProfileChangePositionLists( + @Path() changePositionId: string, + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query() searchKeyword: string = "", + + ) { + const findData = await this.profileChangePositionRepository.find({ + where: { changePositionId: changePositionId } + }); + if (!findData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบย้ายสับเปลี่ยนตำแหน่ง"); + + const [profileChangePosition, total] = await AppDataSource.getRepository(ProfileChangePosition) + .createQueryBuilder("profileChangePosition") + .where("profileChangePosition.changePositionId LIKE :id", { id: changePositionId }) + .andWhere( + searchKeyword ? + "CONCAT(profileChangePosition.prefix, profileChangePosition.firstName, ' ', profileChangePosition.lastName) LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ) + .andWhere( + searchKeyword ? + "profileChangePosition.citizenId LIKE :keyword OR profileChangePosition.status LIKE :keyword" + : "1=1", + { keyword: `%${searchKeyword}%` } + ) + .andWhere( + searchKeyword ? + "profileChangePosition.birthDate LIKE :keyword OR profileChangePosition.lastUpdatedAt LIKE :keyword" + : "1=1", + { keyword: `%${searchKeyword}%` } + ) + .orderBy("profileChangePosition.createdAt", "ASC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + + return new HttpSuccess({ data: profileChangePosition, total }); + } + + /** + * API รายละเอียดรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง + * + * @summary API รายละเอียดรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง (ADMIN) + * + * @param {string} id Id รายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง + */ + @Get("profile/{id}") + async GetProfileChangePositionById( + @Path() id: string + ) { + const profileChangePos = await this.profileChangePositionRepository.findOne({ + where: { id: id } + }); + if (!profileChangePos) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง"); + return new HttpSuccess(profileChangePos); + } + + /** + * API แก้ไขข้อมูลเพื่อลงบัญชีแนบท้าย + * + * @summary แก้ไขข้อมูลเพื่อลงบัญชีแนบท้าย (ADMIN) + * + * @param {string} id Id รายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง + */ + @Put("profile/{id}") + async UpdateProfileChangePositionById( + @Request() request: RequestWithUser, + @Path() id: string, + @Body() body: UpdateProfileChangePosition, + ) { + + const profileChangePos = await this.profileChangePositionRepository.findOneBy({ id }); + if (!profileChangePos) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่งนี้"); + + profileChangePos.lastUpdateUserId = request.user.sub; + profileChangePos.lastUpdateFullName = request.user.name; + profileChangePos.educationOld = body.educationOld; + profileChangePos.posMasterNoOld = body.posMasterNoOld; + profileChangePos.positionTypeOld = body.positionTypeOld; + profileChangePos.positionLevelOld = body.positionLevelOld; + profileChangePos.positionNumberOld = body.positionNumberOld; + profileChangePos.amountOld = body.amountOld; + profileChangePos.reason = body.reason? String(body.reason) : ""; + await this.profileChangePositionRepository.save(profileChangePos); + return new HttpSuccess(); + } + + /** + * API บันทึก เลือกหน่วยงานที่รับย้าย + * + * @summary บันทึก เลือกหน่วยงานที่รับย้าย (ADMIN) + * + * @param {string} id Id รายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่ง + */ + @Put("profile/position/{id}") + async positionProfileEmployee( + @Request() request: RequestWithUser, + @Path() id: string, + @Body() body: SelectProfileChangePosition, + ) { + + const profileChangePos = await this.profileChangePositionRepository.findOneBy({ id }); + if (!profileChangePos) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรายชื่อผู้ที่ย้ายสับเปลี่ยนตำแหน่งนี้"); + + switch (body.node) { + case 0: { + const data = await this.orgRootRepository.findOne({ + where: { id: body.nodeId }, + }); + if (data != null) { + profileChangePos.rootId = data.id; + profileChangePos.root = data.orgRootName; + profileChangePos.rootShortName = data.orgRootShortName; + } + } + case 1: { + const data = await this.child1Repository.findOne({ + where: { id: body.nodeId }, + relations: ["orgRoot"], + }); + if (data != null) { + profileChangePos.rootId = data.orgRoot.id; + profileChangePos.root = data.orgRoot.orgRootName; + profileChangePos.rootShortName = data.orgRoot.orgRootShortName; + profileChangePos.child1Id = data.id; + profileChangePos.child1 = data.orgChild1Name; + profileChangePos.child1ShortName = data.orgChild1ShortName; + } + } + case 2: { + const data = await this.child2Repository.findOne({ + where: { id: body.nodeId }, + relations: ["orgRoot", "orgChild1"], + }); + if (data != null) { + profileChangePos.rootId = data.orgRoot.id; + profileChangePos.root = data.orgRoot.orgRootName; + profileChangePos.rootShortName = data.orgRoot.orgRootShortName; + profileChangePos.child1Id = data.orgChild1.id; + profileChangePos.child1 = data.orgChild1.orgChild1Name; + profileChangePos.child1ShortName = data.orgChild1.orgChild1ShortName; + profileChangePos.child2Id = data.id; + profileChangePos.child2 = data.orgChild2Name; + profileChangePos.child2ShortName = data.orgChild2ShortName; + } + } + case 3: { + const data = await this.child3Repository.findOne({ + where: { id: body.nodeId }, + relations: ["orgRoot", "orgChild1", "orgChild2"], + }); + if (data != null) { + profileChangePos.rootId = data.orgRoot.id; + profileChangePos.root = data.orgRoot.orgRootName; + profileChangePos.rootShortName = data.orgRoot.orgRootShortName; + profileChangePos.child1Id = data.orgChild1.id; + profileChangePos.child1 = data.orgChild1.orgChild1Name; + profileChangePos.child1ShortName = data.orgChild1.orgChild1ShortName; + profileChangePos.child2Id = data.orgChild2.id; + profileChangePos.child2 = data.orgChild2.orgChild2Name; + profileChangePos.child2ShortName = data.orgChild2.orgChild2ShortName; + profileChangePos.child3Id = data.id; + profileChangePos.child3 = data.orgChild3Name; + profileChangePos.child3ShortName = data.orgChild3ShortName; + } + } + case 4: { + const data = await this.child4Repository.findOne({ + where: { id: body.nodeId }, + relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3"], + }); + if (data != null) { + profileChangePos.rootId = data.orgRoot.id; + profileChangePos.root = data.orgRoot.orgRootName; + profileChangePos.rootShortName = data.orgRoot.orgRootShortName; + profileChangePos.child1Id = data.orgChild1.id; + profileChangePos.child1 = data.orgChild1.orgChild1Name; + profileChangePos.child1ShortName = data.orgChild1.orgChild1ShortName; + profileChangePos.child2Id = data.orgChild2.id; + profileChangePos.child2 = data.orgChild2.orgChild2Name; + profileChangePos.child2ShortName = data.orgChild2.orgChild2ShortName; + profileChangePos.child3Id = data.orgChild3.id; + profileChangePos.child3 = data.orgChild3.orgChild3Name; + profileChangePos.child3ShortName = data.orgChild3.orgChild3ShortName; + profileChangePos.child4Id = data.id; + profileChangePos.child4 = data.orgChild4Name; + profileChangePos.child4ShortName = data.orgChild4ShortName; + } + } + } + + profileChangePos.lastUpdateUserId = request.user.sub; + profileChangePos.lastUpdateFullName = request.user.name; + profileChangePos.node = body.node; + profileChangePos.nodeId = body.nodeId; + profileChangePos.orgRevisionId = body.orgRevisionId; + profileChangePos.posmasterId = body.posmasterId; + profileChangePos.posMasterNo = body.posMasterNo; + profileChangePos.positionId = body.positionId; + profileChangePos.position = body.position; + profileChangePos.positionField = body.positionField; + profileChangePos.posTypeId = String(body.posTypeId); + profileChangePos.posTypeName = body.posTypeName; + profileChangePos.posLevelId = String(body.posLevelId); + profileChangePos.posLevelName = body.posLevelName; + profileChangePos.status = "PENDING"; + await this.profileChangePositionRepository.save(profileChangePos); + return new HttpSuccess(); + } + + } diff --git a/src/entities/ProfileChangePosition.ts b/src/entities/ProfileChangePosition.ts index 0b495562..575f1cd6 100644 --- a/src/entities/ProfileChangePosition.ts +++ b/src/entities/ProfileChangePosition.ts @@ -216,6 +216,7 @@ export class ProfileItem { firstName: string; lastName: string; citizenId: string; + birthDate: Date | null; positionOld: string | null; positionTypeOld: string | null; positionLevelOld: string | null; @@ -246,8 +247,20 @@ export class ProfileItem { posLevelNameOld: string | null; } -//เลือกหน่วยงานที่รับย้าย +//แก้ไขข้อมูลเพื่อลงบัญชีแนบท้าย export type UpdateProfileChangePosition = { + educationOld: string; + posMasterNoOld: number; + positionTypeOld: string; + positionLevelOld: string; + positionNumberOld: string; + amountOld: number; + dateCurrent : Date | null + reason: string | null; +}; + +//เลือกหน่วยงานที่รับย้าย +export type SelectProfileChangePosition = { posmasterId: string; node: number; nodeId: string;