303 lines
9.5 KiB
TypeScript
303 lines
9.5 KiB
TypeScript
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 { RequestWithUser } from "../middlewares/user";
|
|
import { Profile } from "../entities/Profile";
|
|
import { Brackets, IsNull, Like, Not } from "typeorm";
|
|
import { OrgRevision } from "../entities/OrgRevision";
|
|
import { OrgRoot } from "../entities/OrgRoot";
|
|
import { ProfileEmployee } from "../entities/ProfileEmployee";
|
|
|
|
@Route("api/v1/org/dotnet")
|
|
@Tags("Dotnet")
|
|
@Security("bearerAuth")
|
|
@Response(
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
)
|
|
@SuccessResponse(HttpStatus.OK, "สำเร็จ")
|
|
export class OrganizationDotnetController extends Controller {
|
|
private orgRootRepo = AppDataSource.getRepository(OrgRoot);
|
|
private orgRevisionRepo = AppDataSource.getRepository(OrgRevision);
|
|
private profileRepo = AppDataSource.getRepository(Profile);
|
|
|
|
/**
|
|
* 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();
|
|
return new HttpSuccess(profiles);
|
|
}
|
|
|
|
/**
|
|
* 6. Search Employee
|
|
*
|
|
* @summary 6. Search Employee
|
|
*
|
|
*/
|
|
@Post("search-employee")
|
|
public async SearchProfileEmployee(
|
|
@Body()
|
|
body: {
|
|
citizenId?: string | null;
|
|
firstName?: string | null;
|
|
lastName?: string | null;
|
|
},
|
|
) {
|
|
const profileRepository = AppDataSource.getRepository(ProfileEmployee);
|
|
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 profileEmp = await queryBuilder.getMany();
|
|
return new HttpSuccess(profileEmp);
|
|
}
|
|
|
|
/**
|
|
* 2. API ข้อมูลหน่วยงานตามโครงสร้าง
|
|
*
|
|
* @summary 2. API ข้อมูลหน่วยงานตามโครงสร้าง
|
|
*
|
|
* @param {string} id Id หน่วยงาน
|
|
*/
|
|
@Get("org/{id}")
|
|
async GetOrganizationById(@Path() id: string) {
|
|
const orgRoot = await this.orgRootRepo.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
return new HttpSuccess(orgRoot);
|
|
}
|
|
|
|
@Get("agency/{id}")
|
|
async GetOrgAgencyById(@Path() id: string) {
|
|
const orgRoot = await this.orgRootRepo.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
return new HttpSuccess(orgRoot);
|
|
}
|
|
|
|
@Get("go-agency/{id}")
|
|
async GetOrgGoAgencyById(@Path() id: string) {
|
|
const orgRoot = await this.orgRootRepo.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
return new HttpSuccess(orgRoot);
|
|
}
|
|
|
|
/**
|
|
* 3. API Get Profile จาก keycloak id
|
|
*
|
|
* @summary 3. API Get Profile จาก keycloak id
|
|
*
|
|
* @param {string} keycloakId Id keycloak
|
|
*/
|
|
@Get("keycloak/{keycloakId}")
|
|
async GetProfileByKeycloakIdAsync(@Path() keycloakId: string) {
|
|
const profile = await this.profileRepo.findOne({
|
|
relations: {
|
|
posLevel: true,
|
|
posType: true,
|
|
},
|
|
where: { keycloak: keycloakId },
|
|
});
|
|
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
return new HttpSuccess(profile);
|
|
}
|
|
|
|
/**
|
|
* 7.Get ข้อมูล GetUserFullName
|
|
*
|
|
* @summary 7.1 Get ข้อมูล GetUserFullName
|
|
*
|
|
* @param {string} keycloakId Id keycloak
|
|
*/
|
|
@Get("user-fullname/{keycloakId}")
|
|
async GetUserFullName(@Path() keycloakId: string) {
|
|
const profile = await this.profileRepo.findOne({
|
|
where: { keycloak: keycloakId },
|
|
select: ["prefix", "firstName", "lastName"]
|
|
});
|
|
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
const fullName = profile? `${profile.prefix}${profile.firstName} ${profile.lastName}` : "-";
|
|
return new HttpSuccess(fullName);
|
|
}
|
|
|
|
/**
|
|
* 7.Get ข้อมูล GetUserOCId
|
|
*
|
|
* @summary 7.2 Get ข้อมูล GetUserOCId
|
|
*
|
|
* @param {string} keycloakId Id keycloak
|
|
*/
|
|
@Get("user-oc/{keycloakId}")
|
|
async getProfileByKeycloak(@Path() keycloakId: string) {
|
|
const profile = await this.profileRepo.findOne({
|
|
where: { keycloak: keycloakId },
|
|
relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"],
|
|
});
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
|
|
}
|
|
|
|
const orgRevisionPublish = await this.orgRevisionRepo
|
|
.createQueryBuilder("orgRevision")
|
|
.where("orgRevision.orgRevisionIsDraft = false")
|
|
.andWhere("orgRevision.orgRevisionIsCurrent = true")
|
|
.getOne();
|
|
if (!orgRevisionPublish) {
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง");
|
|
}
|
|
|
|
const root =
|
|
profile.current_holders == null ||
|
|
profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot == null
|
|
? null
|
|
: profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot;
|
|
|
|
const _profile: any = {
|
|
profileId: profile.id,
|
|
prefix: profile.prefix,
|
|
rank: profile.rank,
|
|
avatar: profile.avatar,
|
|
avatarName: profile.avatarName,
|
|
firstName: profile.firstName,
|
|
lastName: profile.lastName,
|
|
citizenId: profile.citizenId,
|
|
birthDate: profile.birthDate,
|
|
position: profile.position,
|
|
rootId: root == null ? null : root.id,
|
|
root: root == null ? null : root.orgRootName,
|
|
rootShortName: root == null ? null : root.orgRootShortName,
|
|
};
|
|
return new HttpSuccess(_profile);
|
|
}
|
|
|
|
/**
|
|
* 8. หา root OC Id
|
|
*
|
|
* @summary 8. หา root OC Id
|
|
*
|
|
* @param {string} ocId Id หน่วยงาน
|
|
*/
|
|
@Get("root-oc/{ocId}")
|
|
async GetRootOcId(@Path() ocId: string) {
|
|
const orgRoot = await this.orgRootRepo.findOne({
|
|
where: { id: ocId },
|
|
});
|
|
if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
const root = orgRoot? orgRoot.id : "";
|
|
return new HttpSuccess(root);
|
|
}
|
|
|
|
/**
|
|
* 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว
|
|
*
|
|
* @summary 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว
|
|
*
|
|
*/
|
|
@Get("keycloak")
|
|
async GetProfileWithKeycloak() {
|
|
const profile = await this.profileRepo.find({
|
|
where: { keycloak: Not(IsNull()) || Not(""), },
|
|
});
|
|
return new HttpSuccess(profile);
|
|
}
|
|
|
|
/**
|
|
* 4. API Update รอบการลงเวลา ในตาราง profile
|
|
*
|
|
* @summary 4. API Update รอบการลงเวลา ในตาราง profile
|
|
*
|
|
*/
|
|
@Put("update-dutytime")
|
|
async UpdateDutyTimeAsync(
|
|
@Request() req: RequestWithUser,
|
|
@Body() body: {
|
|
profileId: string;
|
|
roundId: string;
|
|
effectiveDate: Date;
|
|
}
|
|
) {
|
|
const profile = await this.profileRepo.findOne({
|
|
where: { id: body.profileId },
|
|
});
|
|
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
Object.assign(profile, body);
|
|
profile.dutyTimeId = body.roundId;
|
|
profile.dutyTimeEffectiveDate = body.effectiveDate;
|
|
profile.lastUpdateUserId = req.user.sub;
|
|
profile.lastUpdateFullName = req.user.name;
|
|
await this.profileRepo.save(profile);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
}
|