Skip to content

Commit

Permalink
feat: expose missing address endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-gbf committed Jan 8, 2024
1 parent c8ca25b commit 3bc62bb
Show file tree
Hide file tree
Showing 26 changed files with 633 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,9 @@ Object {
"deleteUserAttribute": [Function],
"deleteUserClosetItem": [Function],
"deleteUserContact": [Function],
"deleteUserDefaultBillingAddress": [Function],
"deleteUserDefaultContactAddress": [Function],
"deleteUserDefaultShippingAddress": [Function],
"deleteUserExternalLogin": [Function],
"deleteUserPersonalId": [Function],
"deleteWishlistItem": [Function],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { rest, type RestHandler } from 'msw';

const path = '/api/account/v1/users/:userId/addresses/billing/current';

const fixtures = {
success: (): RestHandler =>
rest.delete(path, (_req, res, ctx) => res(ctx.status(204))),
failure: (): RestHandler =>
rest.delete(path, (_req, res, ctx) =>
res(
ctx.status(400),
ctx.json({
errors: [
{
code: 0,
message: 'error',
developerMessage: 'This is developer message',
moreInformation: 'Error more information',
exception: {},
},
],
}),
),
),
};

export default fixtures;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { rest, type RestHandler } from 'msw';

const path = '/api/account/v1/users/:userId/addresses/shipping/current';

const fixtures = {
success: (): RestHandler =>
rest.delete(path, (_req, res, ctx) => res(ctx.status(204))),
failure: (): RestHandler =>
rest.delete(path, (_req, res, ctx) =>
res(
ctx.status(400),
ctx.json({
errors: [
{
code: 0,
message: 'error',
developerMessage: 'This is developer message',
moreInformation: 'Error more information',
exception: {},
},
],
}),
),
),
};

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

exports[`deleteUserDefaultBillingAddress should receive a client request error 1`] = `
Object {
"code": 0,
"developerMessage": "This is developer message",
"exception": Object {},
"message": "error",
"moreInformation": "Error more information",
"name": "AxiosError",
"status": 400,
"transportLayerErrorCode": "ERR_BAD_REQUEST",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`deleteUserDefaultShippingAddress should receive a client request error 1`] = `
Object {
"code": 0,
"developerMessage": "This is developer message",
"exception": Object {},
"message": "error",
"moreInformation": "Error more information",
"name": "AxiosError",
"status": 400,
"transportLayerErrorCode": "ERR_BAD_REQUEST",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { deleteUserDefaultBillingAddress } from '../index.js';
import { userId } from 'tests/__fixtures__/addresses/index.mjs';
import client from '../../../helpers/client/index.js';
import fixtures from '../__fixtures__/deleteUserDefaultBillingAddress.fixtures.js';
import mswServer from '../../../../tests/mswServer.js';

describe('deleteUserDefaultBillingAddress', () => {
const expectedConfig = undefined;
const spy = jest.spyOn(client, 'delete');
const expectedUrl = `/account/v1/users/${userId}/addresses/billing/current`;

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

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

await expect(deleteUserDefaultBillingAddress(userId)).resolves.toBe(204);
expect(spy).toHaveBeenCalledWith(expectedUrl, expectedConfig);
});

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

await expect(
deleteUserDefaultBillingAddress(userId),
).rejects.toMatchSnapshot();
expect(spy).toHaveBeenCalledWith(expectedUrl, expectedConfig);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { deleteUserDefaultShippingAddress } from '../index.js';
import { userId } from 'tests/__fixtures__/addresses/index.mjs';
import client from '../../../helpers/client/index.js';
import fixtures from '../__fixtures__/deleteUserDefaultShippingAddress.fixtures.js';
import mswServer from '../../../../tests/mswServer.js';

describe('deleteUserDefaultShippingAddress', () => {
const expectedConfig = undefined;
const spy = jest.spyOn(client, 'delete');
const expectedUrl = `/account/v1/users/${userId}/addresses/shipping/current`;

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

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

await expect(deleteUserDefaultShippingAddress(userId)).resolves.toBe(204);
expect(spy).toHaveBeenCalledWith(expectedUrl, expectedConfig);
});

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

await expect(
deleteUserDefaultShippingAddress(userId),
).rejects.toMatchSnapshot();
expect(spy).toHaveBeenCalledWith(expectedUrl, expectedConfig);
});
});
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 { DeleteUserDefaultBillingAddress } from './types/index.js';

