Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-signs-ask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@godaddy/react": major
---

modify use-update-orders.ts to detect pickup orders and always send the pickup location address for tax calculation, ensuring consistent tax rates based on where the customer picks up their order.
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { describe, expect, it } from 'vitest';

/**
* Helper function to get the tax destination address based on delivery method.
* This is the core logic used in useUpdateOrder to determine which address
* to send for tax calculation.
*
* For pickup orders: Always use the pickup location address
* For shipping orders: Let backend use the saved shipping address (undefined)
*/
function getTaxDestinationAddress(
deliveryMethod: string | undefined,
pickupLocationId: string | undefined,
locations: Array<{ id: string; address: Record<string, unknown> }> | undefined
): Record<string, unknown> | undefined {
const isPickup = deliveryMethod === 'PICKUP';

if (isPickup) {
const pickupLocationAddress = locations?.find(
loc => loc.id === pickupLocationId
)?.address;
return pickupLocationAddress;
}

// For shipping, return undefined to let backend use saved shipping address
return undefined;
}

describe('useUpdateOrder - Tax calculation address logic', () => {
const mockLocations = [
{
id: 'location-1',
address: {
addressLine1: '599 Stegall Dr',
adminArea1: 'GA',
adminArea2: 'Jasper',
countryCode: 'US',
postalCode: '30143',
},
},
{
id: 'location-2',
address: {
addressLine1: '123 Main St',
adminArea1: 'NY',
adminArea2: 'New York',
countryCode: 'US',
postalCode: '10001',
},
},
];

describe('Pickup orders', () => {
it('should return pickup location address for PICKUP delivery method', () => {
const result = getTaxDestinationAddress(
'PICKUP',
'location-1',
mockLocations
);

expect(result).toEqual({
addressLine1: '599 Stegall Dr',
adminArea1: 'GA',
adminArea2: 'Jasper',
countryCode: 'US',
postalCode: '30143',
});
});

it('should return correct location when multiple locations exist', () => {
const result = getTaxDestinationAddress(
'PICKUP',
'location-2',
mockLocations
);

expect(result).toEqual({
addressLine1: '123 Main St',
adminArea1: 'NY',
adminArea2: 'New York',
countryCode: 'US',
postalCode: '10001',
});
});

it('should return undefined if pickup location not found', () => {
const result = getTaxDestinationAddress(
'PICKUP',
'non-existent-location',
mockLocations
);

expect(result).toBeUndefined();
});

it('should return undefined if locations array is empty', () => {
const result = getTaxDestinationAddress('PICKUP', 'location-1', []);

expect(result).toBeUndefined();
});

it('should return undefined if locations is undefined', () => {
const result = getTaxDestinationAddress(
'PICKUP',
'location-1',
undefined
);

expect(result).toBeUndefined();
});
});

describe('Shipping orders', () => {
it('should return undefined for SHIP delivery method', () => {
const result = getTaxDestinationAddress(
'SHIP',
'location-1',
mockLocations
);

expect(result).toBeUndefined();
});

it('should return undefined when delivery method is undefined', () => {
const result = getTaxDestinationAddress(
undefined,
'location-1',
mockLocations
);

expect(result).toBeUndefined();
});
});

describe('Edge cases', () => {
it('should handle pickupLocationId being undefined for pickup', () => {
const result = getTaxDestinationAddress('PICKUP', undefined, mockLocations);

expect(result).toBeUndefined();
});

it('should handle empty string pickupLocationId for pickup', () => {
const result = getTaxDestinationAddress('PICKUP', '', mockLocations);

expect(result).toBeUndefined();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useFormContext } from 'react-hook-form';
import { useCheckoutContext } from '@/components/checkout/checkout';
import { DeliveryMethods } from '@/components/checkout/delivery/delivery-method';
import { useUpdateTaxes } from '@/components/checkout/order/use-update-taxes';
import { useGoDaddyContext } from '@/godaddy-provider';
import { updateDraftOrder } from '@/lib/godaddy/godaddy';
Expand All @@ -10,6 +12,7 @@ export function useUpdateOrder() {
const { apiHost } = useGoDaddyContext();
const updateTaxes = useUpdateTaxes();
const queryClient = useQueryClient();
const form = useFormContext();

return useMutation({
mutationKey: ['update-draft-order'],
Expand All @@ -32,7 +35,22 @@ export function useUpdateOrder() {
(!input.shipping?.address && input.billing?.address)
) {
if (session?.enableTaxCollection) {
updateTaxes.mutate(undefined);
// For pickup orders, always use the pickup location address for tax calculation
const deliveryMethod = form?.getValues?.('deliveryMethod');
const isPickup = deliveryMethod === DeliveryMethods.PICKUP;

if (isPickup) {
const pickupLocationId = form?.getValues?.('pickupLocationId');
const pickupLocationAddress = session?.locations?.find(
loc => loc.id === pickupLocationId
)?.address;

// Always send pickup location address for pickup orders
updateTaxes.mutate(pickupLocationAddress);
} else {
// For shipping, let backend use the saved shipping address
updateTaxes.mutate(undefined);
}
} else {
queryClient.invalidateQueries({
queryKey: ['draft-order', session.id],
Expand Down
Loading