From 9b928631966317b73393f65154d109b6312acbc3 Mon Sep 17 00:00:00 2001 From: AnandaTon Date: Tue, 14 May 2024 11:50:52 +0700 Subject: [PATCH 1/3] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88?= =?UTF-8?q?=E0=B8=A1=E0=B9=80=E0=B8=87=E0=B8=B4=E0=B8=99=E0=B9=80=E0=B8=94?= =?UTF-8?q?=E0=B8=B7=E0=B8=AD=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProfileSalaryEmployeeController.ts | 158 ++++++++++++++++++ src/entities/ProfileSalary.ts | 30 ++++ 2 files changed, 188 insertions(+) create mode 100644 src/controllers/ProfileSalaryEmployeeController.ts diff --git a/src/controllers/ProfileSalaryEmployeeController.ts b/src/controllers/ProfileSalaryEmployeeController.ts new file mode 100644 index 00000000..e716877b --- /dev/null +++ b/src/controllers/ProfileSalaryEmployeeController.ts @@ -0,0 +1,158 @@ +import { + Body, + Controller, + Delete, + Example, + Get, + Patch, + Path, + Post, + Request, + Route, + Security, + Tags, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import { + CreateProfileSalaryEmployee, + ProfileSalary, + UpdateProfileSalary, +} from "../entities/ProfileSalary"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory"; +import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; +import { LessThan, MoreThan } from "typeorm"; + +@Route("api/v1/org/profile/salary") +@Tags("ProfileSalary") +@Security("bearerAuth") +export class ProfileSalaryController extends Controller { + private profileRepo = AppDataSource.getRepository(ProfileEmployee); + private salaryRepo = AppDataSource.getRepository(ProfileSalary); + private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory); + + @Get("{profileId}") + public async getSalary(@Path() profileId: string) { + const record = await this.salaryRepo.find({ + where: { profileEmployeeId: profileId }, + order: { order: "ASC" }, + }); + return new HttpSuccess(record); + } + + @Get("history/{salaryId}") + public async salaryHistory(@Path() salaryId: string) { + const record = await this.salaryHistoryRepo.findBy({ + profileSalaryId: salaryId, + }); + return new HttpSuccess(record); + } + + @Post() + public async newSalary( + @Request() req: RequestWithUser, + @Body() body: CreateProfileSalaryEmployee, + ) { + if (!body.profileEmployeeId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepo.findOneBy({ id: body.profileEmployeeId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const dest_item = await this.salaryRepo.findOne({ + where: { profileId: body.profileEmployeeId }, + order: { order: "DESC" }, + }); + + 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, + }; + + Object.assign(data, { ...body, ...meta }); + + await this.salaryRepo.save(data); + + return new HttpSuccess(); + } + + @Patch("{salaryId}") + public async editSalary( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileSalary, + @Path() salaryId: string, + ) { + const record = await this.salaryRepo.findOneBy({ id: salaryId }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileSalaryHistory(); + + Object.assign(history, { ...record, id: undefined }); + Object.assign(record, body); + history.profileSalaryId = salaryId; + record.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([this.salaryRepo.save(record), this.salaryHistoryRepo.save(history)]); + + return new HttpSuccess(); + } + + @Delete("{salaryId}") + public async deleteSalary(@Path() salaryId: string) { + await this.salaryHistoryRepo.delete({ + profileSalaryId: salaryId, + }); + + const result = await this.salaryRepo.delete({ id: salaryId }); + + if (result.affected == undefined || result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } + + @Get("swap/{direction}/{salaryId}") + public async swapSalary(@Path() direction: string, salaryId: string) { + const source_item = await this.salaryRepo.findOne({ where: { id: salaryId } }); + if (source_item == null) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const sourceOrder = source_item.order; + if (direction.trim().toUpperCase() == "UP") { + const dest_item = await this.salaryRepo.findOne({ + where: { profileId: source_item.profileId, order: LessThan(sourceOrder) }, + order: { order: "DESC" }, + }); + if (dest_item == null) return new HttpSuccess(); + var destOrder = dest_item.order; + dest_item.order = sourceOrder; + source_item.order = destOrder; + await Promise.all([this.salaryRepo.save(source_item), this.salaryRepo.save(dest_item)]); + } else { + const dest_item = await this.salaryRepo.findOne({ + where: { profileId: source_item.profileId, order: MoreThan(sourceOrder) }, + order: { order: "ASC" }, + }); + if (dest_item == null) return new HttpSuccess(); + var destOrder = dest_item.order; + dest_item.order = sourceOrder; + source_item.order = destOrder; + await Promise.all([this.salaryRepo.save(source_item), this.salaryRepo.save(dest_item)]); + } + return new HttpSuccess(); + } +} diff --git a/src/entities/ProfileSalary.ts b/src/entities/ProfileSalary.ts index 657ae14a..4068e669 100644 --- a/src/entities/ProfileSalary.ts +++ b/src/entities/ProfileSalary.ts @@ -10,6 +10,7 @@ import { } from "typeorm"; import { EntityBase } from "./base/Base"; import { Profile } from "./Profile"; +import { ProfileEmployee } from "./ProfileEmployee"; import { ProfileSalaryHistory } from "./ProfileSalaryHistory"; @Entity("profileSalary") @@ -21,6 +22,14 @@ export class ProfileSalary extends EntityBase { }) profileId: string; + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง ProfileEmployee", + default: null, + }) + profileEmployeeId: string; + @Column({ comment: "วันที่", type: "datetime", @@ -137,6 +146,10 @@ export class ProfileSalary extends EntityBase { @ManyToOne(() => Profile, (profile) => profile.profileSalary) @JoinColumn({ name: "profileId" }) profile: Profile; + + @ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileSalary) + @JoinColumn({ name: "profileEmployeeId" }) + profileEmployee: ProfileEmployee; } export class CreateProfileSalary { @@ -156,6 +169,23 @@ export class CreateProfileSalary { templateDoc: string | null; } +export class CreateProfileSalaryEmployee { + profileEmployeeId: string | null; + 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; +} + export type UpdateProfileSalary = { date?: Date | null; amount?: Double | null; From c5e99ee7715b16fe7f4a50ae341837d8ce9d60a0 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Tue, 14 May 2024 11:51:40 +0700 Subject: [PATCH 2/3] =?UTF-8?q?sort=20=E0=B8=88=E0=B8=B1=E0=B8=87=E0=B8=AB?= =?UTF-8?q?=E0=B8=A7=E0=B8=B1=E0=B8=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/DistrictController.ts | 2 +- src/controllers/ProvinceController.ts | 2 +- src/controllers/SubDistrictController.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/controllers/DistrictController.ts b/src/controllers/DistrictController.ts index 49718ac7..5966c185 100644 --- a/src/controllers/DistrictController.ts +++ b/src/controllers/DistrictController.ts @@ -41,7 +41,7 @@ export class DistrictController extends Controller { async GetResult() { const _district = await this.districtRepository.find({ select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"], - order: { createdAt: "ASC" }, + order: { name: "ASC" }, }); return new HttpSuccess(_district); } diff --git a/src/controllers/ProvinceController.ts b/src/controllers/ProvinceController.ts index 765e27d6..44f77347 100644 --- a/src/controllers/ProvinceController.ts +++ b/src/controllers/ProvinceController.ts @@ -41,7 +41,7 @@ export class ProvinceController extends Controller { async GetResult() { const _province = await this.provinceRepository.find({ select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"], - order: { createdAt: "ASC" }, + order: { name: "ASC" }, }); return new HttpSuccess(_province); } diff --git a/src/controllers/SubDistrictController.ts b/src/controllers/SubDistrictController.ts index 72994c89..ced1e659 100644 --- a/src/controllers/SubDistrictController.ts +++ b/src/controllers/SubDistrictController.ts @@ -41,7 +41,7 @@ export class SubDistrictController extends Controller { async GetResult() { const _subDistrict = await this.subDistrictRepository.find({ select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"], - order: { createdAt: "ASC" }, + order: { name: "ASC" }, }); return new HttpSuccess(_subDistrict); } From f73e0b12619da726aedad9d7b85d3e22a78b9f84 Mon Sep 17 00:00:00 2001 From: AnandaTon Date: Tue, 14 May 2024 11:51:40 +0700 Subject: [PATCH 3/3] Update ProfileSalaryEmployeeController.ts --- src/controllers/ProfileSalaryEmployeeController.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/ProfileSalaryEmployeeController.ts b/src/controllers/ProfileSalaryEmployeeController.ts index e716877b..d9ef1b7f 100644 --- a/src/controllers/ProfileSalaryEmployeeController.ts +++ b/src/controllers/ProfileSalaryEmployeeController.ts @@ -27,10 +27,10 @@ import { Profile } from "../entities/Profile"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { LessThan, MoreThan } from "typeorm"; -@Route("api/v1/org/profile/salary") +@Route("api/v1/org/profile-employee/salary") @Tags("ProfileSalary") @Security("bearerAuth") -export class ProfileSalaryController extends Controller { +export class ProfileSalaryEmployeeController extends Controller { private profileRepo = AppDataSource.getRepository(ProfileEmployee); private salaryRepo = AppDataSource.getRepository(ProfileSalary); private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory);