feat(dashboard, order, medusa, types, js-sdk): Request return e2e flow (#7848)

This commit is contained in:
Frane Polić
2024-07-24 19:19:00 +02:00
committed by GitHub
parent 0a482e972f
commit f7d1cd259e
36 changed files with 2251 additions and 146 deletions
+12 -72
View File
@@ -1,75 +1,15 @@
import { ClaimItem, LineItem, Order } from "@medusajs/medusa"
import { AdminOrderLineItem } from "@medusajs/types"
/**
* Return line items that are returnable from an order
* @param order
* @param isClaim
*/
export const getAllReturnableItems = (
order: Omit<Order, "beforeInserts">,
isClaim: boolean
) => {
let orderItems = order.items.reduce(
(map, obj) =>
map.set(obj.id, {
...obj,
}),
new Map<string, Omit<LineItem, "beforeInsert">>()
export function getReturnableQuantity(item: AdminOrderLineItem): number {
const {
// TODO: this should be `fulfilled_quantity`? now there is check on the BD that we can't return more quantity than we shipped but some items don't require shipping
shipped_quantity,
return_received_quantity,
return_dismissed_quantity, // TODO: check this
return_requested_quantity,
} = item.detail
return (
shipped_quantity - (return_received_quantity + return_requested_quantity)
)
let claimedItems: ClaimItem[] = []
if (order.claims && order.claims.length) {
for (const claim of order.claims) {
if (claim.return_order?.status !== "canceled") {
claim.claim_items = claim.claim_items ?? []
claimedItems = [...claimedItems, ...claim.claim_items]
}
if (
claim.fulfillment_status === "not_fulfilled" &&
claim.payment_status === "na"
) {
continue
}
if (claim.additional_items && claim.additional_items.length) {
orderItems = claim.additional_items
.filter(
(it) =>
it.shipped_quantity ||
it.shipped_quantity === it.fulfilled_quantity
)
.reduce((map, obj) => map.set(obj.id, { ...obj }), orderItems)
}
}
}
if (!isClaim) {
if (order.swaps && order.swaps.length) {
for (const swap of order.swaps) {
if (swap.fulfillment_status === "not_fulfilled") {
continue
}
orderItems = swap.additional_items.reduce(
(map, obj) =>
map.set(obj.id, {
...obj,
}),
orderItems
)
}
}
}
for (const item of claimedItems) {
const i = orderItems.get(item.item_id)
if (i) {
i.quantity = i.quantity - item.quantity
i.quantity !== 0 ? orderItems.set(i.id, i) : orderItems.delete(i.id)
}
}
return [...orderItems.values()]
}