198 lines
6.3 KiB
TypeScript
198 lines
6.3 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
Body,
|
|
Path,
|
|
Request,
|
|
SuccessResponse,
|
|
Response,
|
|
Get,
|
|
Query,
|
|
} from "tsoa";
|
|
import { AppDataSource } from "../database/data-source";
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
import HttpError from "../interfaces/http-error";
|
|
import { CreateSalaryRank, SalaryRanks, UpdateSalaryRank } from "../entities/SalaryRanks";
|
|
import { Not } from "typeorm";
|
|
import { Salarys } from "../entities/Salarys";
|
|
@Route("api/v1/salary/rate")
|
|
@Tags("SalaryRank")
|
|
@Security("bearerAuth")
|
|
@Response(
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
)
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
export class SalaryRanksController extends Controller {
|
|
private salaryRankRepository = AppDataSource.getRepository(SalaryRanks);
|
|
private salaryRepository = AppDataSource.getRepository(Salarys);
|
|
|
|
/**
|
|
* API สร้างอัตราเงินเดือน
|
|
*
|
|
* @summary SLR_009 - สร้างอัตราเงินเดือน #9
|
|
*
|
|
*/
|
|
@Post()
|
|
async CreateSalaryRank(
|
|
@Body()
|
|
requestBody: CreateSalaryRank,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
try {
|
|
const checkSalary = await this.salaryRepository.findOne({
|
|
where: { id: requestBody.salaryId },
|
|
});
|
|
if (!checkSalary) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลไอดีนี้ : " + requestBody.salaryId,
|
|
);
|
|
}
|
|
const salaryRank = Object.assign(new SalaryRanks(), requestBody);
|
|
salaryRank.createdUserId = request.user.sub;
|
|
salaryRank.createdFullName = request.user.name;
|
|
salaryRank.lastUpdateUserId = request.user.sub;
|
|
salaryRank.lastUpdateFullName = request.user.name;
|
|
await this.salaryRankRepository.save(salaryRank);
|
|
return new HttpSuccess();
|
|
} catch (error: any) {
|
|
throw new Error(error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขอัตราเงินเดือน
|
|
*
|
|
* @summary SLR_010 - แก้ไขอัตราเงินเดือน #10
|
|
*
|
|
* @param {string} id Id อัตราเงินเดือน
|
|
*/
|
|
@Put("{id}")
|
|
async updateSalaryRanks(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: UpdateSalaryRank,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
// try {
|
|
const salaryRank = await this.salaryRankRepository.findOne({ where: { id: id } });
|
|
if (!salaryRank) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
|
|
}
|
|
salaryRank.lastUpdateUserId = request.user.sub;
|
|
salaryRank.lastUpdateFullName = request.user.name;
|
|
this.salaryRankRepository.merge(salaryRank, requestBody);
|
|
await this.salaryRankRepository.save(salaryRank);
|
|
return new HttpSuccess();
|
|
// } catch (error: any) {
|
|
// throw new Error(error);
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* API ลบอัตราเงินเดือน
|
|
*
|
|
* @summary SLR_011 - ลบอัตราเงินเดือน #11
|
|
*
|
|
* @param {string} id Id อัตราเงินเดือน
|
|
*/
|
|
@Delete("{id}")
|
|
async deleteSalaryRanks(@Path() id: string) {
|
|
// try {
|
|
const delSalaryRanks = await this.salaryRankRepository.findOne({
|
|
where: { id },
|
|
});
|
|
if (!delSalaryRanks) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id);
|
|
}
|
|
await this.salaryRankRepository.delete({ id: id });
|
|
return new HttpSuccess();
|
|
// } catch (error: any) {
|
|
// throw new Error(error);
|
|
// }
|
|
}
|
|
|
|
// /**
|
|
// * API รายละเอียดรายการอัตราเงินเดือน
|
|
// *
|
|
// * @summary ORG_059 - รายละเอียดรายการอัตราเงินเดือน (ADMIN) #65
|
|
// *
|
|
// * @param {string} id Id อัตราเงินเดือน
|
|
// */
|
|
// @Get("{id}")
|
|
// async detailSalaryRanks(@Path() id: string) {
|
|
// const salaryRank = await this.salaryRankRepository.findOne({
|
|
// where: { id },
|
|
// select: ["id"],
|
|
// });
|
|
// if (!salaryRank) {
|
|
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
|
// }
|
|
// try {
|
|
// return new HttpSuccess(salaryRank);
|
|
// } catch (error) {
|
|
// return error;
|
|
// }
|
|
// }
|
|
|
|
/**
|
|
* API รายการอัตราเงินเดือน
|
|
*
|
|
* @summary SLR_012 - รายการอัตราเงินเดือน #12
|
|
*
|
|
* @param {string} id Id ผังเงินเดือน
|
|
*/
|
|
@Get("{id}")
|
|
async listSalaryRanks(
|
|
@Path() id: string,
|
|
@Query("page") page: number = 1,
|
|
@Query("pageSize") pageSize: number = 10,
|
|
@Query("keyword") keyword?: string,
|
|
) {
|
|
|
|
const [salaryRank, total] = await this.salaryRankRepository.findAndCount({
|
|
where: {
|
|
salaryId: id,
|
|
},
|
|
select: [
|
|
"id",
|
|
"salary",
|
|
"salaryHalf",
|
|
"salaryHalfSpecial",
|
|
"salaryFull",
|
|
"salaryFullSpecial",
|
|
"salaryFullHalf",
|
|
"salaryFullHalfSpecial",
|
|
"isNext",
|
|
],
|
|
order: {
|
|
salary: "DESC",
|
|
salaryHalf: "DESC",
|
|
},
|
|
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
|
});
|
|
if (keyword != undefined && keyword !== "") {
|
|
const filteredSalaryRank = salaryRank.filter(
|
|
(x) =>
|
|
x.salary?.toString().includes(keyword) ||
|
|
x.salaryHalf?.toString().includes(keyword) ||
|
|
x.salaryHalfSpecial?.toString().includes(keyword) ||
|
|
x.salaryFull?.toString().includes(keyword) ||
|
|
x.salaryFullSpecial?.toString().includes(keyword) ||
|
|
x.salaryFullHalf?.toString().includes(keyword) ||
|
|
x.salaryFullHalfSpecial?.toString().includes(keyword),
|
|
);
|
|
const slicedData = filteredSalaryRank.slice((page - 1) * pageSize, page * pageSize);
|
|
return new HttpSuccess({ data: slicedData, total: filteredSalaryRank.length });
|
|
}
|
|
|
|
return new HttpSuccess({ data: salaryRank, total });
|
|
}
|
|
}
|