diff --git a/src/controllers/ProfileFamilyHistoryController.ts b/src/controllers/ProfileFamilyHistoryController.ts index b61ddc86..0ee69de9 100644 --- a/src/controllers/ProfileFamilyHistoryController.ts +++ b/src/controllers/ProfileFamilyHistoryController.ts @@ -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, diff --git a/src/entities/ProfileFamily.ts b/src/entities/ProfileFamily.ts index f7377b70..c939534a 100644 --- a/src/entities/ProfileFamily.ts +++ b/src/entities/ProfileFamily.ts @@ -261,7 +261,7 @@ export class ProfileChildrenHistory extends ProfileChildren { profileChildren: ProfileChildren; } -type CreateChildren = { +export type CreateChildren = { isActive: boolean; childrenCareer: string; childrenFirstName: string; @@ -271,7 +271,7 @@ type CreateChildren = { childrenCitizenId: string; }; -type UpdateChildren = { +export type UpdateChildren = { id: string; isActive?: boolean | null; childrenCareer?: string | null;