Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add promocodes and tags checkout sessions endpoints #869

Merged
merged 2 commits into from
Sep 18, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,7 @@ Object {
"getCheckoutOrderPaymentMethods": [Function],
"getCheckoutSession": [Function],
"getCheckoutSessionCharge": [Function],
"getCheckoutSessionTags": [Function],
"getCollectPoints": [Function],
"getCommercePages": [Function],
"getConfiguration": [Function],
Expand Down Expand Up @@ -1235,6 +1236,8 @@ Object {
"putCheckoutOrderItemTags": [Function],
"putCheckoutOrderPromocodes": [Function],
"putCheckoutOrderTags": [Function],
"putCheckoutSessionPromocodes": [Function],
"putCheckoutSessionTags": [Function],
"putPaymentIntentInstrument": [Function],
"putSharedWishlist": [Function],
"putSubscriptions": [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 { CheckoutSessionTags } from '../types/index.js';

const path = '/api/checkout/v1/checkoutSessions/:id/tags';

const fixtures = {
success: (response: CheckoutSessionTags): 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,14 @@
import { rest, type RestHandler } from 'msw';

const path = '/api/checkout/v1/checkoutSessions/:id/promocodes';

const fixtures = {
success: (): RestHandler =>
rest.put(path, (_req, res, ctx) => res(ctx.status(204))),
failure: (): RestHandler =>
rest.put(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,14 @@
import { rest, type RestHandler } from 'msw';

const path = '/api/checkout/v1/checkoutSessions/:id/tags';

const fixtures = {
success: (): RestHandler =>
rest.put(path, (_req, res, ctx) => res(ctx.status(204))),
failure: (): RestHandler =>
rest.put(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 getCheckoutSessionTags 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
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`checkout client putCheckoutSessionPromocodes 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
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`checkout client putCheckoutSessionTags 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
@@ -0,0 +1,37 @@
import { getCheckoutSessionTags } from '../index.js';
import { mockCheckoutSessionId } from 'tests/__fixtures__/checkout/index.mjs';
import client from '../../helpers/client/index.js';
import fixtures from '../__fixtures__/getCheckoutSessionTags.fixtures.js';
import mswServer from '../../../tests/mswServer.js';
import type { CheckoutSessionTags } from '../types/index.js';

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

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

describe('getCheckoutSessionTags', () => {
const spy = jest.spyOn(client, 'get');
const urlToBeCalled = `/checkout/v1/checkoutSessions/${mockCheckoutSessionId}/tags`;

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

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

await expect(
getCheckoutSessionTags(mockCheckoutSessionId),
).resolves.toStrictEqual(response);
expect(spy).toHaveBeenCalledWith(urlToBeCalled, expectedConfig);
});

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

await expect(
getCheckoutSessionTags(mockCheckoutSessionId),
).rejects.toMatchSnapshot();
expect(spy).toHaveBeenCalledWith(urlToBeCalled, expectedConfig);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import client from '../../helpers/client/index.js';

import putCheckoutSessionPromocodes from '../putCheckoutSessionPromocodes.js';

import mswServer from '../../../tests/mswServer.js';

import { mockCheckoutSessionId } from 'tests/__fixtures__/checkout/checkoutSessions.fixtures.mjs';
import fixtures from '../__fixtures__/putCheckoutSessionPromocodes.fixtures.js';

import type { PutCheckoutSessionPromocodesData } from '../types/index.js';

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

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

describe('putCheckoutSessionPromocodes', () => {
const data: PutCheckoutSessionPromocodesData = {
promocode: 'test_promocodes',
};

const spy = jest.spyOn(client, 'put');
const urlToBeCalled = `/checkout/v1/checkoutSessions/${mockCheckoutSessionId}/promocodes`;

it('should handle a client request successfully', async () => {
mswServer.use(fixtures.success());

await expect(
putCheckoutSessionPromocodes(mockCheckoutSessionId, data),
).resolves.toBe(204);

expect(spy).toHaveBeenCalledWith(urlToBeCalled, data, expectedConfig);
});

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

await expect(
putCheckoutSessionPromocodes(mockCheckoutSessionId, data),
).rejects.toMatchSnapshot();

expect(spy).toHaveBeenCalledWith(urlToBeCalled, data, expectedConfig);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import client from '../../helpers/client/index.js';

import putCheckoutSessionTags from '../putCheckoutSessionTags.js';

import mswServer from '../../../tests/mswServer.js';

import { mockCheckoutSessionId } from 'tests/__fixtures__/checkout/checkoutSessions.fixtures.mjs';
import fixtures from '../__fixtures__/putCheckoutSessionTags.fixtures.js';

import type { CheckoutSessionTags } from '../types/index.js';

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

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

describe('putCheckoutSessionTags', () => {
const data: CheckoutSessionTags = ['someTag'];

const spy = jest.spyOn(client, 'put');
const urlToBeCalled = `/checkout/v1/checkoutSessions/${mockCheckoutSessionId}/tags`;

it('should handle a client request successfully', async () => {
mswServer.use(fixtures.success());

await expect(
putCheckoutSessionTags(mockCheckoutSessionId, data),
).resolves.toBe(204);

expect(spy).toHaveBeenCalledWith(urlToBeCalled, data, expectedConfig);
});

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

await expect(
putCheckoutSessionTags(mockCheckoutSessionId, data),
).rejects.toMatchSnapshot();

expect(spy).toHaveBeenCalledWith(urlToBeCalled, data, expectedConfig);
});
});
});
28 changes: 28 additions & 0 deletions packages/client/src/checkout/getCheckoutSessionTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { adaptError } from '../helpers/client/formatError.js';
import client from '../helpers/client/index.js';
import join from 'proper-url-join';
import type { GetCheckoutSessionTags } from './types/getCheckoutSessionTags.types.js';

/**
* Method responsible for getting the checkout session tags.
*
* @param checkoutSessionId - Id of the checkout session to get.
* @param config - Custom configurations to send to the client instance (axios).
*
* @returns Promise that will resolve when the call to the endpoint finishes.
*/
const getCheckoutSessionTags: GetCheckoutSessionTags = (
checkoutSessionId,
config,
) =>
client
.get(
join('/checkout/v1/checkoutSessions', checkoutSessionId, 'tags'),
config,
)
.then(response => response.data)
.catch(error => {
throw adaptError(error);
});

export default getCheckoutSessionTags;
3 changes: 3 additions & 0 deletions packages/client/src/checkout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export { default as getCheckoutOrderOperations } from './getCheckoutOrderOperati
export { default as getCheckoutOrderPaymentMethods } from './getCheckoutOrderPaymentMethods.js';
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 patchCheckoutOrder } from './patchCheckoutOrder.js';
export { default as patchCheckoutOrderDeliveryBundleUpgrades } from './patchCheckoutOrderDeliveryBundleUpgrades.js';
Expand All @@ -31,6 +32,8 @@ export { default as postCheckoutSessionCharge } from './postCheckoutSessionCharg
export { default as putCheckoutOrderItemTags } from './putCheckoutOrderItemTags.js';
export { default as putCheckoutOrderPromocodes } from './putCheckoutOrderPromocodes.js';
export { default as putCheckoutOrderTags } from './putCheckoutOrderTags.js';
export { default as putCheckoutSessionPromocodes } from './putCheckoutSessionPromocodes.js';
export { default as putCheckoutSessionTags } from './putCheckoutSessionTags.js';

/**
* Checkout types.
Expand Down
31 changes: 31 additions & 0 deletions packages/client/src/checkout/putCheckoutSessionPromocodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { adaptError } from '../helpers/client/formatError.js';
import client from '../helpers/client/index.js';
import join from 'proper-url-join';
import type { PutCheckoutSessionPromocodes } from './types/index.js';

/**
* Method responsible for setting the promocode for the checkout session.
*
* @param checkoutSessionId - Universal identifier of the Checkout Session.
* @param data - Promocode data.
* @param config - Custom configurations to send to the client instance (axios).
*
* @returns Promise that will resolve when the call to the endpoint finishes.
*/
const putCheckoutSessionPromocodes: PutCheckoutSessionPromocodes = (
checkoutSessionId,
data,
config,
) =>
client
.put(
join('/checkout/v1/checkoutSessions/', checkoutSessionId, 'promocodes'),
data,
config,
)
.then(response => response.status)
.catch(error => {
throw adaptError(error);
});

export default putCheckoutSessionPromocodes;
31 changes: 31 additions & 0 deletions packages/client/src/checkout/putCheckoutSessionTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { adaptError } from '../helpers/client/formatError.js';
import client from '../helpers/client/index.js';
import join from 'proper-url-join';
import type { PutCheckoutSessionTags } from './types/index.js';

/**
* Method responsible for setting tags for the checkout session.
*
* @param checkoutSessionId - Universal identifier of the Checkout Session.
* @param data - Tags data.
* @param config - Custom configurations to send to the client instance (axios).
*
* @returns Promise that will resolve when the call to the endpoint finishes.
*/
const putCheckoutSessionTags: PutCheckoutSessionTags = (
checkoutSessionId,
data,
config,
) =>
client
.put(
join('/checkout/v1/checkoutSessions', checkoutSessionId, 'tags'),
data,
config,
)
.then(response => response.status)
.catch(error => {
throw adaptError(error);
});

export default putCheckoutSessionTags;
3 changes: 3 additions & 0 deletions packages/client/src/checkout/types/checkoutSession.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ export type CheckoutSession = {
shippingAddress?: CheckoutShippingAddress;
billingAddress?: CheckoutAddress;
clickAndCollect?: ClickAndCollect;
promotionEvaluationId?: string;
totalCredit?: number;
promocode?: string;
} & Controls;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { CheckoutSession } from './checkoutSession.types.js';
import type { Config } from '../../types/index.js';

export type CheckoutSessionTags = string[];

export type GetCheckoutSessionTags = (
checkoutSessionId: CheckoutSession['id'],
config?: Config,
) => Promise<CheckoutSessionTags>;
3 changes: 3 additions & 0 deletions packages/client/src/checkout/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * from './getCheckoutOrderPaymentMethods.types.js';
export * from './getCheckoutOrderResponse.types.js';
export * from './getCheckoutSession.types.js';
export * from './getCheckoutSessionCharge.types.js';
export * from './getCheckoutSessionTags.types.js';
export * from './getCollectPoints.types.js';
export * from './itemDeliveryProvisioning.types.js';
export * from './patchCheckoutOrder.types.js';
Expand All @@ -43,5 +44,7 @@ export * from './postCheckoutSessionCharge.types.js';
export * from './putCheckoutOrderItemTags.types.js';
export * from './putCheckoutOrderPromocodes.types.js';
export * from './putCheckoutOrderTags.types.js';
export * from './putCheckoutSessionPromocodes.types.js';
export * from './putCheckoutSessionTags.types.js';
export * from './shippingCostType.types.js';
export * from './shippingOption.types.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { CheckoutSession } from './checkoutSession.types.js';
import type { Config } from '../../types/index.js';

export type PutCheckoutSessionPromocodesData = {
promocode: string;
};

export type PutCheckoutSessionPromocodes = (
checkoutSessionId: CheckoutSession['id'],
data: PutCheckoutSessionPromocodesData,
config?: Config,
) => Promise<number>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { CheckoutSession } from './checkoutSession.types.js';
import type { CheckoutSessionTags } from './getCheckoutSessionTags.types.js';
import type { Config } from '../../types/index.js';

export type PutCheckoutSessionTags = (
checkoutSessionId: CheckoutSession['id'],
data: CheckoutSessionTags,
config?: Config,
) => Promise<number>;
Loading