SLR_017-020

This commit is contained in:
Bright 2024-02-22 16:49:23 +07:00
parent 383c31b7a3
commit 2ced1ce127

View file

@ -19,9 +19,7 @@ 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 { CreateSalaryPeriod, SalaryPeriod, UpdateSalaryPeriod } from "../entities/SalaryPeriod";
import Extension from "../interfaces/extension";
@Route("api/v1/salary")
@ -72,5 +70,154 @@ export class SalaryPeriodController extends Controller {
await this.salaryPeriodRepository.save(salaryPeriod);
return new HttpSuccess(salaryPeriod.id);
}
/**
* API
*
* @summary SLR_017 - #17
*
* @param {string} id Guid, *Id
*/
@Put("period/{id}")
async update_salary_period(
@Path() id: string,
@Body() requestBody: UpdateSalaryPeriod,
@Request() request: { user: Record<string, any> },
) {
const chk_SalaryPeriod = await this.salaryPeriodRepository.findOne({
where: { id: id },
});
if (!chk_SalaryPeriod) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
const chk_toUpper = ["SPECIAL", "APR", "OCT"];
if (!chk_toUpper.includes(requestBody.period.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผัง ไม่ถูกต้อง");
}
const startOfYear = new Date(requestBody.effectiveDate.getFullYear(), 0, 1);
const endOfYear = new Date(requestBody.effectiveDate.getFullYear(), 11, 31);
const chk_period = await this.salaryPeriodRepository.findOne({
where: {
period: requestBody.period,
effectiveDate: Between(startOfYear, endOfYear)
},
});
if (chk_period) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผังปี "+ requestBody.effectiveDate.getFullYear() +" ซ้ำ");
}
chk_SalaryPeriod.period = requestBody.period.toUpperCase();
chk_SalaryPeriod.lastUpdateUserId = request.user.sub;
chk_SalaryPeriod.lastUpdateFullName = request.user.name;
chk_SalaryPeriod.lastUpdatedAt = new Date();
this.salaryPeriodRepository.merge(chk_SalaryPeriod, requestBody);
await this.salaryPeriodRepository.save(chk_SalaryPeriod);
return new HttpSuccess(id);
}
/**
* API
*
* @summary SLR_018 - #18
*
* @param {string} id Guid, *Id
*/
@Delete("period/{id}")
async delete_salary_period(@Path() id: string) {
const SalaryPeriod = await this.salaryPeriodRepository.findOne({
where: { id: id },
});
if (!SalaryPeriod) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
await this.salaryPeriodRepository.remove(SalaryPeriod);
return new HttpSuccess();
}
/**
* API
*
* @summary SLR_019 - #19
*
* @param {string} id Guid, *Id
*/
@Get("period/{id}")
async GetSalaryPeriod_ById(@Path() id: string) {
const salaryPeriod = await this.salaryPeriodRepository.findOne({
where: { id: id },
select: [
"id",
"period",
"isActive",
"effectiveDate",
"isActive",
"status",
],
});
if (!salaryPeriod) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
return new HttpSuccess(salaryPeriod);
}
/**
* API
*
* @summary SLR_020 - #20
*
*/
@Get("period")
async GetListsSalaryPeriod(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
@Query("year") year: number = 2024,
) {
const startOfYear = new Date(year, 0, 1);
const endOfYear = new Date(year, 11, 31);
const [salaryPeriod, total] = await this.salaryPeriodRepository.findAndCount({
skip: (page - 1) * pageSize,
take: pageSize,
where: {
effectiveDate: Between(startOfYear, endOfYear)
}
});
if (keyword != undefined && keyword !== "") {
const filteredSalaryPeriod = salaryPeriod.filter(
(x) =>
x.period.toString().includes(keyword) ||
x.isActive.toString().includes(keyword) ||
x.effectiveDate.getFullYear().toString().includes(keyword)
);
const formattedData = filteredSalaryPeriod.map((item) => ({
id: item.id,
salaryType: item.period,
isSpecial: item.isActive,
posTypeId: item.effectiveDate,
posType: item.status,
}));
return new HttpSuccess({ data: formattedData, total: formattedData.length });
}
if (!salaryPeriod) {
return new HttpSuccess([]);
}
const formattedData = salaryPeriod.map((item) => ({
id: item.id,
salaryType: item.period,
isSpecial: item.isActive,
posTypeId: item.effectiveDate,
posType: item.status,
}));
return new HttpSuccess({ data: formattedData, total });
}
}