ผังเงินเดือนลูกจ้าง
This commit is contained in:
parent
0cf3d3dc85
commit
87f29e22e9
7 changed files with 699 additions and 61 deletions
|
|
@ -4,7 +4,6 @@ import {
|
|||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
Patch,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
|
|
@ -18,12 +17,11 @@ import { Salarys, CreateSalary, UpdateSalary } from "../entities/Salarys";
|
|||
import { PosType } from "../entities/PosType";
|
||||
import { PosLevel } from "../entities/PosLevel";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import { DeepPartial, In, IsNull, Not } from "typeorm";
|
||||
import { Not } from "typeorm";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import { SalaryRanks } from "../entities/SalaryRanks";
|
||||
import { query } from "express";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
@Route("api/v1/salary")
|
||||
|
|
@ -56,7 +54,6 @@ export class Salary extends Controller {
|
|||
@Body() requestBody: CreateSalary,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
// try {
|
||||
const salarys = Object.assign(new Salarys(), requestBody);
|
||||
if (!salarys) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
|
|
@ -78,7 +75,6 @@ export class Salary extends Controller {
|
|||
|
||||
const chk_3fields = await this.salaryRepository.findOne({
|
||||
where: {
|
||||
name: salarys.name,
|
||||
posTypeId: salarys.posTypeId,
|
||||
posLevelId: salarys.posLevelId,
|
||||
},
|
||||
|
|
@ -94,9 +90,6 @@ export class Salary extends Controller {
|
|||
salarys.lastUpdateFullName = request.user.name;
|
||||
await this.salaryRepository.save(salarys);
|
||||
return new HttpSuccess(salarys.id);
|
||||
// } catch (error: any) {
|
||||
// throw new Error(error);
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -122,7 +115,6 @@ export class Salary extends Controller {
|
|||
@Body() requestBody: UpdateSalary,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
// try {
|
||||
const chk_Salary = await this.salaryRepository.findOne({
|
||||
where: { id: id },
|
||||
});
|
||||
|
|
@ -147,7 +139,7 @@ export class Salary extends Controller {
|
|||
});
|
||||
|
||||
if (chk_3fields.length > 0 && requestBody.isActive) {
|
||||
chk_3fields.forEach(async (item) => {
|
||||
chk_3fields.forEach(async (item) => {
|
||||
item.isActive = false;
|
||||
item.lastUpdateUserId = request.user.sub;
|
||||
item.lastUpdateFullName = request.user.name;
|
||||
|
|
@ -177,9 +169,6 @@ export class Salary extends Controller {
|
|||
this.salaryRepository.merge(chk_Salary, mergeData);
|
||||
await this.salaryRepository.save(chk_Salary);
|
||||
return new HttpSuccess(id);
|
||||
// } catch (error: any) {
|
||||
// throw new Error(error);
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -191,7 +180,6 @@ export class Salary extends Controller {
|
|||
*/
|
||||
@Delete("{id}")
|
||||
async delete_salary(@Path() id: string) {
|
||||
// try {
|
||||
const chk_Salary = await this.salaryRepository.findOne({
|
||||
where: { id: id },
|
||||
});
|
||||
|
|
@ -210,9 +198,6 @@ export class Salary extends Controller {
|
|||
await this.salaryRankRepository.remove(del_SalaryRank);
|
||||
await this.salaryRepository.remove(chk_Salary);
|
||||
return new HttpSuccess();
|
||||
// } catch (error: any) {
|
||||
// throw new Error(error);
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -234,7 +219,6 @@ export class Salary extends Controller {
|
|||
detail: "string", //คำอธิบาย
|
||||
})
|
||||
async GetSalaryById(@Path() id: string) {
|
||||
// try {
|
||||
const salary = await this.salaryRepository.findOne({
|
||||
where: { id: id },
|
||||
select: [
|
||||
|
|
@ -253,9 +237,6 @@ export class Salary extends Controller {
|
|||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
|
||||
}
|
||||
return new HttpSuccess(salary);
|
||||
// } catch (error: any) {
|
||||
// throw new Error(error);
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -273,13 +254,12 @@ export class Salary extends Controller {
|
|||
const [salary, total] = await this.salaryRepository.findAndCount({
|
||||
relations: ["posLevel_", "posType_"],
|
||||
order: {
|
||||
// startDate: "DESC",
|
||||
isActive: "DESC",
|
||||
posType_: {
|
||||
posTypeRank: "DESC"
|
||||
posTypeRank: "DESC",
|
||||
},
|
||||
posLevel_: {
|
||||
posLevelRank: "DESC"
|
||||
posLevelRank: "DESC",
|
||||
},
|
||||
},
|
||||
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
||||
|
|
|
|||
287
src/controllers/SalaryEmployeeController.ts
Normal file
287
src/controllers/SalaryEmployeeController.ts
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
Body,
|
||||
Path,
|
||||
Request,
|
||||
Example,
|
||||
Query,
|
||||
} from "tsoa";
|
||||
import {
|
||||
SalaryEmployees,
|
||||
CreateSalaryEmployee,
|
||||
UpdateSalaryEmployee,
|
||||
} from "../entities/SalaryEmployees";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import { Not } from "typeorm";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import { SalaryRankEmployees } from "../entities/SalaryRankEmployees";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
@Route("api/v1/salary")
|
||||
@Tags("Salary")
|
||||
@Security("bearerAuth")
|
||||
export class Salary extends Controller {
|
||||
private salaryEmployeeRepository = AppDataSource.getRepository(SalaryEmployees);
|
||||
private salaryRankEmployeeRepository = AppDataSource.getRepository(SalaryRankEmployees);
|
||||
|
||||
/**
|
||||
* API สร้างผังเงินเดือนลูกจ้าง
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Post()
|
||||
@Example({
|
||||
name: "string", //*ชื่อผัง
|
||||
group: "string", //*กลุ่มผัง
|
||||
isActive: "boolean", //*สถานะการใช้งาน
|
||||
date: "datetime", //ให้ไว้ ณ วันที่
|
||||
startDate: "datetime", //วันที่มีผลบังคับใช้
|
||||
endDate: "datetime", //วันที่สิ้นสุดบังคับใช้
|
||||
detail: "string", //คำอธิบาย
|
||||
})
|
||||
async create_salary(
|
||||
@Body() requestBody: CreateSalaryEmployee,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const salarys = Object.assign(new SalaryEmployees(), requestBody);
|
||||
if (!salarys) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
|
||||
const chk_3fields = await this.salaryEmployeeRepository.findOne({
|
||||
where: {
|
||||
group: salarys.group,
|
||||
},
|
||||
});
|
||||
if (chk_3fields && salarys.isActive) {
|
||||
salarys.isActive = false;
|
||||
}
|
||||
salarys.createdUserId = request.user.sub;
|
||||
salarys.createdFullName = request.user.name;
|
||||
salarys.lastUpdateUserId = request.user.sub;
|
||||
salarys.lastUpdateFullName = request.user.name;
|
||||
await this.salaryEmployeeRepository.save(salarys);
|
||||
return new HttpSuccess(salarys.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* API แก้ไขผังเงินเดือนลูกจ้าง
|
||||
*
|
||||
*
|
||||
* @param {string} id Guid, *Id ผังเงินเดือน
|
||||
*/
|
||||
@Put("{id}")
|
||||
@Example({
|
||||
name: "string", //*ชื่อผัง
|
||||
group: "string", //*กลุ่มผัง
|
||||
isActive: "boolean", //*สถานะการใช้งาน
|
||||
date: "datetime", //ให้ไว้ ณ วันที่
|
||||
startDate: "datetime", //วันที่มีผลบังคับใช้
|
||||
endDate: "datetime", //วันที่สิ้นสุดบังคับใช้
|
||||
detail: "string", //คำอธิบาย
|
||||
})
|
||||
async update_salary(
|
||||
@Path() id: string,
|
||||
@Body() requestBody: UpdateSalaryEmployee,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const chk_Salary = await this.salaryEmployeeRepository.findOne({
|
||||
where: { id: id },
|
||||
});
|
||||
if (!chk_Salary) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
|
||||
}
|
||||
|
||||
if (chk_Salary.isActive && !requestBody.isActive) {
|
||||
throw new HttpError(
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ไม่สามารถแก้ไขสถานะการใช้งานที่เป็น Default ได้",
|
||||
);
|
||||
}
|
||||
|
||||
const chk_3fields = await this.salaryEmployeeRepository.find({
|
||||
where: {
|
||||
group: requestBody.group,
|
||||
isActive: true,
|
||||
id: Not(id),
|
||||
},
|
||||
});
|
||||
|
||||
if (chk_3fields.length > 0 && requestBody.isActive) {
|
||||
chk_3fields.forEach(async (item) => {
|
||||
item.isActive = false;
|
||||
item.lastUpdateUserId = request.user.sub;
|
||||
item.lastUpdateFullName = request.user.name;
|
||||
item.lastUpdatedAt = new Date();
|
||||
await this.salaryEmployeeRepository.save(chk_3fields);
|
||||
});
|
||||
}
|
||||
|
||||
const mergeData = Object.assign(new SalaryEmployees(), requestBody);
|
||||
|
||||
chk_Salary.lastUpdateUserId = request.user.sub;
|
||||
chk_Salary.lastUpdateFullName = request.user.name;
|
||||
chk_Salary.lastUpdatedAt = new Date();
|
||||
this.salaryEmployeeRepository.merge(chk_Salary, mergeData);
|
||||
await this.salaryEmployeeRepository.save(chk_Salary);
|
||||
return new HttpSuccess(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* API ลบผังเงินเดือนลูกจ้าง
|
||||
*
|
||||
*
|
||||
* @param {string} id Guid, *Id ผังเงินเดือน
|
||||
*/
|
||||
@Delete("{id}")
|
||||
async delete_salary(@Path() id: string) {
|
||||
const chk_Salary = await this.salaryEmployeeRepository.findOne({
|
||||
where: { id: id },
|
||||
});
|
||||
if (!chk_Salary) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
|
||||
}
|
||||
if (chk_Salary.isActive) {
|
||||
throw new HttpError(
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ไม่สามารถลบข้อมูลนี้ได้ เนื่องจากเปิดใช้งานอยู่",
|
||||
);
|
||||
}
|
||||
const del_SalaryRank = await this.salaryRankEmployeeRepository.find({
|
||||
where: { salaryEmployeeId: chk_Salary.id },
|
||||
});
|
||||
await this.salaryRankEmployeeRepository.remove(del_SalaryRank);
|
||||
await this.salaryEmployeeRepository.remove(chk_Salary);
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API รายละเอียดผังเงินเดือนลูกจ้าง
|
||||
*
|
||||
*
|
||||
* @param {string} id Guid, *Id ผังเงินเดือน
|
||||
*/
|
||||
@Get("{id}")
|
||||
@Example({
|
||||
name: "string", //*ชื่อผัง
|
||||
group: "string", //*กลุ่มผัง
|
||||
isActive: "boolean", //*สถานะการใช้งาน
|
||||
date: "datetime", //ให้ไว้ ณ วันที่
|
||||
startDate: "datetime", //วันที่มีผลบังคับใช้
|
||||
endDate: "datetime", //วันที่สิ้นสุดบังคับใช้
|
||||
detail: "string", //คำอธิบาย
|
||||
})
|
||||
async GetSalaryById(@Path() id: string) {
|
||||
const salary = await this.salaryEmployeeRepository.findOne({
|
||||
where: { id: id },
|
||||
select: ["name", "group", "isActive", "date", "startDate", "endDate", "details"],
|
||||
});
|
||||
if (!salary) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
|
||||
}
|
||||
return new HttpSuccess(salary);
|
||||
}
|
||||
|
||||
/**
|
||||
* API รายการผังเงินเดือนลูกจ้าง
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Get()
|
||||
async listSalary(
|
||||
@Query("page") page: number = 1,
|
||||
@Query("pageSize") pageSize: number = 10,
|
||||
@Query("keyword") keyword?: string,
|
||||
) {
|
||||
const [salary, total] = await this.salaryEmployeeRepository.findAndCount({
|
||||
order: {
|
||||
isActive: "DESC",
|
||||
group: "ASC",
|
||||
},
|
||||
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
||||
});
|
||||
if (keyword != undefined && keyword !== "") {
|
||||
const filteredSalary = salary.filter(
|
||||
(x) =>
|
||||
x.name?.toString().includes(keyword) ||
|
||||
x.group?.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,
|
||||
name: item.name,
|
||||
group: item.group,
|
||||
isActive: item.isActive,
|
||||
date: item.date,
|
||||
startDate: item.startDate,
|
||||
endDate: item.endDate,
|
||||
details: item.details,
|
||||
}));
|
||||
const slicedData = formattedData.slice((page - 1) * pageSize, page * pageSize);
|
||||
return new HttpSuccess({ data: slicedData, total: formattedData.length });
|
||||
}
|
||||
|
||||
const formattedData = salary.map((item) => ({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
group: item.group,
|
||||
isActive: item.isActive,
|
||||
date: item.date,
|
||||
startDate: item.startDate,
|
||||
endDate: item.endDate,
|
||||
details: item.details,
|
||||
}));
|
||||
return new HttpSuccess({ data: formattedData, total });
|
||||
}
|
||||
|
||||
/**
|
||||
* API copy ผังเงินเดือนลูกจ้าง
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Post("copy")
|
||||
async copySalary(
|
||||
@Body() body: { id: string },
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const salary = await this.salaryEmployeeRepository.findOne({
|
||||
relations: ["salaryRankEmployees_"],
|
||||
where: { id: body.id },
|
||||
});
|
||||
|
||||
if (!salary) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
|
||||
}
|
||||
|
||||
const salaryRank = await this.salaryRankEmployeeRepository.find({
|
||||
where: { salaryEmployeeId: salary?.id },
|
||||
});
|
||||
|
||||
const newSalary = { ...salary, id: randomUUID(), isActive: false };
|
||||
|
||||
await this.salaryEmployeeRepository.save(newSalary);
|
||||
|
||||
await Promise.all(
|
||||
salaryRank.map(async (v) => {
|
||||
const newSalaryRank = { ...v, id: randomUUID() };
|
||||
await this.salaryRankEmployeeRepository.save(newSalaryRank);
|
||||
}),
|
||||
);
|
||||
|
||||
return new HttpSuccess({ id: newSalary.id });
|
||||
}
|
||||
}
|
||||
|
|
@ -19,7 +19,6 @@ 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")
|
||||
|
|
@ -50,10 +49,7 @@ export class SalaryRanksController extends Controller {
|
|||
where: { id: requestBody.salaryId },
|
||||
});
|
||||
if (!checkSalary) {
|
||||
throw new HttpError(
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ไม่พบข้อมูลผังเงินเดือนนี้"
|
||||
);
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
|
||||
}
|
||||
const salaryRank = Object.assign(new SalaryRanks(), requestBody);
|
||||
salaryRank.createdUserId = request.user.sub;
|
||||
|
|
@ -81,7 +77,6 @@ export class SalaryRanksController extends Controller {
|
|||
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, "ไม่พบข้อมูลระดับผังเงินเดือนนี้");
|
||||
|
|
@ -91,9 +86,6 @@ export class SalaryRanksController extends Controller {
|
|||
this.salaryRankRepository.merge(salaryRank, requestBody);
|
||||
await this.salaryRankRepository.save(salaryRank);
|
||||
return new HttpSuccess();
|
||||
// } catch (error: any) {
|
||||
// throw new Error(error);
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -105,7 +97,6 @@ export class SalaryRanksController extends Controller {
|
|||
*/
|
||||
@Delete("{id}")
|
||||
async deleteSalaryRanks(@Path() id: string) {
|
||||
// try {
|
||||
const delSalaryRanks = await this.salaryRankRepository.findOne({
|
||||
where: { id },
|
||||
});
|
||||
|
|
@ -114,34 +105,8 @@ export class SalaryRanksController extends Controller {
|
|||
}
|
||||
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 รายการอัตราเงินเดือน
|
||||
*
|
||||
|
|
@ -156,7 +121,6 @@ export class SalaryRanksController extends Controller {
|
|||
@Query("pageSize") pageSize: number = 10,
|
||||
@Query("keyword") keyword?: string,
|
||||
) {
|
||||
|
||||
const [salaryRank, total] = await this.salaryRankRepository.findAndCount({
|
||||
where: {
|
||||
salaryId: id,
|
||||
|
|
|
|||
149
src/controllers/SalaryRankEmployeeController.ts
Normal file
149
src/controllers/SalaryRankEmployeeController.ts
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
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 {
|
||||
CreateSalaryRankEmployee,
|
||||
SalaryRankEmployees,
|
||||
UpdateSalaryRankEmployee,
|
||||
} from "../entities/SalaryRankEmployees";
|
||||
import { Salarys } from "../entities/Salarys";
|
||||
@Route("api/v1/salary/rate")
|
||||
@Tags("SalaryRankEmployee")
|
||||
@Security("bearerAuth")
|
||||
@Response(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||||
)
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
export class SalaryRankEmployeesController extends Controller {
|
||||
private salaryRankEmployeeRepository = AppDataSource.getRepository(SalaryRankEmployees);
|
||||
private salaryRepository = AppDataSource.getRepository(Salarys);
|
||||
|
||||
/**
|
||||
* API สร้างอัตราเงินเดือนลูกจ้าง
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Post()
|
||||
async CreateSalaryRankEmployee(
|
||||
@Body()
|
||||
requestBody: CreateSalaryRankEmployee,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
try {
|
||||
const checkSalary = await this.salaryRepository.findOne({
|
||||
where: { id: requestBody.salaryEmployeeId },
|
||||
});
|
||||
if (!checkSalary) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้");
|
||||
}
|
||||
const salaryRankEmployee = Object.assign(new SalaryRankEmployees(), requestBody);
|
||||
salaryRankEmployee.createdUserId = request.user.sub;
|
||||
salaryRankEmployee.createdFullName = request.user.name;
|
||||
salaryRankEmployee.lastUpdateUserId = request.user.sub;
|
||||
salaryRankEmployee.lastUpdateFullName = request.user.name;
|
||||
await this.salaryRankEmployeeRepository.save(salaryRankEmployee);
|
||||
return new HttpSuccess();
|
||||
} catch (error: any) {
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API แก้ไขอัตราเงินเดือนลูกจ้าง
|
||||
*
|
||||
*
|
||||
* @param {string} id Id อัตราเงินเดือน
|
||||
*/
|
||||
@Put("{id}")
|
||||
async updateSalaryRankEmployees(
|
||||
@Path() id: string,
|
||||
@Body()
|
||||
requestBody: UpdateSalaryRankEmployee,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const salaryRankEmployee = await this.salaryRankEmployeeRepository.findOne({
|
||||
where: { id: id },
|
||||
});
|
||||
if (!salaryRankEmployee) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับผังเงินเดือนนี้");
|
||||
}
|
||||
salaryRankEmployee.lastUpdateUserId = request.user.sub;
|
||||
salaryRankEmployee.lastUpdateFullName = request.user.name;
|
||||
this.salaryRankEmployeeRepository.merge(salaryRankEmployee, requestBody);
|
||||
await this.salaryRankEmployeeRepository.save(salaryRankEmployee);
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API ลบอัตราเงินเดือนลูกจ้าง
|
||||
*
|
||||
*
|
||||
* @param {string} id Id อัตราเงินเดือน
|
||||
*/
|
||||
@Delete("{id}")
|
||||
async deleteSalaryRankEmployees(@Path() id: string) {
|
||||
const delSalaryRankEmployees = await this.salaryRankEmployeeRepository.findOne({
|
||||
where: { id },
|
||||
});
|
||||
if (!delSalaryRankEmployees) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับผังเงินเดือนนี้");
|
||||
}
|
||||
await this.salaryRankEmployeeRepository.delete({ id: id });
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API รายการอัตราเงินเดือนลูกจ้าง
|
||||
*
|
||||
*
|
||||
* @param {string} id Id ผังเงินเดือน
|
||||
*/
|
||||
@Get("{id}")
|
||||
async listSalaryRankEmployees(
|
||||
@Path() id: string,
|
||||
@Query("page") page: number = 1,
|
||||
@Query("pageSize") pageSize: number = 10,
|
||||
@Query("keyword") keyword?: string,
|
||||
) {
|
||||
const [salaryRankEmployee, total] = await this.salaryRankEmployeeRepository.findAndCount({
|
||||
where: {
|
||||
salaryEmployeeId: id,
|
||||
},
|
||||
select: ["id", "step", "salaryMounth", "salaryDay"],
|
||||
order: {
|
||||
step: "ASC",
|
||||
},
|
||||
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
||||
});
|
||||
if (keyword != undefined && keyword !== "") {
|
||||
const filteredSalaryRankEmployee = salaryRankEmployee.filter(
|
||||
(x) =>
|
||||
x.step?.toString().includes(keyword) ||
|
||||
x.salaryMounth?.toString().includes(keyword) ||
|
||||
x.salaryDay?.toString().includes(keyword),
|
||||
);
|
||||
const slicedData = filteredSalaryRankEmployee.slice((page - 1) * pageSize, page * pageSize);
|
||||
return new HttpSuccess({ data: slicedData, total: filteredSalaryRankEmployee.length });
|
||||
}
|
||||
|
||||
return new HttpSuccess({ data: salaryRankEmployee, total });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue