hrms-api-salary/src/controllers/PosTypeController.ts

259 lines
7.9 KiB
TypeScript
Raw Normal View History

2024-02-15 17:19:58 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { PosType, CreatePosType, UpdatePosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
import HttpError from "../interfaces/http-error";
import { Not } from "typeorm";
2024-08-23 14:10:34 +07:00
import { RequestWithUser } from "../middlewares/user";
2024-09-03 15:18:23 +07:00
import { setLogDataDiff } from "../interfaces/utils";
2024-02-15 17:19:58 +07:00
@Route("api/v1/salary/pos/type")
@Tags("PosType")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class PosTypeController extends Controller {
private posTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
/**
* API
*
* @summary (ADMIN)
*
*/
@Post()
@Example({
positionName: "นักบริหาร",
posTypeRank: 1,
})
2024-02-15 17:19:58 +07:00
async createType(
@Body()
requestBody: CreatePosType,
2024-08-23 14:10:34 +07:00
@Request() request: RequestWithUser,
2024-02-15 17:19:58 +07:00
) {
const posType = Object.assign(new PosType(), requestBody);
if (!posType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chkPosTypeName = await this.posTypeRepository.findOne({
where: {
posTypeName: requestBody.posTypeName,
},
});
if (chkPosTypeName) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ชื่อประเภทตำแหน่ง: " + requestBody.posTypeName + " มีอยู่ในระบบแล้ว",
);
2024-02-15 17:19:58 +07:00
}
2024-02-19 17:22:05 +07:00
// try {
2024-08-23 14:10:34 +07:00
const before = null;
posType.createdUserId = request.user.sub;
posType.createdFullName = request.user.name;
posType.lastUpdateUserId = request.user.sub;
posType.lastUpdateFullName = request.user.name;
posType.createdAt = new Date();
posType.lastUpdatedAt = new Date();
2024-08-23 14:10:34 +07:00
await this.posTypeRepository.save(posType, { data: request });
setLogDataDiff(request, { before, after: posType });
return new HttpSuccess(posType);
2024-02-19 17:22:05 +07:00
// } catch (error) {
// return error;
// }
2024-02-15 17:19:58 +07:00
}
/**
* API
*
* @summary (ADMIN)
*
* @param {string} id Id
*/
@Put("{id}")
@Example({
positionName: "นักบริหาร",
posTypeRank: 1,
})
2024-02-15 17:19:58 +07:00
async editType(
@Path() id: string,
@Body() requestBody: UpdatePosType,
2024-08-23 14:10:34 +07:00
@Request() request: RequestWithUser,
2024-02-15 17:19:58 +07:00
) {
const posType = await this.posTypeRepository.findOne({ where: { id } });
if (!posType) {
2024-02-28 13:35:08 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
2024-02-15 17:19:58 +07:00
}
const chkPosTypeName = await this.posTypeRepository.findOne({
where: {
id: Not(id),
posTypeName: requestBody.posTypeName,
},
});
if (chkPosTypeName) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ชื่อประเภทตำแหน่ง: " + requestBody.posTypeName + " มีอยู่ในระบบแล้ว",
);
2024-02-15 17:19:58 +07:00
}
2024-02-19 17:22:05 +07:00
// try {
2024-08-23 14:10:34 +07:00
const before = structuredClone(posType);
posType.lastUpdateUserId = request.user.sub;
posType.lastUpdateFullName = request.user.name;
posType.lastUpdatedAt = new Date();
this.posTypeRepository.merge(posType, requestBody);
2024-08-23 14:10:34 +07:00
await this.posTypeRepository.save(posType, { data: request });
setLogDataDiff(request, { before, after: posType });
return new HttpSuccess(posType.id);
2024-02-19 17:22:05 +07:00
// } catch (error) {
// return error;
// }
2024-02-15 17:19:58 +07:00
}
/**
* API
*
* @summary (ADMIN)
*
* @param {string} id Id
*/
@Delete("{id}")
2024-08-23 14:10:34 +07:00
async deleteType(@Path() id: string, @Request() request: RequestWithUser) {
2024-02-15 17:19:58 +07:00
const delPosType = await this.posTypeRepository.findOne({ where: { id } });
if (!delPosType) {
2024-02-28 13:35:08 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
2024-02-15 17:19:58 +07:00
}
const IdExitsInLevel = await this.posLevelRepository.find({
where: { posTypeId: id },
});
if (IdExitsInLevel.length > 0) {
2024-02-28 13:35:08 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลระดับตำแหน่งนี้");
2024-02-15 17:19:58 +07:00
}
2024-02-19 17:22:05 +07:00
// try {
2024-08-23 14:10:34 +07:00
await this.posTypeRepository.remove(delPosType, { data: request });
return new HttpSuccess();
2024-02-19 17:22:05 +07:00
// } catch (error) {
// return error;
// }
2024-02-15 17:19:58 +07:00
}
/**
* API
*
* @summary (ADMIN)
*
* @param {string} id Id
*/
@Get("{id}")
@Example([
{
id: "00000000-0000-0000-0000-000000000000",
posTypeName: "นักบริหาร",
posLevelposTypeRanks: 1,
posLevels: [
{
id: "00000000-0000-0000-0000-000000000000",
posLevelName: "นักบริหาร",
posLevelRank: 1,
posLevelAuthority: "HEAD",
},
],
2024-02-15 17:19:58 +07:00
},
])
async GetTypeDetail(@Path() id: string) {
2024-02-19 17:22:05 +07:00
// try {
const getPosType = await this.posTypeRepository.findOne({
select: ["id", "posTypeName", "posTypeRank"],
relations: ["posLevels"],
where: { id: id },
});
if (!getPosType) {
2024-02-28 13:35:08 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทต");
}
2024-02-15 17:19:58 +07:00
const mapGetPosType = {
id: getPosType.id,
posTypeName: getPosType.posTypeName,
posTypeRank: getPosType.posTypeRank,
posLevels: getPosType.posLevels.map((posLevel) => ({
id: posLevel.id,
posLevelName: posLevel.posLevelName,
posLevelRank: posLevel.posLevelRank,
posLevelAuthority: posLevel.posLevelAuthority,
})),
};
return new HttpSuccess(mapGetPosType);
2024-02-19 17:22:05 +07:00
// } catch (error) {
// return error;
// }
2024-02-15 17:19:58 +07:00
}
/**
* API
*
* @summary SLR_006 - #6
*
*/
@Get()
@Example([
{
id: "00000000-0000-0000-0000-000000000000",
posTypeName: "นักบริหาร",
posTypeRank: 1,
posLevels: [
{
id: "00000000-0000-0000-0000-000000000000",
posLevelName: "นักบริหาร",
posLevelRank: 1,
posLevelAuthority: "HEAD",
},
],
},
])
async GetPosType() {
2024-02-19 17:22:05 +07:00
// try {
const posType = await this.posTypeRepository.find({
select: ["id", "posTypeName", "posTypeRank"],
relations: ["posLevels"],
});
const mapPosType = posType.map((item) => ({
id: item.id,
posTypeName: item.posTypeName,
posTypeRank: item.posTypeRank,
posLevels: item.posLevels.map((posLevel) => ({
id: posLevel.id,
posLevelName: posLevel.posLevelName,
posLevelRank: posLevel.posLevelRank,
posLevelAuthority: posLevel.posLevelAuthority,
})),
}));
return new HttpSuccess(mapPosType);
2024-02-19 17:22:05 +07:00
// } catch (error) {
// return error;
// }
2024-02-15 17:19:58 +07:00
}
}