Skip to content

Commit

Permalink
fix: particpant not being correctly passed to partner goals while new…
Browse files Browse the repository at this point in the history
… goal moved to shared goal
  • Loading branch information
vinaybadgujar102 committed Nov 4, 2024
1 parent 4f34812 commit 5e297d0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 14 deletions.
37 changes: 31 additions & 6 deletions src/api/SharedWMAPI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { db } from "@models";
import { GoalItem } from "@src/models/GoalItem";
import { createGoalObjectFromTags } from "@src/helpers/GoalProcessor";
import { addGoal } from "../GoalsAPI";
import { getContactByRelId } from "../ContactsAPI";

export const addSharedWMSublist = async (parentGoalId: string, goalIds: string[]) => {
db.transaction("rw", db.sharedWMCollection, async () => {
Expand All @@ -17,29 +18,53 @@ export const addSharedWMSublist = async (parentGoalId: string, goalIds: string[]
});
};

export const addSharedWMGoal = async (goalDetails: GoalItem) => {
export const addSharedWMGoal = async (goalDetails: GoalItem, relId = "") => {
console.log("[addSharedWMGoal] Input goal details:", goalDetails);
console.log("[addSharedWMGoal] Input relId:", relId);

const { participants } = goalDetails;
const newGoal = createGoalObjectFromTags({ ...goalDetails, typeOfGoal: "shared" });
if (participants) newGoal.participants = participants;
let updatedParticipants = participants || [];

if (relId) {
const contact = await getContactByRelId(relId);
if (contact) {
const contactExists = updatedParticipants.some((p) => p.relId === relId);
if (!contactExists) {
updatedParticipants = [...updatedParticipants, { ...contact, following: true, type: "sharer" }];
}
}
}

console.log("[addSharedWMGoal] Updated participants:", updatedParticipants);
const newGoal = createGoalObjectFromTags({
...goalDetails,
typeOfGoal: "shared",
participants: updatedParticipants,
});

await db
.transaction("rw", db.sharedWMCollection, async () => {
await db.sharedWMCollection.add(newGoal);
console.log("[addSharedWMGoal] Goal added to sharedWMCollection");
})
.then(async () => {
const { parentGoalId } = newGoal;
if (parentGoalId !== "root") {
console.log("[addSharedWMGoal] Adding goal to parent sublist. ParentId:", parentGoalId);
await addSharedWMSublist(parentGoalId, [newGoal.id]);
}
})
.catch((e) => {
console.log(e.stack || e);
console.error("[addSharedWMGoal] Error:", e.stack || e);
});

console.log("[addSharedWMGoal] Successfully created goal with ID:", newGoal.id);
return newGoal.id;
};

export const addGoalsInSharedWM = async (goals: GoalItem[]) => {
export const addGoalsInSharedWM = async (goals: GoalItem[], relId: string) => {
goals.forEach((ele) => {
addSharedWMGoal(ele).then((res) => console.log(res, "added"));
addSharedWMGoal(ele, relId).then((res) => console.log(res, "added"));
});
};

Expand Down
45 changes: 40 additions & 5 deletions src/helpers/GoalController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,32 +296,66 @@ export const moveGoalHierarchy = async (goalId: string, newParentGoalId: string)
const ancestorGoalsIdsOfNewParent = await getGoalHistoryToRoot(newParentGoalId);
const ancestorGoalIdsOfNewParent = ancestorGoalsIdsOfNewParent.map((ele) => ele.goalID);

await createSharedGoal(goalToMove, newParentGoalId, ancestorGoalIdsOfNewParent);
const ancestorGoals = await Promise.all(ancestorGoalIdsOfNewParent.map((id) => getGoal(id)));
const allParticipants = new Map<string, IParticipant>();

[...ancestorGoals, newParentGoal].forEach((goal) => {
if (!goal?.participants) return;
goal.participants.forEach((participant) => {
if (participant.following) {
allParticipants.set(participant.relId, participant);
}
});
});

goalToMove.participants.forEach((participant) => {
if (participant.following) {
allParticipants.set(participant.relId, participant);
}
});

const updatedGoal = {
...goalToMove,
participants: Array.from(allParticipants.values()),
};

await createSharedGoal(updatedGoal, newParentGoalId, ancestorGoalIdsOfNewParent);

await Promise.all([
updateGoal(goalToMove.id, { parentGoalId: newParentGoalId }),
updateGoal(goalToMove.id, {
parentGoalId: newParentGoalId,
participants: updatedGoal.participants,
}),
removeGoalFromParentSublist(goalToMove.id, oldParentId),
addGoalToNewParentSublist(goalToMove.id, newParentGoalId),
updateRootGoal(goalToMove.id, newParentGoal?.rootGoalId ?? "root"),
]);

const descendants = await getAllDescendants(goalId);
await Promise.all(
descendants.map((descendant) =>
updateGoal(descendant.id, {
participants: updatedGoal.participants,
rootGoalId: newParentGoal?.rootGoalId ?? "root",
}),
),
);

const subscribers = await getParticipantsOfGoals(ancestorGoalIds);

subscribers.forEach(async ({ sub, rootGoalId }) => {
await sendUpdatesToSubscriber(sub, rootGoalId, "moved", [
{
level: ancestorGoalIds.length,
goal: {
...goalToMove,
...updatedGoal,
parentGoalId: newParentGoalId,
rootGoalId,
},
},
]);
});

const descendants = await getAllDescendants(goalId);

if (descendants.length > 0) {
subscribers.forEach(async ({ sub, rootGoalId }) => {
await sendUpdatesToSubscriber(
Expand All @@ -332,6 +366,7 @@ export const moveGoalHierarchy = async (goalId: string, newParentGoalId: string)
level: ancestorGoalIds.length + 1,
goal: {
...descendant,
participants: updatedGoal.participants,
rootGoalId,
},
})),
Expand Down
13 changes: 10 additions & 3 deletions src/helpers/InboxProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,25 @@ export const handleIncomingChanges = async (payload: Payload, relId: string) =>
console.log("Changes ignored");
return;
}
if (payload.changeType === "subgoals" || payload.changeType === "newGoalMoved") {
if (payload.changeType === "subgoals") {
const changes = [
...payload.changes.map((ele: changesInGoal) => ({ ...ele, goal: fixDateVlauesInGoalObject(ele.goal) })),
];
await addGoalsInSharedWM([changes[0].goal]);
await addGoalsInSharedWM([changes[0].goal], relId);
} else if (payload.changeType === "newGoalMoved") {
const changes = [
...payload.changes.map((ele: changesInGoal) => ({ ...ele, goal: fixDateVlauesInGoalObject(ele.goal) })),
];
changes.map(async (ele) => {
await addGoalsInSharedWM([ele.goal], relId);
});
} else if (payload.changeType === "modifiedGoals") {
const changes = [
...payload.changes.map((ele: changesInGoal) => ({ ...ele, goal: fixDateVlauesInGoalObject(ele.goal) })),
];
await updateSharedWMGoal(changes[0].goal.id, changes[0].goal);
} else if (payload.changeType === "deleted") {
const goalToBeDeleted = await getSharedWMGoal(payload.changes[0].goal.id);
const goalToBeDeleted = await getSharedWMGoal(payload.changes[0].id);
console.log("Deleting goal", goalToBeDeleted);
await removeSharedWMChildrenGoals(goalToBeDeleted.id);
await removeSharedWMGoal(goalToBeDeleted);
Expand Down
1 change: 1 addition & 0 deletions src/pages/GoalsPage/PartnerGoals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const PartnerGoals = () => {

const refreshActiveGoals = async () => {
const rootGoals = await getRootGoalsOfPartner(relId);
console.log("rootGoals", rootGoals);
handleUserGoals(rootGoals);
};

Expand Down

0 comments on commit 5e297d0

Please sign in to comment.