279 lines
9.6 KiB
TypeScript
279 lines
9.6 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Example,
|
|
Get,
|
|
Patch,
|
|
Path,
|
|
Post,
|
|
Request,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
} 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 { ProfileEmployee } from "../entities/ProfileEmployee";
|
|
import {
|
|
ProfileFamilyFather,
|
|
CreateProfileEmployeeFamilyFather,
|
|
UpdateProfileFamilyFather,
|
|
} from "../entities/ProfileFamilyFather";
|
|
import { ProfileFamilyFatherHistory } from "../entities/ProfileFamilyFatherHistory";
|
|
import Extension from "../interfaces/extension";
|
|
import permission from "../interfaces/permission";
|
|
@Route("api/v1/org/profile-employee/family/father")
|
|
@Tags("ProfileEmployeeFamilyFather")
|
|
@Security("bearerAuth")
|
|
export class ProfileFamilyFatherEmployeeController extends Controller {
|
|
private profileRepo = AppDataSource.getRepository(ProfileEmployee);
|
|
private ProfileFamilyFather = AppDataSource.getRepository(ProfileFamilyFather);
|
|
private ProfileFamilyFatherHistory = AppDataSource.getRepository(ProfileFamilyFatherHistory);
|
|
|
|
@Get("user")
|
|
public async getFamilyFatherUser(@Request() request: { user: Record<string, any> }) {
|
|
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
}
|
|
const familyFather = await this.ProfileFamilyFather.findOne({
|
|
select: [
|
|
"id",
|
|
"fatherPrefix",
|
|
"fatherFirstName",
|
|
"fatherLastName",
|
|
"fatherCareer",
|
|
"fatherCitizenId",
|
|
"fatherLive",
|
|
"profileId",
|
|
],
|
|
where: { profileEmployeeId: profile.id },
|
|
order: { lastUpdatedAt: "DESC" },
|
|
});
|
|
|
|
return new HttpSuccess(familyFather);
|
|
}
|
|
|
|
@Get("{profileEmployeeId}")
|
|
@Example({
|
|
status: 200,
|
|
message: "สำเร็จ",
|
|
result: {
|
|
id: "6207ae29-05ef-4abb-9a37-a887265d671e",
|
|
fatherPrefix: "string",
|
|
fatherFirstName: "string",
|
|
fatherLastName: "string",
|
|
fatherCareer: "string",
|
|
fatherCitizenId: "string",
|
|
fatherLive: true,
|
|
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
|
|
},
|
|
})
|
|
public async getFamilyFather(@Path() profileEmployeeId: string) {
|
|
const profile = await this.profileRepo.findOne({
|
|
where: { id: profileEmployeeId },
|
|
});
|
|
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
}
|
|
const familyFather = await this.ProfileFamilyFather.findOne({
|
|
select: [
|
|
"id",
|
|
"fatherPrefix",
|
|
"fatherFirstName",
|
|
"fatherLastName",
|
|
"fatherCareer",
|
|
"fatherCitizenId",
|
|
"fatherLive",
|
|
"profileEmployeeId",
|
|
],
|
|
where: { profileEmployeeId },
|
|
order: { lastUpdatedAt: "DESC" },
|
|
});
|
|
|
|
return new HttpSuccess(familyFather);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @summary ประวัติแก้ไขครอบครัว by keycloak
|
|
*
|
|
*/
|
|
@Get("history/user")
|
|
public async familyFatherHistoryUser(@Request() request: RequestWithUser) {
|
|
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
}
|
|
|
|
const familyFather = await this.ProfileFamilyFather.find({
|
|
relations: ["histories"],
|
|
order: { lastUpdatedAt: "DESC" },
|
|
where: { profileEmployeeId: profile.id },
|
|
});
|
|
|
|
const mapData = familyFather
|
|
.flatMap((x) => x.histories)
|
|
.map((y) => ({
|
|
id: y.id,
|
|
createdAt: y.createdAt,
|
|
createdUserId: y.createdUserId,
|
|
lastUpdatedAt: y.lastUpdatedAt,
|
|
lastUpdateUserId: y.lastUpdateUserId,
|
|
createdFullName: y.createdFullName,
|
|
lastUpdateFullName: y.lastUpdateFullName,
|
|
fatherPrefix: y.fatherPrefix,
|
|
fatherFirstName: y.fatherFirstName,
|
|
fatherLastName: y.fatherLastName,
|
|
fatherCareer: y.fatherCareer,
|
|
fatherCitizenId: y.fatherCitizenId,
|
|
fatherLive: y.fatherLive,
|
|
profileFamilyFatherId: y.profileFamilyFatherId,
|
|
profileEmployeeId: profile.id,
|
|
}));
|
|
|
|
return new HttpSuccess(mapData);
|
|
}
|
|
|
|
@Get("history/{profileEmployeeId}")
|
|
@Example({
|
|
status: 200,
|
|
message: "สำเร็จ",
|
|
result: [
|
|
{
|
|
id: "6207ae29-05ef-4abb-9a37-a887265d671e",
|
|
createdAt: "2024-03-19T11:00:29.769Z",
|
|
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
|
|
lastUpdatedAt: "2024-03-19T11:00:29.769Z",
|
|
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
|
|
createdFullName: "สาวิตรี ศรีสมัย",
|
|
lastUpdateFullName: "สาวิตรี ศรีสมัย",
|
|
fatherPrefix: "string",
|
|
fatherFirstName: "string",
|
|
fatherLastName: "string",
|
|
fatherCareer: "string",
|
|
fatherCitizenId: "string",
|
|
fatherLive: true,
|
|
profileFamilyFatherId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
|
|
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
|
|
},
|
|
],
|
|
})
|
|
public async familyFatherHistory(@Path() profileEmployeeId: string) {
|
|
const profile = await this.profileRepo.findOne({
|
|
where: { id: profileEmployeeId },
|
|
});
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
}
|
|
|
|
const familyFather = await this.ProfileFamilyFather.find({
|
|
relations: ["histories"],
|
|
order: { lastUpdatedAt: "DESC" },
|
|
where: { profileEmployeeId: profileEmployeeId },
|
|
});
|
|
|
|
const mapData = familyFather
|
|
.flatMap((x) => x.histories)
|
|
.map((y) => ({
|
|
id: y.id,
|
|
createdAt: y.createdAt,
|
|
createdUserId: y.createdUserId,
|
|
lastUpdatedAt: y.lastUpdatedAt,
|
|
lastUpdateUserId: y.lastUpdateUserId,
|
|
createdFullName: y.createdFullName,
|
|
lastUpdateFullName: y.lastUpdateFullName,
|
|
fatherPrefix: y.fatherPrefix,
|
|
fatherFirstName: y.fatherFirstName,
|
|
fatherLastName: y.fatherLastName,
|
|
fatherCareer: y.fatherCareer,
|
|
fatherCitizenId: y.fatherCitizenId,
|
|
fatherLive: y.fatherLive,
|
|
profileFamilyFatherId: y.profileFamilyFatherId,
|
|
profileEmployeeId: profileEmployeeId,
|
|
}));
|
|
|
|
return new HttpSuccess(mapData);
|
|
}
|
|
|
|
@Post()
|
|
public async FamilyFather(
|
|
@Request() req: RequestWithUser,
|
|
@Body() body: CreateProfileEmployeeFamilyFather,
|
|
) {
|
|
await new permission().PermissionCreate(req,"SYS_REGISTRY_EMP");
|
|
const familyFather = Object.assign(new ProfileFamilyFather(), body);
|
|
if (!familyFather) {
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
}
|
|
const profile = await this.profileRepo.findOneBy({ id: body.profileEmployeeId });
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
}
|
|
familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId));
|
|
familyFather.createdUserId = req.user.sub;
|
|
familyFather.createdFullName = req.user.name;
|
|
familyFather.lastUpdateUserId = req.user.sub;
|
|
familyFather.lastUpdateFullName = req.user.name;
|
|
await this.ProfileFamilyFather.save(familyFather);
|
|
|
|
if (familyFather) {
|
|
const history: ProfileFamilyFatherHistory = Object.assign(new ProfileFamilyFatherHistory(), {
|
|
profileFamilyFatherId: familyFather.id,
|
|
fatherPrefix: familyFather.fatherPrefix,
|
|
fatherFirstName: familyFather.fatherFirstName,
|
|
fatherLastName: familyFather.fatherLastName,
|
|
fatherCareer: familyFather.fatherCareer,
|
|
fatherCitizenId: familyFather.fatherCitizenId,
|
|
fatherLive: familyFather.fatherLive,
|
|
createdUserId: req.user.sub,
|
|
createdFullName: req.user.name,
|
|
lastUpdateUserId: req.user.sub,
|
|
lastUpdateFullName: req.user.name,
|
|
});
|
|
await this.ProfileFamilyFatherHistory.save(history);
|
|
}
|
|
return new HttpSuccess(familyFather.id);
|
|
}
|
|
|
|
@Patch("{profileEmployeeId}")
|
|
public async editFamilyFather(
|
|
@Request() req: RequestWithUser,
|
|
@Body() body: UpdateProfileFamilyFather,
|
|
@Path() profileEmployeeId: string,
|
|
) {
|
|
await new permission().PermissionUpdate(req,"SYS_REGISTRY_EMP");
|
|
const familyFather = await this.ProfileFamilyFather.findOneBy({
|
|
profileEmployeeId: profileEmployeeId,
|
|
});
|
|
if (!familyFather) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
const history = new ProfileFamilyFatherHistory();
|
|
Object.assign(history, { ...familyFather, id: undefined });
|
|
Object.assign(familyFather, body);
|
|
(familyFather.lastUpdateUserId = req.user.sub),
|
|
(familyFather.lastUpdateFullName = req.user.name);
|
|
familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId));
|
|
history.profileFamilyFatherId = familyFather.id;
|
|
(history.fatherPrefix = familyFather.fatherPrefix),
|
|
(history.fatherFirstName = familyFather.fatherFirstName),
|
|
(history.fatherLastName = familyFather.fatherLastName),
|
|
(history.fatherCareer = familyFather.fatherCareer),
|
|
(history.fatherCitizenId = familyFather.fatherCitizenId),
|
|
(history.fatherLive = familyFather.fatherLive),
|
|
(history.lastUpdateUserId = req.user.sub),
|
|
(history.lastUpdateFullName = req.user.name);
|
|
|
|
await Promise.all([
|
|
this.ProfileFamilyFather.save(familyFather),
|
|
this.ProfileFamilyFatherHistory.save(history),
|
|
]);
|
|
|
|
return new HttpSuccess();
|
|
}
|
|
}
|