hrms-api-org/src/controllers/OrgChild1Controller.ts
2024-01-29 12:54:49 +07:00

248 lines
8.6 KiB
TypeScript

import { AppDataSource } from "../database/data-source";
import { OrgRevision } from "../entities/OrgRevision";
import { OrgRoot } from "../entities/OrgRoot";
import { OrgChild1, CreateOrgChild1, UpdateOrgChild1 } from "../entities/OrgChild1";
import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3 } from "../entities/OrgChild3";
import {
Body,
Delete,
Get,
Path,
Post,
Put,
Response,
Route,
SuccessResponse,
Tags,
Query,
Request,
Security,
} from "tsoa";
import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import { Not } from "typeorm";
@Route("api/v1/org/child1")
@Tags("OrgChild1")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrgChild1Controller {
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/**
* API รายละเอียดโครงสร้างระดับ1
*
* @summary ORG_017 - รายละเอียดโครงสร้างระดับ1 (ADMIN) #17
*
* @param {string} id id โครงสร้างระดับ1
*/
// @Get("{id}")
// async Get(@Path() id: string) {
// try {
// const child2s = await this.child2Repository.find({
// where: {
// orgChild1Id: id,
// },
// select: ["id", "orgChild2Name", "orgChild2ShortName", "orgChild2Code", "orgChild2Order", "orgRootId"],
// order: {
// orgChild2Order: "ASC",
// },
// });
// const orgRoots = await Promise.all(child2s.map(async (child2) => {
// const orgRoot = await this.orgRootRepository.findOne({
// where: {
// id: child2.orgRootId,
// },
// select: ["orgRootCode"],
// });
// const orgChild3s = await this.child3Repository.find({
// where: {
// orgChild2Id: child2.id,
// },
// select: ["id"],
// });
// const orgChild3Ids = orgChild3s.map((orgChild3) => orgChild3.id);
// return {
// orgChild2Id: child2.id,
// orgChild3Id: orgChild3Ids,
// orgChild2Name: child2.orgChild2Name,
// orgChild2ShortName: child2.orgChild2ShortName,
// orgChild2Code: child2.orgChild2Code,
// orgChild2Order: child2.orgChild2Order,
// orgRootCode: orgRoot?.orgRootCode,
// };
// }));
// return new HttpSuccess(orgRoots);
// } catch (error) {
// return error;
// }
// }
/**
* API สร้างโครงสร้างระดับ1
*
* @summary ORG_004 - สร้างโครงสร้างระดับ1 (ADMIN) #4
*
*/
@Post()
async save(
@Body() requestBody: CreateOrgChild1,
@Request() request: { user: Record<string, any> },
) {
try {
const rootIdExits = await this.orgRootRepository.findOne({
where: { id: requestBody.orgRootId },
});
if (!rootIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootId");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: rootIdExits.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild1Rank");
}
const chkCode = await this.child1Repository.findOne({
where: { orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code },
});
if (chkCode != null) {
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว",
);
}
const child1 = Object.assign(new OrgChild1(), requestBody) as OrgChild1;
child1.orgChild1Name = requestBody.orgChild1Name;
child1.createdUserId = request.user.sub;
child1.createdFullName = request.user.name;
child1.lastUpdateUserId = request.user.sub;
child1.lastUpdateFullName = request.user.name;
child1.orgRevisionId = String(rootIdExits?.orgRevisionId);
child1.orgRootId = String(rootIdExits?.id);
await this.child1Repository.save(child1);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API แก้ไขโครงสร้างระดับ1
*
* @summary ORG_005 - แก้ไขโครงสร้างระดับ1 (ADMIN) #5
*
* @param {string} id id สร้างโครงสร้างระดับ1
*/
@Put("{id}")
async Edit(
@Path() id: string,
@Body() requestBody: UpdateOrgChild1,
@Request() request: { user: Record<string, any> },
) {
try {
const rootIdExits = await this.orgRootRepository.findOne({
where: { id: requestBody.orgRootId },
});
if (!rootIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RootId");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: rootIdExits.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (
requestBody.orgChild1Rank == null ||
!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
const child1 = await this.child1Repository.findOne({ where: { id } });
if (!child1) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const chkCode = await this.child1Repository.findOne({
where: {
id: Not(id),
orgRootId: requestBody.orgRootId,
orgChild1Code: requestBody.orgChild1Code,
},
});
if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
child1.lastUpdateUserId = request.user.sub;
child1.lastUpdateFullName = request.user.name;
child1.lastUpdatedAt = new Date();
child1.orgRevisionId = String(rootIdExits?.orgRevisionId);
child1.orgRootId = String(rootIdExits?.id);
this.child1Repository.merge(child1, requestBody);
await this.child1Repository.save(child1);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API ลบโครงสร้างระดับ1
*
* @summary ORG_006 - ลบโครงสร้างระดับ1 (ADMIN) #6
*
* @param {string} id id สร้างโครงสร้างระดับ1
*/
@Delete("{id}")
async delete(@Path() id: string) {
try {
const child1 = await this.child1Repository.findOne({ where: { id } });
if (!child1) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child1.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } });
if (exitsChild2) {
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2",
);
}
await this.child1Repository.remove(child1);
return new HttpSuccess();
} catch (error) {
return error;
}
}
}