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

249 lines
8.6 KiB
TypeScript
Raw Normal View History

import { AppDataSource } from "../database/data-source";
2024-01-29 12:54:49 +07:00
import { OrgRevision } from "../entities/OrgRevision";
2024-01-26 15:21:19 +07:00
import { OrgRoot } from "../entities/OrgRoot";
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-26 15:21:19 +07:00
import { OrgChild3 } from "../entities/OrgChild3";
2024-01-25 11:09:54 +07:00
import {
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";
2024-01-26 16:41:52 +07:00
import { Not } from "typeorm";
@Route("api/v1/org/child1")
2024-01-25 11:09:54 +07:00
@Tags("OrgChild1")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrgChild1Controller {
2024-01-26 16:41:52 +07:00
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
2024-01-25 13:42:17 +07:00
private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2);
2024-01-26 15:21:19 +07:00
private child3Repository = AppDataSource.getRepository(OrgChild3);
2024-01-29 12:54:49 +07:00
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
2024-01-26 15:21:19 +07:00
/**
* API 1
*
* @summary ORG_017 - 1 (ADMIN) #17
*
* @param {string} id id 1
*/
// @Get("{id}")
// async Get(@Path() id: string) {
// try {
// const child2s = await this.child2Repository.find({
// where: {
// orgChild1Id: id,
// },
// select: ["id", "orgChild2Name", "orgChild2ShortName", "orgChild2Code", "orgChild2Order", "orgRootId"],
// order: {
// orgChild2Order: "ASC",
// },
// });
// const orgRoots = await Promise.all(child2s.map(async (child2) => {
// const orgRoot = await this.orgRootRepository.findOne({
// where: {
// id: child2.orgRootId,
// },
// select: ["orgRootCode"],
// });
// const orgChild3s = await this.child3Repository.find({
// where: {
// orgChild2Id: child2.id,
// },
// select: ["id"],
// });
// const orgChild3Ids = orgChild3s.map((orgChild3) => orgChild3.id);
// return {
// orgChild2Id: child2.id,
// orgChild3Id: orgChild3Ids,
// orgChild2Name: child2.orgChild2Name,
// orgChild2ShortName: child2.orgChild2ShortName,
// orgChild2Code: child2.orgChild2Code,
// orgChild2Order: child2.orgChild2Order,
// orgRootCode: orgRoot?.orgRootCode,
// };
// }));
// return new HttpSuccess(orgRoots);
// } catch (error) {
// return error;
// }
// }
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> },
) {
try {
2024-01-26 16:41:52 +07:00
const rootIdExits = await this.orgRootRepository.findOne({
where: { id: requestBody.orgRootId },
});
if (!rootIdExits) {
2024-01-26 15:21:19 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootId");
}
2024-01-29 12:54:49 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: rootIdExits.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
2024-01-26 15:21:19 +07:00
2024-01-26 16:41:52 +07:00
const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"];
2024-01-26 15:21:19 +07:00
if (!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild1Rank");
}
const chkCode = await this.child1Repository.findOne({
where: { orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code },
});
if (chkCode != null) {
2024-01-26 16:41:52 +07:00
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว",
);
2024-01-25 11:09:54 +07:00
}
2024-01-26 16:41:52 +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.lastUpdateUserId = request.user.sub;
child1.lastUpdateFullName = request.user.name;
child1.orgRevisionId = String(rootIdExits?.orgRevisionId);
2024-01-26 16:41:52 +07:00
child1.orgRootId = String(rootIdExits?.id);
await this.child1Repository.save(child1);
return new HttpSuccess();
} catch (error) {
return error;
2024-01-25 13:08:25 +07:00
}
}
2024-01-25 13:08:25 +07:00
/**
* API 1
*
* @summary ORG_005 - 1 (ADMIN) #5
*
* @param {string} id id 1
2024-01-25 13:08:25 +07:00
*/
@Put("{id}")
async Edit(
@Path() id: string,
2024-01-25 13:08:25 +07:00
@Body() requestBody: UpdateOrgChild1,
@Request() request: { user: Record<string, any> },
) {
2024-01-25 13:08:25 +07:00
try {
2024-01-26 16:41:52 +07:00
const rootIdExits = await this.orgRootRepository.findOne({
where: { id: requestBody.orgRootId },
});
if (!rootIdExits) {
2024-01-26 15:21:19 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RootId");
2024-01-25 15:54:03 +07:00
}
2024-01-29 12:54:49 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: rootIdExits.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
2024-01-26 16:41:52 +07:00
2024-01-26 17:09:28 +07:00
const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (
requestBody.orgChild1Rank == null ||
!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
2024-01-25 13:08:25 +07:00
const child1 = await this.child1Repository.findOne({ where: { id } });
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-26 15:21:19 +07:00
const chkCode = await this.child1Repository.findOne({
2024-01-26 16:41:52 +07:00
where: {
id: Not(id),
orgRootId: requestBody.orgRootId,
orgChild1Code: requestBody.orgChild1Code,
},
2024-01-26 15:21:19 +07:00
});
if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
2024-01-25 13:08:25 +07:00
child1.lastUpdateUserId = request.user.sub;
child1.lastUpdateFullName = request.user.name;
child1.lastUpdatedAt = new Date();
child1.orgRevisionId = String(rootIdExits?.orgRevisionId);
2024-01-26 16:41:52 +07:00
child1.orgRootId = String(rootIdExits?.id);
2024-01-25 13:08:25 +07:00
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
*
* @param {string} id id 1
2024-01-25 13:42:17 +07:00
*/
@Delete("{id}")
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-29 12:54:49 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child1.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
2024-01-25 15:54:03 +07:00
const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } });
if (exitsChild2) {
throw new HttpError(
2024-01-26 15:21:19 +07:00
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก 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
}