From 7e87cec8332239cf8c471e825aa5a70847755f9d Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 15 Nov 2024 14:55:59 +0700 Subject: [PATCH] =?UTF-8?q?api=20=E0=B8=AD=E0=B8=B1=E0=B8=9E=E0=B9=80?= =?UTF-8?q?=E0=B8=94=E0=B8=97=E0=B9=80=E0=B8=87=E0=B8=B4=E0=B8=99=E0=B9=80?= =?UTF-8?q?=E0=B8=94=E0=B8=B7=E0=B8=AD=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileSalaryController.ts | 40 +++++++++++++++++ .../ProfileSalaryEmployeeController.ts | 44 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index 2eb735bb..b5b0d8a5 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -131,6 +131,46 @@ export class ProfileSalaryController extends Controller { history.profileSalaryId = data.id; await this.salaryHistoryRepo.save(history, { data: req }); + return new HttpSuccess(); + } + + @Post("update") + public async updateSalary(@Request() req: RequestWithUser, @Body() body: CreateProfileSalary) { + if (!body.profileId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepo.findOneBy({ id: body.profileId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); + + const dest_item = await this.salaryRepo.findOne({ + where: { profileId: body.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, { ...body, ...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 }); + let null_:any = null; profile.amount = body.amount ?? null_; profile.positionSalaryAmount = body.positionSalaryAmount ?? null_; diff --git a/src/controllers/ProfileSalaryEmployeeController.ts b/src/controllers/ProfileSalaryEmployeeController.ts index b0b19e2c..0ebc07d3 100644 --- a/src/controllers/ProfileSalaryEmployeeController.ts +++ b/src/controllers/ProfileSalaryEmployeeController.ts @@ -138,6 +138,50 @@ export class ProfileSalaryEmployeeController extends Controller { 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("update") + public async updateSalaryEmployee( + @Request() req: RequestWithUser, + @Body() body: CreateProfileSalaryEmployee, + ) { + if (!body.profileEmployeeId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId"); + } + + const profile = await this.profileRepo.findOneBy({ id: body.profileEmployeeId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); + + const dest_item = await this.salaryRepo.findOne({ + where: { profileEmployeeId: body.profileEmployeeId }, + 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, { ...body, ...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;