OrgDotnetController

This commit is contained in:
Bright 2024-05-27 17:57:16 +07:00
parent 59cefbeec5
commit ccffbe6c4d
3 changed files with 270 additions and 82 deletions

View file

@ -0,0 +1,188 @@
import {
Controller,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Get,
Query,
Example,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import {
Profile,
CreateProfile,
UpdateProfile,
ProfileHistory,
CreateProfileAllFields,
} from "../entities/Profile";
import { Brackets, IsNull, Like, Not } from "typeorm";
import { OrgRevision } from "../entities/OrgRevision";
import { PosMaster } from "../entities/PosMaster";
import { PosLevel } from "../entities/PosLevel";
import { PosType } from "../entities/PosType";
import {
calculateAge,
calculateRetireDate,
calculateRetireLaw,
calculateRetireYear,
} from "../interfaces/utils";
import { RequestWithUser } from "../middlewares/user";
import { Position } from "../entities/Position";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { Province } from "../entities/Province";
import { District } from "../entities/District";
import { SubDistrict } from "../entities/SubDistrict";
import { ProfileCertificate } from "../entities/ProfileCertificate";
import { ProfileTraining } from "../entities/ProfileTraining";
import { ProfileDiscipline } from "../entities/ProfileDiscipline";
import { ProfileEducation } from "../entities/ProfileEducation";
import { ProfileSalary } from "../entities/ProfileSalary";
import { ProfileFamilyCouple } from "../entities/ProfileFamilyCouple";
import { ProfileFamilyMother } from "../entities/ProfileFamilyMother";
import { ProfileFamilyFather } from "../entities/ProfileFamilyFather";
import Extension from "../interfaces/extension";
@Route("api/v1/org/dotnet")
@Tags("Dotnet")
@Security("bearerAuth")
@Response(
HttpStatus.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatus.OK, "สำเร็จ")
export class OrganizationDotnetController extends Controller {
private orgRevisionRepo = AppDataSource.getRepository(OrgRevision);
private posMasterRepo = AppDataSource.getRepository(PosMaster);
private profileRepo = AppDataSource.getRepository(Profile);
private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee);
private profileHistoryRepo = AppDataSource.getRepository(ProfileHistory);
private posLevelRepo = AppDataSource.getRepository(PosLevel);
private posTypeRepo = AppDataSource.getRepository(PosType);
private positionRepository = AppDataSource.getRepository(Position);
private provinceRepository = AppDataSource.getRepository(Province);
private districtRepository = AppDataSource.getRepository(District);
private subDistrict = AppDataSource.getRepository(SubDistrict);
private certificateRepository = AppDataSource.getRepository(ProfileCertificate);
private profileFamilyCoupleRepository = AppDataSource.getRepository(ProfileFamilyCouple);
private profileFamilyMotherRepository = AppDataSource.getRepository(ProfileFamilyMother);
private profileFamilyFatherRepository = AppDataSource.getRepository(ProfileFamilyFather);
private trainingRepository = AppDataSource.getRepository(ProfileTraining);
private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline);
private educationRepository = AppDataSource.getRepository(ProfileEducation);
private salaryRepository = AppDataSource.getRepository(ProfileSalary);
/**
* 1. API Search Profile
*
* @summary 1. API Search Profile
*
*/
@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);
}
/**
* 3. API Get Profile keycloak id
*
* @summary 3. API Get Profile keycloak id
*
*/
@Get("keycloak/{id}")
async getProfileByKeycloakId(@Path() id: string) {
const profile = await this.profileRepo.findOne({
relations: {
posLevel: true,
posType: true,
},
where: { keycloak: id },
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess(profile);
}
}

View file

@ -873,20 +873,20 @@ export class ProfileController extends Controller {
return new HttpSuccess(profile); return new HttpSuccess(profile);
} }
@Get("keycloak/{id}") // @Get("keycloak/{id}")
async getProfileByKeycloakId(@Path() id: string) { // async getProfileByKeycloakId(@Path() id: string) {
const profile = await this.profileRepo.findOne({ // const profile = await this.profileRepo.findOne({
relations: { // relations: {
posLevel: true, // posLevel: true,
posType: true, // posType: true,
}, // },
where: { keycloak: id }, // where: { keycloak: id },
}); // });
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); // if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess(profile); // return new HttpSuccess(profile);
} // }
@Get("history/{id}") @Get("history/{id}")
async getProfileHistory(@Path() id: string) { async getProfileHistory(@Path() id: string) {

View file

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