From b8d2283698bf54302076ee55e6614b28fc6689b9 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 25 Jan 2024 11:09:54 +0700 Subject: [PATCH] orgChil1 --- src/controllers/OrgChild1Controller.ts | 64 +++++++++++++++++++++++++- src/entities/OrgChild1.ts | 50 ++++++++++++++------ 2 files changed, 98 insertions(+), 16 deletions(-) diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index 8867b133..d1b5e62b 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -1 +1,63 @@ -import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags } from "tsoa"; \ No newline at end of file +import { AppDataSource } from "../database/data-source"; +import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1"; +import { + Body, + Delete, + Get, + Path, + Post, + Put, + Response, + Route, + SuccessResponse, + Tags, + Query, + Request, + Security, + } from "tsoa"; +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 { + private child1Repository = AppDataSource.getRepository(OrgChild1) + /** + * API สร้างโครงสร้างระดับ1 + * + * @summary ORG_004 - สร้างโครงสร้างระดับ1 (ADMIN) + * + */ + @Post() + async save( + @Body() requestBody: CreateOrgChild1, + @Request() request: { user: Record }, + ){ + const chkOrder = await this.child1Repository.findOne({ where: { orgRootId:requestBody.orgRootId, orgChild1Order:requestBody.orgChild1Order }}); + if (chkOrder != null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว"); + } + const chkCode = await this.child1Repository.findOne({ where: { orgRootId:requestBody.orgRootId, orgChild1Code:requestBody.orgChild1Code }}); + if (chkCode != null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); + } + 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(); + } + +} diff --git a/src/entities/OrgChild1.ts b/src/entities/OrgChild1.ts index 0c3b2ea3..c9c8c736 100644 --- a/src/entities/OrgChild1.ts +++ b/src/entities/OrgChild1.ts @@ -1,11 +1,10 @@ -import { Entity, Column, ManyToOne, JoinColumn, OneToOne, OneToMany } from "typeorm"; +import { Entity, Column, ManyToOne, JoinColumn, OneToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"; import { EntityBase } from "./base/Base"; import { OrgRoot } from "./OrgRoot"; import { OrgChild2 } from "./OrgChild2"; import { OrgChild3 } from "./OrgChild3"; import { OrgChild4 } from "./OrgChild4"; -// ENUM orgChild1Rank enum OrgChild1Rank { DEPARTMENT = "department", OFFICE = "office", @@ -14,12 +13,6 @@ enum OrgChild1Rank { } @Entity("orgChild1") export class OrgChild1 extends EntityBase { -// @Column({ -// comment: "", -// length: 40, -// default: "00000000-0000-0000-0000-000000000000", -// }) -// orgChild1Id: string; @Column({ nullable: true, @@ -50,7 +43,6 @@ export class OrgChild1 extends EntityBase { comment: "ระดับส่วนราชการ", type: "enum", enum: OrgChild1Rank, - // default: OrgChild1Rank.DEPARTMENT, }) orgChild1Rank: OrgChild1Rank; @@ -83,7 +75,7 @@ export class OrgChild1 extends EntityBase { @Column({ nullable: true, - comment: "สถานะของหน่วยงาน", //ปกติ = 1 , ยุกเลิก = 0 + comment: "สถานะของหน่วยงาน", default: true }) orgChild1IsNormal: boolean; @@ -98,15 +90,43 @@ export class OrgChild1 extends EntityBase { @JoinColumn({ name: "orgRootId" }) orgRoot: OrgRoot; - //child table 2,3,4 @OneToMany(() => OrgChild2, orgChild2 => orgChild2.orgChild1) orgChild2s: OrgChild2[]; - // @OneToMany(() => OrgChild3, orgChild3 => orgChild3.orgChild1) - // orgChild3s: OrgChild3[]; +} - // @OneToMany(() => OrgChild4, orgChild4 => orgChild4.orgChild1) - // orgChild4s: OrgChild4[]; +export class CreateOrgChild1 { + + @Column() + orgChild1Name: string; + + @Column() + orgChild1ShortName: string; + + @Column() + orgChild1Code: string; + + @Column() + orgChild1Rank: string; + + @Column() + orgChild1Order: number; + + @Column() + orgChild1PhoneEx: string; + + @Column() + orgChild1PhoneIn: string; + + @Column() + orgChild1Fax: string; + + @Column() + orgChild1IsNormal: boolean; + + @PrimaryGeneratedColumn('uuid') + orgRootId: string; } + export type UpdateOrgChild1 = Partial;