225 lines
8.1 KiB
TypeScript
225 lines
8.1 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 Child1
|
|
*/
|
|
@Get("{id}")
|
|
async GetChild1(@Path() id: string) {
|
|
try {
|
|
const orgChild1 = await this.child1Repository.findOne({ where: { id } });
|
|
if (!orgChild1) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
|
}
|
|
const getOrgChild1 = {
|
|
"orgChild1Id" : orgChild1.id,
|
|
"orgChild1Name" : orgChild1.orgChild1Name,
|
|
"orgChild1ShortName" : orgChild1.orgChild1ShortName,
|
|
"orgChild1Code" : orgChild1.orgChild1Code,
|
|
"orgChild1Order" : orgChild1.orgChild1Order,
|
|
"orgChild1PhoneEx" : orgChild1.orgChild1PhoneEx,
|
|
"orgChild1PhoneIn" : orgChild1.orgChild1PhoneIn,
|
|
"orgChild1Fax" : orgChild1.orgChild1Fax,
|
|
"orgRevisionId" : orgChild1.orgRevisionId,
|
|
"orgCode" : orgChild1.orgChild1Code+"00"
|
|
}
|
|
return new HttpSuccess(getOrgChild1);
|
|
} 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;
|
|
}
|
|
}
|
|
}
|