/**
* Responsible for unsetting the users default billing address.
*
* @param userId - Identifier of the user.
* @param config - Custom configurations to send to the client instance (axios).
*
* @returns Promise that will resolve when the call to the endpoint finishes.
*/
const deleteUserDefaultBillingAddress: DeleteUserDefaultBillingAddress = (
userId,
config,
) =>
client
.delete(
join('/account/v1/users', userId, 'addresses/billing/current'),
config,
)
.then(response => response.status)
.catch(error => {
throw adaptError(error);
});

export default deleteUserDefaultBillingAddress;
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 { DeleteUserDefaultShippingAddress } from './types/index.js';

/**
* Responsible for unsetting the users default shipping address.
*
* @param userId - Identifier of the user.
* @param config - Custom configurations to send to the client instance (axios).
*
* @returns Promise that will resolve when the call to the endpoint finishes.
*/
const deleteUserDefaultShippingAddress: DeleteUserDefaultShippingAddress = (
userId,
config,
) =>
client
.delete(
join('/account/v1/users', userId, 'addresses/shipping/current'),
config,
)
.then(response => response.status)
.catch(error => {
throw adaptError(error);
});

export default deleteUserDefaultShippingAddress;
2 changes: 2 additions & 0 deletions packages/client/src/users/addresses/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export { default as deleteUserAddress } from './deleteUserAddress.js';
export { default as deleteUserDefaultContactAddress } from './deleteUserDefaultContactAddress.js';
export { default as deleteUserDefaultBillingAddress } from './deleteUserDefaultBillingAddress.js';
export { default as deleteUserDefaultShippingAddress } from './deleteUserDefaultShippingAddress.js';
export { default as getUserAddress } from './getUserAddress.js';
export { default as getUserAddresses } from './getUserAddresses.js';
export { default as getUserDefaultContactAddress } from './getUserDefaultContactAddress.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Config } from '../../../types/index.js';
import type { User } from '../../authentication/types/user.types.js';

export type DeleteUserDefaultBillingAddress = (
userId: User['id'],
config?: Config,
) => Promise<number>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Config } from '../../../types/index.js';
import type { User } from '../../authentication/types/user.types.js';

