validate orgRevisionId (root, child1)

This commit is contained in:
Bright 2024-01-29 12:54:49 +07:00
parent f9f0782cb2
commit 93683f897b
2 changed files with 38 additions and 12 deletions

View file

@ -1,4 +1,5 @@
import { AppDataSource } from "../database/data-source";
import { OrgRevision } from "../entities/OrgRevision";
import { OrgRoot } from "../entities/OrgRoot";
import { OrgChild1, CreateOrgChild1, UpdateOrgChild1 } from "../entities/OrgChild1";
import { OrgChild2 } from "../entities/OrgChild2";
@ -35,6 +36,8 @@ export class OrgChild1Controller {
private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/**
* API 1
@ -108,6 +111,12 @@ export class OrgChild1Controller {
if (!rootIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootId");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: rootIdExits.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())) {
@ -159,6 +168,12 @@ export class OrgChild1Controller {
if (!rootIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RootId");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: rootIdExits.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (
@ -209,6 +224,14 @@ export class OrgChild1Controller {
if (!child1) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child1.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } });
if (exitsChild2) {
throw new HttpError(

View file

@ -72,7 +72,7 @@ export class OrgRootController extends Controller {
}
const orgRevision = await this.orgRevisionRepository.findOne({
where: { id: requestBody.orgRevisionId },
where: { id: requestBody.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (orgRevision) {
@ -82,7 +82,8 @@ export class OrgRootController extends Controller {
orgRoot.lastUpdateFullName = request.user.name;
await this.orgRootRepository.save(orgRoot);
} else {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี Revision");
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี Revision");
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
return new HttpSuccess();
@ -123,7 +124,7 @@ export class OrgRootController extends Controller {
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: requestBody.orgRevisionId },
where: { id: requestBody.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
@ -164,21 +165,23 @@ export class OrgRootController extends Controller {
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.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1",
);
const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } });
if(orgChild1 != null){
throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR,"ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1",);
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: orgRoot.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
await this.orgRootRepository.remove(orgRoot);
return new HttpSuccess();
} catch (error) {
return error;