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

286 lines
10 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";
2024-02-06 14:03:45 +07:00
import { In, IsNull, Not } from "typeorm";
2024-01-25 13:26:39 +07:00
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-02-06 14:03:45 +07:00
import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3 } from "../entities/OrgChild3";
import { OrgChild4 } from "../entities/OrgChild4";
import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
2024-01-25 10:13:36 +07:00
2024-01-29 17:22:05 +07:00
@Route("api/v1/org/root")
2024-01-25 10:13:36 +07:00
@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-02-06 14:03:45 +07:00
private orgChild2Repository = AppDataSource.getRepository(OrgChild2);
private orgChild3Repository = AppDataSource.getRepository(OrgChild3);
private orgChild4Repository = AppDataSource.getRepository(OrgChild4);
2024-01-26 15:54:07 +07:00
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
2024-02-06 14:03:45 +07:00
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private positionRepository = AppDataSource.getRepository(Position);
2024-01-25 10:13:36 +07:00
/**
* API Root
*
* @summary ORG_016 - Root (ADMIN) #16
*
* @param {string} id Id Root
*/
2024-01-29 17:22:05 +07:00
@Get("{id}")
async GetRoot(@Path() id: string) {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root");
}
try {
const getOrgRoot = {
orgRootId: orgRoot.id,
orgRootName: orgRoot.orgRootName,
2024-01-31 18:24:38 +07:00
orgName: "-",
orgRootShortName: orgRoot.orgRootShortName,
orgRootCode: orgRoot.orgRootCode,
orgRootRank: orgRoot.orgRootRank,
orgRootOrder: orgRoot.orgRootOrder,
orgRootPhoneEx: orgRoot.orgRootPhoneEx,
orgRootPhoneIn: orgRoot.orgRootPhoneIn,
orgRootFax: orgRoot.orgRootFax,
orgRevisionId: orgRoot.orgRevisionId,
orgCode: orgRoot.orgRootCode + "00",
2024-01-29 17:22:05 +07:00
};
return new HttpSuccess(getOrgRoot);
} catch (error) {
return error;
}
}
2024-01-29 17:22:05 +07:00
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
*
*/
2024-01-29 17:22:05 +07:00
@Post()
2024-01-25 10:13:36 +07:00
@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
) {
const validOrgRootRanks = ["DEPARTMENT", "OFFICE", "DIVISION", "SECTION"];
if (!validOrgRootRanks.includes(requestBody.orgRootRank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootRank");
}
const orgRoot = Object.assign(new OrgRoot(), requestBody);
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chkCode = await this.orgRootRepository.findOne({
where: { orgRevisionId: requestBody.orgRevisionId, orgRootCode: requestBody.orgRootCode },
});
if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const orgRevision = await this.orgRevisionRepository.findOne({
where: { id: requestBody.orgRevisionId },
});
if (!orgRevision) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if (orgRevision.orgRevisionIsDraft != true && orgRevision.orgRevisionIsCurrent != false) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
}
const order:any = await this.orgRootRepository.findOne({
where: {
orgRevisionId: requestBody.orgRevisionId
},
order: { orgRootOrder: "DESC" }
})
2024-01-25 10:13:36 +07:00
try {
orgRoot.createdUserId = request.user.sub;
orgRoot.createdFullName = request.user.name;
orgRoot.lastUpdateUserId = request.user.sub;
orgRoot.lastUpdateFullName = request.user.name;
orgRoot.orgRootOrder = order.orgRootOrder == null ? 1 : order.orgRootOrder+1;
await this.orgRootRepository.save(orgRoot);
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
*/
2024-01-29 17:22:05 +07:00
@Put("{id}")
2024-01-25 10:13:36 +07:00
@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
) {
const validOrgRootRanks = ["DEPARTMENT", "OFFICE", "DIVISION", "SECTION"];
if (!validOrgRootRanks.includes(requestBody.orgRootRank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootRank");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: requestBody.orgRevisionId },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if (
revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
}
const chkCode = await this.orgRootRepository.findOne({
where: { orgRevisionId: requestBody.orgRevisionId, orgRootCode: requestBody.orgRootCode },
});
if (chkCode?.id != id && chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลโครงสร้างระดับ Root ตามไอดีนี้ :" + id,
);
}
try {
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
*/
2024-01-29 17:22:05 +07:00
@Delete("{id}")
2024-01-25 13:26:39 +07:00
async delete(@Path() id: string) {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลโครงสร้างระดับ Root ตามไอดีนี้ :" + id,
);
}
2024-02-06 14:03:45 +07:00
// const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } });
// if (orgChild1 != null) {
// throw new HttpError(
// HttpStatusCode.INTERNAL_SERVER_ERROR,
// "ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1",
// );
// }
2024-01-25 13:26:39 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: orgRoot.orgRevisionId },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
if (
revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
}
try {
2024-02-06 14:03:45 +07:00
const posMasters = await this.posMasterRepository.find({
where: { orgRootId: id}
});
const positions = await this.positionRepository.find({
where: [{ posMasterId: In(posMasters.map((x) => x.id)) }],
});
await this.positionRepository.remove(positions);
await this.posMasterRepository.remove(posMasters);
await this.orgChild4Repository.delete({ orgRootId: id });
await this.orgChild3Repository.delete({ orgRootId: id });
await this.orgChild2Repository.delete({ orgRootId: id });
await this.orgChild1Repository.delete({ orgRootId: id });
await this.orgRootRepository.delete({id});
2024-01-25 11:24:50 +07:00
return new HttpSuccess();
} catch (error) {
return error;
}
}
2024-01-25 10:13:36 +07:00
}