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";
|
} from "tsoa";
|
||||||
import { AppDataSource } from "../database/data-source";
|
import { AppDataSource } from "../database/data-source";
|
||||||
import {
|
import {
|
||||||
|
CreateChildren,
|
||||||
CreateProfileFamily,
|
CreateProfileFamily,
|
||||||
ProfileChildren,
|
ProfileChildren,
|
||||||
ProfileChildrenHistory,
|
ProfileChildrenHistory,
|
||||||
|
|
@ -118,11 +119,12 @@ export class ProfileFamilyHistoryController extends Controller {
|
||||||
})
|
})
|
||||||
public async familyHistory(@Path() profileId: string) {
|
public async familyHistory(@Path() profileId: string) {
|
||||||
const family = await this.familyHistoryRepo.find({
|
const family = await this.familyHistoryRepo.find({
|
||||||
skip: 1,
|
|
||||||
order: { lastUpdatedAt: "DESC" },
|
order: { lastUpdatedAt: "DESC" },
|
||||||
where: { profileId },
|
where: { profileId },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
family.pop();
|
||||||
|
|
||||||
const record = await Promise.all(
|
const record = await Promise.all(
|
||||||
family.map(async (v) => ({
|
family.map(async (v) => ({
|
||||||
...v,
|
...v,
|
||||||
|
|
@ -196,20 +198,49 @@ export class ProfileFamilyHistoryController extends Controller {
|
||||||
|
|
||||||
if (!familyRecord) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
if (!familyRecord) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
|
||||||
const childrenRecord = await this.familyHistoryRepo.findBy({
|
const childrenRecord = await this.childrenRepo.findBy({
|
||||||
profileId: familyRecord.profileId,
|
profileId: familyRecord.profileId,
|
||||||
});
|
});
|
||||||
|
|
||||||
const historyData = new ProfileFamilyHistory();
|
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;
|
||||||
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([
|
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) => {
|
...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({
|
this.childrenHistoryRepo.create({
|
||||||
...v,
|
...v,
|
||||||
profileFamilyHistoryId: familyRecord.id,
|
profileFamilyHistoryId: familyRecord.id,
|
||||||
|
|
|
||||||
|
|
@ -261,7 +261,7 @@ export class ProfileChildrenHistory extends ProfileChildren {
|
||||||
profileChildren: ProfileChildren;
|
profileChildren: ProfileChildren;
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateChildren = {
|
export type CreateChildren = {
|
||||||
isActive: boolean;
|
isActive: boolean;
|
||||||
childrenCareer: string;
|
childrenCareer: string;
|
||||||
childrenFirstName: string;
|
childrenFirstName: string;
|
||||||
|
|
@ -271,7 +271,7 @@ type CreateChildren = {
|
||||||
childrenCitizenId: string;
|
childrenCitizenId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type UpdateChildren = {
|
export type UpdateChildren = {
|
||||||
id: string;
|
id: string;
|
||||||
isActive?: boolean | null;
|
isActive?: boolean | null;
|
||||||
childrenCareer?: string | null;
|
childrenCareer?: string | null;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue