198 lines
7.3 KiB
TypeScript
198 lines
7.3 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 { ProfileFamilyMother, CreateProfileEmployeeFamilyMother, UpdateProfileFamilyMother } from "../entities/ProfileFamilyMother";
|
|
import { ProfileFamilyMotherHistory } from "../entities/ProfileFamilyMotherHistory";
|
|
import Extension from "../interfaces/extension";
|
|
@Route("api/v1/org/profile-employee/family/mother")
|
|
@Tags("ProfileEmployeeFamilyMother")
|
|
@Security("bearerAuth")
|
|
export class ProfileFamilyMotherEmployeeController extends Controller {
|
|
private profileRepo = AppDataSource.getRepository(ProfileEmployee);
|
|
private ProfileFamilyMother = AppDataSource.getRepository(ProfileFamilyMother);
|
|
private ProfileFamilyMotherHistory = AppDataSource.getRepository(ProfileFamilyMotherHistory);
|
|
|
|
@Get("{profileEmployeeId}")
|
|
@Example({
|
|
status: 200,
|
|
message: "สำเร็จ",
|
|
result: {
|
|
id: "6207ae29-05ef-4abb-9a37-a887265d671e",
|
|
motherPrefix: "string",
|
|
motherFirstName: "string",
|
|
motherLastName: "string",
|
|
motherCareer: "string",
|
|
motherCitizenId: "string",
|
|
motherLive: true,
|
|
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
|
|
},
|
|
})
|
|
public async getFamilyMother(@Path() profileEmployeeId: string) {
|
|
const profile = await this.profileRepo.findOne({
|
|
where: { id: profileEmployeeId }
|
|
})
|
|
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
}
|
|
const familyMother = await this.ProfileFamilyMother.findOne({
|
|
select: [
|
|
"id", "motherPrefix", "motherFirstName", "motherLastName",
|
|
"motherCareer", "motherCitizenId", "motherLive", "profileEmployeeId",
|
|
],
|
|
where: { profileEmployeeId },
|
|
order: { lastUpdatedAt: "DESC" },
|
|
});
|
|
|
|
return new HttpSuccess(familyMother);
|
|
}
|
|
|
|
@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: "สาวิตรี ศรีสมัย",
|
|
motherPrefix: "string",
|
|
motherFirstName: "string",
|
|
motherLastName: "string",
|
|
motherCareer: "string",
|
|
motherCitizenId: "string",
|
|
motherLive: true,
|
|
profileFamilyMotherId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
|
|
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
|
|
}
|
|
],
|
|
})
|
|
public async familyMotherHistory(@Path() profileEmployeeId: string) {
|
|
const profile = await this.profileRepo.findOne({
|
|
where: { id: profileEmployeeId }
|
|
})
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
}
|
|
|
|
const familyMother = await this.ProfileFamilyMother.find({
|
|
relations: ["histories"],
|
|
order: { lastUpdatedAt: "DESC" },
|
|
where: { profileEmployeeId: profileEmployeeId},
|
|
});
|
|
|
|
const mapData = familyMother.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,
|
|
motherPrefix: y.motherPrefix,
|
|
motherFirstName: y.motherFirstName,
|
|
motherLastName: y.motherLastName,
|
|
motherCareer: y.motherCareer,
|
|
motherCitizenId: y.motherCitizenId,
|
|
motherLive: y.motherLive,
|
|
profileFamilyMotherId: y.profileFamilyMotherId,
|
|
profileEmployeeId: profileEmployeeId,
|
|
}));
|
|
|
|
return new HttpSuccess(mapData);
|
|
}
|
|
|
|
@Post()
|
|
public async FamilyMother(
|
|
@Request() req: RequestWithUser,
|
|
@Body() body: CreateProfileEmployeeFamilyMother,
|
|
) {
|
|
const familyMother = Object.assign(new ProfileFamilyMother(), body);
|
|
if (!familyMother) {
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
}
|
|
const profile = await this.profileRepo.findOneBy({ id: body.profileEmployeeId });
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
}
|
|
familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId));
|
|
familyMother.createdUserId = req.user.sub;
|
|
familyMother.createdFullName = req.user.name;
|
|
familyMother.lastUpdateUserId = req.user.sub;
|
|
familyMother.lastUpdateFullName = req.user.name;
|
|
await this.ProfileFamilyMother.save(familyMother);
|
|
|
|
if (familyMother) {
|
|
const history: ProfileFamilyMotherHistory = Object.assign(new ProfileFamilyMotherHistory(), {
|
|
profileFamilyMotherId: familyMother.id,
|
|
motherPrefix: familyMother.motherPrefix,
|
|
motherFirstName: familyMother.motherFirstName,
|
|
motherLastName: familyMother.motherLastName,
|
|
motherCareer: familyMother.motherCareer,
|
|
motherCitizenId: familyMother.motherCitizenId,
|
|
motherLive: familyMother.motherLive,
|
|
createdUserId: req.user.sub,
|
|
createdFullName: req.user.name,
|
|
lastUpdateUserId: req.user.sub,
|
|
lastUpdateFullName: req.user.name,
|
|
});
|
|
await this.ProfileFamilyMotherHistory.save(history);
|
|
}
|
|
return new HttpSuccess(familyMother.id);
|
|
}
|
|
|
|
@Patch("{profileEmployeeId}")
|
|
public async editFamilyMother(
|
|
@Request() req: RequestWithUser,
|
|
@Body() body: UpdateProfileFamilyMother,
|
|
@Path() profileEmployeeId: string,
|
|
) {
|
|
const familyMother = await this.ProfileFamilyMother.findOneBy({ profileEmployeeId: profileEmployeeId });
|
|
if (!familyMother) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
const history = new ProfileFamilyMotherHistory();
|
|
Object.assign(history, { ...familyMother, id: undefined });
|
|
Object.assign(familyMother, body);
|
|
familyMother.lastUpdateUserId = req.user.sub,
|
|
familyMother.lastUpdateFullName = req.user.name;
|
|
familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId));
|
|
history.profileFamilyMotherId = familyMother.id;
|
|
history.motherPrefix = familyMother.motherPrefix,
|
|
history.motherFirstName = familyMother.motherFirstName,
|
|
history.motherLastName = familyMother.motherLastName,
|
|
history.motherCareer = familyMother.motherCareer,
|
|
history.motherCitizenId = familyMother.motherCitizenId,
|
|
history.motherLive = familyMother.motherLive,
|
|
history.lastUpdateUserId = req.user.sub,
|
|
history.lastUpdateFullName = req.user.name;
|
|
|
|
await Promise.all([
|
|
this.ProfileFamilyMother.save(familyMother),
|
|
this.ProfileFamilyMotherHistory.save(history),
|
|
]);
|
|
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
}
|