api api/v1/org/profile/leave/search

This commit is contained in:
AdisakKanthawilang 2024-05-27 16:30:14 +07:00
parent 7126d64d7e
commit abc4aee8a4

View file

@ -26,6 +26,7 @@ import HttpError from "../interfaces/http-error";
import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import { LeaveType } from "../entities/LeaveType";
import { Brackets } from "typeorm";
@Route("api/v1/org/profile/leave")
@Tags("ProfileLeave")
@ -36,6 +37,82 @@ export class ProfileLeaveController extends Controller {
private leaveHistoryRepo = AppDataSource.getRepository(ProfileLeaveHistory);
private leaveTypeRepository = AppDataSource.getRepository(LeaveType);
@Post("search")
public async searchProfile(
@Body()
body: {
citizenId?: string | null;
firstName?: string | null;
lastName?: string | null;
},
) {
const profileRepository = AppDataSource.getRepository(Profile);
const queryBuilder = profileRepository
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType");
if (body.citizenId || body.firstName || body.lastName) {
queryBuilder.where(
new Brackets((qb) => {
if (body.citizenId) {
qb.orWhere("profile.citizenId LIKE :citizenId", { citizenId: `%${body.citizenId}%` });
}
if (body.firstName) {
qb.orWhere("profile.firstName LIKE :firstName", { firstName: `%${body.firstName}%` });
}
if (body.lastName) {
qb.orWhere("profile.lastName LIKE :lastName", { lastName: `%${body.lastName}%` });
}
}),
);
}
const profiles = await queryBuilder.getMany();
if (!profiles.length) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบข้อมูลโปรไฟล์");
}
const formattedProfiles = profiles.map((profile) => ({
avatar: profile.avatar,
avatarName: profile.avatarName,
rank: profile.rank,
prefix: profile.prefix,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
position: profile.position,
posLevelId: profile.posLevelId,
posLevelName: profile.posLevel.posLevelName,
posTypeId: profile.posTypeId,
posTypeName: profile.posType.posTypeName,
email: profile.email,
phone: profile.phone,
keycloak: profile.keycloak,
isProbation: profile.isProbation,
isLeave: profile.isLeave,
leaveReason: profile.leaveReason,
dateRetire: profile.dateRetire,
dateAppoint: profile.dateAppoint,
dateRetireLaw: profile.dateRetireLaw,
dateStart: profile.dateStart,
govAgeAbsent: profile.govAgeAbsent,
govAgePlus: profile.govAgePlus,
birthDate: profile.birthDate,
reasonSameDate: profile.reasonSameDate,
ethnicity: profile.ethnicity,
telephoneNumber: profile.telephoneNumber,
nationality: profile.nationality,
gender: profile.gender,
relationship: profile.relationship,
religion: profile.religion,
bloodGroup: profile.bloodGroup,
}));
return new HttpSuccess(formattedProfiles);
}
@Get("user")
public async getLeaveUser(@Request() request: { user: Record<string, any> }) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });