Skip to content

Commit

Permalink
fix: use action details diff for item quantity update
Browse files Browse the repository at this point in the history
  • Loading branch information
fPolic committed Nov 13, 2024
1 parent 6f7467f commit 428207c
Showing 1 changed file with 11 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,7 @@ const useActivityItems = (order: AdminOrder): Activity[] => {
: edit.status === "canceled"
? edit.canceled_at
: edit.created_at,
children: isConfirmed ? (
<OrderEditBody edit={edit} itemsMap={itemsMap} />
) : null,
children: isConfirmed ? <OrderEditBody edit={edit} /> : null,
})
}

Expand Down Expand Up @@ -839,18 +837,12 @@ const ExchangeBody = ({
)
}

const OrderEditBody = ({
edit,
itemsMap,
}: {
edit: AdminOrderChange
itemsMap: Map<string, AdminOrderLineItem>
}) => {
const OrderEditBody = ({ edit }: { edit: AdminOrderChange }) => {
const { t } = useTranslation()

const [itemsAdded, itemsRemoved] = useMemo(
() => countItemsChange(edit.actions, itemsMap),
[edit, itemsMap]
() => countItemsChange(edit.actions),
[edit]
)

return (
Expand All @@ -873,10 +865,7 @@ const OrderEditBody = ({
/**
* Returns count of added and removed item quantity
*/
function countItemsChange(
actions: AdminOrderChange["actions"],
itemsMap: Map<string, AdminOrderLineItem>
) {
function countItemsChange(actions: AdminOrderChange["actions"]) {
let added = 0
let removed = 0

Expand All @@ -885,20 +874,12 @@ function countItemsChange(
added += action.details!.quantity as number
}
if (action.action === "ITEM_UPDATE") {
const newQuantity = action.details!.quantity as number
const originalQuantity: number | undefined = itemsMap.get(
action.details!.reference_id as string
)?.quantity

if (typeof originalQuantity === "number") {
const diff = Math.abs(newQuantity - originalQuantity)

if (newQuantity > originalQuantity) {
added += diff
}
if (newQuantity < originalQuantity) {
removed += diff
}
const quantityDiff = action.details!.quantity_diff as number

if (quantityDiff > 0) {
added += quantityDiff
} else {
removed += Math.abs(quantityDiff)
}
}
})
Expand Down

0 comments on commit 428207c

Please sign in to comment.