แก้ชื่อ tag

This commit is contained in:
Kittapath 2024-01-26 17:09:28 +07:00
parent 4b4f2423fa
commit f509e10f8c
9 changed files with 151 additions and 107 deletions

View file

@ -13,21 +13,28 @@ import {
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 } from "../entities/OrgChild2";
import { CreateOrgChild2, OrgChild2, UpdateOrgChild2 } from "../entities/OrgChild2";
import { OrgChild1 } from "../entities/OrgChild1";
import { OrgChild3 } from "../entities/OrgChild3";
@Route("organization")
@Tags("OrgChild2")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrgChild2Controller extends Controller {
private orgChild1Repository = AppDataSource.getRepository(OrgChild1);
private orgChild2Repository = AppDataSource.getRepository(OrgChild2);
private orgChild3Repository = AppDataSource.getRepository(OrgChild3);
private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
/**
* 2 Child2
@ -35,7 +42,7 @@ export class OrgChild2Controller extends Controller {
* @summary ORG_007 - 2 (ADMIN) #7
*
*/
@Post("child2")
@Post()
@Example([
{
orgChild2Name: "string", //ชื่อหน่วยงาน
@ -45,7 +52,7 @@ export class OrgChild2Controller extends Controller {
orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
orgChild2Fax: "string", //หมายเลขโทรสาร
orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
// orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
orgChild1Id: "Guid", //id Child1
},
])
@ -55,24 +62,28 @@ export class OrgChild2Controller extends Controller {
@Request() request: { user: Record<string, any> },
) {
try {
const orgChild1 = await this.orgChild1Repository.findOne({
//BE ใช้ orgChild1Id หา orgChild1Id, orgRootId
const child1 = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
const orgChild2 = Object.assign(new OrgChild2(), requestBody);
if (!orgChild2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
if (!child1) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
if (orgChild1) {
orgChild2.createdUserId = request.user.sub;
orgChild2.createdFullName = request.user.name;
orgChild2.lastUpdateUserId = request.user.sub;
orgChild2.lastUpdateFullName = request.user.name;
await this.orgChild2Repository.save(orgChild2);
} else {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีโครงสร้างระดับ1");
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.orgChild1Id = String(child1?.id);
await this.child2Repository.save(child2);
return new HttpSuccess();
} catch (error) {
return error;
@ -96,33 +107,44 @@ export class OrgChild2Controller extends Controller {
orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
orgChild2Fax: "string", //หมายเลขโทรสาร
orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
// orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
orgChild1Id: "Guid", //id Child1
},
])
async update(
@Path() id: string,
@Body()
requestBody: CreateOrgChild2,
requestBody: UpdateOrgChild2,
@Request() request: { user: Record<string, any> },
) {
try {
const orgChild1 = await this.orgChild1Repository.findOne({
const child1IdExits = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
const orgChild2 = await this.orgChild2Repository.findOne({ where: { id } });
if (!orgChild2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
if (!child1IdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id");
}
if (orgChild1) {
orgChild2.lastUpdateUserId = request.user.sub;
orgChild2.lastUpdateFullName = request.user.name;
orgChild2.lastUpdatedAt = new Date();
await this.orgChild2Repository.save(orgChild2);
} else {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีโครงสร้างระดับ1");
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.orgChild1Id = String(child1IdExits?.id);
this.child2Repository.merge(child2, requestBody);
await this.child2Repository.save(child2);
return new HttpSuccess();
} catch (error) {
return error;
@ -139,22 +161,18 @@ export class OrgChild2Controller extends Controller {
@Delete("Child2/{id}")
async delete(@Path() id: string) {
try {
const orgChild2 = await this.orgChild2Repository.findOne({ where: { id } });
const orgChild3 = await this.orgChild3Repository.findOne({ where: { orgChild2Id: id } });
if (!orgChild2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
const child2 = await this.child2Repository.findOne({ where: { id } });
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
if (!orgChild3) {
await this.orgChild2Repository.remove(orgChild2);
} else {
const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } });
if (exitsChild3) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ3",
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4",
);
}
await this.child2Repository.remove(child2);
return new HttpSuccess();
} catch (error) {
return error;