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

132 lines
3.8 KiB
TypeScript

import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
} from "tsoa";
import { OrgRevision } from "../entities/OrgRevision";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
@Route("api/v1/org")
@Tags("Organization")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class OrganizationController extends Controller {
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/**
* API รายการประวัติโครงสร้าง
*
* @summary ORG_020 - รายการประวัติโครงสร้าง #21
*
*/
@Get("history")
async GetHistory() {
try {
const orgRevision = await this.orgRevisionRepository.find({
select: [
"id",
"orgRevisionName",
"orgRevisionIsCurrent",
"orgRevisionCreatedAt",
"orgRevisionIsDraft",
],
order: { orgRevisionCreatedAt: "DESC" },
});
if (!orgRevision) {
return new HttpSuccess([]);
}
const mapOrgRevisions = orgRevision.map((revision) => ({
orgRevisionId: revision.id,
orgRevisionName: revision.orgRevisionName,
orgRevisionIsCurrent: revision.orgRevisionIsCurrent,
orgRevisionCreatedAt: revision.orgRevisionCreatedAt,
orgRevisionIsDraft: revision.orgRevisionIsDraft,
}));
return new HttpSuccess(mapOrgRevisions);
} catch (error) {
return error;
}
}
/**
* API โครงสร้างปัจจุบันที่ใช้อยู่
*
* @summary ORG_021 - โครงสร้างปัจจุบันที่ใช้อยู่ #22
*
*/
@Get("active")
async GetActive() {
try {
const orgRevisionActive = await this.orgRevisionRepository.findOne({
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
const orgRevisionDraf = await this.orgRevisionRepository.findOne({
where: { orgRevisionIsCurrent: false, orgRevisionIsDraft: true },
});
const mapData = {
activeId: orgRevisionActive == null ? null : orgRevisionActive.id,
activeName: orgRevisionActive == null ? null : orgRevisionActive.orgRevisionName,
draftId: orgRevisionDraf == null ? null : orgRevisionDraf.id,
draftName: orgRevisionDraf == null ? null : orgRevisionDraf.orgRevisionName,
};
return new HttpSuccess(mapData);
} catch (error) {
return error;
}
}
/**
* API สร้างโครงสร้างระดับ4
*
* @summary ORG_022 - สร้างโครงสร้างใหม่ #23 (ยังไม่เสร็จ)
*
*/
@Post()
async CreateOrgRevision(
@Body() requestBody: CreateOrgChild1,
@Request() request: { user: Record<string, any> },
) {
try {
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API รายละเอียดโครงสร้าง
*
* @summary ORG_023 - รายละเอียดโครงสร้าง (ADMIN) #25
*
*/
// @Get()
// async detail(@Path() id: string) {
// try {
// return new HttpSuccess();
// } catch (error) {
// return error;
// }
// }
}