2024-01-26 11:36:21 +07:00
|
|
|
import { AppDataSource } from "../database/data-source";
|
2024-01-25 13:08:25 +07:00
|
|
|
import { OrgChild1, CreateOrgChild1, UpdateOrgChild1 } from "../entities/OrgChild1";
|
2024-01-25 13:42:17 +07:00
|
|
|
import { OrgChild2 } from "../entities/OrgChild2";
|
2024-01-25 11:09:54 +07:00
|
|
|
import {
|
2024-01-26 11:36:21 +07:00
|
|
|
Body,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Response,
|
|
|
|
|
Route,
|
|
|
|
|
SuccessResponse,
|
|
|
|
|
Tags,
|
|
|
|
|
Query,
|
|
|
|
|
Request,
|
|
|
|
|
Security,
|
|
|
|
|
} from "tsoa";
|
2024-01-25 11:09:54 +07:00
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
|
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
@Route("api/v1/organization/child1")
|
|
|
|
|
@Tags("OrgChild1")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
|
|
|
)
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
|
|
|
export class OrgChild1Controller {
|
2024-01-25 13:42:17 +07:00
|
|
|
private child1Repository = AppDataSource.getRepository(OrgChild1);
|
|
|
|
|
private child2Repository = AppDataSource.getRepository(OrgChild2);
|
2024-01-25 13:08:25 +07:00
|
|
|
|
|
|
|
|
/**
|
2024-01-25 11:09:54 +07:00
|
|
|
* API สร้างโครงสร้างระดับ1
|
|
|
|
|
*
|
2024-01-25 13:08:25 +07:00
|
|
|
* @summary ORG_004 - สร้างโครงสร้างระดับ1 (ADMIN) #4
|
2024-01-25 11:09:54 +07:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Post()
|
|
|
|
|
async save(
|
|
|
|
|
@Body() requestBody: CreateOrgChild1,
|
|
|
|
|
@Request() request: { user: Record<string, any> },
|
2024-01-26 11:36:21 +07:00
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
const chkCode = await this.child1Repository.findOne({
|
|
|
|
|
where: { orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code },
|
|
|
|
|
});
|
|
|
|
|
if (chkCode != null) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
|
2024-01-25 11:09:54 +07:00
|
|
|
}
|
2024-01-26 11:36:21 +07:00
|
|
|
const child1 = Object.assign(new OrgChild1(), requestBody) as OrgChild1;
|
|
|
|
|
child1.orgChild1Name = requestBody.orgChild1Name;
|
|
|
|
|
child1.createdUserId = request.user.sub;
|
|
|
|
|
child1.createdFullName = request.user.name;
|
|
|
|
|
child1.createdAt = new Date();
|
|
|
|
|
child1.lastUpdateUserId = request.user.sub;
|
|
|
|
|
child1.lastUpdateFullName = request.user.name;
|
|
|
|
|
child1.lastUpdatedAt = new Date();
|
|
|
|
|
await this.child1Repository.save(child1);
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// return new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, String(error))
|
|
|
|
|
return error;
|
2024-01-25 13:08:25 +07:00
|
|
|
}
|
2024-01-26 11:36:21 +07:00
|
|
|
}
|
|
|
|
|
|
2024-01-25 13:08:25 +07:00
|
|
|
/**
|
|
|
|
|
* API แก้ไขโครงสร้างระดับ1
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_005 - แก้ไขโครงสร้างระดับ1 (ADMIN) #5
|
|
|
|
|
*
|
2024-01-26 11:36:21 +07:00
|
|
|
* @param {string} id id สร้างโครงสร้างระดับ1
|
2024-01-25 13:08:25 +07:00
|
|
|
*/
|
|
|
|
|
@Put("{id}")
|
|
|
|
|
async Edit(
|
2024-01-26 11:36:21 +07:00
|
|
|
@Path() id: string,
|
2024-01-25 13:08:25 +07:00
|
|
|
@Body() requestBody: UpdateOrgChild1,
|
|
|
|
|
@Request() request: { user: Record<string, any> },
|
2024-01-26 11:36:21 +07:00
|
|
|
) {
|
2024-01-25 13:08:25 +07:00
|
|
|
try {
|
2024-01-26 11:36:21 +07:00
|
|
|
const chkCode = await this.child1Repository.findOne({
|
|
|
|
|
where: { id: id, orgChild1Code: requestBody.orgChild1Code },
|
|
|
|
|
});
|
2024-01-26 12:00:43 +07:00
|
|
|
if (chkCode?.id != id && chkCode != null) {
|
2024-01-25 15:54:03 +07:00
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
|
|
|
|
|
}
|
2024-01-25 13:08:25 +07:00
|
|
|
const child1 = await this.child1Repository.findOne({ where: { id } });
|
2024-01-26 11:36:21 +07:00
|
|
|
if (!child1) {
|
2024-01-25 13:08:25 +07:00
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
2024-01-25 11:09:54 +07:00
|
|
|
}
|
2024-01-25 13:08:25 +07:00
|
|
|
child1.lastUpdateUserId = request.user.sub;
|
|
|
|
|
child1.lastUpdateFullName = request.user.name;
|
|
|
|
|
child1.lastUpdatedAt = new Date();
|
|
|
|
|
this.child1Repository.merge(child1, requestBody);
|
2024-01-25 11:09:54 +07:00
|
|
|
await this.child1Repository.save(child1);
|
|
|
|
|
return new HttpSuccess();
|
2024-01-25 13:08:25 +07:00
|
|
|
} catch (error) {
|
2024-01-25 13:42:17 +07:00
|
|
|
return error;
|
2024-01-25 11:09:54 +07:00
|
|
|
}
|
2024-01-25 13:08:25 +07:00
|
|
|
}
|
2024-01-25 11:09:54 +07:00
|
|
|
|
2024-01-25 13:42:17 +07:00
|
|
|
/**
|
|
|
|
|
* API ลบโครงสร้างระดับ1
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_006 - ลบโครงสร้างระดับ1 (ADMIN) #6
|
|
|
|
|
*
|
2024-01-26 11:36:21 +07:00
|
|
|
* @param {string} id id สร้างโครงสร้างระดับ1
|
2024-01-25 13:42:17 +07:00
|
|
|
*/
|
|
|
|
|
@Delete("{id}")
|
2024-01-26 11:36:21 +07:00
|
|
|
async delete(@Path() id: string) {
|
2024-01-25 13:42:17 +07:00
|
|
|
try {
|
|
|
|
|
const child1 = await this.child1Repository.findOne({ where: { id } });
|
|
|
|
|
if (!child1) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
|
|
|
|
}
|
2024-01-25 15:54:03 +07:00
|
|
|
const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } });
|
2024-01-26 11:36:21 +07:00
|
|
|
if (exitsChild2) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatusCode.NOT_FOUND,
|
|
|
|
|
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2",
|
|
|
|
|
);
|
2024-01-25 13:42:17 +07:00
|
|
|
}
|
|
|
|
|
await this.child1Repository.remove(child1);
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-25 11:09:54 +07:00
|
|
|
}
|