org_010 - 012

This commit is contained in:
Bright 2024-01-25 15:54:03 +07:00
parent 55119a72f5
commit 6d136f8a97
4 changed files with 195 additions and 28 deletions

View file

@ -1,7 +1,6 @@
import { AppDataSource } from "../database/data-source";
import { OrgChild1, CreateOrgChild1, UpdateOrgChild1 } from "../entities/OrgChild1";
import { OrgChild2 } from "../entities/OrgChild2";
import {
Body,
Delete,
@ -28,7 +27,6 @@ import HttpError from "../interfaces/http-error";
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrgChild1Controller {
private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2);
@ -84,6 +82,14 @@ export class OrgChild1Controller {
@Request() request: { user: Record<string, any> },
) {
try {
const chkOrder = await this.child1Repository.findOne({ where: { id: id, orgChild1Order:requestBody.orgChild1Order }});
if (chkOrder != null){
throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const chkCode = await this.child1Repository.findOne({ where: { id: id, orgChild1Code:requestBody.orgChild1Code }});
if (chkCode != null){
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const child1 = await this.child1Repository.findOne({ where: { id } });
if (!child1){
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
@ -115,9 +121,9 @@ export class OrgChild1Controller {
if (!child1) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } });
const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } });
if(exitsChild2){
throw new HttpError(HttpStatusCode.CONFLICT, "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2");
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2");
}
await this.child1Repository.remove(child1);
return new HttpSuccess();

View file

@ -1 +1,144 @@
import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags } from "tsoa";
import { AppDataSource } from "../database/data-source";
import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3, CreateOrgChild3, UpdateOrgChild3 } from "../entities/OrgChild3";
import { OrgChild4 } from "../entities/OrgChild4";
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/child3")
@Tags("OrgChild3")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrgChild3Controller {
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
private child4Repository = AppDataSource.getRepository(OrgChild4);
/**
* API 3
*
* @summary ORG_010 - 3 (ADMIN) #10
*
*/
@Post()
async save(
@Body() requestBody: CreateOrgChild3,
@Request() request: { user: Record<string, any> },
){
try {
const chkOrder = await this.child3Repository.findOne({ where: { orgChild2Id:requestBody.orgChild2Id, orgChild3Order:requestBody.orgChild3Order }});
if (chkOrder != null){
throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const chkCode = await this.child3Repository.findOne({ where: { orgChild2Id:requestBody.orgChild2Id, orgChild3Code:requestBody.orgChild3Code }});
if (chkCode != null){
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
//BE ใช้ orgChild2Id หา orgChild1Id, orgRootId
const child2 = await this.child2Repository.findOne({ where: { id :requestBody.orgChild2Id }});
if(!child2){
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const child3 = Object.assign(new OrgChild3(), requestBody) as OrgChild3;
child3.orgChild3Name = requestBody.orgChild3Name
child3.createdUserId = request.user.sub
child3.createdFullName = request.user.name
child3.createdAt = new Date()
child3.lastUpdateUserId = request.user.sub
child3.lastUpdateFullName= request.user.name
child3.lastUpdatedAt = new Date()
child3.orgRootId = String(child2?.orgRootId),
child3.orgChild1Id = String(child2?.orgChild1Id),
await this.child3Repository.save(child3);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API 3
*
* @summary ORG_011 - 3 (ADMIN) #11
*
* @param {string} id id 3
*/
@Put("{id}")
async Edit(
@Path() id: string,
@Body() requestBody: UpdateOrgChild3,
@Request() request: { user: Record<string, any> },
){
try {
const chkOrder = await this.child3Repository.findOne({ where: { id: id, orgChild3Order:requestBody.orgChild3Order, }});
if (chkOrder != null){
throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const chkCode = await this.child3Repository.findOne({ where: { id: id, orgChild3Code:requestBody.orgChild3Code }});
if (chkCode != null){
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const child3 = await this.child3Repository.findOne({ where: { id } });
if (!child3){
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
child3.lastUpdateUserId = request.user.sub;
child3.lastUpdateFullName = request.user.name;
child3.lastUpdatedAt = new Date();
this.child3Repository.merge(child3, requestBody);
await this.child3Repository.save(child3);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API 3
*
* @summary ORG_012 - 3 (ADMIN) #12
*
* @param {string} id id 3
*/
@Delete("{id}")
async delete(
@Path() id: string,
){
try {
const child3 = await this.child3Repository.findOne({ where: { id } });
if (!child3) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } });
if(exitsChild4){
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4");
}
await this.child3Repository.remove(child3);
return new HttpSuccess();
} catch (error) {
return error;
}
}
}

View file

@ -1,4 +1,4 @@
import { Entity, Column, ManyToOne, JoinColumn, OneToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { Entity, Column, ManyToOne, JoinColumn, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { EntityBase } from "./base/Base";
import { OrgRoot } from "./OrgRoot";
import { OrgChild2 } from "./OrgChild2";

View file

@ -1,24 +1,17 @@
import { Entity, Column, ManyToOne, JoinColumn, OneToOne, OneToMany } from "typeorm";
import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from "typeorm";
import { EntityBase } from "./base/Base";
import { OrgRoot } from "./OrgRoot";
import { OrgChild1 } from "./OrgChild1";
import { OrgChild2 } from "./OrgChild2";
import { OrgChild4 } from "./OrgChild4";
// ENUM orgChild3Rank
enum OrgChild3Rank {
DEPARTMENT = "department",
OFFICE = "office",
DIVISION = "division",
SECTION = "section",
}
@Entity("orgChild3")
export class OrgChild3 extends EntityBase {
// @Column({
// comment: "",
// length: 40,
// default: "00000000-0000-0000-0000-000000000000",
// })
// orgChild3Id: string;
@Column({
nullable: true,
@ -49,7 +42,6 @@ export class OrgChild3 extends EntityBase {
comment: "ระดับส่วนราชการ",
type: "enum",
enum: OrgChild3Rank,
// default: OrgChild3Rank.DEPARTMENT,
})
orgChild3Rank: OrgChild3Rank;
@ -82,7 +74,7 @@ export class OrgChild3 extends EntityBase {
@Column({
nullable: true,
comment: "สถานะของหน่วยงาน", //ปกติ = 1 , ยุกเลิก = 0
comment: "สถานะของหน่วยงาน",
default: true
})
orgChild3IsNormal: boolean;
@ -105,21 +97,47 @@ export class OrgChild3 extends EntityBase {
})
orgChild2Id: string;
// @ManyToOne(() => OrgRoot, orgRoot => orgRoot.orgChild3s)
// @JoinColumn({ name: "orgRootId" })
// orgRoot: OrgRoot;
// @ManyToOne(() => OrgChild1, orgChild1 => orgChild1.orgChild3s)
// @JoinColumn({ name: "orgChild1Id" })
// orgChild1: OrgChild1;
@ManyToOne(() => OrgChild2, orgChild2 => orgChild2.orgChild3s)
@JoinColumn({ name: "orgChild2Id" })
orgChild2: OrgChild2;
//child table 4
@OneToMany(() => OrgChild4, orgChild4 => orgChild4.orgChild3)
orgChild4s: OrgChild4[];
}
export type UpdateOrgChild3 = Partial<OrgChild3>;
export class CreateOrgChild3 {
@Column()
orgChild3Name: string;
@Column()
orgChild3ShortName: string;
@Column()
orgChild3Code: string;
@Column()
orgChild3Rank: string;
@Column()
orgChild3Order: number;
@Column()
orgChild3PhoneEx: string;
@Column()
orgChild3PhoneIn: string;
@Column()
orgChild3Fax: string;
@Column()
orgChild3IsNormal: boolean;
@Column('uuid')
orgChild2Id: string;
}
export type UpdateOrgChild3 = Partial<CreateOrgChild3> & { orgChild3Rank?: OrgChild3Rank };;