org_004 - 006

This commit is contained in:
Bright 2024-01-25 13:42:17 +07:00
parent 320ad04e73
commit 0fb7736cac

View file

@ -1,5 +1,7 @@
import { AppDataSource } from "../database/data-source";
import { OrgChild1, CreateOrgChild1, UpdateOrgChild1 } from "../entities/OrgChild1";
import { OrgChild2 } from "../entities/OrgChild2";
import {
Body,
Delete,
@ -28,7 +30,8 @@ import HttpError from "../interfaces/http-error";
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrgChild1Controller {
private child1Repository = AppDataSource.getRepository(OrgChild1)
private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2);
/**
* API 1
@ -43,11 +46,11 @@ export class OrgChild1Controller {
){
try {
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, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
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, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const child1 = Object.assign(new OrgChild1(), requestBody) as OrgChild1;
@ -63,7 +66,7 @@ export class OrgChild1Controller {
} catch (error) {
// return new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, String(error))
return error
return error;
}
}
@ -82,7 +85,7 @@ export class OrgChild1Controller {
) {
try {
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;
@ -92,9 +95,35 @@ export class OrgChild1Controller {
await this.child1Repository.save(child1);
return new HttpSuccess();
} 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;
}
}
}