fix OrgRoot

This commit is contained in:
AdisakKanthawilang 2024-01-26 16:51:06 +07:00
parent 93f1a7c00e
commit a0052474f6

View file

@ -21,7 +21,7 @@ import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { OrgRevision } from "../entities/OrgRevision";
@Route("organization")
@Route("api/v1/organization")
@Tags("OrgRoot")
@Security("bearerAuth")
export class OrgRootController extends Controller {
@ -54,9 +54,10 @@ export class OrgRootController extends Controller {
@Request() request: { user: Record<string, any> },
) {
try {
const orgRevision = await this.orgRevisionRepository.findOne({
where: { id: requestBody.orgRevisionId },
});
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) {
@ -69,6 +70,11 @@ export class OrgRootController extends Controller {
if (chkCode != null) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว");
}
const orgRevision = await this.orgRevisionRepository.findOne({
where: { id: requestBody.orgRevisionId },
});
if (orgRevision) {
orgRoot.createdUserId = request.user.sub;
orgRoot.createdFullName = request.user.name;
@ -111,18 +117,30 @@ export class OrgRootController extends Controller {
@Request() request: { user: Record<string, any> },
) {
try {
const orgRoot = await this.orgRootRepository.findOne({ where: { id } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
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");
}
const chkCode = await this.orgRootRepository.findOne({
where: { 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, "ไม่พบข้อมูล");
}
orgRoot.orgRevisionId = orgRoot.orgRevisionId
orgRoot.lastUpdateUserId = request.user.sub;
orgRoot.lastUpdateFullName = request.user.name;
orgRoot.lastUpdatedAt = new Date();
@ -156,7 +174,7 @@ export class OrgRootController extends Controller {
await this.orgRootRepository.remove(orgRoot);
} else {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1",
);
}