fix bug
This commit is contained in:
parent
7105aeb0f0
commit
00b643c129
2 changed files with 351 additions and 345 deletions
|
|
@ -1,203 +1,204 @@
|
|||
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> },
|
||||
) {
|
||||
const checkSalary = await this.salaryRepository.findOne({
|
||||
where: {id:requestBody.salaryId}
|
||||
})
|
||||
if (!checkSalary) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + requestBody.salaryId)
|
||||
}
|
||||
try {
|
||||
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) {
|
||||
return error;
|
||||
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> },
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
try {
|
||||
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) {
|
||||
return error;
|
||||
}
|
||||
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) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
try {
|
||||
await this.salaryRankRepository.delete({ id: id });
|
||||
return new HttpSuccess();
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 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,
|
||||
) {
|
||||
try {
|
||||
const [salaryRank, total] = await this.salaryRankRepository.findAndCount({
|
||||
where: {
|
||||
salaryId: id,
|
||||
},
|
||||
select: [
|
||||
"id",
|
||||
"salary",
|
||||
"salaryHalf",
|
||||
"salaryHalfSpecial",
|
||||
"salaryFull",
|
||||
"salaryFullSpecial",
|
||||
"salaryFullHalf",
|
||||
"salaryFullHalfSpecial",
|
||||
"isNext",
|
||||
"id",
|
||||
"salary",
|
||||
"salaryHalf",
|
||||
"salaryHalfSpecial",
|
||||
"salaryFull",
|
||||
"salaryFullSpecial",
|
||||
"salaryFullHalf",
|
||||
"salaryFullHalfSpecial",
|
||||
"isNext",
|
||||
],
|
||||
order: {
|
||||
salary: "DESC",
|
||||
salaryHalf: "DESC"
|
||||
salary: "DESC",
|
||||
salaryHalf: "DESC",
|
||||
},
|
||||
skip: (page - 1) * pageSize,
|
||||
take: 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)),
|
||||
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),
|
||||
);
|
||||
return new HttpSuccess({ data: filteredSalaryRank, total: filteredSalaryRank.length });
|
||||
}
|
||||
|
||||
|
||||
if (!salaryRank) {
|
||||
return new HttpSuccess([]);
|
||||
}
|
||||
try {
|
||||
return new HttpSuccess({ data:salaryRank , total });
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
return new HttpSuccess({ data: salaryRank, total });
|
||||
} catch (error: any) {
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue