fix: profile family endpoint behaviour

This commit is contained in:
Methapon2001 2024-03-21 13:19:26 +07:00
parent 86b5221d24
commit 6ae3bc6be9

View file

@ -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,
}),
);