Skip to content

Commit 4fc1211

Browse files
@W-19135066: add saved shipping address (#2956)
Add saved shipping address to the 1CC user registration flow.
1 parent 1cb1791 commit 4fc1211

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

packages/template-retail-react-app/app/pages/checkout-one-click/index.jsx

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import {
2121
useAuthHelper,
2222
AuthHelpers,
2323
useShopperBasketsMutation,
24-
useShopperOrdersMutation
24+
useShopperOrdersMutation,
25+
useShopperCustomersMutation,
26+
ShopperCustomersMutations,
27+
ShopperBasketsMutations,
28+
ShopperOrdersMutations
2529
} from '@salesforce/commerce-sdk-react'
2630
import {useToast} from '@salesforce/retail-react-app/app/hooks/use-toast'
2731
import useNavigation from '@salesforce/retail-react-app/app/hooks/use-navigation'
@@ -43,6 +47,7 @@ import {
4347
getMaskCreditCardNumber
4448
} from '@salesforce/retail-react-app/app/utils/cc-utils'
4549
import {generatePassword} from '@salesforce/retail-react-app/app/utils/password-utils'
50+
import {nanoid} from 'nanoid'
4651

4752
const CheckoutOneClick = () => {
4853
const {formatMessage} = useIntl()
@@ -71,13 +76,16 @@ const CheckoutOneClick = () => {
7176
const appliedPayment = basket?.paymentInstruments && basket?.paymentInstruments[0]
7277

7378
const {mutateAsync: addPaymentInstrumentToBasket} = useShopperBasketsMutation(
74-
'addPaymentInstrumentToBasket'
79+
ShopperBasketsMutations.AddPaymentInstrumentToBasket
7580
)
7681
const {mutateAsync: updateBillingAddressForBasket} = useShopperBasketsMutation(
77-
'updateBillingAddressForBasket'
82+
ShopperBasketsMutations.UpdateBillingAddressForBasket
7883
)
79-
const {mutateAsync: createOrder} = useShopperOrdersMutation('createOrder')
84+
const {mutateAsync: createOrder} = useShopperOrdersMutation(ShopperOrdersMutations.CreateOrder)
8085
const {mutateAsync: register} = useAuthHelper(AuthHelpers.Register)
86+
const {mutateAsync: createCustomerAddress} = useShopperCustomersMutation(
87+
ShopperCustomersMutations.CreateCustomerAddress
88+
)
8189

8290
const showError = (message) => {
8391
showToast({
@@ -141,6 +149,17 @@ const CheckoutOneClick = () => {
141149
}
142150

143151
const submitOrder = async () => {
152+
const saveShippingAddress = async (customerId, address) => {
153+
try {
154+
await createCustomerAddress({
155+
body: address,
156+
parameters: {customerId: customerId}
157+
})
158+
} catch (error) {
159+
// Fail silently
160+
}
161+
}
162+
144163
const registerUser = async (data) => {
145164
try {
146165
const body = {
@@ -153,7 +172,10 @@ const CheckoutOneClick = () => {
153172
},
154173
password: generatePassword()
155174
}
156-
await register(body)
175+
const customer = await register(body)
176+
177+
// Save the shipping address from this order, should not block account creation
178+
await saveShippingAddress(customer.customerId, data.address)
157179

158180
showToast({
159181
variant: 'subtle',
@@ -197,11 +219,16 @@ const CheckoutOneClick = () => {
197219
})
198220

199221
if (enableUserRegistration) {
222+
// Remove the id property from the address
223+
const {id, ...address} = order.shipments[0].shippingAddress
224+
address.addressId = nanoid()
225+
200226
await registerUser({
201227
firstName: order.billingAddress.firstName,
202228
lastName: order.billingAddress.lastName,
203229
email: order.customerInfo.email,
204-
phoneHome: order.billingAddress.phone
230+
phoneHome: order.billingAddress.phone,
231+
address: address
205232
})
206233
}
207234

packages/template-retail-react-app/app/pages/checkout-one-click/index.test.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ jest.mock('@salesforce/pwa-kit-runtime/utils/ssr-config', () => {
3535
})
3636

3737
const mockUseAuthHelper = jest.fn()
38+
mockUseAuthHelper.mockResolvedValue({customerId: 'test-customer-id'})
39+
const mockUseShopperCustomersMutation = jest.fn()
3840
jest.mock('@salesforce/commerce-sdk-react', () => {
3941
const originalModule = jest.requireActual('@salesforce/commerce-sdk-react')
4042
return {
4143
...originalModule,
4244
useAuthHelper: () => ({
4345
mutateAsync: mockUseAuthHelper
46+
}),
47+
useShopperCustomersMutation: () => ({
48+
mutateAsync: mockUseShopperCustomersMutation
4449
})
4550
}
4651
})
@@ -204,7 +209,28 @@ beforeEach(() => {
204209
...currentBasket,
205210
...scapiOrderResponse,
206211
customerInfo: {...scapiOrderResponse.customerInfo, email: '[email protected]'},
207-
status: 'created'
212+
status: 'created',
213+
shipments: [
214+
{
215+
shippingAddress: {
216+
address1: '123 Main St',
217+
city: 'Tampa',
218+
countryCode: 'US',
219+
firstName: 'Test',
220+
fullName: 'Test McTester',
221+
id: '047b18d4aaaf4138f693a4b931',
222+
lastName: 'McTester',
223+
phone: '(727) 555-1234',
224+
postalCode: '33712',
225+
stateCode: 'FL'
226+
}
227+
}
228+
],
229+
billingAddress: {
230+
firstName: 'John',
231+
lastName: 'Smith',
232+
phone: '(727) 555-1234'
233+
}
208234
}
209235
return res(ctx.json(response))
210236
}),
@@ -695,4 +721,23 @@ test('Can register account during checkout as a guest', async () => {
695721
},
696722
password: expect.any(String)
697723
})
724+
725+
// Check that the shipping address is saved
726+
expect(mockUseShopperCustomersMutation).toHaveBeenCalledWith({
727+
body: {
728+
addressId: expect.any(String),
729+
address1: '123 Main St',
730+
city: 'Tampa',
731+
countryCode: 'US',
732+
firstName: 'Test',
733+
fullName: 'Test McTester',
734+
lastName: 'McTester',
735+
phone: '(727) 555-1234',
736+
postalCode: '33712',
737+
stateCode: 'FL'
738+
},
739+
parameters: {
740+
customerId: 'test-customer-id'
741+
}
742+
})
698743
})

0 commit comments

Comments
 (0)