org_001 - 003

This commit is contained in:
AdisakKanthawilang 2024-01-25 13:26:39 +07:00
parent 414d695903
commit 55695783a9

View file

@ -10,22 +10,24 @@ import {
Tags,
Body,
Path,
Request,
Example,
} from "tsoa";
import { CreateOrgRoot, OrgRoot } from "../entities/OrgRoot";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import { OrgChild1 } from "../entities/OrgChild1";
import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
@Route("organization")
@Tags("OrgRoot")
// @Security("bearerAuth")
export class OrgRootController extends Controller {
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private orgChild1Repository = AppDataSource.getRepository(OrgChild1);
/**
* Root
* Root
*
* @summary ORG_001 - Root (ADMIN) #1
*
@ -48,13 +50,25 @@ export class OrgRootController extends Controller {
// @Path() id: string,
@Body()
requestBody: CreateOrgRoot,
@Request() request: { user: Record<string, any> },
) {
try {
const orgRoot = Object.assign(new OrgRoot(), requestBody);
if (!orgRoot) {
return `not found data`;
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chkOrder = await this.orgRootRepository.findOne({
where: { orgRootOrder: requestBody.orgRootOrder },
});
if (chkOrder != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const chkCode = await this.orgRootRepository.findOne({
where: { orgRootCode: requestBody.orgRootCode },
});
if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
orgRoot.orgRootName = requestBody.orgRootName;
orgRoot.orgRootShortName = requestBody.orgRootShortName;
orgRoot.orgRootCode = requestBody.orgRootCode;
@ -71,10 +85,8 @@ export class OrgRootController extends Controller {
}
}
/**
* Root
* Root
*
* @summary ORG_002 - Root (ADMIN) #2
*
@ -101,7 +113,19 @@ export class OrgRootController extends Controller {
try {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
return `not found data`;
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chkOrder = await this.orgRootRepository.findOne({
where: { orgRootOrder: requestBody.orgRootOrder },
});
if (chkOrder != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const chkCode = await this.orgRootRepository.findOne({
where: { orgRootCode: requestBody.orgRootCode },
});
if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
orgRoot.orgRootName = requestBody.orgRootName;
@ -121,36 +145,53 @@ export class OrgRootController extends Controller {
}
/**
* Root
* Root
*
* @summary ORG_003 - Root (ADMIN) #3
*
* @param {string} id Guid, *Id root
*/
@Delete("root/{id}")
async delete(
@Path() id: string,
) {
async delete(@Path() id: string) {
try {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
const orgChild1 = await AppDataSource.getRepository(OrgChild1)
.createQueryBuilder("orgChild1")
.where("orgChild1.orgRootId", { id })
.getOne();
const orgChild1 = await await this.orgRootRepository.findOne({ where: { id: id } });
if (!orgRoot) {
return `not found data`;
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
if(!orgChild1){
await this.orgRootRepository.remove(orgRoot);
}else{
return 'ไม่สามารถลบข้อมูลได้'
if (!orgChild1) {
await this.orgRootRepository.remove(orgRoot);
} else {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลได้");
}
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* Root
*
* @summary ORG_016 - Root (ADMIN) #16
*
* @param {string} id Guid, *Id root
*/
@Get("root/{id}")
async detail(@Path() id: string) {
try {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess(orgRoot);
} catch (error) {
return error;
}
}
}