From 88a224e42a490fd7fae29677c463740b89ad9b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Sun, 23 Apr 2023 13:28:15 +0200 Subject: [PATCH] fix(admin): display correct items in the timeline after OE (#3895) --- .changeset/nasty-sloths-serve.md | 5 ++ .../ui/src/hooks/use-build-timeline.tsx | 55 +++++++++++++++---- 2 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 .changeset/nasty-sloths-serve.md diff --git a/.changeset/nasty-sloths-serve.md b/.changeset/nasty-sloths-serve.md new file mode 100644 index 0000000000..311d32c965 --- /dev/null +++ b/.changeset/nasty-sloths-serve.md @@ -0,0 +1,5 @@ +--- +"@medusajs/admin-ui": patch +--- + +fix(admin): display correct items in the timeline after OE diff --git a/packages/admin-ui/ui/src/hooks/use-build-timeline.tsx b/packages/admin-ui/ui/src/hooks/use-build-timeline.tsx index 94ef922be9..78be8c1e1a 100644 --- a/packages/admin-ui/ui/src/hooks/use-build-timeline.tsx +++ b/packages/admin-ui/ui/src/hooks/use-build-timeline.tsx @@ -323,7 +323,9 @@ export const useBuildTimeline = (orderId: string) => { id: event.id, time: event.created_at, type: "fulfilled", - items: event.items.map((item) => getFulfilmentItem(allItems, item)), + items: event.items.map((item) => + getFulfilmentItem(allItems, edits, item) + ), noNotification: event.no_notification, orderId: order.id, locationName: getLocationNameById(event.location_id), @@ -334,7 +336,9 @@ export const useBuildTimeline = (orderId: string) => { id: event.id, time: event.shipped_at, type: "shipped", - items: event.items.map((item) => getFulfilmentItem(allItems, item)), + items: event.items.map((item) => + getFulfilmentItem(allItems, edits, item) + ), noNotification: event.no_notification, orderId: order.id, locationName: getLocationNameById(event.location_id), @@ -346,8 +350,8 @@ export const useBuildTimeline = (orderId: string) => { events.push({ id: event.id, items: event.items - .map((i) => getReturnItems(allItems, i)) - // After order edit is confirmed, line item that was returned can be deleted + .map((i) => getReturnItems(allItems, edits, i)) + // Can be undefined while `edits` is loading .filter((i) => !!i), status: event.status, currentStatus: event.status, @@ -364,8 +368,8 @@ export const useBuildTimeline = (orderId: string) => { events.push({ id: event.id, items: event.items - .map((i) => getReturnItems(allItems, i)) - // After order edit is confirmed, line item that was returned can be deleted + .map((i) => getReturnItems(allItems, edits, i)) + // Can be undefined while `edits` is loading .filter((i) => !!i), status: "requested", time: event.created_at, @@ -391,7 +395,7 @@ export const useBuildTimeline = (orderId: string) => { type: "exchange", newItems: event.additional_items.map((i) => getSwapItem(i)), returnItems: event.return_order.items.map((i) => - getReturnItems(allItems, i) + getReturnItems(allItems, edits, i) ), exchangeCartId: event.payment_status !== "captured" ? event.cart_id : undefined, @@ -557,8 +561,32 @@ function getLineItem(allItems, itemId) { } } -function getReturnItems(allItems, item) { - const line = allItems.find((li) => li.id === item.item_id) +function findOriginalItemId(edits, originalId) { + let currentId = originalId + + edits = edits + .filter((e) => !!e.confirmed_at) // only confirmed OEs are cloning line items + .sort((a, b) => new Date(a.confirmed_at) - new Date(b.confirmed_at)) + + for (const edit of edits) { + const clonedItem = edit.items.find((e) => e.original_item_id === currentId) + if (clonedItem) { + currentId = clonedItem.id + } else { + break + } + } + + return currentId +} + +function getReturnItems(allItems, edits, item) { + let id = item.item_id + if (edits) { + id = findOriginalItemId(edits, id) + } + + const line = allItems.find((li) => li.id === id) if (!line) { return @@ -606,8 +634,13 @@ function getWasRefundClaim(claimId, order) { return claim.type === "refund" } -function getFulfilmentItem(allItems, item) { - const line = allItems.find((line) => line.id === item.item_id) +function getFulfilmentItem(allItems, edits, item) { + let id = item.item_id + if (edits) { + id = findOriginalItemId(edits, id) + } + + const line = allItems.find((line) => line.id === id) if (!line) { return