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

181 lines
6.5 KiB
TypeScript
Raw Normal View History

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