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

227 lines
8.5 KiB
TypeScript
Raw Normal View History

import { AppDataSource } from "../database/data-source";
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-25 15:54:03 +07:00
import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3, CreateOrgChild3, UpdateOrgChild3 } from "../entities/OrgChild3";
import { OrgChild4 } from "../entities/OrgChild4";
import {
Body,
Delete,
Get,
Path,
Post,
Put,
Response,
Route,
SuccessResponse,
Tags,
Query,
Request,
Security,
} from "tsoa";
2024-01-25 15:54:03 +07:00
import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
2024-01-26 16:41:52 +07:00
@Route("api/v1/org/child3")
2024-01-25 15:54:03 +07:00
@Tags("OrgChild3")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrgChild3Controller {
2024-01-29 14:55:13 +07:00
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
private child4Repository = AppDataSource.getRepository(OrgChild4);
2024-01-29 13:02:32 +07:00
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/**
* API 3
*
* @summary ORG_019 - 3 (ADMIN) #18
*
* @param {string} id Id Child3
*/
@Get("{id}")
async GetChild3(@Path() id: string) {
try {
const orgChild3 = await this.child3Repository.findOne({ where: { id } });
if (!orgChild3) {
2024-01-29 14:55:13 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 3");
}
const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild3.orgRootId } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root");
}
const getOrgChild3 = {
"orgChild3Id" : orgChild3.id,
"orgChild3Name" : orgChild3.orgChild3Name,
"orgChild3ShortName" : orgChild3.orgChild3ShortName,
"orgChild3Code" : orgChild3.orgChild3Code,
2024-01-30 10:37:07 +07:00
"orgChild3Rank" : orgChild3.orgChild3Rank,
"orgChild3Order" : orgChild3.orgChild3Order,
"orgChild3PhoneEx" : orgChild3.orgChild3PhoneEx,
"orgChild3PhoneIn" : orgChild3.orgChild3PhoneIn,
"orgChild3Fax" : orgChild3.orgChild3Fax,
"orgRevisionId" : orgChild3.orgRevisionId,
2024-01-29 14:55:13 +07:00
"orgCode" : orgRoot.orgRootCode + orgChild3.orgChild3Code
}
return new HttpSuccess(getOrgChild3);
} catch (error) {
return error;
}
}
/**
* API 3
*
* @summary ORG_010 - 3 (ADMIN) #10
*
*/
@Post()
async save(
@Body() requestBody: CreateOrgChild3,
@Request() request: { user: Record<string, any> },
) {
try {
//BE ใช้ orgChild2Id หา orgChild1Id, orgRootId
const child2 = await this.child2Repository.findOne({
where: { id: requestBody.orgChild2Id },
});
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
2024-01-26 16:41:52 +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 16:41:52 +07:00
const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild3Ranks.includes(requestBody.orgChild3Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild3Rank");
}
const child3 = Object.assign(new OrgChild3(), requestBody) as OrgChild3;
child3.orgChild3Name = requestBody.orgChild3Name;
child3.createdUserId = request.user.sub;
child3.createdFullName = request.user.name;
child3.lastUpdateUserId = request.user.sub;
child3.lastUpdateFullName = request.user.name;
2024-01-26 16:41:52 +07:00
child3.orgRootId = String(child2?.orgRootId);
child3.orgChild1Id = String(child2?.orgChild1Id);
child3.orgRevisionId = String(child2?.orgRevisionId);
2024-01-26 16:41:52 +07:00
child3.orgChild2Id = String(child2?.id);
await this.child3Repository.save(child3);
return new HttpSuccess();
} catch (error) {
return error;
2024-01-25 15:54:03 +07:00
}
}
2024-01-25 15:54:03 +07:00
/**
* API 3
*
* @summary ORG_011 - 3 (ADMIN) #11
*
* @param {string} id id 3
*/
@Put("{id}")
async Edit(
@Path() id: string,
@Body() requestBody: UpdateOrgChild3,
@Request() request: { user: Record<string, any> },
) {
try {
2024-01-26 16:41:52 +07:00
const child2IdExits = await this.child2Repository.findOne({
where: { id: requestBody.orgChild2Id },
});
if (!child2IdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child2Id");
}
2024-01-29 13:02:32 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child2IdExits.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 validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (
requestBody.orgChild3Rank == null ||
!validOrgChild3Ranks.includes(requestBody.orgChild3Rank.toUpperCase())
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
const child3 = await this.child3Repository.findOne({ where: { id } });
if (!child3) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
2024-01-25 15:54:03 +07:00
child3.lastUpdateUserId = request.user.sub;
child3.lastUpdateFullName = request.user.name;
child3.lastUpdatedAt = new Date();
2024-01-26 16:41:52 +07:00
child3.orgRootId = String(child2IdExits?.orgRootId);
child3.orgChild1Id = String(child2IdExits?.orgChild1Id);
child3.orgRevisionId = String(child2IdExits?.orgRevisionId);
2024-01-26 16:41:52 +07:00
child3.orgChild2Id = String(child2IdExits?.id);
this.child3Repository.merge(child3, requestBody);
await this.child3Repository.save(child3);
return new HttpSuccess();
} catch (error) {
return error;
2024-01-25 15:54:03 +07:00
}
}
2024-01-25 15:54:03 +07:00
/**
* API 3
*
* @summary ORG_012 - 3 (ADMIN) #12
*
* @param {string} id id 3
*/
@Delete("{id}")
async delete(@Path() id: string) {
try {
const child3 = await this.child3Repository.findOne({ where: { id } });
if (!child3) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
2024-01-29 13:02:32 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child3.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
const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } });
if (exitsChild4) {
throw new HttpError(
2024-01-26 16:41:52 +07:00
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4",
);
}
await this.child3Repository.remove(child3);
return new HttpSuccess();
} catch (error) {
return error;
2024-01-25 15:54:03 +07:00
}
}
}