Family Controller
This commit is contained in:
parent
0df3191fcc
commit
702ed615ea
6 changed files with 1208 additions and 0 deletions
207
src/controllers/ProfileFamilyCoupleEmployeeController.ts
Normal file
207
src/controllers/ProfileFamilyCoupleEmployeeController.ts
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
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 { ProfileFamilyCouple, CreateProfileEmployeeFamilyCouple, UpdateProfileFamilyCouple } from "../entities/ProfileFamilyCouple";
|
||||
import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHistory";
|
||||
|
||||
@Route("api/v1/org/profile-employee/family/couple")
|
||||
@Tags("ProfileEmployeeFamilyCouple")
|
||||
@Security("bearerAuth")
|
||||
export class ProfileFamilyCoupleEmployeeController extends Controller {
|
||||
private profileRepo = AppDataSource.getRepository(ProfileEmployee);
|
||||
private ProfileFamilyCouple = AppDataSource.getRepository(ProfileFamilyCouple);
|
||||
private ProfileFamilyCoupleHistory = AppDataSource.getRepository(ProfileFamilyCoupleHistory);
|
||||
|
||||
@Get("{profileEmployeeId}")
|
||||
@Example({
|
||||
status: 200,
|
||||
message: "สำเร็จ",
|
||||
result: {
|
||||
id: "6207ae29-05ef-4abb-9a37-a887265d671e",
|
||||
couple: true,
|
||||
couplePrefix: "string",
|
||||
coupleFirstName: "string",
|
||||
coupleLastName: "string",
|
||||
coupleLastNameOld: "string",
|
||||
coupleCareer: "string",
|
||||
coupleCitizenId: "string",
|
||||
coupleLive: true,
|
||||
relationship: "string",
|
||||
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
|
||||
},
|
||||
})
|
||||
public async getFamilyCouple(@Path() profileEmployeeId: string) {
|
||||
const profile = await this.profileRepo.findOne({
|
||||
where: { id: profileEmployeeId }
|
||||
})
|
||||
|
||||
if (!profile) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||
}
|
||||
const familyCouple = await this.ProfileFamilyCouple.findOne({
|
||||
select: [
|
||||
"id", "couple", "couplePrefix", "coupleFirstName", "coupleLastName", "coupleLastNameOld",
|
||||
"coupleCareer", "coupleCitizenId", "coupleLive", "relationship", "profileEmployeeId",
|
||||
],
|
||||
where: { profileEmployeeId },
|
||||
});
|
||||
|
||||
return new HttpSuccess(familyCouple);
|
||||
}
|
||||
|
||||
@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,
|
||||
relationship: "string",
|
||||
profileFamilyCoupleId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
|
||||
profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
|
||||
}
|
||||
],
|
||||
})
|
||||
public async familyCoupleHistory(@Path() profileEmployeeId: string) {
|
||||
const profile = await this.profileRepo.findOne({
|
||||
where: { id: profileEmployeeId }
|
||||
})
|
||||
if (!profile) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||
}
|
||||
|
||||
const familyCouple = await this.ProfileFamilyCouple.find({
|
||||
relations: ["histories"],
|
||||
order: { lastUpdatedAt: "DESC" },
|
||||
where: { profileEmployeeId: profileEmployeeId},
|
||||
});
|
||||
|
||||
const mapData = familyCouple.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,
|
||||
couple: y.couple,
|
||||
couplePrefix: y.couplePrefix,
|
||||
coupleFirstName: y.coupleFirstName,
|
||||
coupleLastName: y.coupleLastName,
|
||||
coupleLastNameOld: y.coupleLastNameOld,
|
||||
coupleCareer: y.coupleCareer,
|
||||
coupleLive: y.coupleLive,
|
||||
relationship: y.relationship,
|
||||
profileFamilyCoupleId: y.profileFamilyCoupleId,
|
||||
profileEmployeeId: profileEmployeeId,
|
||||
}));
|
||||
|
||||
return new HttpSuccess(mapData);
|
||||
}
|
||||
|
||||
@Post()
|
||||
public async FamilyCouple(
|
||||
@Request() req: RequestWithUser,
|
||||
@Body() body: CreateProfileEmployeeFamilyCouple,
|
||||
) {
|
||||
const familyCouple = Object.assign(new ProfileFamilyCouple(), body);
|
||||
if (!familyCouple) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
const profile = await this.profileRepo.findOneBy({ id: body.profileEmployeeId });
|
||||
if (!profile) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||
}
|
||||
familyCouple.createdUserId = req.user.sub;
|
||||
familyCouple.createdFullName = req.user.name;
|
||||
familyCouple.lastUpdateUserId = req.user.sub;
|
||||
familyCouple.lastUpdateFullName = req.user.name;
|
||||
await this.ProfileFamilyCouple.save(familyCouple);
|
||||
|
||||
if (familyCouple) {
|
||||
const history: ProfileFamilyCoupleHistory = Object.assign(new ProfileFamilyCoupleHistory(), {
|
||||
profileFamilyCoupleId: familyCouple.id,
|
||||
couple: familyCouple.couple,
|
||||
couplePrefix: familyCouple.couplePrefix,
|
||||
coupleFirstName: familyCouple.coupleFirstName,
|
||||
coupleLastName: familyCouple.coupleLastName,
|
||||
coupleLastNameOld: familyCouple.coupleLastNameOld,
|
||||
coupleCareer: familyCouple.coupleCareer,
|
||||
coupleLive: familyCouple.coupleLive,
|
||||
relationship: familyCouple.relationship,
|
||||
createdUserId: req.user.sub,
|
||||
createdFullName: req.user.name,
|
||||
lastUpdateUserId: req.user.sub,
|
||||
lastUpdateFullName: req.user.name,
|
||||
});
|
||||
await this.ProfileFamilyCoupleHistory.save(history);
|
||||
}
|
||||
return new HttpSuccess(familyCouple.id);
|
||||
}
|
||||
|
||||
@Patch("{profileEmployeeId}")
|
||||
public async editFamilyCouple(
|
||||
@Request() req: RequestWithUser,
|
||||
@Body() body: UpdateProfileFamilyCouple,
|
||||
@Path() profileEmployeeId: string,
|
||||
) {
|
||||
const familyCouple = await this.ProfileFamilyCouple.findOneBy({ profileEmployeeId: profileEmployeeId });
|
||||
if (!familyCouple) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
|
||||
const history = new ProfileFamilyCoupleHistory();
|
||||
Object.assign(history, { ...familyCouple, id: undefined });
|
||||
Object.assign(familyCouple, body);
|
||||
familyCouple.lastUpdateUserId = req.user.sub,
|
||||
familyCouple.lastUpdateFullName = req.user.name;
|
||||
history.profileFamilyCoupleId = familyCouple.id;
|
||||
history.couple = familyCouple.couple,
|
||||
history.couplePrefix = familyCouple.couplePrefix,
|
||||
history.coupleFirstName = familyCouple.coupleFirstName,
|
||||
history.coupleLastName = familyCouple.coupleLastName,
|
||||
history.coupleLastNameOld = familyCouple.coupleLastNameOld,
|
||||
history.coupleCareer = familyCouple.coupleCareer,
|
||||
history.coupleLive = familyCouple.coupleLive,
|
||||
history.relationship = familyCouple.relationship,
|
||||
history.lastUpdateUserId = req.user.sub,
|
||||
history.lastUpdateFullName = req.user.name;
|
||||
|
||||
await Promise.all([
|
||||
this.ProfileFamilyCouple.save(familyCouple),
|
||||
this.ProfileFamilyCoupleHistory.save(history),
|
||||
]);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue