2024-02-02 10:47:19 +07:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Delete,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
Body,
|
|
|
|
|
Path,
|
|
|
|
|
Request,
|
|
|
|
|
SuccessResponse,
|
|
|
|
|
Response,
|
2024-02-02 13:16:40 +07:00
|
|
|
Get,
|
2024-02-02 10:47:19 +07:00
|
|
|
} from "tsoa";
|
|
|
|
|
import { AppDataSource } from "../database/data-source";
|
|
|
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
2024-02-02 13:16:40 +07:00
|
|
|
import { CreatePosExecutive, PosExecutive } from "../entities/PosExecutive";
|
2024-02-02 10:47:19 +07:00
|
|
|
import { Position } from "../entities/Position";
|
|
|
|
|
@Route("api/v1/org/pos")
|
2024-02-02 12:52:49 +07:00
|
|
|
@Tags("PosExecutive")
|
2024-02-02 10:47:19 +07:00
|
|
|
@Security("bearerAuth")
|
|
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
|
|
|
)
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
|
|
|
export class PosExecutiveController extends Controller {
|
|
|
|
|
private posExecutiveRepository = AppDataSource.getRepository(PosExecutive);
|
|
|
|
|
private positionRepository = AppDataSource.getRepository(Position);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API เพิ่มตำแหน่งทางการบริหาร
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_041 - เพิ่มตำแหน่งทางการบริหาร (ADMIN) #44
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Post("executive")
|
|
|
|
|
async createPosExecutive(
|
|
|
|
|
@Body()
|
2024-02-02 13:16:40 +07:00
|
|
|
requestBody: CreatePosExecutive,
|
2024-02-02 10:47:19 +07:00
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
|
|
|
|
const posExecutive = Object.assign(new PosExecutive(), requestBody);
|
|
|
|
|
if (!posExecutive) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
posExecutive.createdUserId = request.user.sub;
|
|
|
|
|
posExecutive.createdFullName = request.user.name;
|
|
|
|
|
posExecutive.lastUpdateUserId = request.user.sub;
|
|
|
|
|
posExecutive.lastUpdateFullName = request.user.name;
|
|
|
|
|
await this.posExecutiveRepository.save(posExecutive);
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API แก้ไขตำแหน่งทางการบริหาร
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_042 - แก้ไขตำแหน่งทางการบริหาร (ADMIN) #45
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id Id ตำแหน่งทางการบริหาร
|
|
|
|
|
*/
|
|
|
|
|
@Put("executive/{id}")
|
|
|
|
|
async updatePosExecutive(
|
|
|
|
|
@Path() id: string,
|
|
|
|
|
@Body()
|
2024-02-02 13:16:40 +07:00
|
|
|
requestBody: CreatePosExecutive,
|
2024-02-02 10:47:19 +07:00
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
2024-02-02 13:16:40 +07:00
|
|
|
const posExecutive = await this.posExecutiveRepository.findOne({where: {id: id}});
|
2024-02-02 10:47:19 +07:00
|
|
|
if (!posExecutive) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
posExecutive.lastUpdateUserId = request.user.sub;
|
|
|
|
|
posExecutive.lastUpdateFullName = request.user.name;
|
2024-02-02 13:16:40 +07:00
|
|
|
this.posExecutiveRepository.merge(posExecutive, requestBody);
|
2024-02-02 10:47:19 +07:00
|
|
|
await this.posExecutiveRepository.save(posExecutive);
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API ลบอัตรากำลัง
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_043 - ลบตำแหน่งทางการบริหาร (ADMIN) #46
|
|
|
|
|
*
|
2024-02-02 13:16:40 +07:00
|
|
|
* @param {string} id Id ตำแหน่งทางการบริหาร
|
2024-02-02 10:47:19 +07:00
|
|
|
*/
|
|
|
|
|
@Delete("executive/{id}")
|
|
|
|
|
async deletePosExecutive(@Path() id: string) {
|
|
|
|
|
const delPosExecutive = await this.posExecutiveRepository.findOne({
|
|
|
|
|
where: { id },
|
|
|
|
|
});
|
|
|
|
|
if (!delPosExecutive) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await this.positionRepository.delete({ posExecutiveId: id });
|
|
|
|
|
await this.posExecutiveRepository.delete({ id });
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-02 13:16:40 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API รายละเอียดตำแหน่งทางการบริหาร
|
|
|
|
|
*
|
|
|
|
|
* @summary ORG_044 - รายละเอียดตำแหน่งทางการบริหาร (ADMIN) #47
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id Id ตำแหน่งทางการบริหาร
|
|
|
|
|
*/
|
|
|
|
|
@Get("executive/{id}")
|
|
|
|
|
async detailPosExecutive(@Path() id: string) {
|
|
|
|
|
|
|
|
|
|
const posExecutive = await this.posExecutiveRepository.findOne({
|
|
|
|
|
where: { id },
|
|
|
|
|
select:[
|
|
|
|
|
"id",
|
|
|
|
|
"posExecutiveName",
|
|
|
|
|
"posExecutivePriority"
|
|
|
|
|
]
|
|
|
|
|
});
|
|
|
|
|
if (!posExecutive) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
return new HttpSuccess(posExecutive);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-02 10:47:19 +07:00
|
|
|
}
|