From 6ae3bc6be98f3dcb5b3fc092430d12530df98634 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 21 Mar 2024 13:19:26 +0700 Subject: [PATCH] fix: profile family endpoint behaviour --- .../ProfileFamilyHistoryController.ts | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/controllers/ProfileFamilyHistoryController.ts b/src/controllers/ProfileFamilyHistoryController.ts index 7a112091..495a5742 100644 --- a/src/controllers/ProfileFamilyHistoryController.ts +++ b/src/controllers/ProfileFamilyHistoryController.ts @@ -94,11 +94,11 @@ export class ProfileFamilyHistoryController extends Controller { public async getFamilyHistory(@Path() profileId: string) { const family = await this.familyHistoryRepo.find({ take: 1, - order: { createdAt: "DESC" }, + order: { lastUpdatedAt: "DESC" }, where: { profileId }, }); const children = await this.childrenRepo.find({ - order: { createdAt: "DESC" }, + order: { createdAt: "ASC" }, where: { profileId }, }); return new HttpSuccess( @@ -227,7 +227,7 @@ export class ProfileFamilyHistoryController extends Controller { where: { profileId }, }); - family.pop(); + family.shift(); const record = await Promise.all( family.map(async (v) => ({ @@ -317,38 +317,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, }), );