diff --git a/src/controllers/ProfileFamilyHistoryController.ts b/src/controllers/ProfileFamilyHistoryController.ts index d0f0fe55..9dd58088 100644 --- a/src/controllers/ProfileFamilyHistoryController.ts +++ b/src/controllers/ProfileFamilyHistoryController.ts @@ -221,7 +221,7 @@ export class ProfileFamilyHistoryController extends Controller { where: { profileId }, }); - family.pop(); + family.shift(); const record = await Promise.all( family.map(async (v) => ({ @@ -312,38 +312,35 @@ export class ProfileFamilyHistoryController extends Controller { 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, { + newChild.push( + Object.assign(new ProfileChildren(), { ...child, profileId, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, - id: undefined, - }); - newChild.push(newProfileChild); - } + id: !childrenRecord.find((v) => v.id === child.id) ? undefined : child.id, + }), + ); } await Promise.all([ - this.familyHistoryRepo.save(historyData), + this.familyHistoryRepo.save({ ...historyData, lastUpdatedAt: undefined }), ...newChild.map(async (v) => { return await this.childrenRepo.save(v); }), ...childrenRecord.map(async (v) => { - return await this.childrenRepo.save(v); + if (!children.find((x) => x.id === v.id)) { + return await this.childrenRepo.delete({ id: v.id }); + } }), ...childrenRecord.map(async (v) => { return await this.childrenHistoryRepo.save( this.childrenHistoryRepo.create({ ...v, profileFamilyHistoryId: familyRecord.id, - profileChildrenId: v.id, + profileChildrenId: children.find((x) => x.id === v.id) ? v.id : undefined, id: undefined, }), );