org_010 - 012
This commit is contained in:
parent
55119a72f5
commit
6d136f8a97
4 changed files with 195 additions and 28 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue