Merge branch 'dev/methapon' into develop
This commit is contained in:
commit
36e0c907b8
1 changed files with 236 additions and 0 deletions
236
src/controllers/ProfileFamilyHistoryController.ts
Normal file
236
src/controllers/ProfileFamilyHistoryController.ts
Normal file
|
|
@ -0,0 +1,236 @@
|
||||||
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Delete,
|
||||||
|
Example,
|
||||||
|
Get,
|
||||||
|
Patch,
|
||||||
|
Path,
|
||||||
|
Post,
|
||||||
|
Request,
|
||||||
|
Route,
|
||||||
|
Security,
|
||||||
|
Tags,
|
||||||
|
} from "tsoa";
|
||||||
|
import { AppDataSource } from "../database/data-source";
|
||||||
|
import {
|
||||||
|
CreateProfileFamily,
|
||||||
|
ProfileChildren,
|
||||||
|
ProfileChildrenHistory,
|
||||||
|
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 { Profile } from "../entities/Profile";
|
||||||
|
|
||||||
|
@Route("api/v1/org/profile/familyHistory")
|
||||||
|
@Tags("ProfileFamilyHistory")
|
||||||
|
@Security("bearerAuth")
|
||||||
|
export class ProfileFamilyHistoryController extends Controller {
|
||||||
|
private profileRepo = AppDataSource.getRepository(Profile);
|
||||||
|
private familyHistoryRepo = AppDataSource.getRepository(ProfileFamilyHistory);
|
||||||
|
private childrenRepo = AppDataSource.getRepository(ProfileChildren);
|
||||||
|
private childrenHistoryRepo = AppDataSource.getRepository(ProfileChildrenHistory);
|
||||||
|
|
||||||
|
@Get("{profileId}")
|
||||||
|
@Example({
|
||||||
|
status: 200,
|
||||||
|
message: "สำเร็จ",
|
||||||
|
result: {
|
||||||
|
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: "สาวิตรี ศรีสมัย",
|
||||||
|
isActive: true,
|
||||||
|
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,
|
||||||
|
profileId: "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: "สาวิตรี ศรีสมัย",
|
||||||
|
isActive: true,
|
||||||
|
childrenCareer: "string",
|
||||||
|
childrenFirstName: "string",
|
||||||
|
childrenLastName: "string",
|
||||||
|
childrenPrefix: "string",
|
||||||
|
childrenLive: "string",
|
||||||
|
childrenCitizenId: "string",
|
||||||
|
profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
public async getFamilyHistory(@Path() profileId: string) {
|
||||||
|
const family = await this.familyHistoryRepo.find({
|
||||||
|
take: 1,
|
||||||
|
order: { createdAt: "DESC" },
|
||||||
|
where: { profileId },
|
||||||
|
});
|
||||||
|
const children = await this.childrenRepo.find({
|
||||||
|
order: { createdAt: "DESC" },
|
||||||
|
where: { profileId },
|
||||||
|
});
|
||||||
|
return new HttpSuccess({
|
||||||
|
...family[0],
|
||||||
|
children,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get("history/{profileId}")
|
||||||
|
@Example({
|
||||||
|
status: 200,
|
||||||
|
message: "สำเร็จ",
|
||||||
|
result: [],
|
||||||
|
})
|
||||||
|
public async familyHistory(@Path() profileId: string) {
|
||||||
|
const family = await this.familyHistoryRepo.find({
|
||||||
|
skip: 1,
|
||||||
|
order: { lastUpdatedAt: "DESC" },
|
||||||
|
where: { profileId },
|
||||||
|
});
|
||||||
|
|
||||||
|
const record = await Promise.all(
|
||||||
|
family.map(async (v) => ({
|
||||||
|
...v,
|
||||||
|
children: await this.childrenHistoryRepo.findBy({
|
||||||
|
profileFamilyHistoryId: v.id,
|
||||||
|
}),
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
|
return new HttpSuccess(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
public async newFamilyHistory(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Body() body: CreateProfileFamily,
|
||||||
|
) {
|
||||||
|
if (!body.profileId) {
|
||||||
|
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
|
||||||
|
}
|
||||||
|
|
||||||
|
const profile = await this.profileRepo.findOneBy({ id: body.profileId });
|
||||||
|
|
||||||
|
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 profileId = dataFamily.profileId;
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
children.map(async (v) => {
|
||||||
|
const dataChildren = new ProfileChildrenHistory();
|
||||||
|
|
||||||
|
Object.assign(dataChildren, { ...v, profileId, ...meta });
|
||||||
|
|
||||||
|
await this.childrenRepo.save(dataChildren);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Patch("{profileId}")
|
||||||
|
public async editFamilyHistory(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Body() body: UpdateProfileFamily,
|
||||||
|
@Path() profileId: string,
|
||||||
|
) {
|
||||||
|
const familyRecord = (
|
||||||
|
await this.familyHistoryRepo.find({
|
||||||
|
take: 1,
|
||||||
|
order: { lastUpdatedAt: "DESC" },
|
||||||
|
where: { profileId },
|
||||||
|
})
|
||||||
|
)[0];
|
||||||
|
|
||||||
|
if (!familyRecord) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
|
||||||
|
const childrenRecord = await this.familyHistoryRepo.findBy({
|
||||||
|
profileId: familyRecord.profileId,
|
||||||
|
});
|
||||||
|
|
||||||
|
const historyData = new ProfileFamilyHistory();
|
||||||
|
|
||||||
|
Object.assign(historyData, { ...familyRecord, ...body, id: undefined });
|
||||||
|
historyData.lastUpdateFullName = req.user.name;
|
||||||
|
historyData.lastUpdateFullName = req.user.name;
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
this.familyHistoryRepo.save(familyRecord),
|
||||||
|
...childrenRecord.map(async (v) => {
|
||||||
|
await this.childrenHistoryRepo.save(
|
||||||
|
this.childrenHistoryRepo.create({
|
||||||
|
...v,
|
||||||
|
profileFamilyHistoryId: familyRecord.id,
|
||||||
|
profileChildrenId: v.id,
|
||||||
|
id: undefined,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete("{profileId}")
|
||||||
|
public async deleteFamilyHistory(@Path() profileId: string) {
|
||||||
|
const result = await this.familyHistoryRepo.delete({ id: profileId });
|
||||||
|
|
||||||
|
if (result.affected && result.affected <= 0) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue