diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 3b3fdde8..36a72324 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -32,6 +32,15 @@ import HttpStatus from "../interfaces/http-status"; import Extension from "../interfaces/extension"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import CallAPI from "../interfaces/call-api"; +import { ProfileSalary } from "../entities/ProfileSalary"; +import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory"; +import { removeProfileInOrganize, setLogDataDiff } from "../interfaces/utils"; +import { Position } from "../entities/Position"; +import { PosMaster } from "../entities/PosMaster"; +import { EmployeePosition } from "../entities/EmployeePosition"; +import { EmployeePosMaster } from "../entities/EmployeePosMaster"; +import { ProfileDiscipline } from "../entities/ProfileDiscipline"; +import { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory"; @Route("api/v1/org/command") @Tags("Command") @@ -51,6 +60,14 @@ export class CommandController extends Controller { private profileRepository = AppDataSource.getRepository(Profile); private profileEmployeeRepository = AppDataSource.getRepository(ProfileEmployee); private orgRevisionRepo = AppDataSource.getRepository(OrgRevision); + private salaryRepo = AppDataSource.getRepository(ProfileSalary); + private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory); + private posMasterRepository = AppDataSource.getRepository(PosMaster); + private positionRepository = AppDataSource.getRepository(Position); + private employeePosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); + private employeePositionRepository = AppDataSource.getRepository(EmployeePosition); + private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline); + private disciplineHistoryRepository = AppDataSource.getRepository(ProfileDisciplineHistory); /** * API list รายการคำสั่ง @@ -1100,6 +1117,640 @@ export class CommandController extends Controller { return new HttpSuccess(command.id); } + @Post("excexute/salary-current") + public async newSalaryAndUpdateCurrent( + @Request() req: RequestWithUser, + @Body() + body: { + data: { + profileId: string; + date?: Date | null; + amount?: Double | null; + positionSalaryAmount?: Double | null; + mouthSalaryAmount?: Double | null; + posNo: string | null; + position: string | null; + positionLine: string | null; + positionPathSide: string | null; + positionExecutive: string | null; + positionType: string | null; + positionLevel: string | null; + refCommandNo: string | null; + templateDoc: string | null; + posmasterId: string; + positionId: string; + }[]; + }, + ) { + await Promise.all( + body.data.map(async (item) => { + const profile = await this.profileRepository.findOneBy({ id: item.profileId }); + if (!profile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); + } + + const dest_item = await this.salaryRepo.findOne({ + where: { profileId: item.profileId }, + order: { order: "DESC" }, + }); + const before = null; + const data = new ProfileSalary(); + + const meta = { + order: dest_item == null ? 1 : dest_item.order + 1, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(data, { ...item, ...meta }); + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + + await this.salaryRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); + history.profileSalaryId = data.id; + await this.salaryHistoryRepo.save(history, { data: req }); + + const posMaster = await this.posMasterRepository.findOne({ + where: { id: item.posmasterId }, + }); + if (posMaster == null) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); + + const posMasterOld = await this.posMasterRepository.findOne({ + where: { + current_holderId: item.profileId, + orgRevisionId: posMaster.orgRevisionId, + }, + }); + if (posMasterOld != null) posMasterOld.current_holderId = null; + + const positionOld = await this.positionRepository.findOne({ + where: { + posMasterId: posMasterOld?.id, + positionIsSelected: true, + }, + }); + if (positionOld != null) { + positionOld.positionIsSelected = false; + await this.positionRepository.save(positionOld); + } + + const checkPosition = await this.positionRepository.find({ + where: { + posMasterId: item.posmasterId, + positionIsSelected: true, + }, + }); + if (checkPosition.length > 0) { + const clearPosition = checkPosition.map((positions) => ({ + ...positions, + positionIsSelected: false, + })); + await this.positionRepository.save(clearPosition); + } + + posMaster.current_holderId = item.profileId; + if (posMasterOld != null) await this.posMasterRepository.save(posMasterOld); + await this.posMasterRepository.save(posMaster); + + const positionNew = await this.positionRepository.findOne({ + where: { + id: item.positionId, + posMasterId: item.posmasterId, + }, + }); + if (positionNew != null) { + positionNew.positionIsSelected = true; + profile.posLevelId = positionNew.posLevelId; + profile.posTypeId = positionNew.posTypeId; + profile.position = positionNew.positionName; + await this.profileRepository.save(profile); + await this.positionRepository.save(positionNew); + } + }), + ); + + return new HttpSuccess(); + } + + @Post("excexute/salary-employee-current") + public async newSalaryEmployeeAndUpdateCurrent( + @Request() req: RequestWithUser, + @Body() + body: { + data: { + profileId: string; + date?: Date | null; + amount?: Double | null; + positionSalaryAmount?: Double | null; + mouthSalaryAmount?: Double | null; + posNo: string | null; + position: string | null; + positionType: string | null; + positionLevel: string | null; + refCommandNo: string | null; + templateDoc: string | null; + posmasterId: string; + positionId: string; + }[]; + }, + ) { + await Promise.all( + body.data.map(async (item) => { + const profile = await this.profileEmployeeRepository.findOneBy({ id: item.profileId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const dest_item = await this.salaryRepo.findOne({ + where: { profileEmployeeId: item.profileId }, + order: { order: "DESC" }, + }); + const before = null; + const data = new ProfileSalary(); + + const meta = { + order: dest_item == null ? 1 : dest_item.order + 1, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(data, { ...item, ...meta }); + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + + await this.salaryRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); + history.profileSalaryId = data.id; + await this.salaryHistoryRepo.save(history, { data: req }); + + const posMaster = await this.employeePosMasterRepository.findOne({ + where: { id: item.posmasterId }, + relations: ["orgRoot"], + }); + if (posMaster == null) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); + + const posMasterOld = await this.employeePosMasterRepository.findOne({ + where: { + current_holderId: item.profileId, + orgRevisionId: posMaster.orgRevisionId, + }, + }); + if (posMasterOld != null) posMasterOld.current_holderId = null; + if (posMasterOld != null) posMasterOld.next_holderId = null; + + const positionOld = await this.employeePositionRepository.findOne({ + where: { + posMasterId: posMasterOld?.id, + positionIsSelected: true, + }, + }); + if (positionOld != null) { + positionOld.positionIsSelected = false; + await this.employeePositionRepository.save(positionOld); + } + + const checkPosition = await this.employeePositionRepository.find({ + where: { + posMasterId: item.posmasterId, + positionIsSelected: true, + }, + }); + if (checkPosition.length > 0) { + const clearPosition = checkPosition.map((positions) => ({ + ...positions, + positionIsSelected: false, + })); + await this.employeePositionRepository.save(clearPosition); + } + + posMaster.current_holderId = item.profileId; + posMaster.next_holderId = item.profileId; + if (posMasterOld != null) await this.employeePosMasterRepository.save(posMasterOld); + await this.employeePosMasterRepository.save(posMaster); + + const positionNew = await this.employeePositionRepository.findOne({ + where: { + id: item.positionId, + posMasterId: item.posmasterId, + }, + }); + if (positionNew != null) { + positionNew.positionIsSelected = true; + profile.posLevelId = positionNew.posLevelId; + profile.posTypeId = positionNew.posTypeId; + profile.position = positionNew.positionName; + profile.employeeOc = posMaster?.orgRoot?.orgRootName ?? null; + profile.positionEmployeePositionId = positionNew.positionName; + + await this.profileEmployeeRepository.save(profile); + await this.employeePositionRepository.save(positionNew); + } + }), + ); + + return new HttpSuccess(); + } + + @Post("excexute/salary-leave") + public async newSalaryAndUpdateLeave( + @Request() req: RequestWithUser, + @Body() + body: { + data: { + profileId: string; + date?: Date | null; + amount?: Double | null; + positionSalaryAmount?: Double | null; + mouthSalaryAmount?: Double | null; + posNo: string | null; + position: string | null; + positionLine: string | null; + positionPathSide: string | null; + positionExecutive: string | null; + positionType: string | null; + positionLevel: string | null; + refCommandNo: string | null; + templateDoc: string | null; + isLeave: boolean; + leaveReason?: string | null; + dateLeave?: Date | null; + }[]; + }, + ) { + await Promise.all( + body.data.map(async (item) => { + const profile = await this.profileRepository.findOneBy({ id: item.profileId }); + if (!profile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); + } + + const dest_item = await this.salaryRepo.findOne({ + where: { profileId: item.profileId }, + order: { order: "DESC" }, + }); + const before = null; + const data = new ProfileSalary(); + + const meta = { + order: dest_item == null ? 1 : dest_item.order + 1, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(data, { ...item, ...meta }); + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + + await this.salaryRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); + history.profileSalaryId = data.id; + await this.salaryHistoryRepo.save(history, { data: req }); + + const _null: any = null; + profile.isLeave = item.isLeave; + profile.leaveReason = item.leaveReason ?? _null; + profile.dateLeave = item.dateLeave ?? _null; + profile.lastUpdateUserId = req.user.sub; + profile.lastUpdateFullName = req.user.name; + profile.lastUpdatedAt = new Date(); + if (item.isLeave == true) { + await removeProfileInOrganize(profile.id, "OFFICER"); + } + await this.profileRepository.save(profile); + }), + ); + + return new HttpSuccess(); + } + + @Post("excexute/salary-employee-leave") + public async newSalaryEmployeeAndUpdateLeave( + @Request() req: RequestWithUser, + @Body() + body: { + data: { + profileId: string; + date?: Date | null; + amount?: Double | null; + positionSalaryAmount?: Double | null; + mouthSalaryAmount?: Double | null; + posNo: string | null; + position: string | null; + positionType: string | null; + positionLevel: string | null; + refCommandNo: string | null; + templateDoc: string | null; + isLeave: boolean; + leaveReason?: string | null; + dateLeave?: Date | null; + }[]; + }, + ) { + await Promise.all( + body.data.map(async (item) => { + const profile = await this.profileEmployeeRepository.findOneBy({ id: item.profileId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const dest_item = await this.salaryRepo.findOne({ + where: { profileEmployeeId: item.profileId }, + order: { order: "DESC" }, + }); + const before = null; + const data = new ProfileSalary(); + + const meta = { + order: dest_item == null ? 1 : dest_item.order + 1, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(data, { ...item, ...meta }); + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + + await this.salaryRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); + history.profileSalaryId = data.id; + await this.salaryHistoryRepo.save(history, { data: req }); + + const _null: any = null; + profile.isLeave = item.isLeave; + profile.leaveReason = item.leaveReason ?? _null; + profile.dateLeave = item.dateLeave ?? _null; + profile.lastUpdateUserId = req.user.sub; + profile.lastUpdateFullName = req.user.name; + profile.lastUpdatedAt = new Date(); + if (item.isLeave == true) { + await removeProfileInOrganize(profile.id, "EMPLOYEE"); + } + await this.profileEmployeeRepository.save(profile); + }), + ); + + return new HttpSuccess(); + } + + @Post("excexute/salary") + public async newSalaryAndUpdate( + @Request() req: RequestWithUser, + @Body() + body: { + data: { + profileId: string; + date?: Date | null; + amount?: Double | null; + positionSalaryAmount?: Double | null; + mouthSalaryAmount?: Double | null; + posNo: string | null; + position: string | null; + positionLine: string | null; + positionPathSide: string | null; + positionExecutive: string | null; + positionType: string | null; + positionLevel: string | null; + refCommandNo: string | null; + templateDoc: string | null; + }[]; + }, + ) { + await Promise.all( + body.data.map(async (item) => { + const profile = await this.profileRepository.findOneBy({ id: item.profileId }); + if (!profile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); + } + + const dest_item = await this.salaryRepo.findOne({ + where: { profileId: item.profileId }, + order: { order: "DESC" }, + }); + const before = null; + const data = new ProfileSalary(); + + const meta = { + order: dest_item == null ? 1 : dest_item.order + 1, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(data, { ...item, ...meta }); + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + + await this.salaryRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); + history.profileSalaryId = data.id; + await this.salaryHistoryRepo.save(history, { data: req }); + }), + ); + + return new HttpSuccess(); + } + + @Post("excexute/salary-employee") + public async newSalaryEmployeeAndUpdate( + @Request() req: RequestWithUser, + @Body() + body: { + data: { + profileId: string; + date?: Date | null; + amount?: Double | null; + positionSalaryAmount?: Double | null; + mouthSalaryAmount?: Double | null; + posNo: string | null; + position: string | null; + positionType: string | null; + positionLevel: string | null; + refCommandNo: string | null; + templateDoc: string | null; + }[]; + }, + ) { + await Promise.all( + body.data.map(async (item) => { + const profile = await this.profileEmployeeRepository.findOneBy({ id: item.profileId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const dest_item = await this.salaryRepo.findOne({ + where: { profileEmployeeId: item.profileId }, + order: { order: "DESC" }, + }); + const before = null; + const data = new ProfileSalary(); + + const meta = { + order: dest_item == null ? 1 : dest_item.order + 1, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(data, { ...item, ...meta }); + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + + await this.salaryRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); + history.profileSalaryId = data.id; + await this.salaryHistoryRepo.save(history, { data: req }); + }), + ); + + return new HttpSuccess(); + } + + @Post("excexute/salary-leave-discipline") + public async newSalaryAndUpdateLeaveDiscipline( + @Request() req: RequestWithUser, + @Body() + body: { + data: { + profileId: string; + date?: Date | null; + refCommandNo?: string | null; + salaryRef?: string | null; + isLeave: boolean | null; + leaveReason?: string | null; + dateLeave?: Date | null; + refCommandDate?: Date | null; + detail?: string | null; + level?: string | null; + unStigma?: string | null; + }[]; + }, + ) { + await Promise.all( + body.data.map(async (item) => { + const profile = await this.profileRepository.findOne({ + relations: ["profileSalary"], + where: { id: item.profileId }, + order: { + profileSalary: { + order: "DESC", + }, + }, + }); + if (!profile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); + } + + // ประวัติตำแหน่ง + const data = new ProfileSalary(); + const meta = { + profileId: item.profileId, + date: item.date, + refCommandNo: item.refCommandNo, + templateDoc: item.salaryRef, + position: profile.profileSalary.length > 0 ? profile.profileSalary[0].position : null, + positionType: + profile.profileSalary.length > 0 ? profile.profileSalary[0].positionType : null, + positionLevel: + profile.profileSalary.length > 0 ? profile.profileSalary[0].positionLevel : null, + posNo: profile.profileSalary.length > 0 ? profile.profileSalary[0].posNo : null, + positionLine: + profile.profileSalary.length > 0 ? profile.profileSalary[0].positionLine : null, + positionPathSide: + profile.profileSalary.length > 0 ? profile.profileSalary[0].positionPathSide : null, + positionExecutive: + profile.profileSalary.length > 0 ? profile.profileSalary[0].positionExecutive : null, + amount: profile.profileSalary.length > 0 ? profile.profileSalary[0].amount : null, + positionSalaryAmount: + profile.profileSalary.length > 0 ? profile.profileSalary[0].positionSalaryAmount : null, + mouthSalaryAmount: + profile.profileSalary.length > 0 ? profile.profileSalary[0].mouthSalaryAmount : null, + order: + profile.profileSalary.length >= 0 + ? profile.profileSalary.length > 0 + ? profile.profileSalary[0].order + 1 + : 1 + : null, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(data, meta); + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + + await this.salaryRepo.save(data); + history.profileSalaryId = data.id; + await this.salaryHistoryRepo.save(history); + + // ประวัติวินัย + const dataDis = new ProfileDiscipline(); + + const metaDis = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(dataDis, { ...body, ...metaDis }); + const historyDis = new ProfileDisciplineHistory(); + Object.assign(historyDis, { ...dataDis, id: undefined }); + + await this.disciplineRepository.save(dataDis); + historyDis.profileDisciplineId = dataDis.id; + await this.disciplineHistoryRepository.save(historyDis); + + // ทะเบียนประวัติ + if (item.isLeave != null) { + const _null: any = null; + profile.isLeave = item.isLeave; + profile.leaveReason = item.leaveReason ?? _null; + profile.dateLeave = item.dateLeave ?? _null; + profile.lastUpdateUserId = req.user.sub; + profile.lastUpdateFullName = req.user.name; + profile.lastUpdatedAt = new Date(); + if (item.isLeave == true) { + await removeProfileInOrganize(profile.id, "OFFICER"); + } + await this.profileRepository.save(profile); + } + }), + ); + + return new HttpSuccess(); + } + commandTypePath(commandCode: string) { switch (commandCode) { case "C-PM-01": diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 8e8aa332..cb3c5461 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -5746,26 +5746,14 @@ export class ProfileController extends Controller { where: { id: id }, }); if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - const _null: any = null; + profile.isLeave = requestBody.isLeave; + profile.leaveReason = requestBody.leaveReason; + profile.dateLeave = requestBody.dateLeave; if (requestBody.isLeave == true) { await removeProfileInOrganize(profile.id, "OFFICER"); } - if (requestBody.leaveReason != undefined && requestBody.leaveReason != null) { - profile.leaveReason = requestBody.leaveReason; - } else { - profile.leaveReason = _null; - } - if (requestBody.dateLeave != undefined && requestBody.dateLeave != null) { - profile.dateLeave = requestBody.dateLeave; - } else { - profile.dateLeave = _null; - } await this.profileRepo.save(profile); - // const profileSalary = await this.salaryRepository.findOne({ - // where: { profileId: id }, - // order: { createdAt: "DESC" }, - // }); return new HttpSuccess(); } diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 671ce062..f4222790 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -160,7 +160,7 @@ export class ProfileEmployeeController extends Controller { }, }); ImgUrl = response_.data.downloadUrl; - } catch { } + } catch {} } const province = await this.provinceRepository.findOneBy({ id: profile.registrationProvinceId, @@ -328,7 +328,7 @@ export class ProfileEmployeeController extends Controller { }, }); ImgUrl = response_.data.downloadUrl; - } catch { } + } catch {} } const profileOc = await this.profileRepo.findOne({ relations: [ @@ -3151,7 +3151,6 @@ export class ProfileEmployeeController extends Controller { where: { id: id }, }); if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - // await new permission().PermissionOrgUserUpdate(request, "SYS_REGISTRY_EMP", profile.id);//ไม่แน่ใจEMPปิดไว้ก่อน profile.isLeave = requestBody.isLeave; profile.leaveReason = requestBody.leaveReason;