2024-01-26 09:56:27 +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 16:41:52 +07:00
|
|
|
SuccessResponse,
|
|
|
|
|
Response,
|
2024-01-26 09:56:27 +07:00
|
|
|
} from "tsoa";
|
|
|
|
|
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
|
|
|
import { CreateOrgChild4, OrgChild4, UpdateOrgChild4 } from "../entities/OrgChild4";
|
2024-01-26 09:56:27 +07:00
|
|
|
import { OrgChild1 } from "../entities/OrgChild1";
|
|
|
|
|
import { OrgChild3 } from "../entities/OrgChild3";
|
|
|
|
|
|
2024-01-26 16:41:52 +07:00
|
|
|
@Route("api/v1/org/child4")
|
2024-01-26 09:56:27 +07:00
|
|
|
@Tags("OrgChild4")
|
2024-01-26 16:41:52 +07:00
|
|
|
@Security("bearerAuth")
|
|
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
|
|
|
)
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
2024-01-26 09:56:27 +07:00
|
|
|
export class OrgChild4Controller extends Controller {
|
2024-01-26 16:41:52 +07:00
|
|
|
private child3Repository = AppDataSource.getRepository(OrgChild3);
|
|
|
|
|
private child4Repository = AppDataSource.getRepository(OrgChild4);
|
2024-01-26 09:56:27 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* สร้างโครงสร้างระดับ4 Child4
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_013 - สร้างโครงสร้างระดับ4 (ADMIN) #13
|
|
|
|
|
*
|
|
|
|
|
*/
|
2024-01-26 20:25:18 +07:00
|
|
|
@Post()
|
2024-01-26 09:56:27 +07:00
|
|
|
@Example([
|
|
|
|
|
{
|
|
|
|
|
orgChild4Name: "string", //ชื่อหน่วยงาน
|
|
|
|
|
orgChild4ShortName: "string", //อักษรย่อ
|
|
|
|
|
orgChild4Code: "string", //รหัสหน่วยงาน
|
2024-01-26 11:36:21 +07:00
|
|
|
// orgChild4Order: "number", //ลำดับที่ของหน่วยงาน
|
2024-01-26 09:56:27 +07:00
|
|
|
orgChild4PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
|
|
|
|
|
orgChild4PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
|
|
|
|
|
orgChild4Fax: "string", //หมายเลขโทรสาร
|
2024-01-26 17:09:28 +07:00
|
|
|
// orgChild4IsNormal: "boolean", //สถานะของหน่วยงาน
|
2024-01-26 09:56:27 +07:00
|
|
|
orgChild3Id: "Guid", //id Child1
|
|
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
async create(
|
|
|
|
|
@Body()
|
|
|
|
|
requestBody: CreateOrgChild4,
|
2024-01-26 11:36:21 +07:00
|
|
|
@Request() request: { user: Record<string, any> },
|
2024-01-26 09:56:27 +07:00
|
|
|
) {
|
|
|
|
|
try {
|
2024-01-26 16:41:52 +07:00
|
|
|
//BE ใช้ orgChild3Id หา orgChild1Id, orgRootId
|
|
|
|
|
const child3 = await this.child3Repository.findOne({
|
2024-01-26 09:56:27 +07:00
|
|
|
where: { id: requestBody.orgChild3Id },
|
|
|
|
|
});
|
2024-01-26 16:41:52 +07:00
|
|
|
if (!child3) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
2024-01-26 09:56:27 +07:00
|
|
|
}
|
2024-01-26 11:36:21 +07:00
|
|
|
|
2024-01-26 16:41:52 +07:00
|
|
|
const validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"];
|
|
|
|
|
if (!validOrgChild4Ranks.includes(requestBody.orgChild4Rank.toUpperCase())) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild4Rank");
|
2024-01-26 09:56:27 +07:00
|
|
|
}
|
|
|
|
|
|
2024-01-26 16:41:52 +07:00
|
|
|
const child4 = Object.assign(new OrgChild4(), requestBody) as OrgChild4;
|
|
|
|
|
child4.orgChild4Name = requestBody.orgChild4Name;
|
|
|
|
|
child4.createdUserId = request.user.sub;
|
|
|
|
|
child4.createdFullName = request.user.name;
|
|
|
|
|
child4.lastUpdateUserId = request.user.sub;
|
|
|
|
|
child4.lastUpdateFullName = request.user.name;
|
|
|
|
|
child4.orgRootId = String(child3?.orgRootId);
|
|
|
|
|
child4.orgChild1Id = String(child3?.orgChild1Id);
|
|
|
|
|
child4.orgChild2Id = String(child3?.orgChild2Id);
|
2024-01-26 20:25:18 +07:00
|
|
|
child4.orgRevisionId = String(child3?.orgRevisionId);
|
2024-01-26 16:41:52 +07:00
|
|
|
child4.orgChild3Id = String(child3?.id);
|
|
|
|
|
await this.child4Repository.save(child4);
|
|
|
|
|
|
2024-01-26 09:56:27 +07:00
|
|
|
return new HttpSuccess();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* แก้ไขโครงสร้างระดับ4 Child4
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_014 - แก้ไขโครงสร้างระดับ4 (ADMIN) #14
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id Guid, *Id Child4
|
|
|
|
|
*/
|
2024-01-26 20:25:18 +07:00
|
|
|
@Put("{id}")
|
2024-01-26 09:56:27 +07:00
|
|
|
@Example([
|
|
|
|
|
{
|
|
|
|
|
orgChild4Name: "string", //ชื่อหน่วยงาน
|
|
|
|
|
orgChild4ShortName: "string", //อักษรย่อ
|
|
|
|
|
orgChild4Code: "string", //รหัสหน่วยงาน
|
2024-01-26 11:36:21 +07:00
|
|
|
// orgChild4Order: "number", //ลำดับที่ของหน่วยงาน
|
2024-01-26 09:56:27 +07:00
|
|
|
orgChild4PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
|
|
|
|
|
orgChild4PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
|
|
|
|
|
orgChild4Fax: "string", //หมายเลขโทรสาร
|
2024-01-26 17:09:28 +07:00
|
|
|
// orgChild4IsNormal: "boolean", //สถานะของหน่วยงาน
|
2024-01-26 09:56:27 +07:00
|
|
|
orgChild3Id: "Guid", //id Child1
|
|
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
async update(
|
|
|
|
|
@Path() id: string,
|
|
|
|
|
@Body()
|
2024-01-26 16:41:52 +07:00
|
|
|
requestBody: UpdateOrgChild4,
|
2024-01-26 09:56:27 +07:00
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
|
|
|
|
try {
|
2024-01-26 16:41:52 +07:00
|
|
|
const child3IdExits = await this.child3Repository.findOne({
|
2024-01-26 09:56:27 +07:00
|
|
|
where: { id: requestBody.orgChild3Id },
|
|
|
|
|
});
|
2024-01-26 16:41:52 +07:00
|
|
|
if (!child3IdExits) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child3Id");
|
2024-01-26 09:56:27 +07:00
|
|
|
}
|
2024-01-26 11:36:21 +07:00
|
|
|
|
2024-01-26 17:09:28 +07:00
|
|
|
const validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"];
|
|
|
|
|
if (
|
|
|
|
|
requestBody.orgChild4Rank == null ||
|
|
|
|
|
!validOrgChild4Ranks.includes(requestBody.orgChild4Rank.toUpperCase())
|
|
|
|
|
) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-26 16:41:52 +07:00
|
|
|
const child4 = await this.child4Repository.findOne({ where: { id } });
|
|
|
|
|
if (!child4) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
2024-01-26 09:56:27 +07:00
|
|
|
}
|
2024-01-26 16:41:52 +07:00
|
|
|
|
|
|
|
|
child4.lastUpdateUserId = request.user.sub;
|
|
|
|
|
child4.lastUpdateFullName = request.user.name;
|
|
|
|
|
child4.lastUpdatedAt = new Date();
|
|
|
|
|
child4.orgRootId = String(child3IdExits?.orgRootId);
|
|
|
|
|
child4.orgChild1Id = String(child3IdExits?.orgChild1Id);
|
2024-01-26 20:25:18 +07:00
|
|
|
child4.orgChild2Id = String(child3IdExits?.orgChild2Id);
|
|
|
|
|
child4.orgRevisionId = String(child3IdExits?.orgRevisionId);
|
2024-01-26 16:41:52 +07:00
|
|
|
child4.orgChild3Id = String(child3IdExits?.id);
|
|
|
|
|
this.child4Repository.merge(child4, requestBody);
|
|
|
|
|
await this.child4Repository.save(child4);
|
2024-01-26 09:56:27 +07:00
|
|
|
return new HttpSuccess();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ลบโครงสร้างระดับ4 Child4
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_015 - ลบโครงสร้างระดับ4 (ADMIN) #15
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id Guid, *Id Child4
|
|
|
|
|
*/
|
2024-01-26 20:25:18 +07:00
|
|
|
@Delete("{id}")
|
2024-01-26 09:56:27 +07:00
|
|
|
async delete(@Path() id: string) {
|
|
|
|
|
try {
|
2024-01-26 16:41:52 +07:00
|
|
|
const child4 = await this.child4Repository.findOne({ where: { id } });
|
|
|
|
|
if (!child4) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
2024-01-26 09:56:27 +07:00
|
|
|
}
|
2024-01-26 16:41:52 +07:00
|
|
|
await this.child4Repository.remove(child4);
|
2024-01-26 09:56:27 +07:00
|
|
|
return new HttpSuccess();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|