This commit is contained in:
AdisakKanthawilang 2024-02-16 17:35:48 +07:00
parent 7105aeb0f0
commit 00b643c129
2 changed files with 351 additions and 345 deletions

View file

@ -56,42 +56,41 @@ export class Salary extends Controller {
@Body() requestBody: CreateSalary,
@Request() request: { user: Record<string, any> },
) {
const salarys = Object.assign(new Salarys(), requestBody);
if (!salarys) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chk_salaryType = ["OFFICER", "EMPLOYEE"];
if (!chk_salaryType.includes(salarys.salaryType.toUpperCase())) {
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: {
salaryType: salarys.salaryType,
posTypeId: salarys.posTypeId,
posLevelId: salarys.posLevelId,
},
});
if (chk_3fields && salarys.isActive) {
salarys.isActive = false;
}
try {
const salarys = Object.assign(new Salarys(), requestBody);
if (!salarys) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chk_salaryType = ["OFFICER", "EMPLOYEE"];
if (!chk_salaryType.includes(salarys.salaryType.toUpperCase())) {
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: {
salaryType: salarys.salaryType,
posTypeId: salarys.posTypeId,
posLevelId: salarys.posLevelId,
},
});
if (chk_3fields && salarys.isActive) {
salarys.isActive = false;
}
salarys.salaryType = salarys.salaryType.toUpperCase();
salarys.createdUserId = request.user.sub;
salarys.createdFullName = request.user.name;
@ -99,8 +98,8 @@ export class Salary extends Controller {
salarys.lastUpdateFullName = request.user.name;
await this.salaryRepository.save(salarys);
return new HttpSuccess(salarys.id);
} catch (error) {
return error;
} catch (error: any) {
throw new Error(error);
}
}
@ -127,66 +126,66 @@ export class Salary extends Controller {
@Body() requestBody: UpdateSalary,
@Request() request: { user: Record<string, any> },
) {
const chk_Salary = await this.salaryRepository.findOne({
where: { id: id },
});
if (!chk_Salary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
if (chk_Salary.isActive && !requestBody.isActive) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถแก้ไขสถานะการใช้งานที่เป็น Default ได้",
);
}
const chk_3fields = await this.salaryRepository.findOne({
where: {
salaryType: requestBody.salaryType,
posTypeId: requestBody.posTypeId,
posLevelId: requestBody.posLevelId,
isActive: true,
id: Not(id),
},
});
if (chk_3fields != null && requestBody.isActive) {
chk_3fields.isActive = false;
chk_3fields.lastUpdateUserId = request.user.sub;
chk_3fields.lastUpdateFullName = request.user.name;
chk_3fields.lastUpdatedAt = new Date();
await this.salaryRepository.save(chk_3fields);
}
const chk_salaryType = ["OFFICER", "EMPLOYEE"];
if (!chk_salaryType.includes(String(requestBody.salaryType).toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผัง ไม่ถูกต้อง");
}
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, "ระดับของตำแหน่ง ไม่ถูกต้อง");
}
try {
const chk_Salary = await this.salaryRepository.findOne({
where: { id: id },
});
if (!chk_Salary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
if (chk_Salary.isActive && !requestBody.isActive) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถแก้ไขสถานะการใช้งานที่เป็น Default ได้",
);
}
const chk_3fields = await this.salaryRepository.findOne({
where: {
salaryType: requestBody.salaryType,
posTypeId: requestBody.posTypeId,
posLevelId: requestBody.posLevelId,
isActive: true,
id: Not(id),
},
});
if (chk_3fields != null && requestBody.isActive) {
chk_3fields.isActive = false;
chk_3fields.lastUpdateUserId = request.user.sub;
chk_3fields.lastUpdateFullName = request.user.name;
chk_3fields.lastUpdatedAt = new Date();
await this.salaryRepository.save(chk_3fields);
}
const chk_salaryType = ["OFFICER", "EMPLOYEE"];
if (!chk_salaryType.includes(String(requestBody.salaryType).toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผัง ไม่ถูกต้อง");
}
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, "ระดับของตำแหน่ง ไม่ถูกต้อง");
}
chk_Salary.lastUpdateUserId = request.user.sub;
chk_Salary.lastUpdateFullName = request.user.name;
chk_Salary.lastUpdatedAt = new Date();
this.salaryRepository.merge(chk_Salary, requestBody);
await this.salaryRepository.save(chk_Salary);
return new HttpSuccess(id);
} catch (error) {
return error;
} catch (error: any) {
throw new Error(error);
}
}
@ -199,24 +198,23 @@ export class Salary extends Controller {
*/
@Delete("{id}")
async delete_salary(@Path() id: string) {
const chk_Salary = await this.salaryRepository.findOne({
where: { id: id },
});
if (!chk_Salary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
if (chk_Salary.isActive) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถลบไอดี: " + id + " ได้ เนื่องสถานะการใช้งานที่เป็น Default",
);
}
try {
const chk_Salary = await this.salaryRepository.findOne({
where: { id: id },
});
if (!chk_Salary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
if (chk_Salary.isActive) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถลบไอดี: " + id + " ได้ เนื่องสถานะการใช้งานที่เป็น Default",
);
}
await this.salaryRepository.remove(chk_Salary);
return new HttpSuccess();
} catch (error) {
return error;
} catch (error: any) {
throw new Error(error);
}
}
@ -257,8 +255,8 @@ export class Salary extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
return new HttpSuccess(salary);
} catch (error) {
return error;
} catch (error: any) {
throw new Error(error);
}
}
@ -274,28 +272,50 @@ export class Salary extends Controller {
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
) {
const [salary, total] = await this.salaryRepository.findAndCount({
relations: ["posLevel_", "posType_"],
order: {
startDate: "DESC",
},
skip: (page - 1) * pageSize,
take: pageSize,
});
if (keyword != undefined && keyword !== "") {
const filteredSalary = salary.filter(
(x) =>
x.salaryType?.toString().includes(keyword) ||
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),
);
try {
const [salary, total] = await this.salaryRepository.findAndCount({
relations: ["posLevel_", "posType_"],
order: {
startDate: "DESC",
},
skip: (page - 1) * pageSize,
take: pageSize,
});
if (keyword != undefined && keyword !== "") {
const filteredSalary = salary.filter(
(x) =>
x.salaryType?.toString().includes(keyword) ||
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),
);
const formattedData = filteredSalary.map((item) => ({
const formattedData = filteredSalary.map((item) => ({
id: item.id,
salaryType: item.salaryType,
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: formattedData.length });
}
if (!salary) {
return new HttpSuccess([]);
}
const formattedData = salary.map((item) => ({
id: item.id,
salaryType: item.salaryType,
posTypeId: item.posType_?.id,
@ -308,31 +328,9 @@ export class Salary extends Controller {
endDate: item.endDate,
details: item.details,
}));
return new HttpSuccess({ data: formattedData, total: formattedData.length });
}
if (!salary) {
return new HttpSuccess([]);
}
const formattedData = salary.map((item) => ({
id: item.id,
salaryType: item.salaryType,
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,
}));
try {
return new HttpSuccess({ data: formattedData, total });
} catch (error) {
return error;
} catch (error: any) {
throw new Error(error);
}
}
@ -347,30 +345,37 @@ export class Salary extends Controller {
@Body() body: { id: string },
@Request() request: { user: Record<string, any> },
) {
const salary = await this.salaryRepository.findOne({
relations: ["posLevel_", "posType_", "salaryRanks_"],
where: { id: body.id },
});
try {
const salary = await this.salaryRepository.findOne({
relations: ["posLevel_", "posType_", "salaryRanks_"],
where: { id: body.id },
});
if (!salary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเงินเดือนจากไอดีนี้ : " + body.id);
if (!salary) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลเงินเดือนจากไอดีนี้ : " + body.id,
);
}
const salaryRank = await this.salaryRankRepository.find({
where: { salaryId: salary?.id },
});
const newSalary = { ...salary, id: randomUUID(), isActive: false };
await this.salaryRepository.save(newSalary);
await Promise.all(
salaryRank.map(async (v) => {
const newSalaryRank = { ...v, id: randomUUID() };
await this.salaryRankRepository.save(newSalaryRank);
}),
);
return new HttpSuccess({ id: newSalary.id });
} catch (error: any) {
throw new Error(error);
}
const salaryRank = await this.salaryRankRepository.find({
where: { salaryId: salary?.id },
});
const newSalary = { ...salary, id: randomUUID() };
await this.salaryRepository.save(newSalary);
await Promise.all(
salaryRank.map(async (v) => {
const newSalaryRank = { ...v, id: randomUUID() };
await this.salaryRankRepository.save(newSalaryRank);
}),
);
return new HttpSuccess({ id: newSalary.id });
}
}

View file

@ -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);
}
}
}