Skip to content

Commit

Permalink
fix(composables): shipping and payment method (#1564)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanilowicz authored Jan 7, 2025
1 parent 68bdc9b commit 3d2f2b5
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-timers-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/composables-next": patch
---

Return the last element for the payment and shipping methods in `useOrderDetails`. The reason for this change is that the backend returns collections sorted by the entry date
90 changes: 90 additions & 0 deletions packages/composables/src/useOrderDetails/useOrderDetails.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,94 @@ describe("useOrderDetails", () => {
await vm.loadOrderDetails();
expect(vm.paymentChangeable).toEqual(true);
});

it("should return current payment method", async () => {
const { vm, injections } = useSetup(() => useOrderDetails("123-test"));
injections.apiClient.invoke.mockResolvedValue({
data: {
orders: {
elements: [
{
transactions: [
{
paymentMethod: {
shortName: "invoice_payment",
},
},
{
paymentMethod: {
shortName: "cash_payment",
},
},
],
},
],
},
},
});
await vm.loadOrderDetails();
expect(vm.paymentMethod?.shortName).toEqual("cash_payment");
});

it("should return current delivery method", async () => {
const { vm, injections } = useSetup(() => useOrderDetails("123-test"));
injections.apiClient.invoke.mockResolvedValue({
data: {
orders: {
elements: [
{
deliveries: [
{
shippingMethod: {
name: "test",
},
},
{
shippingMethod: {
name: "Standard",
},
},
],
},
],
},
},
});
await vm.loadOrderDetails();
expect(vm.shippingMethod?.name).toEqual("Standard");
});

it("should return undefined if payment method does not exists", async () => {
const { vm, injections } = useSetup(() => useOrderDetails("123-test"));
injections.apiClient.invoke.mockResolvedValue({
data: {
orders: {
elements: [
{
transactions: [],
},
],
},
},
});
await vm.loadOrderDetails();
expect(vm.paymentMethod?.shortName).toEqual(undefined);
});

it("should return undefined if shipping method does not exists", async () => {
const { vm, injections } = useSetup(() => useOrderDetails("123-test"));
injections.apiClient.invoke.mockResolvedValue({
data: {
orders: {
elements: [
{
deliveries: [],
},
],
},
},
});
await vm.loadOrderDetails();
expect(vm.shippingMethod?.name).toEqual(undefined);
});
});
21 changes: 15 additions & 6 deletions packages/composables/src/useOrderDetails/useOrderDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export type UseOrderDetailsReturn = {
*/
paymentUrl: Ref<null | string>;
/**
* Selected shipping method
* Returns current selected shipping method for the order. Last element in delivery array.
*/
shippingMethod: ComputedRef<Schemas["ShippingMethod"] | undefined | null>;
/**
* Selected payment method
* Returns current selected payment method for the order. Last element in transactions array.
*/
paymentMethod: ComputedRef<Schemas["PaymentMethod"] | undefined | null>;
/**
Expand Down Expand Up @@ -139,12 +139,21 @@ export function useOrderDetails(

const orderAssociations = useDefaultOrderAssociations();

const paymentMethod = computed(
() => _sharedOrder.value?.transactions?.[0]?.paymentMethod,
const paymentMethod = computed(() =>
_sharedOrder.value?.transactions?.length
? _sharedOrder.value.transactions[
_sharedOrder.value.transactions.length - 1
].paymentMethod
: undefined,
);
const shippingMethod = computed(
() => _sharedOrder.value?.deliveries?.[0]?.shippingMethod,

const shippingMethod = computed(() =>
_sharedOrder.value?.deliveries?.length
? _sharedOrder.value.deliveries[_sharedOrder.value.deliveries.length - 1]
.shippingMethod
: undefined,
);

const paymentUrl = ref();

const personalDetails = computed(() => ({
Expand Down

0 comments on commit 3d2f2b5

Please sign in to comment.