2024-01-26 13:32:56 +07:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Get,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Delete,
|
|
|
|
|
Patch,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
Body,
|
|
|
|
|
Path,
|
|
|
|
|
Request,
|
|
|
|
|
Example,
|
2024-01-26 16:41:52 +07:00
|
|
|
SuccessResponse,
|
|
|
|
|
Response,
|
2024-01-26 13:32:56 +07:00
|
|
|
} from "tsoa";
|
2024-01-26 20:25:18 +07:00
|
|
|
import { CreateOrgRevision, OrgRevision } from "../entities/OrgRevision";
|
2024-01-26 13:32:56 +07:00
|
|
|
import { AppDataSource } from "../database/data-source";
|
|
|
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
|
|
|
import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
2024-01-26 20:25:18 +07:00
|
|
|
import { In, Not } from "typeorm";
|
|
|
|
|
import { OrgRoot } from "../entities/OrgRoot";
|
|
|
|
|
import { OrgChild2 } from "../entities/OrgChild2";
|
|
|
|
|
import { OrgChild3 } from "../entities/OrgChild3";
|
|
|
|
|
import { OrgChild4 } from "../entities/OrgChild4";
|
2024-01-26 13:32:56 +07:00
|
|
|
|
2024-01-26 16:41:52 +07:00
|
|
|
@Route("api/v1/org")
|
2024-01-26 13:32:56 +07:00
|
|
|
@Tags("Organization")
|
|
|
|
|
@Security("bearerAuth")
|
2024-01-26 16:41:52 +07:00
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
|
|
|
)
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
2024-01-26 13:32:56 +07:00
|
|
|
export class OrganizationController extends Controller {
|
|
|
|
|
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
|
2024-01-26 20:25:18 +07:00
|
|
|
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
|
|
|
|
|
private child1Repository = AppDataSource.getRepository(OrgChild1);
|
|
|
|
|
private child2Repository = AppDataSource.getRepository(OrgChild2);
|
|
|
|
|
private child3Repository = AppDataSource.getRepository(OrgChild3);
|
|
|
|
|
private child4Repository = AppDataSource.getRepository(OrgChild4);
|
2024-01-26 13:32:56 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API รายการประวัติโครงสร้าง
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_020 - รายการประวัติโครงสร้าง #21
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Get("history")
|
|
|
|
|
async GetHistory() {
|
|
|
|
|
try {
|
|
|
|
|
const orgRevision = await this.orgRevisionRepository.find({
|
2024-01-26 16:41:52 +07:00
|
|
|
select: [
|
|
|
|
|
"id",
|
|
|
|
|
"orgRevisionName",
|
|
|
|
|
"orgRevisionIsCurrent",
|
|
|
|
|
"orgRevisionCreatedAt",
|
|
|
|
|
"orgRevisionIsDraft",
|
|
|
|
|
],
|
|
|
|
|
order: { orgRevisionCreatedAt: "DESC" },
|
2024-01-26 13:32:56 +07:00
|
|
|
});
|
|
|
|
|
if (!orgRevision) {
|
2024-01-26 16:41:52 +07:00
|
|
|
return new HttpSuccess([]);
|
2024-01-26 13:32:56 +07:00
|
|
|
}
|
|
|
|
|
const mapOrgRevisions = orgRevision.map((revision) => ({
|
|
|
|
|
orgRevisionId: revision.id,
|
|
|
|
|
orgRevisionName: revision.orgRevisionName,
|
|
|
|
|
orgRevisionIsCurrent: revision.orgRevisionIsCurrent,
|
|
|
|
|
orgRevisionCreatedAt: revision.orgRevisionCreatedAt,
|
|
|
|
|
orgRevisionIsDraft: revision.orgRevisionIsDraft,
|
|
|
|
|
}));
|
2024-01-26 16:41:52 +07:00
|
|
|
|
2024-01-26 13:32:56 +07:00
|
|
|
return new HttpSuccess(mapOrgRevisions);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API โครงสร้างปัจจุบันที่ใช้อยู่
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_021 - โครงสร้างปัจจุบันที่ใช้อยู่ #22
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Get("active")
|
|
|
|
|
async GetActive() {
|
|
|
|
|
try {
|
|
|
|
|
const orgRevisionActive = await this.orgRevisionRepository.findOne({
|
2024-01-26 16:41:52 +07:00
|
|
|
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
|
2024-01-26 13:32:56 +07:00
|
|
|
});
|
|
|
|
|
const orgRevisionDraf = await this.orgRevisionRepository.findOne({
|
2024-01-26 16:41:52 +07:00
|
|
|
where: { orgRevisionIsCurrent: false, orgRevisionIsDraft: true },
|
2024-01-26 13:32:56 +07:00
|
|
|
});
|
|
|
|
|
const mapData = {
|
2024-01-26 16:41:52 +07:00
|
|
|
activeId: orgRevisionActive == null ? null : orgRevisionActive.id,
|
|
|
|
|
activeName: orgRevisionActive == null ? null : orgRevisionActive.orgRevisionName,
|
|
|
|
|
draftId: orgRevisionDraf == null ? null : orgRevisionDraf.id,
|
|
|
|
|
draftName: orgRevisionDraf == null ? null : orgRevisionDraf.orgRevisionName,
|
2024-01-26 13:32:56 +07:00
|
|
|
};
|
|
|
|
|
return new HttpSuccess(mapData);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API สร้างโครงสร้างระดับ4
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_022 - สร้างโครงสร้างใหม่ #23 (ยังไม่เสร็จ)
|
|
|
|
|
*
|
|
|
|
|
*/
|
2024-01-29 11:36:03 +07:00
|
|
|
@Post("draft")
|
2024-01-26 13:32:56 +07:00
|
|
|
async CreateOrgRevision(
|
2024-01-26 20:25:18 +07:00
|
|
|
@Body() requestBody: CreateOrgRevision,
|
2024-01-26 13:32:56 +07:00
|
|
|
@Request() request: { user: Record<string, any> },
|
2024-01-26 16:41:52 +07:00
|
|
|
) {
|
|
|
|
|
try {
|
2024-01-26 20:25:18 +07:00
|
|
|
const revision = Object.assign(new OrgRevision(), requestBody) as OrgRevision;
|
|
|
|
|
revision.orgRevisionIsDraft = true;
|
|
|
|
|
revision.orgRevisionIsCurrent = false;
|
|
|
|
|
revision.createdUserId = request.user.sub;
|
|
|
|
|
revision.createdFullName = request.user.name;
|
|
|
|
|
revision.lastUpdateUserId = request.user.sub;
|
|
|
|
|
revision.lastUpdateFullName = request.user.name;
|
|
|
|
|
await this.orgRevisionRepository.save(revision);
|
|
|
|
|
|
|
|
|
|
if (requestBody.typeDraft.toUpperCase() == "ORG") {
|
|
|
|
|
if (requestBody.orgRevisionId == null)
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
|
|
|
|
const _revision = await this.orgRevisionRepository.findOne({
|
|
|
|
|
where: { id: requestBody.orgRevisionId },
|
|
|
|
|
});
|
|
|
|
|
if (!_revision) throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
|
|
|
|
|
|
|
|
|
//clone data
|
|
|
|
|
const orgRoot = await this.orgRootRepository.find({
|
|
|
|
|
where: { orgRevisionId: requestBody.orgRevisionId },
|
|
|
|
|
});
|
|
|
|
|
const _orgRoot = orgRoot.map((x) => ({
|
|
|
|
|
...x,
|
|
|
|
|
isAncestorDNA:
|
|
|
|
|
x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
|
|
|
|
|
? x.id
|
|
|
|
|
: x.isAncestorDNA,
|
|
|
|
|
}));
|
|
|
|
|
await this.orgRootRepository.save(_orgRoot);
|
|
|
|
|
|
|
|
|
|
_orgRoot.forEach(async (x: any) => {
|
|
|
|
|
delete x.id;
|
|
|
|
|
const data = Object.assign(new OrgRoot(), x);
|
|
|
|
|
data.orgRevisionId = revision.id;
|
|
|
|
|
data.createdUserId = request.user.sub;
|
|
|
|
|
data.createdFullName = request.user.name;
|
|
|
|
|
data.createdAt = new Date();
|
|
|
|
|
data.lastUpdateUserId = request.user.sub;
|
|
|
|
|
data.lastUpdateFullName = request.user.name;
|
|
|
|
|
data.lastUpdatedAt = new Date();
|
|
|
|
|
await this.orgRootRepository.save(data);
|
|
|
|
|
});
|
|
|
|
|
//clone data
|
|
|
|
|
const orgChild1 = await this.child1Repository.find({
|
|
|
|
|
where: { orgRevisionId: requestBody.orgRevisionId },
|
|
|
|
|
});
|
|
|
|
|
const _orgChild1 = orgChild1.map((x) => ({
|
|
|
|
|
...x,
|
|
|
|
|
isAncestorDNA:
|
|
|
|
|
x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
|
|
|
|
|
? x.id
|
|
|
|
|
: x.isAncestorDNA,
|
|
|
|
|
}));
|
|
|
|
|
await this.child1Repository.save(_orgChild1);
|
|
|
|
|
|
|
|
|
|
_orgChild1.forEach(async (x: any) => {
|
|
|
|
|
delete x.id;
|
|
|
|
|
const data = Object.assign(new OrgChild1(), x);
|
|
|
|
|
data.orgRevisionId = revision.id;
|
|
|
|
|
data.createdUserId = request.user.sub;
|
|
|
|
|
data.createdFullName = request.user.name;
|
|
|
|
|
data.createdAt = new Date();
|
|
|
|
|
data.lastUpdateUserId = request.user.sub;
|
|
|
|
|
data.lastUpdateFullName = request.user.name;
|
|
|
|
|
data.lastUpdatedAt = new Date();
|
|
|
|
|
await this.child1Repository.save(data);
|
|
|
|
|
});
|
|
|
|
|
//clone data
|
|
|
|
|
const orgChild2 = await this.child2Repository.find({
|
|
|
|
|
where: { orgRevisionId: requestBody.orgRevisionId },
|
|
|
|
|
});
|
|
|
|
|
const _orgChild2 = orgChild2.map((x) => ({
|
|
|
|
|
...x,
|
|
|
|
|
isAncestorDNA:
|
|
|
|
|
x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
|
|
|
|
|
? x.id
|
|
|
|
|
: x.isAncestorDNA,
|
|
|
|
|
}));
|
|
|
|
|
await this.child2Repository.save(_orgChild2);
|
|
|
|
|
|
|
|
|
|
_orgChild2.forEach(async (x: any) => {
|
|
|
|
|
delete x.id;
|
|
|
|
|
const data = Object.assign(new OrgChild2(), x);
|
|
|
|
|
data.orgRevisionId = revision.id;
|
|
|
|
|
data.createdUserId = request.user.sub;
|
|
|
|
|
data.createdFullName = request.user.name;
|
|
|
|
|
data.createdAt = new Date();
|
|
|
|
|
data.lastUpdateUserId = request.user.sub;
|
|
|
|
|
data.lastUpdateFullName = request.user.name;
|
|
|
|
|
data.lastUpdatedAt = new Date();
|
|
|
|
|
await this.child2Repository.save(data);
|
|
|
|
|
});
|
|
|
|
|
//clone data
|
|
|
|
|
const orgChild3 = await this.child3Repository.find({
|
|
|
|
|
where: { orgRevisionId: requestBody.orgRevisionId },
|
|
|
|
|
});
|
|
|
|
|
const _orgChild3 = orgChild3.map((x) => ({
|
|
|
|
|
...x,
|
|
|
|
|
isAncestorDNA:
|
|
|
|
|
x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
|
|
|
|
|
? x.id
|
|
|
|
|
: x.isAncestorDNA,
|
|
|
|
|
}));
|
|
|
|
|
await this.child3Repository.save(_orgChild3);
|
|
|
|
|
|
|
|
|
|
_orgChild3.forEach(async (x: any) => {
|
|
|
|
|
delete x.id;
|
|
|
|
|
const data = Object.assign(new OrgChild3(), x);
|
|
|
|
|
data.orgRevisionId = revision.id;
|
|
|
|
|
data.createdUserId = request.user.sub;
|
|
|
|
|
data.createdFullName = request.user.name;
|
|
|
|
|
data.createdAt = new Date();
|
|
|
|
|
data.lastUpdateUserId = request.user.sub;
|
|
|
|
|
data.lastUpdateFullName = request.user.name;
|
|
|
|
|
data.lastUpdatedAt = new Date();
|
|
|
|
|
await this.child3Repository.save(data);
|
|
|
|
|
});
|
|
|
|
|
//clone data
|
|
|
|
|
const orgChild4 = await this.child4Repository.find({
|
|
|
|
|
where: { orgRevisionId: requestBody.orgRevisionId },
|
|
|
|
|
});
|
|
|
|
|
const _orgChild4 = orgChild4.map((x) => ({
|
|
|
|
|
...x,
|
|
|
|
|
isAncestorDNA:
|
|
|
|
|
x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
|
|
|
|
|
? x.id
|
|
|
|
|
: x.isAncestorDNA,
|
|
|
|
|
}));
|
|
|
|
|
await this.child4Repository.save(_orgChild4);
|
|
|
|
|
|
|
|
|
|
_orgChild4.forEach(async (x: any) => {
|
|
|
|
|
delete x.id;
|
|
|
|
|
const data = Object.assign(new OrgChild4(), x);
|
|
|
|
|
data.orgRevisionId = revision.id;
|
|
|
|
|
data.createdUserId = request.user.sub;
|
|
|
|
|
data.createdFullName = request.user.name;
|
|
|
|
|
data.createdAt = new Date();
|
|
|
|
|
data.lastUpdateUserId = request.user.sub;
|
|
|
|
|
data.lastUpdateFullName = request.user.name;
|
|
|
|
|
data.lastUpdatedAt = new Date();
|
|
|
|
|
await this.child4Repository.save(data);
|
|
|
|
|
});
|
|
|
|
|
} else if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") {
|
|
|
|
|
} else if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const orgRevisions = await this.orgRevisionRepository.find({
|
|
|
|
|
where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }],
|
|
|
|
|
});
|
|
|
|
|
await this.child4Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) });
|
|
|
|
|
await this.child3Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) });
|
|
|
|
|
await this.child2Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) });
|
|
|
|
|
await this.child1Repository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) });
|
|
|
|
|
await this.orgRootRepository.delete({ orgRevisionId: In(orgRevisions.map((x) => x.id)) });
|
|
|
|
|
await this.orgRevisionRepository.remove(orgRevisions);
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess(revision);
|
2024-01-26 16:41:52 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
2024-01-26 13:32:56 +07:00
|
|
|
}
|
2024-01-26 16:41:52 +07:00
|
|
|
}
|
2024-01-26 20:25:18 +07:00
|
|
|
|
2024-01-26 17:03:41 +07:00
|
|
|
/**
|
|
|
|
|
* API รายละเอียดโครงสร้าง
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_023 - รายละเอียดโครงสร้าง (ADMIN) #25
|
|
|
|
|
*
|
|
|
|
|
*/
|
2024-01-29 10:23:58 +07:00
|
|
|
@Get("{id}")
|
|
|
|
|
async detail(@Path() id: string) {
|
|
|
|
|
try {
|
|
|
|
|
const orgRevisionData = await AppDataSource.getRepository(OrgRevision)
|
2024-01-29 11:36:03 +07:00
|
|
|
.createQueryBuilder("orgRevision")
|
|
|
|
|
// .leftJoin("orgRevision.orgRoots", "orgRoot")
|
|
|
|
|
// .leftJoin("orgRoot.orgChild1s", "orgChild1")
|
|
|
|
|
// .leftJoin("orgChild1.orgChild2s", "orgChild2")
|
|
|
|
|
// .leftJoin("orgChild2.orgChild3s", "orgChild3")
|
|
|
|
|
// .leftJoin("orgChild3.orgChild4s", "orgChild4")
|
|
|
|
|
.where("orgRevision.id = :id", { id })
|
|
|
|
|
// .select([
|
|
|
|
|
// "orgRoot.id",
|
|
|
|
|
// "orgRoot.orgRootName",
|
|
|
|
|
// "orgRoot.orgRootShortName",
|
|
|
|
|
// "orgRoot.orgRootCode",
|
|
|
|
|
// "orgRoot.orgRootOrder",
|
2024-01-29 10:23:58 +07:00
|
|
|
|
2024-01-29 11:36:03 +07:00
|
|
|
// "orgChild1.id",
|
|
|
|
|
// "orgChild1.orgChild1Name",
|
|
|
|
|
// "orgChild1.orgChild1ShortName",
|
|
|
|
|
// "orgChild1.orgChild1Code",
|
|
|
|
|
// "orgChild1.orgChild1Order",
|
2024-01-29 10:23:58 +07:00
|
|
|
|
2024-01-29 11:36:03 +07:00
|
|
|
// "orgChild2.id",
|
|
|
|
|
// "orgChild2.orgChild2Name",
|
|
|
|
|
// "orgChild2.orgChild2ShortName",
|
|
|
|
|
// "orgChild2.orgChild2Code",
|
|
|
|
|
// "orgChild2.orgChild2Order",
|
2024-01-29 10:23:58 +07:00
|
|
|
|
2024-01-29 11:36:03 +07:00
|
|
|
// "orgChild3.id",
|
|
|
|
|
// "orgChild3.orgChild3Name",
|
|
|
|
|
// "orgChild3.orgChild3ShortName",
|
|
|
|
|
// "orgChild3.orgChild3Code",
|
|
|
|
|
// "orgChild3.orgChild3Order",
|
2024-01-29 10:23:58 +07:00
|
|
|
|
2024-01-29 11:36:03 +07:00
|
|
|
// "orgChild4.id",
|
|
|
|
|
// "orgChild4.orgChild4Name",
|
|
|
|
|
// "orgChild4.orgChild4ShortName",
|
|
|
|
|
// "orgChild4.orgChild4Code",
|
|
|
|
|
// "orgChild4.orgChild4Order",
|
|
|
|
|
// ])
|
|
|
|
|
.getOne();
|
2024-01-29 10:23:58 +07:00
|
|
|
|
|
|
|
|
return new HttpSuccess(orgRevisionData);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-26 13:32:56 +07:00
|
|
|
}
|
2024-01-26 20:25:18 +07:00
|
|
|
function ANY(arg0: OrgRevision[]): string | import("typeorm").FindOperator<string> | undefined {
|
|
|
|
|
throw new Error("Function not implemented.");
|
|
|
|
|
}
|