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

422 lines
14 KiB
TypeScript

import {
Body,
Controller,
Delete,
Example,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import {
CreateChildren,
CreateProfileFamilyEmployee,
ProfileFamilyHistory,
UpdateProfileFamily,
} from "../entities/ProfileFamily";
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 { ProfileChildren } from "../entities/ProfileChildren";
import { ProfileChildrenHistory } from "../entities/ProfileChildrenHistory";
@Route("api/v1/org/profile-employee/family")
@Tags("ProfileFamilyHistoryEmployee")
@Security("bearerAuth")
export class ProfileFamilyHistoryEmployeeController extends Controller {
private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee);
private familyHistoryRepo = AppDataSource.getRepository(ProfileFamilyHistory);
private childrenRepo = AppDataSource.getRepository(ProfileChildren);
private childrenHistoryRepo = AppDataSource.getRepository(ProfileChildrenHistory);
@Get("user")
public async getFamilyHistoryUser(@Request() request: { user: Record<string, any> }) {
const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const family = await this.familyHistoryRepo.find({
take: 1,
order: { lastUpdatedAt: "DESC" },
where: { profileEmployeeId: profile.id },
});
const children = await this.childrenRepo.find({
order: { createdAt: "ASC" },
where: { profileEmployeeId: profile.id },
});
return new HttpSuccess(
family.length > 0
? {
...family[0],
children,
}
: null,
);
}
@Get("{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: "สาวิตรี ศรีสมัย",
couple: true,
couplePrefix: "string",
coupleFirstName: "string",
coupleLastName: "string",
coupleLastNameOld: "string",
coupleCareer: "string",
coupleCitizenId: "string",
coupleLive: true,
fatherPrefix: "string",
fatherFirstName: "string",
fatherLastName: "string",
fatherCareer: "string",
fatherCitizenId: "string",
fatherLive: true,
motherPrefix: "string",
motherFirstName: "string",
motherLastName: "string",
motherCareer: "string",
motherCitizenId: "string",
motherLive: true,
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
children: [
{
id: "9717f95a-cd20-4edb-a8b9-443c1dd99f90",
createdAt: "2024-03-19T11:00:29.785Z",
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
lastUpdatedAt: "2024-03-19T11:00:29.785Z",
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
createdFullName: "สาวิตรี ศรีสมัย",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
childrenCareer: "string",
childrenFirstName: "string",
childrenLastName: "string",
childrenPrefix: "string",
childrenLive: "string",
childrenCitizenId: "string",
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
},
],
},
})
public async getFamilyHistory(@Path() profileEmployeeId: string) {
const family = await this.familyHistoryRepo.find({
take: 1,
order: { lastUpdatedAt: "DESC" },
where: { profileEmployeeId },
});
const children = await this.childrenRepo.find({
order: { createdAt: "ASC" },
where: { profileEmployeeId },
});
return new HttpSuccess(
family.length > 0
? {
...family[0],
children,
}
: null,
);
}
/**
*
* @summary ประวัติแก้ไขครอบครัว by keycloak
*
*/
@Get("history/user")
public async familyHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const family = await this.familyHistoryRepo.find({
order: { lastUpdatedAt: "DESC" },
where: {
profileEmployeeId: profile.id,
},
});
family.shift();
const record = await Promise.all(
family.map(async (v) => ({
...v,
children: await this.childrenHistoryRepo.find({
order: { createdAt: "ASC" },
}),
})),
);
return new HttpSuccess(record);
}
@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: "สาวิตรี ศรีสมัย",
couple: true,
couplePrefix: "string",
coupleFirstName: "string",
coupleLastName: "string",
coupleLastNameOld: "string",
coupleCareer: "string",
coupleCitizenId: "string",
coupleLive: true,
fatherPrefix: "string",
fatherFirstName: "string",
fatherLastName: "string",
fatherCareer: "string",
fatherCitizenId: "string",
fatherLive: true,
motherPrefix: "string",
motherFirstName: "string",
motherLastName: "string",
motherCareer: "string",
motherCitizenId: "string",
motherLive: true,
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
children: [
{
id: "980835f8-d766-4b6b-b7dc-1726db889352",
createdAt: "2024-03-19T12:42:11.437Z",
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
lastUpdatedAt: "2024-03-19T12:42:11.437Z",
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
createdFullName: "สาวิตรี ศรีสมัย",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
childrenCareer: "hey",
childrenFirstName: "hey",
childrenLastName: "hey",
childrenPrefix: "hey",
childrenLive: "hey",
childrenCitizenId: "hey",
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
profileFamilyHistoryId: "6207ae29-05ef-4abb-9a37-a887265d671e",
profileChildrenId: "222db098-3f08-4d0b-ac58-7914fa41032b",
},
{
id: "cd539e67-3f34-4b8c-a9da-b7eb193fa0b4",
createdAt: "2024-03-19T12:42:11.438Z",
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
lastUpdatedAt: "2024-03-19T12:42:11.438Z",
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
createdFullName: "สาวิตรี ศรีสมัย",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
childrenCareer: "ay",
childrenFirstName: "ay",
childrenLastName: "ay",
childrenPrefix: "ay",
childrenLive: "ay",
childrenCitizenId: "ay",
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
profileFamilyHistoryId: "6207ae29-05ef-4abb-9a37-a887265d671e",
profileChildrenId: "d0495491-9e9f-4d2e-bc3b-c2833955fa12",
},
],
},
{
id: "60bbf8d1-f674-4d88-b3fb-b5603e408319",
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: "สาวิตรี ศรีสมัย",
couple: true,
couplePrefix: "string",
coupleFirstName: "string",
coupleLastName: "string",
coupleLastNameOld: "string",
coupleCareer: "yeah",
coupleCitizenId: "yeah",
coupleLive: true,
fatherPrefix: "yeah",
fatherFirstName: "haaa",
fatherLastName: "yeah",
fatherCareer: "haaa",
fatherCitizenId: "yeah",
fatherLive: true,
motherPrefix: "string",
motherFirstName: "string",
motherLastName: "string",
motherCareer: "string",
motherCitizenId: "string",
motherLive: true,
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
children: [],
},
],
})
public async familyHistory(@Path() profileEmployeeId: string) {
const family = await this.familyHistoryRepo.find({
order: { lastUpdatedAt: "DESC" },
where: { profileEmployeeId },
});
family.shift();
const record = await Promise.all(
family.map(async (v) => ({
...v,
children: await this.childrenHistoryRepo.find({
order: { createdAt: "ASC" },
// where: { profileFamilyHistoryId: v.id },
}),
})),
);
return new HttpSuccess(record);
}
@Post()
public async newFamilyHistory(
@Request() req: RequestWithUser,
@Body() body: CreateProfileFamilyEmployee,
) {
if (!body.profileEmployeeId) {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId");
}
const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const dataFamily = new ProfileFamilyHistory();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
const { children, ...family } = body;
Object.assign(dataFamily, { ...family, ...meta });
await this.familyHistoryRepo.save(dataFamily);
const profileEmployeeId = dataFamily.profileEmployeeId;
await Promise.all(
children.map(async (v) => {
const dataChildren = new ProfileChildrenHistory();
Object.assign(dataChildren, { ...v, profileEmployeeId, ...meta });
await this.childrenRepo.save(dataChildren);
}),
);
return new HttpSuccess();
}
@Patch("{profileEmployeeId}")
public async editFamilyHistory(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileFamily,
@Path() profileEmployeeId: string,
) {
const familyRecord = (
await this.familyHistoryRepo.find({
take: 1,
order: { lastUpdatedAt: "DESC" },
where: { profileEmployeeId },
})
)[0];
if (!familyRecord) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const childrenRecord = await this.childrenRepo.findBy({
profileEmployeeId: familyRecord.profileEmployeeId,
});
const historyData = new ProfileFamilyHistory();
const { children, ...family } = body;
Object.assign(historyData, { ...familyRecord, ...family, id: undefined });
historyData.lastUpdateFullName = req.user.name;
historyData.lastUpdateFullName = req.user.name;
const newChild: CreateChildren[] = [];
for (let child of children) {
newChild.push(
Object.assign(new ProfileChildren(), {
...child,
profileEmployeeId,
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
id: !childrenRecord.find((v) => v.id === child.id) ? undefined : child.id,
}),
);
}
await Promise.all([
this.familyHistoryRepo.save({ ...historyData, lastUpdatedAt: undefined }),
...newChild.map(async (v) => {
return await this.childrenRepo.save(v);
}),
...childrenRecord.map(async (v) => {
if (!children.find((x) => x.id === v.id)) {
return await this.childrenRepo.delete({ id: v.id });
}
}),
...childrenRecord.map(async (v) => {
if (children.find((x) => x.id === v.id)) {
return await this.childrenHistoryRepo.save(
this.childrenHistoryRepo.create({
...v,
// profileFamilyHistoryId: familyRecord.id,
profileChildrenId: v.id,
id: undefined,
}),
);
}
}),
]);
return new HttpSuccess();
}
@Delete("{profileEmployeeId}")
public async deleteFamilyHistory(@Path() profileEmployeeId: string) {
const result = await this.familyHistoryRepo.delete({ id: profileEmployeeId });
if (result.affected == undefined || result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}