แก้ไขเงินเดือน

This commit is contained in:
Kittapath 2024-02-27 08:58:22 +07:00
parent f79704c7a7
commit bf12751bfa
10 changed files with 969 additions and 582 deletions

View file

@ -57,48 +57,48 @@ export class Salary extends Controller {
@Request() request: { user: Record<string, any> },
) {
// try {
const salarys = Object.assign(new Salarys(), requestBody);
if (!salarys) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
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_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_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_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.isSpecial = salarys.isSpecial;
salarys.createdUserId = request.user.sub;
salarys.createdFullName = request.user.name;
salarys.lastUpdateUserId = request.user.sub;
salarys.lastUpdateFullName = request.user.name;
await this.salaryRepository.save(salarys);
return new HttpSuccess(salarys.id);
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.isSpecial = salarys.isSpecial;
salarys.createdUserId = request.user.sub;
salarys.createdFullName = request.user.name;
salarys.lastUpdateUserId = request.user.sub;
salarys.lastUpdateFullName = request.user.name;
await this.salaryRepository.save(salarys);
return new HttpSuccess(salarys.id);
// } catch (error: any) {
// throw new Error(error);
// }
@ -128,64 +128,64 @@ export class Salary extends Controller {
@Request() request: { user: Record<string, any> },
) {
// try {
const chk_Salary = await this.salaryRepository.findOne({
where: { id: id },
});
if (!chk_Salary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
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 ได้",
);
}
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),
},
});
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);
}
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_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_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, "ระดับของตำแหน่ง ไม่ถูกต้อง");
}
const mergeData = Object.assign(new Salarys(), requestBody);
chk_Salary.lastUpdateUserId = request.user.sub;
chk_Salary.lastUpdateFullName = request.user.name;
chk_Salary.lastUpdatedAt = new Date();
this.salaryRepository.merge(chk_Salary, mergeData);
await this.salaryRepository.save(chk_Salary);
return new HttpSuccess(id);
const chk_posLevelId = await this.posLevelRepository.findOne({
where: { id: requestBody.posLevelId },
});
if (!chk_posLevelId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ระดับของตำแหน่ง ไม่ถูกต้อง");
}
const mergeData = Object.assign(new Salarys(), requestBody);
chk_Salary.lastUpdateUserId = request.user.sub;
chk_Salary.lastUpdateFullName = request.user.name;
chk_Salary.lastUpdatedAt = new Date();
this.salaryRepository.merge(chk_Salary, mergeData);
await this.salaryRepository.save(chk_Salary);
return new HttpSuccess(id);
// } catch (error: any) {
// throw new Error(error);
// }
@ -201,25 +201,25 @@ export class Salary extends Controller {
@Delete("{id}")
async delete_salary(@Path() id: string) {
// 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",
"ไม่สามารถลบข้อมูลนี้ได้ เนื่องจากเปิดใช้งานอยู่",
);
}
const del_SalaryRank = await this.salaryRankRepository.find({
where: { salaryId: chk_Salary.id }
});
await this.salaryRankRepository.remove(del_SalaryRank);
await this.salaryRepository.remove(chk_Salary);
return new HttpSuccess();
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",
"ไม่สามารถลบข้อมูลนี้ได้ เนื่องจากเปิดใช้งานอยู่",
);
}
const del_SalaryRank = await this.salaryRankRepository.find({
where: { salaryId: chk_Salary.id },
});
await this.salaryRankRepository.remove(del_SalaryRank);
await this.salaryRepository.remove(chk_Salary);
return new HttpSuccess();
// } catch (error: any) {
// throw new Error(error);
// }
@ -245,24 +245,24 @@ export class Salary extends Controller {
})
async GetSalaryById(@Path() id: string) {
// try {
const salary = await this.salaryRepository.findOne({
where: { id: id },
select: [
"salaryType",
"isSpecial",
"posTypeId",
"posLevelId",
"isActive",
"date",
"startDate",
"endDate",
"details",
],
});
if (!salary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
return new HttpSuccess(salary);
const salary = await this.salaryRepository.findOne({
where: { id: id },
select: [
"salaryType",
"isSpecial",
"posTypeId",
"posLevelId",
"isActive",
"date",
"startDate",
"endDate",
"details",
],
});
if (!salary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
return new HttpSuccess(salary);
// } catch (error: any) {
// throw new Error(error);
// }
@ -280,52 +280,31 @@ export class Salary extends Controller {
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
) {
//ssss total ผิด
// 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.isSpecial?.toString().includes(keyword) || //new 20.02.67
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 [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.isSpecial?.toString().includes(keyword) || //new 20.02.67
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) => ({
id: item.id,
salaryType: item.salaryType,
isSpecial: item.isSpecial,
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) => ({
const formattedData = filteredSalary.map((item) => ({
id: item.id,
salaryType: item.salaryType,
isSpecial: item.isSpecial,
@ -339,7 +318,25 @@ export class Salary extends Controller {
endDate: item.endDate,
details: item.details,
}));
return new HttpSuccess({ data: formattedData, total });
return new HttpSuccess({ data: formattedData, total: formattedData.length });
}
const formattedData = salary.map((item) => ({
id: item.id,
salaryType: item.salaryType,
isSpecial: item.isSpecial,
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 });
// } catch (error: any) {
// throw new Error(error);
// }
@ -357,34 +354,31 @@ export class Salary extends Controller {
@Request() request: { user: Record<string, any> },
) {
// try {
const salary = await this.salaryRepository.findOne({
relations: ["posLevel_", "posType_", "salaryRanks_"],
where: { id: body.id },
});
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 salaryRank = await this.salaryRankRepository.find({
where: { salaryId: salary?.id },
});
const newSalary = { ...salary, id: randomUUID(), isActive: false };
const newSalary = { ...salary, id: randomUUID(), isActive: false };
await this.salaryRepository.save(newSalary);
await this.salaryRepository.save(newSalary);
await Promise.all(
salaryRank.map(async (v) => {
const newSalaryRank = { ...v, id: randomUUID() };
await this.salaryRankRepository.save(newSalaryRank);
}),
);
await Promise.all(
salaryRank.map(async (v) => {
const newSalaryRank = { ...v, id: randomUUID() };
await this.salaryRankRepository.save(newSalaryRank);
}),
);
return new HttpSuccess({ id: newSalary.id });
return new HttpSuccess({ id: newSalary.id });
// } catch (error: any) {
// throw new Error(error);
// }