Skip to content

Commit a5797d4

Browse files
author
Luis Sanchez
committed
feat(shopper): CHECKOUT-3277 Remove cart dependency from customer mapper
1 parent 69a8431 commit a5797d4

File tree

7 files changed

+18
-21
lines changed

7 files changed

+18
-21
lines changed

src/checkout/checkout-store-selector.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('CheckoutStoreSelector', () => {
7171
});
7272

7373
it('returns customer', () => {
74-
expect(selector.getCustomer()).toEqual(getCustomer()) ;
74+
expect(selector.getCustomer()).toEqual(internalSelectors.customer.getCustomer());
7575
});
7676

7777
it('returns billing address', () => {

src/checkout/checkout-store-selector.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { selector } from '../common/selector';
55
import { ConfigSelector } from '../config';
66
import { StoreConfig } from '../config/config';
77
import { Coupon, CouponSelector, GiftCertificate, GiftCertificateSelector } from '../coupon';
8-
import { mapToInternalCustomer, CustomerSelector, InternalCustomer } from '../customer';
8+
import { Customer, CustomerSelector } from '../customer';
99
import { FormField, FormSelector } from '../form';
1010
import { Country, CountrySelector } from '../geography';
1111
import { Order, OrderSelector } from '../order';
@@ -259,17 +259,8 @@ export default class CheckoutStoreSelector {
259259
* @returns The current customer object if it is loaded, otherwise
260260
* undefined.
261261
*/
262-
getCustomer(): InternalCustomer | undefined {
263-
const customer = this._customer.getCustomer();
264-
const checkout = this._checkout.getCheckout();
265-
const cart = checkout && checkout.cart;
266-
const billingAddress = checkout && checkout.billingAddress;
267-
268-
if (!customer || !billingAddress || !cart) {
269-
return;
270-
}
271-
272-
return mapToInternalCustomer(customer, cart, billingAddress);
262+
getCustomer(): Customer | undefined {
263+
return this._customer.getCustomer();
273264
}
274265

275266
/**

src/customer/customer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Address } from '../address';
22

33
export default interface Customer {
4+
id: number;
45
addresses: Address[];
56
storeCredit: number;
67
email: string;

src/customer/customers.mock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Customer from './customer';
44

55
export function getGuestCustomer(): Customer {
66
return {
7+
id: 0,
78
addresses: [],
89
email: '',
910
firstName: '',
@@ -16,6 +17,7 @@ export function getGuestCustomer(): Customer {
1617

1718
export function getCustomer(): Customer {
1819
return {
20+
id: 4,
1921
email: 'test@bigcommerce.com',
2022
firstName: 'Foo',
2123
fullName: 'Foo Bar',

src/customer/map-to-internal-customer.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ import mapToInternalCustomer from './map-to-internal-customer';
99

1010
describe('mapToInternalCustomer', () => {
1111
it('maps to internal guest customer', () => {
12-
expect(mapToInternalCustomer(getGuestCustomer(), { ...getCart(), customerId: 0 }, getBillingAddress()))
12+
expect(mapToInternalCustomer(getGuestCustomer(), getBillingAddress()))
1313
.toEqual(getInternalGuestCustomer());
1414
});
1515

16+
it('maps to internal guest customer', () => {
17+
expect(mapToInternalCustomer(getGuestCustomer()))
18+
.toEqual({ ...getInternalGuestCustomer(), email: '' });
19+
});
20+
1621
it('maps to internal customer', () => {
1722
const checkout = getCheckout();
1823

19-
expect(mapToInternalCustomer(checkout.customer, checkout.cart, checkout.billingAddress))
24+
expect(mapToInternalCustomer(checkout.customer, checkout.billingAddress))
2025
.toEqual(getInternalCustomer());
2126
});
2227
});

src/customer/map-to-internal-customer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { mapToInternalAddress, Address } from '../address';
2-
import { Cart } from '../cart';
32

43
import { Customer } from '.';
54
import InternalCustomer from './internal-customer';
65

7-
export default function mapToInternalCustomer(customer: Customer, cart: Cart, billingAddress: Address): InternalCustomer {
6+
export default function mapToInternalCustomer(customer: Customer, billingAddress?: Address): InternalCustomer {
87
return {
98
addresses: (customer.addresses || []).map(address => mapToInternalAddress(address)),
10-
customerId: cart.customerId,
9+
customerId: customer.id,
1110
isGuest: customer.isGuest,
1211
storeCredit: customer.storeCredit,
13-
email: customer.email || billingAddress.email || '',
12+
email: customer.email || (billingAddress && billingAddress.email) || '',
1413
firstName: customer.firstName,
1514
name: customer.fullName,
1615
};

src/payment/payment-action-creator.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ export default class PaymentActionCreator {
7979
const config = state.config.getStoreConfig();
8080
const instrumentMeta = state.instruments.getInstrumentsMeta();
8181
const paymentMeta = state.paymentMethods.getPaymentMethodsMeta();
82-
const internalCustomer = customer && billingAddress && checkout && checkout.cart &&
83-
mapToInternalCustomer(customer, checkout.cart, billingAddress);
82+
const internalCustomer = customer && mapToInternalCustomer(customer, billingAddress);
8483

8584
const authToken = instrumentMeta && isVaultedInstrument(payment.paymentData) ?
8685
`${state.payment.getPaymentToken()}, ${instrumentMeta.vaultAccessToken}` :

0 commit comments

Comments
 (0)