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

257 lines
9.1 KiB
TypeScript

import { AppDataSource } from "../database/data-source";
import { OrgRevision } from "../entities/OrgRevision";
import { OrgRoot } from "../entities/OrgRoot";
import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3, CreateOrgChild3, UpdateOrgChild3 } from "../entities/OrgChild3";
import { OrgChild4 } from "../entities/OrgChild4";
import {
Body,
Delete,
Get,
Path,
Post,
Put,
Response,
Route,
SuccessResponse,
Tags,
Query,
Request,
Security,
} from "tsoa";
import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import { In } from "typeorm";
import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
@Route("api/v1/org/child3")
@Tags("OrgChild3")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrgChild3Controller {
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
private child4Repository = AppDataSource.getRepository(OrgChild4);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private positionRepository = AppDataSource.getRepository(Position);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/**
* API รายละเอียดโครงสร้างระดับ 3
*
* @summary ORG_019 - รายละเอียดโครงสร้างระดับ3 (ADMIN) #18
*
* @param {string} id Id Child3
*/
@Get("{id}")
async GetChild3(@Path() id: string) {
const orgChild3 = await this.child3Repository.findOne({
where: { id },
relations: ["orgRoot", "orgChild1", "orgChild2"],
});
if (!orgChild3) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 3");
}
try {
const getOrgChild3 = {
orgChild3Id: orgChild3.id,
orgRootName: orgChild3.orgRoot.orgRootName,
orgName: `${orgChild3.orgChild3Name}/${orgChild3.orgChild2.orgChild2Name}/${orgChild3.orgChild1.orgChild1Name}/${orgChild3.orgRoot.orgRootName}`,
orgChild3Name: orgChild3.orgChild3Name,
orgChild3ShortName: orgChild3.orgChild3ShortName,
orgChild3Code: orgChild3.orgChild3Code,
orgChild3Rank: orgChild3.orgChild3Rank,
orgChild3Order: orgChild3.orgChild3Order,
orgChild3PhoneEx: orgChild3.orgChild3PhoneEx,
orgChild3PhoneIn: orgChild3.orgChild3PhoneIn,
orgChild3Fax: orgChild3.orgChild3Fax,
orgRevisionId: orgChild3.orgRevisionId,
orgCode: orgChild3.orgRoot.orgRootCode + orgChild3.orgChild3Code,
};
return new HttpSuccess(getOrgChild3);
} catch (error) {
return error;
}
}
/**
* API สร้างโครงสร้างระดับ3
*
* @summary ORG_010 - สร้างโครงสร้างระดับ3 (ADMIN) #10
*
*/
@Post()
async save(
@Body() requestBody: CreateOrgChild3,
@Request() request: { user: Record<string, any> },
) {
const child2 = await this.child2Repository.findOne({
where: { id: requestBody.orgChild2Id },
});
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 validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild3Ranks.includes(requestBody.orgChild3Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild3Rank");
}
try {
const child3 = Object.assign(new OrgChild3(), requestBody) as OrgChild3;
child3.orgChild3Name = requestBody.orgChild3Name;
child3.createdUserId = request.user.sub;
child3.createdFullName = request.user.name;
child3.lastUpdateUserId = request.user.sub;
child3.lastUpdateFullName = request.user.name;
child3.orgRootId = String(child2?.orgRootId);
child3.orgChild1Id = String(child2?.orgChild1Id);
child3.orgRevisionId = String(child2?.orgRevisionId);
child3.orgChild2Id = String(child2?.id);
await this.child3Repository.save(child3);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API แก้ไขโครงสร้างระดับ3
*
* @summary ORG_011 - แก้ไขโครงสร้างระดับ3 (ADMIN) #11
*
* @param {string} id id สร้างโครงสร้างระดับ3
*/
@Put("{id}")
async Edit(
@Path() id: string,
@Body() requestBody: UpdateOrgChild3,
@Request() request: { user: Record<string, any> },
) {
const child2IdExits = await this.child2Repository.findOne({
where: { id: requestBody.orgChild2Id },
});
if (!child2IdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child2Id");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child2IdExits.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 validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (
requestBody.orgChild3Rank == null ||
!validOrgChild3Ranks.includes(requestBody.orgChild3Rank.toUpperCase())
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
const child3 = await this.child3Repository.findOne({ where: { id } });
if (!child3) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
try {
child3.lastUpdateUserId = request.user.sub;
child3.lastUpdateFullName = request.user.name;
child3.lastUpdatedAt = new Date();
child3.orgRootId = String(child2IdExits?.orgRootId);
child3.orgChild1Id = String(child2IdExits?.orgChild1Id);
child3.orgRevisionId = String(child2IdExits?.orgRevisionId);
child3.orgChild2Id = String(child2IdExits?.id);
this.child3Repository.merge(child3, requestBody);
await this.child3Repository.save(child3);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API ลบโครงสร้างระดับ3
*
* @summary ORG_012 - ลบโครงสร้างระดับ3 (ADMIN) #12
*
* @param {string} id id สร้างโครงสร้างระดับ3
*/
@Delete("{id}")
async delete(@Path() id: string) {
const child3 = await this.child3Repository.findOne({ where: { id } });
if (!child3) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child3.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 exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } });
// if (exitsChild4) {
// throw new HttpError(
// HttpStatusCode.INTERNAL_SERVER_ERROR,
// "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4",
// );
// }
try {
const posMasters = await this.posMasterRepository.find({
where: { orgChild3Id: 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.child4Repository.delete({ orgChild3Id: id });
await this.child3Repository.delete( id );
return new HttpSuccess();
} catch (error) {
return error;
}
}
}