From 5e297d0b1dc01d0e3b07f3cee70977928e82aae9 Mon Sep 17 00:00:00 2001 From: Vinay Badgujar Date: Mon, 4 Nov 2024 16:29:59 +0530 Subject: [PATCH] fix: particpant not being correctly passed to partner goals while new goal moved to shared goal --- src/api/SharedWMAPI/index.ts | 37 +++++++++++++++++++---- src/helpers/GoalController.ts | 45 ++++++++++++++++++++++++---- src/helpers/InboxProcessor.ts | 13 ++++++-- src/pages/GoalsPage/PartnerGoals.tsx | 1 + 4 files changed, 82 insertions(+), 14 deletions(-) diff --git a/src/api/SharedWMAPI/index.ts b/src/api/SharedWMAPI/index.ts index 82820f957..c1d942b73 100644 --- a/src/api/SharedWMAPI/index.ts +++ b/src/api/SharedWMAPI/index.ts @@ -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 () => { @@ -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")); }); }; diff --git a/src/helpers/GoalController.ts b/src/helpers/GoalController.ts index 811dd77b4..7614c0e44 100644 --- a/src/helpers/GoalController.ts +++ b/src/helpers/GoalController.ts @@ -296,15 +296,51 @@ 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(); + + [...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 }) => { @@ -312,7 +348,7 @@ export const moveGoalHierarchy = async (goalId: string, newParentGoalId: string) { level: ancestorGoalIds.length, goal: { - ...goalToMove, + ...updatedGoal, parentGoalId: newParentGoalId, rootGoalId, }, @@ -320,8 +356,6 @@ export const moveGoalHierarchy = async (goalId: string, newParentGoalId: string) ]); }); - const descendants = await getAllDescendants(goalId); - if (descendants.length > 0) { subscribers.forEach(async ({ sub, rootGoalId }) => { await sendUpdatesToSubscriber( @@ -332,6 +366,7 @@ export const moveGoalHierarchy = async (goalId: string, newParentGoalId: string) level: ancestorGoalIds.length + 1, goal: { ...descendant, + participants: updatedGoal.participants, rootGoalId, }, })), diff --git a/src/helpers/InboxProcessor.ts b/src/helpers/InboxProcessor.ts index f6dff31fb..38b4c5662 100644 --- a/src/helpers/InboxProcessor.ts +++ b/src/helpers/InboxProcessor.ts @@ -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); diff --git a/src/pages/GoalsPage/PartnerGoals.tsx b/src/pages/GoalsPage/PartnerGoals.tsx index 80f467f56..e62a9ab5b 100644 --- a/src/pages/GoalsPage/PartnerGoals.tsx +++ b/src/pages/GoalsPage/PartnerGoals.tsx @@ -56,6 +56,7 @@ const PartnerGoals = () => { const refreshActiveGoals = async () => { const rootGoals = await getRootGoalsOfPartner(relId); + console.log("rootGoals", rootGoals); handleUserGoals(rootGoals); };