From 383c31b7a377b384c30f27c69ed91ee4e2fb7975 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 22 Feb 2024 15:32:42 +0700 Subject: [PATCH] =?UTF-8?q?API=20=E0=B8=AA=E0=B8=A3=E0=B9=89=E0=B8=B2?= =?UTF-8?q?=E0=B8=87=E0=B8=A3=E0=B8=AD=E0=B8=9A=E0=B9=80=E0=B8=87=E0=B8=B4?= =?UTF-8?q?=E0=B8=99=E0=B9=80=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/SalaryPeriodController.ts | 76 +++++++++++++++++++ .../{SalarysPeriod.ts => SalaryPeriod.ts} | 5 -- 2 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 src/controllers/SalaryPeriodController.ts rename src/entities/{SalarysPeriod.ts => SalaryPeriod.ts} (94%) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts new file mode 100644 index 0000000..506f11b --- /dev/null +++ b/src/controllers/SalaryPeriodController.ts @@ -0,0 +1,76 @@ +import { + Controller, + Get, + Post, + Put, + Delete, + Patch, + Route, + Security, + Tags, + Body, + Path, + Request, + Example, + Query, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import { DeepPartial, In, IsNull, Not, Between } from "typeorm"; +import HttpSuccess from "../interfaces/http-success"; +import HttpError from "../interfaces/http-error"; +import HttpStatusCode from "../interfaces/http-status"; +import { query } from "express"; +import { randomUUID } from "crypto"; +import { CreateSalaryPeriod, SalaryPeriod } from "../entities/SalaryPeriod"; +import Extension from "../interfaces/extension"; + +@Route("api/v1/salary") +@Tags("Salary") +@Security("bearerAuth") +export class SalaryPeriodController extends Controller { + private salaryPeriodRepository = AppDataSource.getRepository(SalaryPeriod); + + /** + * API สร้างรอบเงินเดือน + * + * @summary SLR_016 - สร้างรอบเงินเดือน #16 + * + */ + @Post("period") + async create_salary_period( + @Body() requestBody: CreateSalaryPeriod, + @Request() request: { user: Record }, + ) { + const salaryPeriod = Object.assign(new SalaryPeriod(), requestBody); + if (!salaryPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const chk_toUpper = ["SPECIAL", "APR", "OCT"]; + if (!chk_toUpper.includes(salaryPeriod.period.toUpperCase())) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผัง ไม่ถูกต้อง"); + } + + const startOfYear = new Date(salaryPeriod.effectiveDate.getFullYear(), 0, 1); + const endOfYear = new Date(salaryPeriod.effectiveDate.getFullYear(), 11, 31); + const chk_period = await this.salaryPeriodRepository.findOne({ + where: { + period: salaryPeriod.period, + effectiveDate: Between(startOfYear, endOfYear) + }, + }); + + if (chk_period) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผังปี "+ salaryPeriod.effectiveDate.getFullYear() +" ซ้ำ"); + } + + salaryPeriod.period = salaryPeriod.period.toUpperCase(); + salaryPeriod.createdUserId = request.user.sub; + salaryPeriod.createdFullName = request.user.name; + salaryPeriod.lastUpdateUserId = request.user.sub; + salaryPeriod.lastUpdateFullName = request.user.name; + await this.salaryPeriodRepository.save(salaryPeriod); + return new HttpSuccess(salaryPeriod.id); + } + +} diff --git a/src/entities/SalarysPeriod.ts b/src/entities/SalaryPeriod.ts similarity index 94% rename from src/entities/SalarysPeriod.ts rename to src/entities/SalaryPeriod.ts index 8bb9348..bf2cbe3 100644 --- a/src/entities/SalarysPeriod.ts +++ b/src/entities/SalaryPeriod.ts @@ -42,8 +42,6 @@ export class CreateSalaryPeriod { @Column() effectiveDate: Date; - @Column() - status?: string; } export class UpdateSalaryPeriod { @@ -57,7 +55,4 @@ export class UpdateSalaryPeriod { @Column() effectiveDate: Date; - @Column() - status?: string; - } \ No newline at end of file