export type DeleteUserDefaultShippingAddress = (
userId: User['id'],
config?: Config,
) => Promise<number>;
2 changes: 2 additions & 0 deletions packages/client/src/users/addresses/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './deleteUserAddress.types.js';
export * from './deleteUserDefaultBillingAddress.types.js';
export * from './deleteUserDefaultContactAddress.types.js';
export * from './deleteUserDefaultShippingAddress.types.js';
export * from './getDefaultContactAddress.types.js';
export * from './getUserAddress.types.js';
export * from './getUserAddresses.types.js';
Expand Down
10 changes: 10 additions & 0 deletions packages/redux/src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1657,8 +1657,12 @@ Object {
"removeUserClosetItemFactory": [Function],
"removeUserContact": [Function],
"removeUserContactFactory": [Function],
"removeUserDefaultBillingAddress": [Function],
"removeUserDefaultBillingAddressFactory": [Function],
"removeUserDefaultContactAddress": [Function],
"removeUserDefaultContactAddressFactory": [Function],
"removeUserDefaultShippingAddress": [Function],
"removeUserDefaultShippingAddressFactory": [Function],
"removeUserPersonalId": [Function],
"removeUserPersonalIdFactory": [Function],
"removeWishlistItem": [Function],
Expand Down Expand Up @@ -2042,9 +2046,15 @@ Object {
"REMOVE_USER_CONTACT_FAILURE": "@farfetch/blackout-redux/REMOVE_USER_CONTACT_FAILURE",
"REMOVE_USER_CONTACT_REQUEST": "@farfetch/blackout-redux/REMOVE_USER_CONTACT_REQUEST",
"REMOVE_USER_CONTACT_SUCCESS": "@farfetch/blackout-redux/REMOVE_USER_CONTACT_SUCCESS",
"REMOVE_USER_DEFAULT_BILLING_ADDRESS_FAILURE": "@farfetch/blackout-redux/REMOVE_USER_DEFAULT_BILLING_ADDRESS_FAILURE",
"REMOVE_USER_DEFAULT_BILLING_ADDRESS_REQUEST": "@farfetch/blackout-redux/REMOVE_USER_DEFAULT_BILLING_ADDRESS_REQUEST",
"REMOVE_USER_DEFAULT_BILLING_ADDRESS_SUCCESS": "@farfetch/blackout-redux/REMOVE_USER_DEFAULT_BILLING_ADDRESS_SUCCESS",
"REMOVE_USER_DEFAULT_CONTACT_ADDRESS_FAILURE": "@farfetch/blackout-redux/REMOVE_USER_DEFAULT_CONTACT_ADDRESS_FAILURE",
"REMOVE_USER_DEFAULT_CONTACT_ADDRESS_REQUEST": "@farfetch/blackout-redux/REMOVE_USER_DEFAULT_CONTACT_ADDRESS_REQUEST",
"REMOVE_USER_DEFAULT_CONTACT_ADDRESS_SUCCESS": "@farfetch/blackout-redux/REMOVE_USER_DEFAULT_CONTACT_ADDRESS_SUCCESS",
"REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_FAILURE": "@farfetch/blackout-redux/REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_FAILURE",
"REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_REQUEST": "@farfetch/blackout-redux/REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_REQUEST",
"REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_SUCCESS": "@farfetch/blackout-redux/REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_SUCCESS",
"REMOVE_USER_PERSONAL_ID_FAILURE": "@farfetch/blackout-redux/REMOVE_USER_PERSONAL_ID_FAILURE",
"REMOVE_USER_PERSONAL_ID_REQUEST": "@farfetch/blackout-redux/REMOVE_USER_PERSONAL_ID_REQUEST",
"REMOVE_USER_PERSONAL_ID_SUCCESS": "@farfetch/blackout-redux/REMOVE_USER_PERSONAL_ID_SUCCESS",
Expand Down
32 changes: 32 additions & 0 deletions packages/redux/src/users/addresses/actionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,38 @@ export const REMOVE_USER_DEFAULT_CONTACT_ADDRESS_REQUEST =
export const REMOVE_USER_DEFAULT_CONTACT_ADDRESS_SUCCESS =
'@farfetch/blackout-redux/REMOVE_USER_DEFAULT_CONTACT_ADDRESS_SUCCESS';

/**
* Action type dispatched when delete default contact address request fails.
*/
export const REMOVE_USER_DEFAULT_BILLING_ADDRESS_FAILURE =
'@farfetch/blackout-redux/REMOVE_USER_DEFAULT_BILLING_ADDRESS_FAILURE';
/**
* Action type dispatched when delete default contact address request starts.
*/
export const REMOVE_USER_DEFAULT_BILLING_ADDRESS_REQUEST =
'@farfetch/blackout-redux/REMOVE_USER_DEFAULT_BILLING_ADDRESS_REQUEST';
/**
* Action type dispatched when delete default contact address request succeeds.
*/
export const REMOVE_USER_DEFAULT_BILLING_ADDRESS_SUCCESS =
'@farfetch/blackout-redux/REMOVE_USER_DEFAULT_BILLING_ADDRESS_SUCCESS';

/**
* Action type dispatched when delete default contact address request fails.
*/
export const REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_FAILURE =
'@farfetch/blackout-redux/REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_FAILURE';
/**
* Action type dispatched when delete default contact address request starts.
*/
export const REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_REQUEST =
'@farfetch/blackout-redux/REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_REQUEST';
/**
* Action type dispatched when delete default contact address request succeeds.
*/
export const REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_SUCCESS =
'@farfetch/blackout-redux/REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_SUCCESS';

/**
* Action type dispatched when set default billing address request fails.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`removeUserDefaultBillingAddress() action creator should create the correct actions for when the delete user default billing address procedure is successful: delete user default billing address success payload 1`] = `
Object {
"meta": Object {
"addressId": "2222222",
"userId": 29556478,
},
"type": "@farfetch/blackout-redux/REMOVE_USER_DEFAULT_BILLING_ADDRESS_SUCCESS",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`removeUserDefaultShippingAddress() action creator should create the correct actions for when the delete user default shipping address procedure is successful: delete user default shipping address success payload 1`] = `
Object {
"meta": Object {
"addressId": "2222222",
"userId": 29556478,
},
"type": "@farfetch/blackout-redux/REMOVE_USER_DEFAULT_SHIPPING_ADDRESS_SUCCESS",
}
`;
Loading

0 comments on commit 3bc62bb

Please sign in to comment.