hrms-api-org/src/controllers/OrgChild2Controller.ts
2024-01-26 20:25:18 +07:00

182 lines
6.7 KiB
TypeScript

import { AppDataSource } from "../database/data-source";
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Path,
Body,
Request,
Example,
SuccessResponse,
Response,
} from "tsoa";
import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import { CreateOrgChild2, OrgChild2, UpdateOrgChild2 } from "../entities/OrgChild2";
import { OrgChild1 } from "../entities/OrgChild1";
import { OrgChild3 } from "../entities/OrgChild3";
@Route("api/v1/org/child2")
@Tags("OrgChild2")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrgChild2Controller extends Controller {
private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
/**
* สร้างโครงสร้างระดับ2 Child2
*
* @summary ORG_007 - สร้างโครงสร้างระดับ2 (ADMIN) #7
*
*/
@Post()
@Example([
{
orgChild2Name: "string", //ชื่อหน่วยงาน
orgChild2ShortName: "string", //อักษรย่อ
orgChild2Code: "string", //รหัสหน่วยงาน
// orgChild2Order: "number", //ลำดับที่ของหน่วยงาน
orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
orgChild2Fax: "string", //หมายเลขโทรสาร
// orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
orgChild1Id: "Guid", //id Child1
},
])
async create(
@Body()
requestBody: CreateOrgChild2,
@Request() request: { user: Record<string, any> },
) {
try {
//BE ใช้ orgChild1Id หา orgChild1Id, orgRootId
const child1 = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
if (!child1) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
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);
child2.orgChild1Id = String(child1?.id);
await this.child2Repository.save(child2);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* แก้ไขโครงสร้างระดับ2 Child2
*
* @summary ORG_008 - แก้ไขโครงสร้างระดับ2 (ADMIN) #8
*
* @param {string} id Guid, *Id Child2
*/
@Put("child2/{id}")
@Example([
{
orgChild2Name: "string", //ชื่อหน่วยงาน
orgChild2ShortName: "string", //อักษรย่อ
orgChild2Code: "string", //รหัสหน่วยงาน
// orgChild2Order: "number", //ลำดับที่ของหน่วยงาน
orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
orgChild2Fax: "string", //หมายเลขโทรสาร
// orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
orgChild1Id: "Guid", //id Child1
},
])
async update(
@Path() id: string,
@Body()
requestBody: UpdateOrgChild2,
@Request() request: { user: Record<string, any> },
) {
try {
const child1IdExits = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
if (!child1IdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id");
}
const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (
requestBody.orgChild2Rank == null ||
!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
const child2 = await this.child2Repository.findOne({ where: { id } });
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
child2.lastUpdateUserId = request.user.sub;
child2.lastUpdateFullName = request.user.name;
child2.lastUpdatedAt = new Date();
child2.orgRootId = String(child1IdExits?.orgRootId);
child2.orgRevisionId = String(child1IdExits?.orgRevisionId);
child2.orgChild1Id = String(child1IdExits?.id);
this.child2Repository.merge(child2, requestBody);
await this.child2Repository.save(child2);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* ลบโครงสร้างระดับ Child2
*
* @summary ORG_009 - ลบโครงสร้างระดับ2 (ADMIN) #9
*
* @param {string} id Guid, *Id Child2
*/
@Delete("Child2/{id}")
async delete(@Path() id: string) {
try {
const child2 = await this.child2Repository.findOne({ where: { id } });
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } });
if (exitsChild3) {
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4",
);
}
await this.child2Repository.remove(child2);
return new HttpSuccess();
} catch (error) {
return error;
}
}
}