saleor-dashboard/src/orders/utils/data.ts

78 lines
2.3 KiB
TypeScript
Raw Normal View History

Refunds (#870) * 1721 - add refunds miscellaneous view (#860) * Create new page for Miscellaneous Refunds * Replace refund order dialog with dedicated page * Add data test ids * Update order details view for refunds (#874) * 1719 - add refund entry to order history (#875) * Add refund order history entry * Update refund event with the right query * 1722 - add refunds product view (#873) * Create new page for Miscellaneous Refunds * Replace refund order dialog with dedicated page * Add data test ids * Create refund products table * Implement refund products view * Update refund mutation with product lines input * Fix products quantities on refund page * Fix order refund submission * Fix products refund submission input variables * Filter out fulfillments on refund page * Update refund page in storybook * Fix test snapshots after wrong refunds rebase * Set max refund as captured amount * Refund queries adjustments * Display refund values with nullish coalescing operator * Update test snapshots with refunds * Refactor order refund values calculation * Create and use refund order line fragment * Use old simple refund mutation for miscellaneous refund * Submit for refund only lines with non-zero quantity set * Fix showing refund error * Fix refund details on order details page (#879) * Update order details view for refunds (#874) * 1719 - add refund entry to order history (#875) * Add refund order history entry * Update refund event with the right query * 1722 - add refunds product view (#873) * Create new page for Miscellaneous Refunds * Replace refund order dialog with dedicated page * Add data test ids * Create refund products table * Implement refund products view * Update refund mutation with product lines input * Fix products quantities on refund page * Fix order refund submission * Fix products refund submission input variables * Filter out fulfillments on refund page * Update refund page in storybook * Fix test snapshots after wrong refunds rebase * Set max refund as captured amount * Refund queries adjustments * Display refund values with nullish coalescing operator * Update test snapshots with refunds * Refactor order refund values calculation * Create and use refund order line fragment * Use old simple refund mutation for miscellaneous refund * Submit for refund only lines with non-zero quantity set * Fix showing refund error * Add missing refund amount to order history * Merge repeated order lines in fulfillment lines * Update order history events types and test snapshots * Update changelog with refunds changes
2020-12-01 13:13:05 +00:00
import { IMoney, subtractMoney } from "@saleor/components/Money";
import { FormsetData } from "@saleor/hooks/useFormset";
import { OrderDetails_order_fulfillments_lines } from "../types/OrderDetails";
import {
OrderRefundData_order,
OrderRefundData_order_fulfillments,
OrderRefundData_order_lines
} from "../types/OrderRefundData";
export type OrderWithTotalAndTotalCaptured = Pick<
OrderRefundData_order,
"total" | "totalCaptured"
>;
export function getPreviouslyRefundedPrice(
order: OrderWithTotalAndTotalCaptured
): IMoney {
return (
order?.totalCaptured &&
order?.total?.gross &&
subtractMoney(order?.totalCaptured, order?.total?.gross)
);
}
export function getRefundedLinesPriceSum(
lines: OrderRefundData_order_lines[],
refundedProductQuantities: FormsetData<null, string>
): number {
return lines?.reduce((sum, line) => {
const refundedLine = refundedProductQuantities.find(
refundedLine => refundedLine.id === line.id
);
return sum + line.unitPrice.gross.amount * Number(refundedLine?.value || 0);
}, 0);
}
export function getAllFulfillmentLinesPriceSum(
fulfillments: OrderRefundData_order_fulfillments[],
refundedFulfilledProductQuantities: FormsetData<null, string>
): number {
return fulfillments?.reduce((sum, fulfillment) => {
const fulfilmentLinesSum = fulfillment?.lines.reduce((sum, line) => {
const refundedLine = refundedFulfilledProductQuantities.find(
refundedLine => refundedLine.id === line.id
);
return (
sum +
line.orderLine.unitPrice.gross.amount * Number(refundedLine?.value || 0)
);
}, 0);
return sum + fulfilmentLinesSum;
}, 0);
}
export function mergeRepeatedOrderLines(
fulfillmentLines: OrderDetails_order_fulfillments_lines[]
) {
return fulfillmentLines.reduce((prev, curr) => {
const existingOrderLineIndex = prev.findIndex(
prevLine => prevLine.orderLine.id === curr.orderLine.id
);
if (existingOrderLineIndex === -1) {
prev.push(curr);
} else {
const existingOrderLine = prev[existingOrderLineIndex];
prev[existingOrderLineIndex] = {
...existingOrderLine,
quantity: existingOrderLine.quantity + curr.quantity
};
}
return prev;
}, Array<OrderDetails_order_fulfillments_lines>());
}