edit status code

This commit is contained in:
Bright 2024-01-30 13:52:37 +07:00
parent 1384b41b9b
commit 4abee90c38
4 changed files with 309 additions and 321 deletions

View file

@ -48,15 +48,15 @@ export class OrgChild2Controller extends Controller {
*/
@Get("{id}")
async GetChild2(@Path() id: string) {
const orgChild2 = await this.child2Repository.findOne({ where: { id } });
if (!orgChild2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 2");
}
const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild2.orgRootId } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root");
}
try {
const orgChild2 = await this.child2Repository.findOne({ where: { id } });
if (!orgChild2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 2");
}
const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild2.orgRootId } });
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root");
}
const getOrgChild2 = {
"orgChild2Id" : orgChild2.id,
"orgChild2Name" : orgChild2.orgChild2Name,
@ -101,30 +101,28 @@ export class OrgChild2Controller extends Controller {
requestBody: CreateOrgChild2,
@Request() request: { user: Record<string, any> },
) {
const child1 = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
if (!child1) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child1.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 validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
try {
//BE ใช้ orgChild1Id หา orgChild1Id, orgRootId
const child1 = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
if (!child1) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child1.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 validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
const child2 = Object.assign(new OrgChild2(), requestBody) as OrgChild2;
child2.orgChild2Name = requestBody.orgChild2Name;
child2.createdUserId = request.user.sub;
@ -168,37 +166,36 @@ export class OrgChild2Controller extends Controller {
requestBody: UpdateOrgChild2,
@Request() request: { user: Record<string, any> },
) {
const child1IdExits = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
if (!child1IdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child1IdExits.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 validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (
requestBody.orgChild2Rank == null ||
!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
const child2 = await this.child2Repository.findOne({ where: { id } });
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
try {
const child1IdExits = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
if (!child1IdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child1IdExits.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 validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (
requestBody.orgChild2Rank == null ||
!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
const child2 = await this.child2Repository.findOne({ where: { id } });
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
child2.lastUpdateUserId = request.user.sub;
child2.lastUpdateFullName = request.user.name;
child2.lastUpdatedAt = new Date();
@ -222,29 +219,29 @@ export class OrgChild2Controller extends Controller {
*/
@Delete("{id}")
async delete(@Path() id: string) {
try {
const child2 = await this.child2Repository.findOne({ where: { id } });
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child2.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 child2 = await this.child2Repository.findOne({ where: { id } });
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child2.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 exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } });
if (exitsChild3) {
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4",
);
}
const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } });
if (exitsChild3) {
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4",
);
}
try {
await this.child2Repository.remove(child2);
return new HttpSuccess();
} catch (error) {