171 lines
5.9 KiB
TypeScript
171 lines
5.9 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Patch,
|
|
Route,
|
|
Security,
|
|
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 { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1";
|
|
import HttpError from "../interfaces/http-error";
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
import { OrgRevision } from "../entities/OrgRevision";
|
|
|
|
@Route("organization")
|
|
@Tags("OrgRoot")
|
|
@Security("bearerAuth")
|
|
export class OrgRootController extends Controller {
|
|
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
|
|
private orgChild1Repository = AppDataSource.getRepository(OrgChild1);
|
|
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
|
|
|
|
/**
|
|
* สร้างโครงสร้างระดับ Root
|
|
*
|
|
* @summary ORG_001 - สร้างโครงสร้างระดับ Root (ADMIN) #1
|
|
*
|
|
*/
|
|
@Post("root")
|
|
@Example([
|
|
{
|
|
orgRootName: "string", //ชื่อหน่วยงาน
|
|
orgRootShortName: "string", //อักษรย่อ
|
|
orgRootCode: "string", //รหัสหน่วยงาน
|
|
orgRootPhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
|
|
orgRootPhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
|
|
orgRootFax: "string", //หมายเลขโทรสาร
|
|
orgRootIsNormal: "boolean", //สถานะของหน่วยงาน
|
|
},
|
|
])
|
|
async create(
|
|
// @Path() id: string,
|
|
@Body()
|
|
requestBody: CreateOrgRoot,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
try {
|
|
const orgRevision = await this.orgRevisionRepository.findOne({
|
|
where: { id: requestBody.orgRevisionId },
|
|
});
|
|
|
|
const orgRoot = Object.assign(new OrgRoot(), requestBody);
|
|
if (!orgRoot) {
|
|
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, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
if (orgRevision) {
|
|
orgRoot.createdUserId = request.user.sub;
|
|
orgRoot.createdFullName = request.user.name;
|
|
orgRoot.createdAt = new Date();
|
|
orgRoot.lastUpdateUserId = request.user.sub;
|
|
orgRoot.lastUpdateFullName = request.user.name;
|
|
orgRoot.lastUpdatedAt = new Date();
|
|
await this.orgRootRepository.save(orgRoot);
|
|
}else{
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี Revision");
|
|
}
|
|
|
|
return new HttpSuccess();
|
|
} catch (error) {
|
|
return error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* แก้ไขโครงสร้างระดับ Root
|
|
*
|
|
* @summary ORG_002 - แก้ไขโครงสร้างระดับ Root (ADMIN) #2
|
|
*
|
|
* @param {string} id Guid, *Id root
|
|
*/
|
|
@Put("root/{id}")
|
|
@Example([
|
|
{
|
|
orgRootName: "string", //ชื่อหน่วยงาน
|
|
orgRootShortName: "string", //อักษรย่อ
|
|
orgRootCode: "string", //รหัสหน่วยงาน
|
|
orgRootPhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
|
|
orgRootPhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
|
|
orgRootFax: "string", //หมายเลขโทรสาร
|
|
orgRootIsNormal: "boolean", //สถานะของหน่วยงาน
|
|
},
|
|
])
|
|
async update(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: CreateOrgRoot,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
try {
|
|
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
|
|
if (!orgRoot) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
|
}
|
|
|
|
const chkCode = await this.orgRootRepository.findOne({
|
|
where: { orgRootCode: requestBody.orgRootCode },
|
|
});
|
|
if (chkCode?.id != id && chkCode != null) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
|
|
orgRoot.lastUpdateUserId = request.user.sub;
|
|
orgRoot.lastUpdateFullName = request.user.name;
|
|
orgRoot.lastUpdatedAt = new Date();
|
|
this.orgRootRepository.merge(orgRoot, requestBody);
|
|
await this.orgRootRepository.save(orgRoot);
|
|
|
|
return new HttpSuccess();
|
|
} catch (error) {
|
|
return error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* ลบโครงสร้างระดับ Root
|
|
*
|
|
* @summary ORG_003 - ลบโครงสร้างระดับ Root (ADMIN) #3
|
|
*
|
|
* @param {string} id Guid, *Id root
|
|
*/
|
|
@Delete("root/{id}")
|
|
async delete(@Path() id: string) {
|
|
try {
|
|
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
|
|
const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } });
|
|
|
|
if (!orgRoot) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
|
}
|
|
|
|
if (!orgChild1) {
|
|
await this.orgRootRepository.remove(orgRoot);
|
|
} else {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1",
|
|
);
|
|
}
|
|
|
|
return new HttpSuccess();
|
|
} catch (error) {
|
|
return error;
|
|
}
|
|
}
|
|
}
|