196 lines
7 KiB
TypeScript
196 lines
7 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";
|
|
|
|
@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("{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 },
|
|
});
|
|
|
|
return new HttpSuccess(familyFather);
|
|
}
|
|
|
|
@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,
|
|
) {
|
|
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.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,
|
|
) {
|
|
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;
|
|
|
|
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();
|
|
}
|
|
|
|
}
|