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

296 lines
11 KiB
TypeScript
Raw Normal View History

2024-01-25 14:35:19 +07:00
import { AppDataSource } from "../database/data-source";
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Path,
Body,
Request,
Example,
2024-01-26 17:09:28 +07:00
SuccessResponse,
Response,
2024-01-25 14:35:19 +07:00
} from "tsoa";
import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
2024-02-06 15:18:20 +07:00
import { In } from "typeorm";
2024-01-29 13:02:32 +07:00
import { OrgRevision } from "../entities/OrgRevision";
2024-01-29 14:55:13 +07:00
import { OrgRoot } from "../entities/OrgRoot";
2024-01-26 17:09:28 +07:00
import { CreateOrgChild2, OrgChild2, UpdateOrgChild2 } from "../entities/OrgChild2";
2024-01-25 15:45:57 +07:00
import { OrgChild1 } from "../entities/OrgChild1";
import { OrgChild3 } from "../entities/OrgChild3";
2024-02-06 15:18:20 +07:00
import { OrgChild4 } from "../entities/OrgChild4";
import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
@Route("api/v1/org/child2")
2024-01-25 14:35:19 +07:00
@Tags("OrgChild2")
@Security("bearerAuth")
2024-01-26 17:09:28 +07:00
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
2024-01-25 14:35:19 +07:00
export class OrgChild2Controller extends Controller {
2024-01-29 14:55:13 +07:00
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
2024-01-26 17:09:28 +07:00
private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
2024-02-06 15:18:20 +07:00
private child4Repository = AppDataSource.getRepository(OrgChild4);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private positionRepository = AppDataSource.getRepository(Position);
2024-01-29 13:02:32 +07:00
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
2024-01-25 14:35:19 +07:00
/**
* API 2
*
* @summary ORG_018 - 2 (ADMIN) #20
*
* @param {string} id Id Child2
*/
@Get("{id}")
async GetChild2(@Path() id: string) {
2024-01-31 18:24:38 +07:00
const orgChild2 = await this.child2Repository.findOne({
where: { id },
relations: ["orgRoot", "orgChild1"],
});
2024-01-30 13:52:37 +07:00
if (!orgChild2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 2");
}
try {
const getOrgChild2 = {
2024-01-30 16:02:34 +07:00
orgChild2Id: orgChild2.id,
2024-01-31 18:24:38 +07:00
orgRootName: orgChild2.orgRoot.orgRootName,
orgName: `${orgChild2.orgChild2Name}/${orgChild2.orgChild1.orgChild1Name}/${orgChild2.orgRoot.orgRootName}`,
2024-01-30 16:02:34 +07:00
orgChild2Name: orgChild2.orgChild2Name,
orgChild2ShortName: orgChild2.orgChild2ShortName,
orgChild2Code: orgChild2.orgChild2Code,
orgChild2Rank: orgChild2.orgChild2Rank,
orgChild2Order: orgChild2.orgChild2Order,
orgChild2PhoneEx: orgChild2.orgChild2PhoneEx,
orgChild2PhoneIn: orgChild2.orgChild2PhoneIn,
orgChild2Fax: orgChild2.orgChild2Fax,
orgRevisionId: orgChild2.orgRevisionId,
2024-01-31 18:24:38 +07:00
orgCode: orgChild2.orgRoot.orgRootCode + orgChild2.orgChild2Code,
2024-01-30 16:02:34 +07:00
};
return new HttpSuccess(getOrgChild2);
} catch (error) {
return error;
}
}
2024-01-25 14:35:19 +07:00
/**
* 2 Child2
*
* @summary ORG_007 - 2 (ADMIN) #7
*
*/
2024-01-26 17:09:28 +07:00
@Post()
2024-01-25 14:35:19 +07:00
@Example([
{
orgChild2Name: "string", //ชื่อหน่วยงาน
orgChild2ShortName: "string", //อักษรย่อ
orgChild2Code: "string", //รหัสหน่วยงาน
// orgChild2Order: "number", //ลำดับที่ของหน่วยงาน
2024-01-25 14:35:19 +07:00
orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
orgChild2Fax: "string", //หมายเลขโทรสาร
2024-01-26 17:09:28 +07:00
// orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
2024-01-25 14:35:19 +07:00
orgChild1Id: "Guid", //id Child1
},
])
async create(
@Body()
requestBody: CreateOrgChild2,
@Request() request: { user: Record<string, any> },
) {
2024-01-30 13:52:37 +07:00
const child1 = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
if (!child1) {
2024-01-30 16:02:34 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id");
2024-01-30 13:52:37 +07:00
}
2024-01-29 13:02:32 +07:00
2024-01-30 13:52:37 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child1.orgRevisionId },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
2024-01-30 16:02:34 +07:00
if (
revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
2024-01-30 13:52:37 +07:00
}
2024-01-25 14:35:19 +07:00
2024-01-30 13:52:37 +07:00
const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
const order:any = await this.child2Repository.findOne({
where: {
orgChild1Id: requestBody.orgChild1Id
},
order: { orgChild2Order: "DESC" }
})
2024-01-30 13:52:37 +07:00
try {
2024-01-26 17:09:28 +07:00
const child2 = Object.assign(new OrgChild2(), requestBody) as OrgChild2;
child2.orgChild2Name = requestBody.orgChild2Name;
child2.createdUserId = request.user.sub;
child2.createdFullName = request.user.name;
child2.lastUpdateUserId = request.user.sub;
child2.lastUpdateFullName = request.user.name;
child2.orgRootId = String(child1?.orgRootId);
child2.orgRevisionId = String(child1?.orgRevisionId);
2024-01-26 17:09:28 +07:00
child2.orgChild1Id = String(child1?.id);
child2.orgChild2Order = order.orgChild2Order == null ? 1 : order.orgChild2Order+1;
2024-01-26 17:09:28 +07:00
await this.child2Repository.save(child2);
2024-01-25 14:35:19 +07:00
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
2024-01-25 15:45:57 +07:00
* 2 Child2
2024-01-25 14:35:19 +07:00
*
2024-01-25 15:45:57 +07:00
* @summary ORG_008 - 2 (ADMIN) #8
2024-01-25 14:35:19 +07:00
*
* @param {string} id Guid, *Id Child2
*/
2024-01-30 13:37:45 +07:00
@Put("{id}")
2024-01-25 15:45:57 +07:00
@Example([
{
orgChild2Name: "string", //ชื่อหน่วยงาน
orgChild2ShortName: "string", //อักษรย่อ
orgChild2Code: "string", //รหัสหน่วยงาน
// orgChild2Order: "number", //ลำดับที่ของหน่วยงาน
2024-01-25 15:45:57 +07:00
orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
orgChild2Fax: "string", //หมายเลขโทรสาร
2024-01-26 17:09:28 +07:00
// orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
2024-01-25 15:45:57 +07:00
orgChild1Id: "Guid", //id Child1
},
])
async update(
@Path() id: string,
@Body()
2024-01-26 17:09:28 +07:00
requestBody: UpdateOrgChild2,
2024-01-25 15:45:57 +07:00
@Request() request: { user: Record<string, any> },
) {
2024-01-30 13:52:37 +07:00
const child1IdExits = await this.child1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
if (!child1IdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id");
}
2024-01-29 13:02:32 +07:00
2024-01-30 13:52:37 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child1IdExits.orgRevisionId },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
2024-01-30 16:02:34 +07:00
if (
revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
2024-01-30 13:52:37 +07:00
}
2024-01-30 13:52:37 +07:00
const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"];
if (
requestBody.orgChild2Rank == null ||
!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank");
}
2024-01-26 17:09:28 +07:00
2024-01-30 13:52:37 +07:00
const child2 = await this.child2Repository.findOne({ where: { id } });
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
try {
2024-01-26 17:09:28 +07:00
child2.lastUpdateUserId = request.user.sub;
child2.lastUpdateFullName = request.user.name;
child2.lastUpdatedAt = new Date();
child2.orgRootId = String(child1IdExits?.orgRootId);
child2.orgRevisionId = String(child1IdExits?.orgRevisionId);
2024-01-26 17:09:28 +07:00
child2.orgChild1Id = String(child1IdExits?.id);
this.child2Repository.merge(child2, requestBody);
await this.child2Repository.save(child2);
2024-01-25 15:45:57 +07:00
return new HttpSuccess();
} catch (error) {
return error;
}
}
2024-01-25 14:35:19 +07:00
/**
* Child2
*
2024-01-25 15:45:57 +07:00
* @summary ORG_009 - 2 (ADMIN) #9
2024-01-25 14:35:19 +07:00
*
* @param {string} id Guid, *Id Child2
*/
2024-01-30 13:37:45 +07:00
@Delete("{id}")
2024-01-25 15:45:57 +07:00
async delete(@Path() id: string) {
2024-01-30 13:52:37 +07:00
const child2 = await this.child2Repository.findOne({ where: { id } });
if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
}
2024-01-30 16:02:34 +07:00
2024-01-30 13:52:37 +07:00
const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: child2.orgRevisionId },
});
if (!revisionIdExits) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId");
}
2024-01-30 16:02:34 +07:00
if (
revisionIdExits.orgRevisionIsDraft != true &&
revisionIdExits.orgRevisionIsCurrent != false
) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
);
2024-01-30 13:52:37 +07:00
}
2024-01-29 13:02:32 +07:00
2024-02-06 15:18:20 +07:00
// const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } });
// if (exitsChild3) {
// throw new HttpError(
// HttpStatusCode.INTERNAL_SERVER_ERROR,
// "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4",
// );
// }
2024-01-30 13:52:37 +07:00
try {
2024-02-06 15:18:20 +07:00
const posMasters = await this.posMasterRepository.find({
where: { orgChild2Id: 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({ orgChild2Id: id });
await this.child3Repository.delete({ orgChild2Id: id });
await this.child2Repository.delete({ id });
2024-01-25 15:45:57 +07:00
return new HttpSuccess();
} catch (error) {
return error;
}
}
2024-01-25 14:35:19 +07:00
}