152 lines
5.4 KiB
TypeScript
152 lines
5.4 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
Body,
|
|
Path,
|
|
Request,
|
|
SuccessResponse,
|
|
Response,
|
|
Get,
|
|
Query,
|
|
} from "tsoa";
|
|
import { Brackets, Like } from "typeorm";
|
|
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 { 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, "ไม่พบข้อมูลผังเงินเดือนนี้");
|
|
}
|
|
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> },
|
|
) {
|
|
const salaryRank = await this.salaryRankRepository.findOne({ where: { id: id } });
|
|
if (!salaryRank) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับผังเงินเดือนนี้");
|
|
}
|
|
salaryRank.lastUpdateUserId = request.user.sub;
|
|
salaryRank.lastUpdateFullName = request.user.name;
|
|
this.salaryRankRepository.merge(salaryRank, requestBody);
|
|
await this.salaryRankRepository.save(salaryRank);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API ลบอัตราเงินเดือน
|
|
*
|
|
* @summary SLR_011 - ลบอัตราเงินเดือน #11
|
|
*
|
|
* @param {string} id Id อัตราเงินเดือน
|
|
*/
|
|
@Delete("{id}")
|
|
async deleteSalaryRanks(@Path() id: string) {
|
|
const delSalaryRanks = await this.salaryRankRepository.findOne({
|
|
where: { id },
|
|
});
|
|
if (!delSalaryRanks) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับผังเงินเดือนนี้");
|
|
}
|
|
await this.salaryRankRepository.delete({ id: id });
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* 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 AppDataSource.getRepository(SalaryRanks)
|
|
.createQueryBuilder("salaryRank")
|
|
.andWhere(
|
|
new Brackets((qb) => {
|
|
qb.andWhere(`salaryRank.salaryId LIKE :id`, { id: id }).andWhere(
|
|
new Brackets((qb) => {
|
|
qb.orWhere("salaryRank.salary LIKE :keyword", { keyword: `%${keyword}%` })
|
|
.orWhere("salaryRank.salaryHalf LIKE :keyword", { keyword: `%${keyword}%` })
|
|
.orWhere("salaryRank.salaryHalfSpecial LIKE :keyword", { keyword: `%${keyword}%` })
|
|
.orWhere("salaryRank.salaryFull LIKE :keyword", { keyword: `%${keyword}%` })
|
|
.orWhere("salaryRank.salaryFullSpecial LIKE :keyword", { keyword: `%${keyword}%` })
|
|
.orWhere("salaryRank.salaryFullHalf LIKE :keyword", { keyword: `%${keyword}%` })
|
|
.orWhere("salaryRank.salaryFullHalfSpecial LIKE :keyword", {
|
|
keyword: `%${keyword}%`,
|
|
});
|
|
}),
|
|
);
|
|
}),
|
|
)
|
|
.orderBy("salaryRank.salary", "DESC")
|
|
.addOrderBy("salaryRank.salaryHalf", "DESC")
|
|
.skip((page - 1) * pageSize)
|
|
.take(pageSize)
|
|
.getManyAndCount();
|
|
|
|
return new HttpSuccess({ data: salaryRank, total });
|
|
}
|
|
}
|