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

183 lines
6.7 KiB
TypeScript
Raw Normal View History

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