hrms-api-org/src/controllers/OrgChild2Controller.ts

255 lines
9.9 KiB
TypeScript
Raw Normal View History

2024-01-25 14:35:19 +07:00
import { AppDataSource } from "../database/data-source";
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Path,
Body,
Request,
Example,
2024-01-26 17:09:28 +07:00
SuccessResponse,
Response,
2024-01-25 14:35:19 +07:00
} from "tsoa";
import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
2024-01-29 13:02:32 +07:00
import { OrgRevision } from "../entities/OrgRevision";
2024-01-29 14:55:13 +07:00
import { OrgRoot } from "../entities/OrgRoot";
2024-01-26 17:09:28 +07:00
import { CreateOrgChild2, OrgChild2, UpdateOrgChild2 } from "../entities/OrgChild2";
2024-01-25 15:45:57 +07:00
import { OrgChild1 } from "../entities/OrgChild1";
import { OrgChild3 } from "../entities/OrgChild3";
@Route("api/v1/org/child2")
2024-01-25 14:35:19 +07:00
@Tags("OrgChild2")
@Security("bearerAuth")
2024-01-26 17:09:28 +07:00
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
2024-01-25 14:35:19 +07:00
export class OrgChild2Controller extends Controller {
2024-01-29 14:55:13 +07:00
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
2024-01-26 17:09:28 +07:00
private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
2024-01-29 13:02:32 +07:00
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
2024-01-25 14:35:19 +07:00
/**
* API 2
*
* @summary ORG_018 - 2 (ADMIN) #20
*
* @param {string} id Id Child2
*/
@Get("{id}")
async GetChild2(@Path() id: string) {
try {
const orgChild2 = await this.child2Repository.findOne({ where: { id } });
if (!orgChild2) {
2024-01-29 14:55:13 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 2");
}
const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild2.orgRootId } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root");
}
const getOrgChild2 = {
"orgChild2Id" : orgChild2.id,
"orgChild2Name" : orgChild2.orgChild2Name,
"orgChild2ShortName" : orgChild2.orgChild2ShortName,
"orgChild2Code" : orgChild2.orgChild2Code,
2024-01-30 10:37:07 +07:00
"orgChild2Rank" : orgChild2.orgChild2Rank,
"orgChild2Order" : orgChild2.orgChild2Order,
"orgChild2PhoneEx" : orgChild2.orgChild2PhoneEx,
"orgChild2PhoneIn" : orgChild2.orgChild2PhoneIn,
"orgChild2Fax" : orgChild2.orgChild2Fax,
"orgRevisionId" : orgChild2.orgRevisionId,
2024-01-29 14:55:13 +07:00
"orgCode" : orgRoot.orgRootCode + orgChild2.orgChild2Code
}
return new HttpSuccess(getOrgChild2);
} catch (error) {
return error;
}
}
2024-01-25 14:35:19 +07:00
/**
* 2 Child2
*
* @summary ORG_007 - 2 (ADMIN) #7
*
*/
2024-01-26 17:09:28 +07:00
@Post()
2024-01-25 14:35:19 +07:00
@Example([
{
orgChild2Name: "string", //ชื่อหน่วยงาน
orgChild2ShortName: "string", //อักษรย่อ
orgChild2Code: "string", //รหัสหน่วยงาน
// orgChild2Order: "number", //ลำดับที่ของหน่วยงาน
2024-01-25 14:35:19 +07:00
orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
orgChild2Fax: "string", //หมายเลขโทรสาร
2024-01-26 17:09:28 +07:00
// orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
2024-01-25 14:35:19 +07:00
orgChild1Id: "Guid", //id Child1
},
])
async create(
@Body()
requestBody: CreateOrgChild2,
@Request() request: { user: Record<string, any> },
) {
try {
2024-01-26 17:09:28 +07:00
//BE ใช้ orgChild1Id หา orgChild1Id, orgRootId
const child1 = await this.child1Repository.findOne({
2024-01-25 14:35:19 +07:00
where: { id: requestBody.orgChild1Id },
});
2024-01-26 17:09:28 +07:00
if (!child1) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
2024-01-25 14:35:19 +07:00
}
2024-01-29 13:02:32 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child1.orgRevisionId },
2024-01-29 13:02:32 +07:00
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false");
}
2024-01-29 13:02:32 +07:00
2024-01-26 17:09:28 +07:00
const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
2024-01-25 14:35:19 +07:00
}
2024-01-26 17:09:28 +07:00
const child2 = Object.assign(new OrgChild2(), requestBody) as OrgChild2;
child2.orgChild2Name = requestBody.orgChild2Name;
child2.createdUserId = request.user.sub;
child2.createdFullName = request.user.name;
child2.lastUpdateUserId = request.user.sub;
child2.lastUpdateFullName = request.user.name;
child2.orgRootId = String(child1?.orgRootId);
child2.orgRevisionId = String(child1?.orgRevisionId);
2024-01-26 17:09:28 +07:00
child2.orgChild1Id = String(child1?.id);
await this.child2Repository.save(child2);
2024-01-25 14:35:19 +07:00
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
2024-01-25 15:45:57 +07:00
* 2 Child2
2024-01-25 14:35:19 +07:00
*
2024-01-25 15:45:57 +07:00
* @summary ORG_008 - 2 (ADMIN) #8
2024-01-25 14:35:19 +07:00
*
* @param {string} id Guid, *Id Child2
*/
2024-01-30 13:37:45 +07:00
@Put("{id}")
2024-01-25 15:45:57 +07:00
@Example([
{
orgChild2Name: "string", //ชื่อหน่วยงาน
orgChild2ShortName: "string", //อักษรย่อ
orgChild2Code: "string", //รหัสหน่วยงาน
// orgChild2Order: "number", //ลำดับที่ของหน่วยงาน
2024-01-25 15:45:57 +07:00
orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
orgChild2Fax: "string", //หมายเลขโทรสาร
2024-01-26 17:09:28 +07:00
// orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
2024-01-25 15:45:57 +07:00
orgChild1Id: "Guid", //id Child1
},
])
async update(
@Path() id: string,
@Body()
2024-01-26 17:09:28 +07:00
requestBody: UpdateOrgChild2,
2024-01-25 15:45:57 +07:00
@Request() request: { user: Record<string, any> },
) {
try {
2024-01-26 17:09:28 +07:00
const child1IdExits = await this.child1Repository.findOne({
2024-01-25 15:45:57 +07:00
where: { id: requestBody.orgChild1Id },
});
2024-01-26 17:09:28 +07:00
if (!child1IdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id");
}
2024-01-29 13:02:32 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child1IdExits.orgRevisionId },
2024-01-29 13:02:32 +07:00
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false");
}
2024-01-29 13:02:32 +07:00
2024-01-26 17:09:28 +07:00
const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (
requestBody.orgChild2Rank == null ||
!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
2024-01-25 15:45:57 +07:00
}
2024-01-26 17:09:28 +07:00
const child2 = await this.child2Repository.findOne({ where: { id } });
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
2024-01-25 15:45:57 +07:00
}
2024-01-26 17:09:28 +07:00
child2.lastUpdateUserId = request.user.sub;
child2.lastUpdateFullName = request.user.name;
child2.lastUpdatedAt = new Date();
child2.orgRootId = String(child1IdExits?.orgRootId);
child2.orgRevisionId = String(child1IdExits?.orgRevisionId);
2024-01-26 17:09:28 +07:00
child2.orgChild1Id = String(child1IdExits?.id);
this.child2Repository.merge(child2, requestBody);
await this.child2Repository.save(child2);
2024-01-25 15:45:57 +07:00
return new HttpSuccess();
} catch (error) {
return error;
}
}
2024-01-25 14:35:19 +07:00
/**
* Child2
*
2024-01-25 15:45:57 +07:00
* @summary ORG_009 - 2 (ADMIN) #9
2024-01-25 14:35:19 +07:00
*
* @param {string} id Guid, *Id Child2
*/
2024-01-30 13:37:45 +07:00
@Delete("{id}")
2024-01-25 15:45:57 +07:00
async delete(@Path() id: string) {
try {
2024-01-26 17:09:28 +07:00
const child2 = await this.child2Repository.findOne({ where: { id } });
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
2024-01-25 15:45:57 +07:00
}
2024-01-29 13:02:32 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child2.orgRevisionId },
2024-01-29 13:02:32 +07:00
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false");
}
2024-01-29 13:02:32 +07:00
2024-01-26 17:09:28 +07:00
const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } });
if (exitsChild3) {
throw new HttpError(
2024-01-26 17:09:28 +07:00
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4",
);
2024-01-25 15:45:57 +07:00
}
2024-01-26 17:09:28 +07:00
await this.child2Repository.remove(child2);
2024-01-25 15:45:57 +07:00
return new HttpSuccess();
} catch (error) {
return error;
}
}
2024-01-25 14:35:19 +07:00
}