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