diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 251ce0a4..825364bb 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -28,6 +28,7 @@ import { UpdateProfileEmployee, ProfileEmployeeHistory, UpdatePositionTempProfileEmployee, + UpdateInformationProfileEmployee, } from "../entities/ProfileEmployee"; import { EmployeePosLevel } from "../entities/EmployeePosLevel"; import { EmployeePosType } from "../entities/EmployeePosType"; @@ -50,6 +51,13 @@ import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; +import { ProfileEmployeeInformationHistory } from "../entities/ProfileEmployeeInformationHistory"; +import { + ProfileEmployeeEmployment, + CreateEmploymentProfileEmployee, + UpdateEmploymentProfileEmployee +} from "../entities/ProfileEmployeeEmployment"; +import { ProfileEmployeeEmploymentHistory } from "../entities/ProfileEmployeeEmploymentHistory"; @Route("api/v1/org/profile-employee") @Tags("ProfileEmployee") @@ -82,6 +90,9 @@ export class ProfileEmployeeController extends Controller { private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); + private informationHistoryRepository = AppDataSource.getRepository(ProfileEmployeeInformationHistory); + private employmentRepository = AppDataSource.getRepository(ProfileEmployeeEmployment); + private employmentHistoryRepository = AppDataSource.getRepository(ProfileEmployeeEmploymentHistory); /** * report ประวัติแบบย่อ ลูกจ้าง @@ -631,6 +642,127 @@ export class ProfileEmployeeController extends Controller { return new HttpSuccess(profile); } + /** + * API รายการทะเบียนประวัติลูกจ้างชั่วคราว + * + * @summary รายการทะเบียนประวัติลูกจ้างชั่วคราว (ADMIN) + * + */ + @Get("temp") + async listProfileEmp() { + const [record, total] = await this.profileRepo + .createQueryBuilder("profileEmployee") + .leftJoinAndSelect("profileEmployee.posLevel", "posLevel") + .leftJoinAndSelect("profileEmployee.posType", "posType") + .leftJoinAndSelect("profileEmployee.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.positions", "positions") + .leftJoinAndSelect("current_holders.orgRoot", "orgRoot") + .leftJoinAndSelect("current_holders.orgChild1", "orgChild1") + .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") + .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") + .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") + .andWhere("profileEmployee.employeeClass = :employeeClass", { employeeClass: "TEMP" }) + .andWhere("profileEmployee.statusTemp = :statusTemp", { statusTemp: "REPORT" }) + .getManyAndCount(); + const data = await Promise.all( + record.map((_data) => { + const shortName = + _data.current_holders.length == 0 + ? null + : _data.current_holders[0].orgChild4 != null + ? `${_data.current_holders[0].orgChild4.orgChild4ShortName}${_data.current_holders[0].posMasterNo}` + : _data.current_holders[0].orgChild3 != null + ? `${_data.current_holders[0].orgChild3.orgChild3ShortName}${_data.current_holders[0].posMasterNo}` + : _data.current_holders[0].orgChild2 != null + ? `${_data.current_holders[0].orgChild2.orgChild2ShortName}${_data.current_holders[0].posMasterNo}` + : _data.current_holders[0].orgChild1 != null + ? `${_data.current_holders[0].orgChild1.orgChild1ShortName}${_data.current_holders[0].posMasterNo}` + : _data.current_holders[0].orgRoot != null + ? `${_data.current_holders[0].orgRoot.orgRootShortName}${_data.current_holders[0].posMasterNo}` + : null; + return { + id: _data.id, + prefix: _data.prefix, + rank: _data.rank, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + posType: _data.posType == null ? null : _data.posType.posTypeName, + posTypeShortName: _data.posType == null ? null : _data.posType.posTypeShortName, + posLevelId: _data.posLevel == null ? null : _data.posLevel.id, + posTypeId: _data.posType == null ? null : _data.posType.id, + position: _data.position, + posNo: shortName, + employeeClass: _data.employeeClass == null ? null : _data.employeeClass, + govAge: Extension.CalculateGovAge(_data.dateAppoint, 0, 0), + age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0), + dateAppoint: _data.dateAppoint, + dateStart: _data.dateStart, + createdAt: _data.createdAt, + dateRetireLaw: _data.dateRetireLaw, + draftOrganizationOrganization: + _data.nodeTemp == "0" + ? _data.rootTemp + : _data.nodeTemp == "1" + ? _data.child1Temp + : _data.nodeTemp == "2" + ? _data.child2Temp + : _data.nodeTemp == "3" + ? _data.child3Temp + : _data.nodeTemp == "4" + ? _data.child4Temp + : null, + draftPositionEmployee: _data.positionTemp, + draftOrgEmployeeStatus: _data.statusTemp, + node: _data.nodeTemp, + nodeId: _data.nodeIdTemp, + nodeName: + _data.nodeTemp == "0" + ? _data.rootTemp + : _data.nodeTemp == "1" + ? _data.child1Temp + : _data.nodeTemp == "2" + ? _data.child2Temp + : _data.nodeTemp == "3" + ? _data.child3Temp + : _data.nodeTemp == "4" + ? _data.child4Temp + : null, + nodeShortName: + _data.nodeTemp == "0" + ? _data.rootShortNameTemp + : _data.nodeTemp == "1" + ? _data.child1ShortNameTemp + : _data.nodeTemp == "2" + ? _data.child1ShortNameTemp + : _data.nodeTemp == "3" + ? _data.child3ShortNameTemp + : _data.nodeTemp == "4" + ? _data.child4ShortNameTemp + : null, + root: _data.rootTemp ? _data.rootTemp : null, + rootId: _data.rootIdTemp ? _data.rootIdTemp : null, + rootShortName: _data.rootShortNameTemp ? _data.rootShortNameTemp : null, + child1: _data.child1Temp ? _data.child1Temp : null, + child1Id: _data.child1IdTemp ? _data.child1IdTemp : null, + child1ShortName: _data.child1ShortNameTemp ? _data.child1ShortNameTemp : null, + child2: _data.child2Temp ? _data.child2Temp : null, + child2Id: _data.child2IdTemp ? _data.child2IdTemp : null, + child2ShortName: _data.child2ShortNameTemp ? _data.child2ShortNameTemp : null, + child3: _data.child3Temp ? _data.child3Temp : null, + child3Id: _data.child3IdTemp ? _data.child3IdTemp : null, + child3ShortName: _data.child3ShortNameTemp ? _data.child3ShortNameTemp : null, + child4: _data.child4Temp ? _data.child4Temp : null, + child4Id: _data.child4IdTemp ? _data.child4IdTemp : null, + child4ShortName: _data.child4ShortNameTemp ? _data.child4ShortNameTemp : null, + }; + }), + ); + + return new HttpSuccess({ data: data, total }); + } + /** * API ประวัติการแก้ไขรายการทะเบียนประวัติ * @@ -2372,4 +2504,228 @@ export class ProfileEmployeeController extends Controller { await this.profileRepo.save(profile); return new HttpSuccess(); } + + /** + * API แก้ไขข้อมูลลูกจ้างชั่วคราว + * + * @summary แก้ไขข้อมูลลูกจ้างชั่วคราว (ADMIN) + * + * @param {string} profileEmployeeId profileEmployeeId ทะเบียนประวัติลูกจ้างชั่วคราว + */ + @Put("information/{profileEmployeeId}") + async ProfileEmployeeInformation( + @Request() request: RequestWithUser, + @Path() profileEmployeeId: string, + @Body() body: UpdateInformationProfileEmployee, + ) { + const profileEmp = await this.profileRepo.findOneBy({ id: profileEmployeeId }); + if (!profileEmp) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); + + const history = new ProfileEmployeeInformationHistory(); + Object.assign(history, { ...profileEmp, id: undefined }); + Object.assign(profileEmp, body) + history.profileEmployeeId = profileEmployeeId; + history.lastUpdateFullName = request.user.name; + history.lastUpdateUserId = request.user.sub; + profileEmp.lastUpdateUserId = request.user.sub; + profileEmp.lastUpdateFullName = request.user.name; + + await Promise.all([ + this.profileRepo.save(profileEmp), + this.informationHistoryRepository.save(history), + ]); + return new HttpSuccess(); + } + + /** + * API ประวัติการแก้ไขข้อมูลลูกจ้างชั่วคราว + * + * @summary ประวัติการแก้ไขข้อมูลลูกจ้างชั่วคราว (ADMIN) + * + * @param {string} profileEmployeeId profileEmployeeId ทะเบียนประวัติลูกจ้างชั่วคราว + */ + @Get("information/history/{profileEmployeeId}") + async getInformationHistory(@Path() profileEmployeeId: string) { + const profileInformation = await this.profileRepo.find({ + relations: { + information_histories: true, + }, + where: { id: profileEmployeeId }, + }); + if (!profileInformation) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const mapData = profileInformation.flatMap((profile) => + profile.information_histories).map((history) => ({ + id: history.id, + positionEmployeeGroupId: history.positionEmployeeGroupId, + positionEmployeeLineId: history.positionEmployeeLineId, + positionEmployeePositionId: history.positionEmployeePositionId, + employeeOc: history.employeeOc, + employeeTypeIndividual: history.employeeTypeIndividual, + employeeWage: history.employeeWage, + employeeMoneyIncrease: history.employeeMoneyIncrease, + employeeMoneyAllowance: history.employeeMoneyAllowance, + employeeMoneyEmployee: history.employeeMoneyEmployee, + employeeMoneyEmployer: history.employeeMoneyEmployer, + createdAt: history.createdAt, + createdUserId: history.createdUserId, + createdFullName: history.createdFullName, + lastUpdatedAt: history.lastUpdatedAt, + lastUpdateUserId: history.lastUpdateUserId, + lastUpdateFullName: history.lastUpdateFullName, + })); + return new HttpSuccess(mapData); + } + + /** + * API รายการข้อมูลการจ้าง + * + * @summary รายการข้อมูลการจ้าง + * + * @param {string} profileEmployeeId profileEmployeeId ทะเบียนประวัติลูกจ้างชั่วคราว + */ + @Get("employment/{profileEmployeeId}") + async ProfileEmployeeEmployment( + @Path() profileEmployeeId: string, + ) { + const employment = await this.employmentRepository.find({ + where: { profileEmployeeId: profileEmployeeId }, + order: { createdAt : "ASC"} + }); + const mapData = employment.map((employment) => ({ + id: employment.id, + date: employment.date, + command: employment.command, + })); + return new HttpSuccess(mapData); + } + + /** + * API รายละเอียดข้อมูลการจ้าง + * + * @summary รายละเอียดข้อมูลการจ้าง + * + * @param {string} id Id ข้อมูลการจ้าง + */ + @Get("employment/id/{id}") + async GetEmploymentById( + @Path() id: string, + ) { + const employment = await this.employmentRepository.findOne({ + where: { id: id }, + }); + return new HttpSuccess(employment); + } + + /** + * API ประวัติของข้อมูลการจ้าง + * + * @summary ประวัติของข้อมูลการจ้าง + * + * @param {string} id Id ข้อมูลการจ้าง + */ + @Get("employment/history/{id}") + async GetHistoryEmploymentById( + @Path() id: string, + ) { + const employmentHistory = await this.employmentHistoryRepository.find({ + where: { profileEmployeeEmploymentId: id }, + order: { lastUpdatedAt : "ASC"} + }); + return new HttpSuccess(employmentHistory); + } + + /** + * API เพิ่มข้อมูลการจ้าง + * + * @summary เพิ่มข้อมูลการจ้าง + * + * @param {string} profileEmployeeId profileEmployeeId ทะเบียนประวัติลูกจ้างชั่วคราว + */ + @Post("employment/{profileEmployeeId}") + async CreateEmployment( + @Path() profileEmployeeId: string, + @Body() body: CreateEmploymentProfileEmployee, + @Request() request: RequestWithUser, + ) { + const profile = await this.profileRepo.findOne({ + where: { id: profileEmployeeId }, + }); + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const employment = new ProfileEmployeeEmployment(); + // const history = new ProfileEmployeeEmploymentHistory(); + Object.assign(employment, body) + employment.profileEmployeeId = profileEmployeeId; + employment.createdUserId = request.user.sub; + employment.createdFullName = request.user.name; + employment.lastUpdateUserId = request.user.sub; + employment.lastUpdateFullName = request.user.name; + await this.employmentRepository.save(employment); + // if (employment) { + // Object.assign(history, { ...employment, id: undefined }); + // history.profileEmployeeEmploymentId = employment.id; + // history.createdUserId = request.user.sub; + // history.createdFullName = request.user.name; + // history.lastUpdateFullName = request.user.name; + // history.lastUpdateUserId = request.user.sub; + // await this.employmentHistoryRepository.save(history); + // } + return new HttpSuccess(); + } + + /** + * API ลบข้อมูลการจ้าง + * + * @summary ลบข้อมูลการจ้าง (ADMIN) + * + * @param {string} id Id ข้อมูลการจ้าง + */ + @Delete("employment/{id}") + async DeleteEmployment(@Path() id: string) { + await this.employmentHistoryRepository.delete({ + profileEmployeeEmploymentId: id, + }); + + const result = await this.employmentRepository.delete({ id }); + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + return new HttpSuccess(); + } + + /** + * API แก้ไขข้อมูลการจ้าง + * + * @summary แก้ไขข้อมูลการจ้าง (ADMIN) + * + * @param {string} id Id ข้อมูลการจ้าง + */ + @Put("employment/{id}") + async UpdateEmployment( + @Request() request: RequestWithUser, + @Path() id: string, + @Body() body: UpdateEmploymentProfileEmployee, + ) { + const employment = await this.employmentRepository.findOneBy({ id }); + if (!employment) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileEmployeeEmploymentHistory(); + Object.assign(history, { ...employment, id: undefined }); + Object.assign(employment, body) + + employment.lastUpdateUserId = request.user.sub; + employment.lastUpdateFullName = request.user.name; + history.profileEmployeeEmploymentId = id; + history.lastUpdateFullName = request.user.name; + history.lastUpdateUserId = request.user.sub; + + await Promise.all([ + this.employmentRepository.save(employment), + this.employmentHistoryRepository.save(history) + ]); + + return new HttpSuccess(); + } + } diff --git a/src/entities/ProfileEmployeeEmployment.ts b/src/entities/ProfileEmployeeEmployment.ts index 379c5744..901f0b6e 100644 --- a/src/entities/ProfileEmployeeEmployment.ts +++ b/src/entities/ProfileEmployeeEmployment.ts @@ -41,3 +41,8 @@ export class CreateEmploymentProfileEmployee { date: Date | null; command: string | null; } + +export class UpdateEmploymentProfileEmployee { + date: Date | null; + command: string | null; +} \ No newline at end of file