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

196 lines
7.7 KiB
TypeScript
Raw Normal View History

import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} 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";
import { Equal, ILike, In, IsNull, Like, Not, Brackets, MoreThan } from "typeorm";
import { RequestWithUser } from "../middlewares/user";
import { ChangePosition, CreateChangePosition, UpdateChangePosition } from "../entities/ChangePosition";
2024-06-12 14:17:55 +07:00
import { ProfileChangePosition, CreateProfileChangePosition } from "../entities/ProfileChangePosition";
2024-06-12 10:25:13 +07:00
@Route("api/v1/org/placement/change-position")
@Tags("Placement")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class ChangePositionController extends Controller {
2024-06-12 14:17:55 +07:00
private ChangePositionRepository = AppDataSource.getRepository(ChangePosition);
private ProfileChangePositionRepository = AppDataSource.getRepository(ProfileChangePosition);
/**
* API
*
* @summary (ADMIN)
*
*/
@Post()
async CreateChangePosition(
@Body() body: CreateChangePosition,
@Request() request: RequestWithUser,
) {
const _changePosition = await this.ChangePositionRepository.findOne({
where: { name: body.name },
});
if(_changePosition){
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ชื่อรอบการย้ายสับเปลี่ยนตำแหน่งนี้มีอยู่ในระบบแล้ว",
);
}
const changePosition = new ChangePosition();
Object.assign(changePosition, body);
changePosition.date = new Date,
changePosition.status = "WAITTING",
changePosition.createdUserId = request.user.sub;
changePosition.createdFullName = request.user.name;
changePosition.lastUpdateUserId = request.user.sub;
changePosition.lastUpdateFullName = request.user.name;
await this.ChangePositionRepository.save(changePosition);
return new HttpSuccess();
}
2024-06-12 14:17:55 +07:00
/**
* API
*
* @summary (ADMIN)
*
* @param {string} id Id
*/
@Post("profile/{id}")
async CreateProfileChangePosition(
@Path() id: string,
@Body() body: CreateProfileChangePosition,
@Request() request: RequestWithUser,
) {
const changePosition = await this.ChangePositionRepository.findOneBy({ id });
if (!changePosition) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบย้ายสับเปลี่ยนตำแหน่ง");
const profileChangePositions: ProfileChangePosition[] = [];
const profiles = new ProfileChangePosition();
for (const data of body.profiles) {
Object.assign(profiles, data);
let positionOld = data.positionOld ? `${data.positionOld}` : "";
let rootOld = data.rootOld ? data.positionOld ? `/${data.rootOld}` : `${data.rootOld}` : "";
profiles.changePositionId = id;
profiles.organizationPositionOld = `${positionOld}${rootOld}`,
profiles.status = "WAITTING",
profiles.createdUserId = request.user.sub;
profiles.createdFullName = request.user.name;
profiles.lastUpdateUserId = request.user.sub;
profiles.lastUpdateFullName = request.user.name;
profileChangePositions.push(profiles);
}
await this.ProfileChangePositionRepository.save(profileChangePositions);
return new HttpSuccess();
}
/**
* API
*
* @summary (ADMIN)
*
* @param {string} id Id
*/
@Delete("{id}")
async DeleteChangePosition(@Path() id: string) {
let result: any;
try {
result = await this.ChangePositionRepository.delete({ id: id });
} catch {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถลบได้เนื่องจากมีการใช้งานรอบย้ายสับเปลี่ยนตำแหน่งนี้อยู่",
);
}
if (result.affected == undefined || result.affected <= 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
/**
* API
*
* @summary (ADMIN)
*
* @param {string} id Id
*/
@Put("{id}")
async UpdateChangePosition(
@Request() request: RequestWithUser,
@Path() id: string,
@Body() body: UpdateChangePosition,
) {
const changePosition = await this.ChangePositionRepository.findOneBy({ id });
2024-06-12 14:17:55 +07:00
if (!changePosition) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบย้ายสับเปลี่ยนตำแหน่ง");
const checkDuplicate = await this.ChangePositionRepository.find({
where: {
id: Not(id),
name: body.name
},
});
if(checkDuplicate.length > 0){
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ชื่อรอบการย้ายสับเปลี่ยนตำแหน่งนี้มีอยู่ในระบบแล้ว",
);
}
Object.assign(changePosition, body);
changePosition.lastUpdateUserId = request.user.sub;
changePosition.lastUpdateFullName = request.user.name;
await this.ChangePositionRepository.save(changePosition);
return new HttpSuccess();
}
/**
* API
*
* @summary API (ADMIN)
*
*/
2024-06-12 14:17:55 +07:00
@Get()
async GetChangePositionLists() {
const data = await this.ChangePositionRepository.find();
return new HttpSuccess(data);
}
/**
* API
*
* @summary API (ADMIN)
*
* @param {string} id Id
*/
@Get("{id}")
async GetChangePositionById( @Path() id: string ) {
const data = await this.ChangePositionRepository.findOne({ where: { id: id }});
if (!data) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบย้ายสับเปลี่ยนตำแหน่ง");
return new HttpSuccess(data);
}
}