hrms-api-org/src/controllers/OrgRootController.ts

172 lines
5.9 KiB
TypeScript
Raw Normal View History

2024-01-25 10:13:36 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
2024-01-25 13:26:39 +07:00
Request,
2024-01-25 10:13:36 +07:00
Example,
} from "tsoa";
import { CreateOrgRoot, OrgRoot } from "../entities/OrgRoot";
2024-01-25 10:52:44 +07:00
import { AppDataSource } from "../database/data-source";
2024-01-25 10:13:36 +07:00
import HttpSuccess from "../interfaces/http-success";
2024-01-25 13:26:39 +07:00
import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
2024-01-26 15:54:07 +07:00
import { OrgRevision } from "../entities/OrgRevision";
2024-01-25 10:13:36 +07:00
@Route("organization")
@Tags("OrgRoot")
2024-01-25 14:35:19 +07:00
@Security("bearerAuth")
2024-01-25 10:13:36 +07:00
export class OrgRootController extends Controller {
2024-01-25 10:52:44 +07:00
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
2024-01-26 12:00:43 +07:00
private orgChild1Repository = AppDataSource.getRepository(OrgChild1);
2024-01-26 15:54:07 +07:00
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
2024-01-25 10:13:36 +07:00
/**
2024-01-25 13:26:39 +07:00
* Root
2024-01-25 10:13:36 +07:00
*
* @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,
2024-01-25 13:26:39 +07:00
@Request() request: { user: Record<string, any> },
2024-01-25 10:13:36 +07:00
) {
try {
2024-01-26 15:54:07 +07:00
const orgRevision = await this.orgRevisionRepository.findOne({
where: { id: requestBody.orgRevisionId },
});
2024-01-25 10:13:36 +07:00
const orgRoot = Object.assign(new OrgRoot(), requestBody);
if (!orgRoot) {
2024-01-25 13:26:39 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-01-25 13:26:39 +07:00
const chkCode = await this.orgRootRepository.findOne({
where: { orgRootCode: requestBody.orgRootCode },
});
if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
2024-01-25 10:13:36 +07:00
}
2024-01-26 15:54:07 +07:00
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");
}
2024-01-25 10:13:36 +07:00
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
2024-01-25 13:26:39 +07:00
* Root
2024-01-25 10:13:36 +07:00
*
* @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,
2024-01-25 14:35:19 +07:00
@Request() request: { user: Record<string, any> },
2024-01-25 10:13:36 +07:00
) {
try {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
2024-01-25 13:26:39 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-01-25 13:26:39 +07:00
const chkCode = await this.orgRootRepository.findOne({
where: { orgRootCode: requestBody.orgRootCode },
});
2024-01-26 15:54:07 +07:00
if (chkCode?.id != id && chkCode != null) {
2024-01-25 13:26:39 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
2024-01-25 10:13:36 +07:00
}
2024-01-26 15:54:07 +07:00
2024-01-25 14:35:19 +07:00
orgRoot.lastUpdateUserId = request.user.sub;
orgRoot.lastUpdateFullName = request.user.name;
2024-01-26 15:54:07 +07:00
orgRoot.lastUpdatedAt = new Date();
this.orgRootRepository.merge(orgRoot, requestBody);
2024-01-25 10:13:36 +07:00
await this.orgRootRepository.save(orgRoot);
return new HttpSuccess();
} catch (error) {
return error;
}
}
2024-01-25 11:24:50 +07:00
/**
2024-01-25 13:26:39 +07:00
* Root
2024-01-25 11:24:50 +07:00
*
* @summary ORG_003 - Root (ADMIN) #3
*
* @param {string} id Guid, *Id root
*/
@Delete("root/{id}")
2024-01-25 13:26:39 +07:00
async delete(@Path() id: string) {
2024-01-25 11:24:50 +07:00
try {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
2024-01-26 12:00:43 +07:00
const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } });
2024-01-25 13:26:39 +07:00
2024-01-25 11:24:50 +07:00
if (!orgRoot) {
2024-01-25 13:26:39 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
2024-01-25 11:24:50 +07:00
}
2024-01-25 13:26:39 +07:00
if (!orgChild1) {
await this.orgRootRepository.remove(orgRoot);
} else {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1",
);
2024-01-25 11:24:50 +07:00
}
2024-01-25 13:26:39 +07:00
2024-01-25 11:24:50 +07:00
return new HttpSuccess();
} catch (error) {
return error;
}
}
2024-01-25 10:13:36 +07:00
}