diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index d1b5e62b..c13f6ca2 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -1,5 +1,5 @@ import { AppDataSource } from "../database/data-source"; -import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1"; +import { OrgChild1, CreateOrgChild1, UpdateOrgChild1 } from "../entities/OrgChild1"; import { Body, Delete, @@ -29,10 +29,11 @@ import HttpError from "../interfaces/http-error"; export class OrgChild1Controller { private child1Repository = AppDataSource.getRepository(OrgChild1) - /** + + /** * API สร้างโครงสร้างระดับ1 * - * @summary ORG_004 - สร้างโครงสร้างระดับ1 (ADMIN) + * @summary ORG_004 - สร้างโครงสร้างระดับ1 (ADMIN) #4 * */ @Post() @@ -40,24 +41,60 @@ export class OrgChild1Controller { @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, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว"); + try { + 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(); + + } catch (error) { + // return new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, String(error)) + return error } - const chkCode = await this.child1Repository.findOne({ where: { orgRootId:requestBody.orgRootId, orgChild1Code:requestBody.orgChild1Code }}); - if (chkCode != null) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); + } + + /** + * API แก้ไขโครงสร้างระดับ1 + * + * @summary ORG_005 - แก้ไขโครงสร้างระดับ1 (ADMIN) #5 + * + * @param {string} id id สร้างโครงสร้างระดับ1 + */ + @Put("{id}") + async Edit( + @Path() id: string, + @Body() requestBody: UpdateOrgChild1, + @Request() request: { user: Record }, + ) { + try { + const child1 = await this.child1Repository.findOne({ where: { id } }); + if (!child1) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "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() + child1.lastUpdateUserId = request.user.sub; + child1.lastUpdateFullName = request.user.name; + child1.lastUpdatedAt = new Date(); + this.child1Repository.merge(child1, requestBody); await this.child1Repository.save(child1); return new HttpSuccess(); + } catch (error) { + return error } + } + } diff --git a/src/entities/OrgChild1.ts b/src/entities/OrgChild1.ts index c9c8c736..e05606c0 100644 --- a/src/entities/OrgChild1.ts +++ b/src/entities/OrgChild1.ts @@ -2,8 +2,6 @@ import { Entity, Column, ManyToOne, JoinColumn, OneToOne, OneToMany, PrimaryGene import { EntityBase } from "./base/Base"; import { OrgRoot } from "./OrgRoot"; import { OrgChild2 } from "./OrgChild2"; -import { OrgChild3 } from "./OrgChild3"; -import { OrgChild4 } from "./OrgChild4"; enum OrgChild1Rank { DEPARTMENT = "department", @@ -11,6 +9,7 @@ enum OrgChild1Rank { DIVISION = "division", SECTION = "section", } + @Entity("orgChild1") export class OrgChild1 extends EntityBase { @@ -124,9 +123,9 @@ export class CreateOrgChild1 { @Column() orgChild1IsNormal: boolean; - @PrimaryGeneratedColumn('uuid') + @Column('uuid') orgRootId: string; } -export type UpdateOrgChild1 = Partial; +export type UpdateOrgChild1 = Partial & { orgChild1Rank?: OrgChild1Rank };