hrms-api-salary/src/controllers/SalaryController.ts

374 lines
12 KiB
TypeScript
Raw Normal View History

import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
Query,
} from "tsoa";
import { Salarys, CreateSalary, UpdateSalary } from "../entities/Salarys";
import { PosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
import { AppDataSource } from "../database/data-source";
2024-02-22 13:53:58 +07:00
import { DeepPartial, In, IsNull, Not } from "typeorm";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
2024-02-16 13:08:35 +07:00
import { SalaryRanks } from "../entities/SalaryRanks";
import { query } from "express";
2024-02-16 16:15:53 +07:00
import { randomUUID } from "crypto";
@Route("api/v1/salary")
@Tags("Salary")
@Security("bearerAuth")
export class Salary extends Controller {
private salaryRepository = AppDataSource.getRepository(Salarys);
2024-02-16 13:08:35 +07:00
private salaryRankRepository = AppDataSource.getRepository(SalaryRanks);
private poTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
/**
* API
*
* @summary SLR_001 - #1
*
*/
@Post()
@Example({
2024-03-07 14:25:14 +07:00
name: "string", //*ชื่อผัง
posTypeId: "string(Guid)", //*ระดับของตำแหน่ง
posLevelId: "string(Guid)", //*ประเภทของตำแหน่ง
isActive: "boolean", //*สถานะการใช้งาน
date: "datetime", //ให้ไว้ ณ วันที่
startDate: "datetime", //วันที่มีผลบังคับใช้
endDate: "datetime", //วันที่สิ้นสุดบังคับใช้
detail: "string", //คำอธิบาย
})
async create_salary(
@Body() requestBody: CreateSalary,
@Request() request: { user: Record<string, any> },
) {
2024-02-19 17:22:05 +07:00
// try {
const salarys = Object.assign(new Salarys(), requestBody);
if (!salarys) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chk_posTypeId = await this.poTypeRepository.findOne({
where: { id: salarys.posTypeId },
});
if (!chk_posTypeId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทของตำแหน่ง ไม่ถูกต้อง");
}
const chk_posLevelId = await this.posLevelRepository.findOne({
where: { id: salarys.posLevelId },
});
if (!chk_posLevelId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ระดับของตำแหน่ง ไม่ถูกต้อง");
}
const chk_3fields = await this.salaryRepository.findOne({
where: {
2024-03-07 14:25:14 +07:00
name: salarys.name,
posTypeId: salarys.posTypeId,
posLevelId: salarys.posLevelId,
},
});
if (chk_3fields && salarys.isActive) {
salarys.isActive = false;
}
2024-03-07 14:25:14 +07:00
salarys.name = salarys.name;
salarys.isSpecial = salarys.isSpecial;
salarys.createdUserId = request.user.sub;
salarys.createdFullName = request.user.name;
salarys.lastUpdateUserId = request.user.sub;
salarys.lastUpdateFullName = request.user.name;
await this.salaryRepository.save(salarys);
return new HttpSuccess(salarys.id);
2024-02-19 17:22:05 +07:00
// } catch (error: any) {
// throw new Error(error);
// }
}
/**
* API
*
* @summary SLR_002 - #2
*
* @param {string} id Guid, *Id
*/
@Put("{id}")
@Example({
2024-03-07 14:25:14 +07:00
name: "string", //*ชื่อผัง
posTypeId: "string(Guid)", //*ระดับของตำแหน่ง
posLevelId: "string(Guid)", //*ประเภทของตำแหน่ง
isActive: "boolean", //*สถานะการใช้งาน
date: "datetime", //ให้ไว้ ณ วันที่
startDate: "datetime", //วันที่มีผลบังคับใช้
endDate: "datetime", //วันที่สิ้นสุดบังคับใช้
detail: "string", //คำอธิบาย
})
async update_salary(
@Path() id: string,
@Body() requestBody: UpdateSalary,
@Request() request: { user: Record<string, any> },
) {
2024-02-19 17:22:05 +07:00
// try {
const chk_Salary = await this.salaryRepository.findOne({
where: { id: id },
});
if (!chk_Salary) {
2024-02-28 13:35:08 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
}
if (chk_Salary.isActive && !requestBody.isActive) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถแก้ไขสถานะการใช้งานที่เป็น Default ได้",
);
}
const chk_3fields = await this.salaryRepository.find({
where: {
posTypeId: requestBody.posTypeId,
posLevelId: requestBody.posLevelId,
isActive: true,
id: Not(id),
},
});
if (chk_3fields.length > 0 && requestBody.isActive) {
chk_3fields.forEach(async (item) => {
item.isActive = false;
item.lastUpdateUserId = request.user.sub;
item.lastUpdateFullName = request.user.name;
item.lastUpdatedAt = new Date();
await this.salaryRepository.save(chk_3fields);
});
}
const chk_posTypeId = await this.poTypeRepository.findOne({
where: { id: requestBody.posTypeId },
});
if (!chk_posTypeId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทของตำแหน่ง ไม่ถูกต้อง");
}
const chk_posLevelId = await this.posLevelRepository.findOne({
where: { id: requestBody.posLevelId },
});
if (!chk_posLevelId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ระดับของตำแหน่ง ไม่ถูกต้อง");
}
const mergeData = Object.assign(new Salarys(), requestBody);
chk_Salary.lastUpdateUserId = request.user.sub;
chk_Salary.lastUpdateFullName = request.user.name;
chk_Salary.lastUpdatedAt = new Date();
this.salaryRepository.merge(chk_Salary, mergeData);
await this.salaryRepository.save(chk_Salary);
return new HttpSuccess(id);
2024-02-19 17:22:05 +07:00
// } catch (error: any) {
// throw new Error(error);
// }
}
/**
* API
*
* @summary SLR_003 - #3
*
* @param {string} id Guid, *Id
*/
@Delete("{id}")
async delete_salary(@Path() id: string) {
2024-02-19 17:22:05 +07:00
// try {
const chk_Salary = await this.salaryRepository.findOne({
where: { id: id },
});
if (!chk_Salary) {
2024-02-28 13:35:08 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
}
if (chk_Salary.isActive) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถลบข้อมูลนี้ได้ เนื่องจากเปิดใช้งานอยู่",
);
}
const del_SalaryRank = await this.salaryRankRepository.find({
where: { salaryId: chk_Salary.id },
});
await this.salaryRankRepository.remove(del_SalaryRank);
await this.salaryRepository.remove(chk_Salary);
return new HttpSuccess();
2024-02-19 17:22:05 +07:00
// } catch (error: any) {
// throw new Error(error);
// }
}
/**
* API
*
* @summary SLR_004 - #4
*
* @param {string} id Guid, *Id
*/
@Get("{id}")
@Example({
2024-03-07 14:25:14 +07:00
name: "string", //*ชื่อผัง
posTypeId: "string(Guid)", //*ระดับของตำแหน่ง
posLevelId: "string(Guid)", //*ประเภทของตำแหน่ง
isActive: "boolean", //*สถานะการใช้งาน
date: "datetime", //ให้ไว้ ณ วันที่
startDate: "datetime", //วันที่มีผลบังคับใช้
endDate: "datetime", //วันที่สิ้นสุดบังคับใช้
detail: "string", //คำอธิบาย
})
async GetSalaryById(@Path() id: string) {
2024-02-19 17:22:05 +07:00
// try {
const salary = await this.salaryRepository.findOne({
where: { id: id },
select: [
2024-03-07 14:25:14 +07:00
"name",
"isSpecial",
"posTypeId",
"posLevelId",
"isActive",
"date",
"startDate",
"endDate",
"details",
],
});
if (!salary) {
2024-02-28 13:35:08 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
}
return new HttpSuccess(salary);
2024-02-19 17:22:05 +07:00
// } catch (error: any) {
// throw new Error(error);
// }
}
2024-02-16 16:15:53 +07:00
/**
* API
*
* @summary SLR_005 - #5
*
*/
@Get()
async listSalary(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
) {
const [salary, total] = await this.salaryRepository.findAndCount({
relations: ["posLevel_", "posType_"],
order: {
2024-03-07 15:47:22 +07:00
// startDate: "DESC",
2024-03-08 16:41:04 +07:00
isActive: "DESC",
2024-03-07 15:47:22 +07:00
posType_: {
2024-03-08 16:41:04 +07:00
posTypeRank: "DESC"
2024-03-07 15:47:22 +07:00
},
posLevel_: {
2024-03-08 16:41:04 +07:00
posLevelRank: "DESC"
2024-03-07 15:47:22 +07:00
},
},
2024-02-27 10:09:51 +07:00
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
});
if (keyword != undefined && keyword !== "") {
const filteredSalary = salary.filter(
(x) =>
2024-03-07 14:25:14 +07:00
x.name?.toString().includes(keyword) ||
x.isSpecial?.toString().includes(keyword) || //new 20.02.67
x.posLevel_?.posLevelName?.toString().includes(keyword) ||
x.posType_?.posTypeName?.toString().includes(keyword) ||
x.isActive?.toString().includes(keyword) ||
x.date?.toString().includes(keyword) ||
x.startDate?.toString().includes(keyword) ||
x.endDate?.toString().includes(keyword) ||
x.details?.toString().includes(keyword),
);
2024-02-16 17:35:48 +07:00
const formattedData = filteredSalary.map((item) => ({
id: item.id,
2024-03-07 14:25:14 +07:00
name: item.name,
2024-02-20 17:09:52 +07:00
isSpecial: item.isSpecial,
2024-02-16 13:08:35 +07:00
posTypeId: item.posType_?.id,
posType: item.posType_?.posTypeName,
2024-02-16 13:08:35 +07:00
posLevelId: item.posLevel_?.id,
posLevel: item.posLevel_?.posLevelName,
isActive: item.isActive,
date: item.date,
startDate: item.startDate,
endDate: item.endDate,
details: item.details,
}));
2024-02-27 10:09:51 +07:00
const slicedData = formattedData.slice((page - 1) * pageSize, page * pageSize);
return new HttpSuccess({ data: slicedData, total: formattedData.length });
}
const formattedData = salary.map((item) => ({
id: item.id,
2024-03-07 14:25:14 +07:00
name: item.name,
isSpecial: item.isSpecial,
posTypeId: item.posType_?.id,
posType: item.posType_?.posTypeName,
posLevelId: item.posLevel_?.id,
posLevel: item.posLevel_?.posLevelName,
isActive: item.isActive,
date: item.date,
startDate: item.startDate,
endDate: item.endDate,
details: item.details,
}));
return new HttpSuccess({ data: formattedData, total });
}
2024-02-16 13:08:35 +07:00
2024-02-16 16:15:53 +07:00
/**
* API copy
*
* @summary SLR_008 - copy #8
*
*/
@Post("copy")
async copySalary(
@Body() body: { id: string },
@Request() request: { user: Record<string, any> },
) {
const salary = await this.salaryRepository.findOne({
relations: ["posLevel_", "posType_", "salaryRanks_"],
where: { id: body.id },
});
2024-02-16 16:15:53 +07:00
if (!salary) {
2024-02-28 13:35:08 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
}
2024-02-16 13:08:35 +07:00
const salaryRank = await this.salaryRankRepository.find({
where: { salaryId: salary?.id },
});
2024-02-16 16:15:53 +07:00
const newSalary = { ...salary, id: randomUUID(), isActive: false };
2024-02-16 13:08:35 +07:00
await this.salaryRepository.save(newSalary);
2024-02-16 13:08:35 +07:00
await Promise.all(
salaryRank.map(async (v) => {
const newSalaryRank = { ...v, id: randomUUID() };
await this.salaryRankRepository.save(newSalaryRank);
}),
);
2024-02-16 13:08:35 +07:00
return new HttpSuccess({ id: newSalary.id });
2024-02-16 16:15:53 +07:00
}
}