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

422 lines
15 KiB
TypeScript
Raw Normal View History

import {
Controller,
Get,
Post,
Put,
Delete,
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-09-03 15:18:23 +07:00
import { Brackets, 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";
2024-02-16 16:15:53 +07:00
import { randomUUID } from "crypto";
2024-08-09 14:17:24 +07:00
import { RequestWithUser } from "../middlewares/user";
import permission from "../interfaces/permission";
2024-09-03 15:18:23 +07:00
import { setLogDataDiff } from "../interfaces/utils";
@Route("api/v1/salary")
@Tags("Salary")
@Security("bearerAuth")
2024-03-13 09:52:05 +07:00
export class SalaryController 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);
2024-12-19 23:35:04 +07:00
/**
* API
*
* @summary
*
*/
@Get("clear-db")
async ClearDb() {
return new HttpSuccess();
}
/**
* 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", //คำอธิบาย
})
2024-08-09 14:17:24 +07:00
async create_salary(@Body() requestBody: CreateSalary, @Request() request: RequestWithUser) {
await new permission().PermissionCreate(request, "SYS_SALARY_CHART_OFFICER");
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, "ระดับของตำแหน่ง ไม่ถูกต้อง");
}
2024-04-26 12:40:23 +07:00
let chk_fields: any;
if (salarys.isSpecial) {
2024-04-03 14:52:37 +07:00
chk_fields = await this.salaryRepository.findOne({
where: {
posTypeId: salarys.posTypeId,
posLevelId: salarys.posLevelId,
2024-04-26 12:40:23 +07:00
isSpecial: true,
2024-04-03 14:52:37 +07:00
},
});
2024-04-26 12:40:23 +07:00
} else {
2024-04-03 14:52:37 +07:00
chk_fields = await this.salaryRepository.findOne({
where: {
posTypeId: salarys.posTypeId,
posLevelId: salarys.posLevelId,
2024-04-26 12:40:23 +07:00
isSpecial: false,
2024-04-03 14:52:37 +07:00
},
});
}
2024-04-26 12:40:23 +07:00
2024-04-03 14:52:37 +07:00
if (chk_fields && salarys.isActive) {
salarys.isActive = false;
}
2024-08-23 14:10:34 +07:00
const before = null;
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;
salarys.createdAt = new Date();
salarys.lastUpdatedAt = new Date();
2024-08-23 14:10:34 +07:00
await this.salaryRepository.save(salarys, { data: request });
setLogDataDiff(request, { before, after: salarys });
return new HttpSuccess(salarys.id);
}
/**
* 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,
2024-08-09 14:17:24 +07:00
@Request() request: RequestWithUser,
) {
2024-08-09 14:17:24 +07:00
await new permission().PermissionUpdate(request, "SYS_SALARY_CHART_OFFICER");
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 ได้",
);
}
2024-04-26 12:40:23 +07:00
let chk_fields: any;
if (chk_Salary.isSpecial) {
2024-04-03 14:52:37 +07:00
chk_fields = await this.salaryRepository.find({
where: {
posTypeId: requestBody.posTypeId,
posLevelId: requestBody.posLevelId,
isActive: true,
isSpecial: true,
id: Not(id),
},
});
2024-04-26 12:40:23 +07:00
} else {
2024-04-03 14:52:37 +07:00
chk_fields = await this.salaryRepository.find({
where: {
posTypeId: requestBody.posTypeId,
posLevelId: requestBody.posLevelId,
isActive: true,
isSpecial: false,
id: Not(id),
},
});
}
2024-04-26 12:40:23 +07:00
2024-04-03 14:52:37 +07:00
if (chk_fields.length > 0 && requestBody.isActive) {
2024-08-23 14:10:34 +07:00
const before = structuredClone(chk_fields);
2024-04-26 12:40:23 +07:00
chk_fields.forEach(async (item: any) => {
item.isActive = false;
item.lastUpdateUserId = request.user.sub;
item.lastUpdateFullName = request.user.name;
item.lastUpdatedAt = new Date();
2024-08-23 14:10:34 +07:00
await this.salaryRepository.save(chk_fields, { data: request });
2025-03-31 18:13:03 +07:00
setLogDataDiff(request, { before, after: chk_fields });
});
}
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);
2024-08-23 14:10:34 +07:00
const before = structuredClone(chk_Salary);
chk_Salary.lastUpdateUserId = request.user.sub;
chk_Salary.lastUpdateFullName = request.user.name;
chk_Salary.lastUpdatedAt = new Date();
this.salaryRepository.merge(chk_Salary, mergeData);
2024-08-23 14:10:34 +07:00
await this.salaryRepository.save(chk_Salary, { data: request });
return new HttpSuccess(id);
}
/**
* API
*
* @summary SLR_003 - #3
*
* @param {string} id Guid, *Id
*/
@Delete("{id}")
2024-08-09 14:17:24 +07:00
async delete_salary(@Path() id: string, @Request() request: RequestWithUser) {
await new permission().PermissionDelete(request, "SYS_SALARY_CHART_OFFICER");
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 },
});
2024-08-23 14:10:34 +07:00
await this.salaryRankRepository.remove(del_SalaryRank, { data: request });
await this.salaryRepository.remove(chk_Salary, { data: request });
return new HttpSuccess();
}
/**
* 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", //คำอธิบาย
})
2024-08-22 17:23:48 +07:00
async GetSalaryById(@Request() request: RequestWithUser, @Path() id: string) {
2024-10-22 08:21:00 +07:00
let _workflow = await new permission().Workflow(request, id, "SYS_SALARY_CHART_OFFICER");
if (_workflow == false)
await new permission().PermissionGet(request, "SYS_SALARY_CHART_OFFICER");
const salary = await this.salaryRepository.findOne({
2024-07-03 17:51:44 +07:00
relations: ["posType_", "posLevel_"],
where: { id: id },
});
2024-07-03 17:51:44 +07:00
const formattedData = {
name: salary?.name ?? null,
isSpecial: salary?.isSpecial ?? null,
posTypeId: salary?.posTypeId ?? null,
posTypeName: salary?.posType_.posTypeName ?? null,
posLevelId: salary?.posLevelId ?? null,
posLevelName: salary?.posLevel_.posLevelName ?? null,
isActive: salary?.isActive ?? null,
date: salary?.date ?? null,
startDate: salary?.startDate ?? null,
endDate: salary?.endDate ?? null,
details: salary?.details ?? null,
};
if (!salary) {
2024-02-28 13:35:08 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
}
2024-07-03 17:51:44 +07:00
return new HttpSuccess(formattedData);
}
2024-02-16 16:15:53 +07:00
/**
* API
*
* @summary SLR_005 - #5
*
*/
@Get()
async listSalary(
2024-08-22 17:23:48 +07:00
@Request() request: RequestWithUser,
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
2025-10-01 13:24:26 +07:00
@Query("sortBy") sortBy?: string,
@Query("descending") descending?: boolean,
) {
2024-08-22 17:23:48 +07:00
await new permission().PermissionList(request, "SYS_SALARY_CHART_OFFICER");
2025-10-01 13:24:26 +07:00
let query = await AppDataSource.getRepository(Salarys)
2024-04-26 12:40:23 +07:00
.createQueryBuilder("salary")
2024-04-26 17:37:21 +07:00
.leftJoinAndSelect("salary.posType_", "posType_")
.leftJoinAndSelect("salary.posLevel_", "posLevel_")
2024-04-26 12:40:23 +07:00
.andWhere(
new Brackets((qb) => {
qb.orWhere("salary.name LIKE :keyword", { keyword: `%${keyword}%` })
2024-04-26 17:37:21 +07:00
.orWhere("posType_.posTypeName LIKE :keyword", { keyword: `%${keyword}%` })
.orWhere("posLevel_.posLevelName LIKE :keyword", { keyword: `%${keyword}%` });
2024-04-26 12:40:23 +07:00
}),
)
2025-10-01 13:24:26 +07:00
if (sortBy) {
if(sortBy === "posType"){
query = query.orderBy(
`posType_.posTypeName`,
descending ? "DESC" : "ASC"
);
}else if(sortBy === "posLevel"){
query = query.orderBy(
`posLevel_.posLevelName`,
descending ? "DESC" : "ASC"
);
}else{
query = query.orderBy(
`salary.${sortBy}`,
descending ? "DESC" : "ASC"
);
}
}else{
query = query.orderBy("salary.isActive", "DESC")
.addOrderBy("posType_.posTypeRank", "DESC")
.addOrderBy("posLevel_.posLevelRank", "DESC")
}
const [salary, total] = await query
2024-04-26 12:40:23 +07:00
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
2025-10-01 13:24:26 +07:00
2024-04-26 12:40:23 +07:00
const _salary = 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,
}));
2024-04-26 12:40:23 +07:00
return new HttpSuccess({ data: _salary, 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")
2024-08-22 17:23:48 +07:00
async copySalary(@Body() body: { id: string }, @Request() request: RequestWithUser) {
2024-08-09 14:17:24 +07:00
await new permission().PermissionCreate(request, "SYS_SALARY_CHART_OFFICER");
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
2024-08-23 14:10:34 +07:00
const before = null;
const newSalary = { ...salary, id: randomUUID(), isActive: false };
2024-02-16 13:08:35 +07:00
newSalary.createdUserId = request.user.sub;
newSalary.createdFullName = request.user.name;
newSalary.lastUpdateUserId = request.user.sub;
newSalary.lastUpdateFullName = request.user.name;
newSalary.createdAt = new Date();
newSalary.lastUpdatedAt = new Date();
2024-08-23 14:10:34 +07:00
await this.salaryRepository.save(newSalary, { data: request });
setLogDataDiff(request, { before, after: newSalary });
2024-02-16 13:08:35 +07:00
await Promise.all(
salaryRank.map(async (v) => {
const newSalaryRank = { ...v, id: randomUUID() };
newSalary.createdUserId = request.user.sub;
newSalary.createdFullName = request.user.name;
newSalary.lastUpdateUserId = request.user.sub;
newSalary.lastUpdateFullName = request.user.name;
newSalary.createdAt = new Date();
newSalary.lastUpdatedAt = new Date();
2024-08-23 14:10:34 +07:00
await this.salaryRankRepository.save(newSalaryRank, { data: request });
setLogDataDiff(request, { before, after: newSalaryRank });
}),
);
2024-02-16 13:08:35 +07:00
return new HttpSuccess({ id: newSalary.id });
2024-02-16 16:15:53 +07:00
}
}