Skip to content

Commit

Permalink
feat(client): add packaging options client
Browse files Browse the repository at this point in the history
This adds the client for packaging options for an order. It also updates
missing types for the checkout order such as metadata.
  • Loading branch information
Ana Pimentel authored and boliveira committed Feb 14, 2024
1 parent 60fec59 commit 8616eb7
Show file tree
Hide file tree
Showing 24 changed files with 141 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,7 @@ Object {
"getOrderItemAvailableActivities": [Function],
"getOrderReturnOptions": [Function],
"getOrderShippingAddressChangeRequests": [Function],
"getPackagingOptions": [Function],
"getPaymentIntent": [Function],
"getPaymentIntentCharge": [Function],
"getPaymentIntentInstrument": [Function],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { rest, type RestHandler } from 'msw';
import type { PackagingOption } from '../types/index.js';

const path = '/api/checkout/v1/packagingOptions';

const fixtures = {
success: (response: PackagingOption[]): RestHandler =>
rest.get(path, (_req, res, ctx) =>
res(ctx.status(200), ctx.json(response)),
),
failure: (): RestHandler =>
rest.get(path, (_req, res, ctx) =>
res(ctx.status(404), ctx.json({ message: 'stub error' })),
),
};

export default fixtures;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`checkout client getPackagingOptions should receive a client request error 1`] = `
Object {
"code": "-1",
"message": "stub error",
"name": "AxiosError",
"status": 404,
"transportLayerErrorCode": "ERR_BAD_REQUEST",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ describe('checkout client', () => {
hadUnavailableItems: true,
isGuestUser: true,
shippingMode: ShippingMode.ByMerchant,
customerId: 123,
},
shippingOptions: [
{
Expand Down
38 changes: 38 additions & 0 deletions packages/client/src/checkout/__tests__/getPackagingOptions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as checkoutClient from '../index.js';
import { mockPackagingOptionsResponse } from 'tests/__fixtures__/checkout/packagingOptions.fixtures.mjs';
import { type PackagingOption } from '../types/index.js';
import client from '../../helpers/client/index.js';
import fixtures from '../__fixtures__/getPackagingOptions.fixtures.js';
import mswServer from '../../../tests/mswServer.js';

describe('checkout client', () => {
const expectedConfig = undefined;

beforeEach(() => jest.clearAllMocks());

describe('getPackagingOptions', () => {
const spy = jest.spyOn(client, 'get');
const query = { channelCode: 'code' };
const urlToBeCalled = `/checkout/v1/packagingOptions?channelCode=${query.channelCode}`;

it('should handle a client request successfully', async () => {
const response: PackagingOption[] = mockPackagingOptionsResponse;

mswServer.use(fixtures.success(response));

await expect(
checkoutClient.getPackagingOptions(query),
).resolves.toStrictEqual(response);
expect(spy).toHaveBeenCalledWith(urlToBeCalled, expectedConfig);
});

it('should receive a client request error', async () => {
mswServer.use(fixtures.failure());

await expect(
checkoutClient.getPackagingOptions(query),
).rejects.toMatchSnapshot();
expect(spy).toHaveBeenCalledWith(urlToBeCalled, expectedConfig);
});
});
});
22 changes: 22 additions & 0 deletions packages/client/src/checkout/getPackagingOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { adaptError } from '../helpers/client/formatError.js';
import client from '../helpers/client/index.js';
import join from 'proper-url-join';
import type { GetPackagingOptions } from './types/index.js';

/**
* Method responsible for loading the packaging options.
*
* @param query - Query params.
* @param config - Custom configurations to send to the client instance (axios).
*
* @returns Promise that will resolve when the call to the endpoint finishes.
*/
const getPackagingOptions: GetPackagingOptions = (query, config) =>
client
.get(join('/checkout/v1/packagingOptions', { query }), config)
.then(response => response.data)
.catch(error => {
throw adaptError(error);
});

export default getPackagingOptions;
1 change: 1 addition & 0 deletions packages/client/src/checkout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export { default as getCheckoutSession } from './getCheckoutSession.js';
export { default as getCheckoutSessionCharge } from './getCheckoutSessionCharge.js';
export { default as getCheckoutSessionTags } from './getCheckoutSessionTags.js';
export { default as getCollectPoints } from './getCollectPoints.js';
export { default as getPackagingOptions } from './getPackagingOptions.js';
export { default as patchCheckoutOrder } from './patchCheckoutOrder.js';
export { default as patchCheckoutOrderDeliveryBundles } from './patchCheckoutOrderDeliveryBundles.js';
export { default as patchCheckoutOrderDeliveryBundleUpgrades } from './patchCheckoutOrderDeliveryBundleUpgrades.js';
Expand Down
3 changes: 3 additions & 0 deletions packages/client/src/checkout/types/checkoutOrder.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
import type { CheckoutOrderItem, CheckoutOrderMerchant } from './index.js';
import type { CustomerTypeLegacy } from '../../orders/types/order.types.js';
import type { MerchantLocation } from '../../merchantsLocations/types/merchantLocation.types.js';
import type { Metadata } from '../../types/index.js';
import type { PaymentIntent } from '../../payments/index.js';
import type { PromotionEvaluationId } from '../../promotionEvaluations/index.js';

Expand Down Expand Up @@ -78,4 +79,6 @@ export type CheckoutOrder = {
shippingMode: ShippingMode;
paymentIntentId?: PaymentIntent['id'];
promotionEvaluationId?: PromotionEvaluationId;
customerId: number;
metadata?: Metadata;
};
2 changes: 2 additions & 0 deletions packages/client/src/checkout/types/checkoutOrderItem.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
Brand,
CheckoutOrderItemStatus,
Color,
Metadata,
OrderItemCreationChannelLegacy,
Price,
Product,
Expand Down Expand Up @@ -77,4 +78,5 @@ export type CheckoutOrderItem = {
subTotalOriginalAmount: number;
};
selectedSaleIntent?: SaleIntent & string;
metadata?: Metadata;
};
11 changes: 11 additions & 0 deletions packages/client/src/checkout/types/getPackagingOptions.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Config } from '../../types/index.js';
import type { PackagingOption } from './index.js';

export type GetPackagingOptionsQuery = {
channelCode: string;
};

export type GetPackagingOptions = (
query?: GetPackagingOptionsQuery,
config?: Config,
) => Promise<PackagingOption[]>;
2 changes: 2 additions & 0 deletions packages/client/src/checkout/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export * from './getCheckoutSession.types.js';
export * from './getCheckoutSessionCharge.types.js';
export * from './getCheckoutSessionTags.types.js';
export * from './getCollectPoints.types.js';
export * from './getPackagingOptions.types.js';
export * from './itemDeliveryProvisioning.types.js';
export * from './packagingOption.types.js';
export * from './patchCheckoutOrder.types.js';
export * from './patchCheckoutOrderDeliveryBundles.types.js';
export * from './patchCheckoutOrderDeliveryBundleUpgrades.types.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type PackagingOption = string;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CheckoutAddress, Config } from '../../types/index.js';
import type { CheckoutAddress, Config, Metadata } from '../../types/index.js';
import type {
CheckoutOrder,
CheckoutOrderShippingOption,
Expand All @@ -18,6 +18,7 @@ export type PatchCheckoutOrderData = {
shippingOption?: CheckoutOrderShippingOption;
deliveryBundleUpdate?: CheckoutOrderDeliveryBundleUpdate;
email?: string;
metadata?: Metadata;
};

export type PatchCheckoutOrder = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Bag } from '../../bags/index.js';
import type { Config } from '../../types/index.js';
import type { Config, Metadata } from '../../types/index.js';
import type { GetCheckoutOrderResponse, ShippingMode } from './index.js';
import type { Product } from '../../products/types/index.js';

Expand All @@ -22,11 +22,13 @@ export type PostCheckoutOrderItem = {

export type PostCheckoutOrderDataWithItems = PostCheckoutOrderData & {
items: PostCheckoutOrderItem[];
metadata?: Metadata;
};

export type PostCheckoutOrderDataWithBag = PostCheckoutOrderData & {
bagId: Bag['id'];
removePurchasedItemsFromBag?: boolean;
metadata?: Metadata;
};

export type PostCheckoutOrderDataWithDraftOrder = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ Object {
},
],
"currency": "EUR",
"customerId": 123,
"customerType": 0,
"formattedGrandTotal": "102 €",
"formattedSubTotalAmount": "100 €",
Expand All @@ -310,6 +311,7 @@ Object {
30380051,
],
"locale": "en-US",
"metadata": Object {},
"orderId": "D7XM6Y",
"paymentIntentId": "123",
"shippingAddress": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ Object {
},
],
"currency": "EUR",
"customerId": 123,
"customerType": 0,
"formattedGrandTotal": "102 €",
"formattedSubTotalAmount": "100 €",
Expand All @@ -310,6 +311,7 @@ Object {
30380051,
],
"locale": "en-US",
"metadata": Object {},
"orderId": "D7XM6Y",
"paymentIntentId": "123",
"shippingAddress": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ Object {
},
],
"currency": "EUR",
"customerId": 123,
"customerType": 0,
"formattedGrandTotal": "102 €",
"formattedSubTotalAmount": "100 €",
Expand All @@ -310,6 +311,7 @@ Object {
30380051,
],
"locale": "en-US",
"metadata": Object {},
"orderId": "D7XM6Y",
"paymentIntentId": "123",
"shippingAddress": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ Object {
},
],
"currency": "EUR",
"customerId": 123,
"customerType": 0,
"formattedGrandTotal": "102 €",
"formattedSubTotalAmount": "100 €",
Expand All @@ -310,6 +311,7 @@ Object {
30380051,
],
"locale": "en-US",
"metadata": Object {},
"orderId": "D7XM6Y",
"paymentIntentId": "123",
"shippingAddress": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ Object {
},
],
"currency": "EUR",
"customerId": 123,
"customerType": 0,
"formattedGrandTotal": "102 €",
"formattedSubTotalAmount": "100 €",
Expand All @@ -310,6 +311,7 @@ Object {
30380051,
],
"locale": "en-US",
"metadata": Object {},
"orderId": "D7XM6Y",
"paymentIntentId": "123",
"promocode": "123",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ Object {
},
],
"currency": "EUR",
"customerId": 123,
"customerType": 0,
"formattedGrandTotal": "102 €",
"formattedSubTotalAmount": "100 €",
Expand All @@ -310,6 +311,7 @@ Object {
30380051,
],
"locale": "en-US",
"metadata": Object {},
"orderId": "D7XM6Y",
"paymentIntentId": "123",
"shippingAddress": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ Object {
},
],
"currency": "EUR",
"customerId": 123,
"customerType": 0,
"formattedGrandTotal": "102 €",
"formattedSubTotalAmount": "100 €",
Expand All @@ -310,6 +311,7 @@ Object {
30380051,
],
"locale": "en-US",
"metadata": Object {},
"orderId": "D7XM6Y",
"paymentIntentId": "123",
"shippingAddress": Object {
Expand Down Expand Up @@ -739,6 +741,7 @@ Object {
},
],
"currency": "EUR",
"customerId": 123,
"customerType": 0,
"formattedGrandTotal": "102 €",
"formattedSubTotalAmount": "100 €",
Expand All @@ -756,6 +759,7 @@ Object {
30380051,
],
"locale": "en-US",
"metadata": Object {},
"orderId": "D7XM6Y",
"paymentIntentId": "123",
"shippingAddress": Object {
Expand Down Expand Up @@ -1185,6 +1189,7 @@ Object {
},
],
"currency": "EUR",
"customerId": 123,
"customerType": 0,
"formattedGrandTotal": "102 €",
"formattedSubTotalAmount": "100 €",
Expand All @@ -1202,6 +1207,7 @@ Object {
30380051,
],
"locale": "en-US",
"metadata": Object {},
"orderId": "D7XM6Y",
"paymentIntentId": "123",
"shippingAddress": Object {
Expand Down
10 changes: 4 additions & 6 deletions tests/__fixtures__/checkout/checkout.fixtures.mts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ export const mockResponse = {
isGuestUser: true,
shippingMode: ShippingMode.ByMerchant,
status: CheckoutOrderStatus.Opened,
metadata: {},
customerId: 123,
},
deliveryBundles: mockDeliveryBundlesResponse,
shippingOptions: [],
Expand Down Expand Up @@ -791,12 +793,6 @@ export const mockCheckoutDetailsEntity = {
shippingOptions: [],
};

// export const checkoutOrderItemEntity = {
// id: checkoutOrderItemId,
// product: productId,
// tags: ['GIFT'],
// };

export const mockCheckoutOrderItemEntity = {
// Since a lot of properties are ommited for the CheckoutOrderItemEntity type,
// we didn't spread the properties from the original mockCheckoutOrderItem (...mockCheckoutOrderItem)
Expand Down Expand Up @@ -937,6 +933,8 @@ export const mockUpdateCheckoutResponse = {
},
orderStatus: OrderStatusError.NoError,
id: 123,
customerId: 123,
metadata: {},
},
},
};
Expand Down
1 change: 1 addition & 0 deletions tests/__fixtures__/checkout/index.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './checkout.fixtures.mjs';
export * from './checkoutSessions.fixtures.mjs';
export * from './draftOrders.fixtures.mjs';
export * from './packagingOptions.fixtures.mjs';
3 changes: 3 additions & 0 deletions tests/__fixtures__/checkout/packagingOptions.fixtures.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const mockPackagingOption = 'Basic';

export const mockPackagingOptionsResponse = [mockPackagingOption, 'Signature'];

0 comments on commit 8616eb7

Please sign in to comment.