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

133 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-01-26 13:32:56 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
2024-01-26 16:41:52 +07:00
SuccessResponse,
Response,
2024-01-26 13:32:56 +07:00
} 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";
2024-01-26 16:41:52 +07:00
@Route("api/v1/org")
2024-01-26 13:32:56 +07:00
@Tags("Organization")
@Security("bearerAuth")
2024-01-26 16:41:52 +07:00
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
2024-01-26 13:32:56 +07:00
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({
2024-01-26 16:41:52 +07:00
select: [
"id",
"orgRevisionName",
"orgRevisionIsCurrent",
"orgRevisionCreatedAt",
"orgRevisionIsDraft",
],
order: { orgRevisionCreatedAt: "DESC" },
2024-01-26 13:32:56 +07:00
});
if (!orgRevision) {
2024-01-26 16:41:52 +07:00
return new HttpSuccess([]);
2024-01-26 13:32:56 +07:00
}
const mapOrgRevisions = orgRevision.map((revision) => ({
orgRevisionId: revision.id,
orgRevisionName: revision.orgRevisionName,
orgRevisionIsCurrent: revision.orgRevisionIsCurrent,
orgRevisionCreatedAt: revision.orgRevisionCreatedAt,
orgRevisionIsDraft: revision.orgRevisionIsDraft,
}));
2024-01-26 16:41:52 +07:00
2024-01-26 13:32:56 +07:00
return new HttpSuccess(mapOrgRevisions);
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_021 - #22
*
*/
@Get("active")
async GetActive() {
try {
const orgRevisionActive = await this.orgRevisionRepository.findOne({
2024-01-26 16:41:52 +07:00
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
2024-01-26 13:32:56 +07:00
});
const orgRevisionDraf = await this.orgRevisionRepository.findOne({
2024-01-26 16:41:52 +07:00
where: { orgRevisionIsCurrent: false, orgRevisionIsDraft: true },
2024-01-26 13:32:56 +07:00
});
const mapData = {
2024-01-26 16:41:52 +07:00
activeId: orgRevisionActive == null ? null : orgRevisionActive.id,
activeName: orgRevisionActive == null ? null : orgRevisionActive.orgRevisionName,
draftId: orgRevisionDraf == null ? null : orgRevisionDraf.id,
draftName: orgRevisionDraf == null ? null : orgRevisionDraf.orgRevisionName,
2024-01-26 13:32:56 +07:00
};
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> },
2024-01-26 16:41:52 +07:00
) {
try {
return new HttpSuccess();
} catch (error) {
return error;
2024-01-26 13:32:56 +07:00
}
2024-01-26 16:41:52 +07:00
}
2024-01-26 17:03:41 +07:00
/**
* API
*
* @summary ORG_023 - (ADMIN) #25
*
*/
// @Get()
// async detail(@Path() id: string) {
// try {
// return new HttpSuccess();
// } catch (error) {
// return error;
// }
// }
2024-01-26 13:32:56 +07:00
}