feat: complete family history endpoint
This commit is contained in:
parent
5a63ac36bd
commit
88b567070a
2 changed files with 38 additions and 7 deletions
|
|
@ -14,6 +14,7 @@ import {
|
|||
} from "tsoa";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import {
|
||||
CreateChildren,
|
||||
CreateProfileFamily,
|
||||
ProfileChildren,
|
||||
ProfileChildrenHistory,
|
||||
|
|
@ -118,11 +119,12 @@ export class ProfileFamilyHistoryController extends Controller {
|
|||
})
|
||||
public async familyHistory(@Path() profileId: string) {
|
||||
const family = await this.familyHistoryRepo.find({
|
||||
skip: 1,
|
||||
order: { lastUpdatedAt: "DESC" },
|
||||
where: { profileId },
|
||||
});
|
||||
|
||||
family.pop();
|
||||
|
||||
const record = await Promise.all(
|
||||
family.map(async (v) => ({
|
||||
...v,
|
||||
|
|
@ -196,20 +198,49 @@ export class ProfileFamilyHistoryController extends Controller {
|
|||
|
||||
if (!familyRecord) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
|
||||
const childrenRecord = await this.familyHistoryRepo.findBy({
|
||||
const childrenRecord = await this.childrenRepo.findBy({
|
||||
profileId: familyRecord.profileId,
|
||||
});
|
||||
|
||||
const historyData = new ProfileFamilyHistory();
|
||||
|
||||
Object.assign(historyData, { ...familyRecord, ...body, id: undefined });
|
||||
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) {
|
||||
let match = childrenRecord.find((v) => v.id === child.id);
|
||||
|
||||
if (match) Object.assign(match, child);
|
||||
else {
|
||||
let newProfileChild = new ProfileChildren();
|
||||
Object.assign(newProfileChild, {
|
||||
...child,
|
||||
profileId,
|
||||
createdUserId: req.user.sub,
|
||||
createdFullName: req.user.name,
|
||||
lastUpdateUserId: req.user.sub,
|
||||
lastUpdateFullName: req.user.name,
|
||||
id: undefined,
|
||||
});
|
||||
newChild.push(newProfileChild);
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all([
|
||||
this.familyHistoryRepo.save(familyRecord),
|
||||
this.familyHistoryRepo.save(historyData),
|
||||
...newChild.map(async (v) => {
|
||||
return await this.childrenRepo.save(v);
|
||||
}),
|
||||
...childrenRecord.map(async (v) => {
|
||||
await this.childrenHistoryRepo.save(
|
||||
return await this.childrenRepo.save(v);
|
||||
}),
|
||||
...childrenRecord.map(async (v) => {
|
||||
return await this.childrenHistoryRepo.save(
|
||||
this.childrenHistoryRepo.create({
|
||||
...v,
|
||||
profileFamilyHistoryId: familyRecord.id,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue