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

346 lines
10 KiB
TypeScript
Raw Normal View History

2024-02-08 10:56:03 +07:00
import {
Controller,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Get,
} 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 { Profile, CreateProfile, UpdateProfile } from "../entities/Profile";
import { Brackets, In, IsNull, Like, Not } from "typeorm";
import { OrgRevision } from "../entities/OrgRevision";
import { PosMaster } from "../entities/PosMaster";
2024-02-08 15:40:47 +07:00
import { PosLevel } from "../entities/PosLevel";
import { PosType } from "../entities/PosType";
2024-02-08 10:56:03 +07:00
@Route("api/v1/org/profile")
@Tags("Profile")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class ProfileController extends Controller {
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
2024-02-08 10:56:03 +07:00
private profileRepository = AppDataSource.getRepository(Profile);
2024-02-08 15:40:47 +07:00
private posLevelRepository = AppDataSource.getRepository(PosLevel);
private posTypeRepository = AppDataSource.getRepository(PosType);
2024-02-08 10:56:03 +07:00
/**
* API
*
* @summary ORG_065 - (ADMIN) #70
*
*/
@Post()
async createProfile(
@Body()
requestBody: CreateProfile,
@Request() request: { user: Record<string, any> },
) {
2024-02-08 15:40:47 +07:00
if (requestBody.posLevelId) {
const checkPosLevel = await this.posLevelRepository.findOne({
where: { id: requestBody.posLevelId },
});
if (!checkPosLevel) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลใน PosLevel จากไอดีนี้ : " + requestBody.posLevelId,
);
}
}
if (requestBody.posTypeId) {
const checkPosType = await this.posTypeRepository.findOne({
where: { id: requestBody.posTypeId },
});
if (!checkPosType) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลใน PosType จากไอดีนี้ : " + requestBody.posTypeId,
);
}
}
2024-02-08 10:56:03 +07:00
try {
const profile = Object.assign(new Profile(), requestBody);
profile.createdUserId = request.user.sub;
profile.createdFullName = request.user.name;
profile.lastUpdateUserId = request.user.sub;
profile.lastUpdateFullName = request.user.name;
await this.profileRepository.save(profile);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_065 - (ADMIN) #70
*
* @param {string} id Id
*/
@Put("{id}")
async updateProfile(
@Path() id: string,
@Body()
requestBody: CreateProfile,
@Request() request: { user: Record<string, any> },
) {
const profile = await this.profileRepository.findOne({ where: { id: id } });
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id);
}
2024-02-08 15:40:47 +07:00
if (requestBody.posLevelId) {
const checkPosLevel = await this.posLevelRepository.findOne({
where: { id: requestBody.posLevelId },
});
if (!checkPosLevel) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลใน PosLevel จากไอดีนี้ : " + requestBody.posLevelId,
);
}
}
if (requestBody.posTypeId) {
const checkPosType = await this.posTypeRepository.findOne({
where: { id: requestBody.posTypeId },
});
if (!checkPosType) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลใน PosType จากไอดีนี้ : " + requestBody.posTypeId,
);
}
}
2024-02-08 10:56:03 +07:00
try {
profile.lastUpdateUserId = request.user.sub;
profile.lastUpdateFullName = request.user.name;
this.profileRepository.merge(profile, requestBody);
await this.profileRepository.save(profile);
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_065 - (ADMIN) #70
*
* @param {string} id Id
*/
@Delete("{id}")
async deleteProfile(@Path() id: string) {
const delProfile = await this.profileRepository.findOne({
where: { id },
});
if (!delProfile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id);
}
try {
await this.profileRepository.delete({ id: id });
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_065 - (ADMIN) #70
*
* @param {string} id Id
*/
@Get("{id}")
async detailProfile(@Path() id: string) {
const profile = await this.profileRepository.findOne({
where: { id },
select: [
"id",
"prefix",
"firstName",
"lastName",
"citizenId",
"position",
"posLevelId",
"posTypeId",
],
2024-02-08 10:56:03 +07:00
});
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
try {
return new HttpSuccess(profile);
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_065 - (ADMIN) #70
*
*/
@Get()
async listProfile() {
const profile = await this.profileRepository.find({
select: [
"id",
"prefix",
"firstName",
"lastName",
"citizenId",
"position",
"posLevelId",
"posTypeId",
],
2024-02-08 10:56:03 +07:00
order: { createdAt: "ASC" },
});
if (!profile) {
return new HttpSuccess([]);
}
try {
return new HttpSuccess(profile);
} catch (error) {
return error;
}
}
/**
* API
*
* @summary ORG_063 - (ADMIN) #68
*
*/
@Post("search")
async searchProfileOrg(
@Body()
requestBody: {
position?: string;
posLevelId?: string;
posTypeId?: string;
page: number;
pageSize: number;
keyword?: string;
},
) {
try {
const orgRevision = await this.orgRevisionRepository.findOne({
where: {
orgRevisionIsDraft: true,
orgRevisionIsCurrent: false,
},
relations: ["posMasters"],
});
if (!orgRevision) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง");
}
const [profiles, total] = await this.profileRepository
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.next_holders", "next_holders")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
.where(
requestBody.position != null && requestBody.position != ""
? "profile.position LIKE :position"
: "1=1",
{
position: `%${requestBody.position}%`,
},
)
.andWhere(
new Brackets((qb) => {
qb.where(
requestBody.keyword != null && requestBody.keyword != ""
? "profile.prefix LIKE :keyword"
: "1=1",
{
keyword: `%${requestBody.keyword}%`,
},
)
.orWhere(
requestBody.keyword != null && requestBody.keyword != ""
? "profile.firstName LIKE :keyword"
: "1=1",
{
keyword: `%${requestBody.keyword}%`,
},
)
.orWhere(
requestBody.keyword != null && requestBody.keyword != ""
? "profile.lastName LIKE :keyword"
: "1=1",
{
keyword: `%${requestBody.keyword}%`,
},
);
}),
)
.andWhere(
requestBody.posTypeId != null && requestBody.posTypeId != ""
? "profile.posTypeId LIKE :posTypeId"
: "1=1",
{
posTypeId: `%${requestBody.posTypeId}%`,
},
)
.andWhere(
requestBody.posLevelId != null && requestBody.posLevelId != ""
? "profile.posLevelId LIKE :posLevelId"
: "1=1",
{
posLevelId: `%${requestBody.posLevelId}%`,
},
)
.andWhere(
new Brackets((qb) => {
qb.where("next_holders.id IS NULL").orWhere("next_holders.id NOT IN (:...ids)", {
ids: orgRevision.posMasters.map((x) => x.id),
});
}),
)
.skip((requestBody.page - 1) * requestBody.pageSize)
.take(requestBody.pageSize)
.getManyAndCount();
const data = profiles.map((_data) => ({
id: _data.id,
prefix: _data.prefix,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
position: _data.position,
}));
return new HttpSuccess({ data: data, total });
} catch (error) {
return error;
}
}
2024-02-08 10:56:03 +07:00
}