ตัดการเพิ่มฟิว order ไปก่อนและฟิว code validate เฉพาะ root และ child 1
This commit is contained in:
parent
2dc74fc7d2
commit
aca680f4f4
10 changed files with 204 additions and 274 deletions
|
|
@ -1,22 +1,22 @@
|
|||
import { AppDataSource } from "../database/data-source";
|
||||
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";
|
||||
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";
|
||||
|
|
@ -29,116 +29,101 @@ import HttpError from "../interfaces/http-error";
|
|||
)
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
export class OrgChild3Controller {
|
||||
private child2Repository = AppDataSource.getRepository(OrgChild2);
|
||||
private child3Repository = AppDataSource.getRepository(OrgChild3);
|
||||
private child4Repository = AppDataSource.getRepository(OrgChild4);
|
||||
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_010 - สร้างโครงสร้างระดับ3 (ADMIN) #10
|
||||
*
|
||||
*/
|
||||
@Post()
|
||||
async save(
|
||||
@Body() requestBody: CreateOrgChild3,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
try {
|
||||
//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.");
|
||||
}
|
||||
/**
|
||||
* 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 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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* 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