diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts new file mode 100644 index 00000000..a07aa36c --- /dev/null +++ b/src/controllers/OrganizationController.ts @@ -0,0 +1,107 @@ +import { + Controller, + Get, + Post, + Put, + Delete, + Patch, + Route, + Security, + Tags, + Body, + Path, + Request, + Example, +} from "tsoa"; +import { OrgRevision } from "../entities/OrgRevision"; +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"; + +@Route("organization") +@Tags("Organization") +@Security("bearerAuth") +export class OrganizationController extends Controller { + + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); + + /** + * API รายการประวัติโครงสร้าง + * + * @summary ORG_020 - รายการประวัติโครงสร้าง #21 + * + */ + @Get("history") + async GetHistory() { + try { + const orgRevision = await this.orgRevisionRepository.find({ + select: ["id", "orgRevisionName", "orgRevisionIsCurrent", "orgRevisionCreatedAt", "orgRevisionIsDraft"], + }); + if (!orgRevision) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + const mapOrgRevisions = orgRevision.map((revision) => ({ + orgRevisionId: revision.id, + orgRevisionName: revision.orgRevisionName, + orgRevisionIsCurrent: revision.orgRevisionIsCurrent, + orgRevisionCreatedAt: revision.orgRevisionCreatedAt, + orgRevisionIsDraft: revision.orgRevisionIsDraft, + })); + + return new HttpSuccess(mapOrgRevisions); + } catch (error) { + return error; + } + } + + /** + * API โครงสร้างปัจจุบันที่ใช้อยู่ + * + * @summary ORG_021 - โครงสร้างปัจจุบันที่ใช้อยู่ #22 + * + */ + @Get("active") + async GetActive() { + try { + const orgRevisionActive = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent : true, orgRevisionIsDraft : false } + }); + const orgRevisionDraf = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent : false, orgRevisionIsDraft : true } + }); + if (!orgRevisionActive || !orgRevisionDraf) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + const mapData = { + activeId: orgRevisionActive.id, + activeName: orgRevisionActive.orgRevisionName, + draftId: orgRevisionDraf.id, + draftName: orgRevisionDraf.orgRevisionName + }; + return new HttpSuccess(mapData); + } catch (error) { + return error; + } + } + + /** + * API สร้างโครงสร้างระดับ4 + * + * @summary ORG_022 - สร้างโครงสร้างใหม่ #23 (ยังไม่เสร็จ) + * + */ + @Post() + async CreateOrgRevision( + @Body() requestBody: CreateOrgChild1, + @Request() request: { user: Record }, + ){ + try { + return new HttpSuccess(); + } catch (error) { + return error; + } + } + +}