org_004 - 006
This commit is contained in:
parent
320ad04e73
commit
0fb7736cac
1 changed files with 35 additions and 6 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
import { AppDataSource } from "../database/data-source";
|
import { AppDataSource } from "../database/data-source";
|
||||||
import { OrgChild1, CreateOrgChild1, UpdateOrgChild1 } from "../entities/OrgChild1";
|
import { OrgChild1, CreateOrgChild1, UpdateOrgChild1 } from "../entities/OrgChild1";
|
||||||
|
import { OrgChild2 } from "../entities/OrgChild2";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Delete,
|
Delete,
|
||||||
|
|
@ -28,7 +30,8 @@ import HttpError from "../interfaces/http-error";
|
||||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||||
|
|
||||||
export class OrgChild1Controller {
|
export class OrgChild1Controller {
|
||||||
private child1Repository = AppDataSource.getRepository(OrgChild1)
|
private child1Repository = AppDataSource.getRepository(OrgChild1);
|
||||||
|
private child2Repository = AppDataSource.getRepository(OrgChild2);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API สร้างโครงสร้างระดับ1
|
* API สร้างโครงสร้างระดับ1
|
||||||
|
|
@ -43,11 +46,11 @@ export class OrgChild1Controller {
|
||||||
){
|
){
|
||||||
try {
|
try {
|
||||||
const chkOrder = await this.child1Repository.findOne({ where: { orgRootId:requestBody.orgRootId, orgChild1Order:requestBody.orgChild1Order }});
|
const chkOrder = await this.child1Repository.findOne({ where: { orgRootId:requestBody.orgRootId, orgChild1Order:requestBody.orgChild1Order }});
|
||||||
if (chkOrder != null) {
|
if (chkOrder != null){
|
||||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว");
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว");
|
||||||
}
|
}
|
||||||
const chkCode = await this.child1Repository.findOne({ where: { orgRootId:requestBody.orgRootId, orgChild1Code:requestBody.orgChild1Code }});
|
const chkCode = await this.child1Repository.findOne({ where: { orgRootId:requestBody.orgRootId, orgChild1Code:requestBody.orgChild1Code }});
|
||||||
if (chkCode != null) {
|
if (chkCode != null){
|
||||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
|
||||||
}
|
}
|
||||||
const child1 = Object.assign(new OrgChild1(), requestBody) as OrgChild1;
|
const child1 = Object.assign(new OrgChild1(), requestBody) as OrgChild1;
|
||||||
|
|
@ -63,7 +66,7 @@ export class OrgChild1Controller {
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// return new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, String(error))
|
// return new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, String(error))
|
||||||
return error
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -82,7 +85,7 @@ export class OrgChild1Controller {
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const child1 = await this.child1Repository.findOne({ where: { id } });
|
const child1 = await this.child1Repository.findOne({ where: { id } });
|
||||||
if (!child1) {
|
if (!child1){
|
||||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||||||
}
|
}
|
||||||
child1.lastUpdateUserId = request.user.sub;
|
child1.lastUpdateUserId = request.user.sub;
|
||||||
|
|
@ -92,9 +95,35 @@ export class OrgChild1Controller {
|
||||||
await this.child1Repository.save(child1);
|
await this.child1Repository.save(child1);
|
||||||
return new HttpSuccess();
|
return new HttpSuccess();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return error
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API ลบโครงสร้างระดับ1
|
||||||
|
*
|
||||||
|
* @summary ORG_006 - ลบโครงสร้างระดับ1 (ADMIN) #6
|
||||||
|
*
|
||||||
|
* @param {string} id id สร้างโครงสร้างระดับ1
|
||||||
|
*/
|
||||||
|
@Delete("{id}")
|
||||||
|
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.CONFLICT, "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2");
|
||||||
|
}
|
||||||
|
await this.child1Repository.remove(child1);
|
||||||
|
return new HttpSuccess();
|
||||||
|
} catch (error) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue