ตัดการเพิ่มฟิว 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,21 +1,21 @@
|
|||
import { AppDataSource } from "../database/data-source";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import { OrgChild1, CreateOrgChild1, UpdateOrgChild1 } from "../entities/OrgChild1";
|
||||
import { OrgChild2 } from "../entities/OrgChild2";
|
||||
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";
|
||||
|
|
@ -41,57 +41,52 @@ export class OrgChild1Controller {
|
|||
async save(
|
||||
@Body() requestBody: CreateOrgChild1,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
){
|
||||
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;
|
||||
) {
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* API แก้ไขโครงสร้างระดับ1
|
||||
*
|
||||
* @summary ORG_005 - แก้ไขโครงสร้างระดับ1 (ADMIN) #5
|
||||
*
|
||||
* @param {string} id id สร้างโครงสร้างระดับ1
|
||||
* @param {string} id id สร้างโครงสร้างระดับ1
|
||||
*/
|
||||
@Put("{id}")
|
||||
async Edit(
|
||||
@Path() id: string,
|
||||
@Path() id: string,
|
||||
@Body() requestBody: UpdateOrgChild1,
|
||||
@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){
|
||||
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){
|
||||
if (!child1) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||||
}
|
||||
child1.lastUpdateUserId = request.user.sub;
|
||||
|
|
@ -110,20 +105,21 @@ export class OrgChild1Controller {
|
|||
*
|
||||
* @summary ORG_006 - ลบโครงสร้างระดับ1 (ADMIN) #6
|
||||
*
|
||||
* @param {string} id id สร้างโครงสร้างระดับ1
|
||||
* @param {string} id id สร้างโครงสร้างระดับ1
|
||||
*/
|
||||
@Delete("{id}")
|
||||
async delete(
|
||||
@Path() id: string,
|
||||
) {
|
||||
async delete(@Path() id: string) {
|
||||
try {
|
||||
const child1 = await this.child1Repository.findOne({ where: { id } });
|
||||
if (!child1) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||||
}
|
||||
const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } });
|
||||
if(exitsChild2){
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2");
|
||||
if (exitsChild2) {
|
||||
throw new HttpError(
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2",
|
||||
);
|
||||
}
|
||||
await this.child1Repository.remove(child1);
|
||||
return new HttpSuccess();
|
||||
|
|
@ -131,5 +127,4 @@ export class OrgChild1Controller {
|
|||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue