Skip to content
Closed
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 @@ -57,7 +57,7 @@ import {nanoid} from 'nanoid'
const CheckoutOneClick = () => {
const {formatMessage} = useIntl()
const navigate = useNavigation()
const {step, STEPS} = useCheckout()
const {step, STEPS, contactPhone} = useCheckout()
const showToast = useToast()
const [isLoading, setIsLoading] = useState(false)
const [enableUserRegistration, setEnableUserRegistration] = useState(false)
Expand Down Expand Up @@ -369,15 +369,19 @@ const CheckoutOneClick = () => {
}
})
}
}

// Also persist billing phone as phoneHome
const phoneHome = order?.billingAddress?.phone
if (phoneHome) {
await updateCustomer.mutateAsync({
parameters: {customerId},
body: {phoneHome}
})
}
// Persist phone number as phoneHome for all order types (delivery and pickup)
// Try billing address phone first, then fall back to contact phone from context
const phoneHome =
order?.billingAddress?.phone ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting - we actually trying to extract phone number from the billing address?? So that means the field is required (?) I'd assume we should rely on the one filled out at the Contact Info stage..
Also wondering why this does not work for BOPIS (even though there is a phone number field on billing address form for BOPIS orders)

contactPhone ||
basket?.customerInfo?.phone
if (phoneHome) {
await updateCustomer.mutateAsync({
parameters: {customerId},
body: {phoneHome}
})
}
} catch (_e) {
// Only surface error if shopper opted to register/save details; otherwise fail silently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1717,4 +1717,255 @@ describe('Checkout One Click', () => {
expect(calls[2][0].body.address1).toBe('789 Pine Rd')
expect(calls[2][0].body.city).toBe('Miami')
})

test('saves phone number to customer profile for pickup-only orders during user registration', async () => {
// Reset mock
mockUseShopperCustomersMutation.mockClear()

// Create a pickup-only order (no delivery shipments)
const pickupOnlyOrder = {
orderNo: '00000101',
customerInfo: {
customerId: 'new-customer-id',
customerNo: 'guest123',
email: 'pickup@test.com'
},
shipments: [
{
shipmentId: 'pickup1',
c_fromStoreId: 'store1',
shippingMethod: {id: 'PICKUP', c_storePickupEnabled: true},
shippingAddress: {
firstName: 'Store 1',
lastName: 'Pickup',
address1: '1 Market St',
city: 'San Francisco',
postalCode: '94105',
stateCode: 'CA',
countryCode: 'US'
}
}
],
billingAddress: {
firstName: 'John',
lastName: 'Doe',
phone: '(555) 123-4567'
}
}

const currentCustomer = {isRegistered: true}
const registeredUserChoseGuest = false
const enableUserRegistration = true
const contactPhone = '(555) 987-6543' // Phone from contact info form

// Simulate the phone saving logic from index.jsx
const customerId = pickupOnlyOrder.customerInfo?.customerId
if (customerId) {
const {isPickupShipment} = await import(
'@salesforce/retail-react-app/app/utils/shipment-utils'
)
const deliveryShipments =
pickupOnlyOrder?.shipments?.filter(
(shipment) => !isPickupShipment(shipment) && shipment.shippingAddress
) || []

if (
enableUserRegistration &&
currentCustomer?.isRegistered &&
!registeredUserChoseGuest
) {
// For pickup-only orders, deliveryShipments.length will be 0
// but phone number should still be saved
if (deliveryShipments.length > 0) {
// This block won't execute for pickup-only orders
// but we're testing that phone is saved regardless
}

// Persist phone number as phoneHome for all order types (delivery and pickup)
// Try billing address phone first, then fall back to contact phone from context
const phoneHome =
pickupOnlyOrder?.billingAddress?.phone ||
contactPhone ||
null
if (phoneHome) {
await mockUseShopperCustomersMutation({
parameters: {customerId},
body: {phoneHome}
})
}
}
}

// Verify updateCustomer was called with phoneHome
expect(mockUseShopperCustomersMutation).toHaveBeenCalledTimes(1)
expect(mockUseShopperCustomersMutation).toHaveBeenCalledWith({
parameters: {customerId: 'new-customer-id'},
body: {phoneHome: '(555) 123-4567'} // Should use billing address phone first
})
})

test('saves phone number from contact info when billing address phone is missing for pickup orders', async () => {
// Reset mock
mockUseShopperCustomersMutation.mockClear()

// Create a pickup-only order without billing address phone
const pickupOnlyOrder = {
orderNo: '00000101',
customerInfo: {
customerId: 'new-customer-id',
customerNo: 'guest123',
email: 'pickup@test.com'
},
shipments: [
{
shipmentId: 'pickup1',
c_fromStoreId: 'store1',
shippingMethod: {id: 'PICKUP', c_storePickupEnabled: true},
shippingAddress: {
firstName: 'Store 1',
lastName: 'Pickup',
address1: '1 Market St',
city: 'San Francisco',
postalCode: '94105',
stateCode: 'CA',
countryCode: 'US'
}
}
],
billingAddress: {
firstName: 'John',
lastName: 'Doe'
// No phone in billing address
}
}

const currentCustomer = {isRegistered: true}
const registeredUserChoseGuest = false
const enableUserRegistration = true
const contactPhone = '(555) 987-6543' // Phone from contact info form

// Simulate the phone saving logic from index.jsx
const customerId = pickupOnlyOrder.customerInfo?.customerId
if (customerId) {
const {isPickupShipment} = await import(
'@salesforce/retail-react-app/app/utils/shipment-utils'
)
const deliveryShipments =
pickupOnlyOrder?.shipments?.filter(
(shipment) => !isPickupShipment(shipment) && shipment.shippingAddress
) || []

if (
enableUserRegistration &&
currentCustomer?.isRegistered &&
!registeredUserChoseGuest
) {
// Persist phone number as phoneHome for all order types (delivery and pickup)
// Try billing address phone first, then fall back to contact phone from context
const phoneHome =
pickupOnlyOrder?.billingAddress?.phone ||
contactPhone ||
null
if (phoneHome) {
await mockUseShopperCustomersMutation({
parameters: {customerId},
body: {phoneHome}
})
}
}
}

// Verify updateCustomer was called with phoneHome from contact info
expect(mockUseShopperCustomersMutation).toHaveBeenCalledTimes(1)
expect(mockUseShopperCustomersMutation).toHaveBeenCalledWith({
parameters: {customerId: 'new-customer-id'},
body: {phoneHome: '(555) 987-6543'} // Should fall back to contact phone
})
})

test('saves phone number to customer profile for delivery orders during user registration', async () => {
// Reset mock
mockUseShopperCustomersMutation.mockClear()

// Create a delivery order
const deliveryOrder = {
orderNo: '00000101',
customerInfo: {
customerId: 'new-customer-id',
customerNo: 'guest123',
email: 'delivery@test.com'
},
shipments: [
{
shipmentId: 'shipment1',
shippingMethod: {
id: '002',
c_storePickupEnabled: false
},
shippingAddress: {
address1: '123 Main St',
city: 'Tampa',
countryCode: 'US',
firstName: 'Test',
lastName: 'User',
phone: '(727) 555-1234',
postalCode: '33712',
stateCode: 'FL'
}
}
],
billingAddress: {
firstName: 'John',
lastName: 'Smith',
phone: '(555) 999-8888'
}
}

const currentCustomer = {isRegistered: true}
const registeredUserChoseGuest = false
const enableUserRegistration = true
const contactPhone = '(555) 777-6666'

// Simulate the phone saving logic from index.jsx
const customerId = deliveryOrder.customerInfo?.customerId
if (customerId) {
const {isPickupShipment} = await import(
'@salesforce/retail-react-app/app/utils/shipment-utils'
)
const deliveryShipments =
deliveryOrder?.shipments?.filter(
(shipment) => !isPickupShipment(shipment) && shipment.shippingAddress
) || []

if (
enableUserRegistration &&
currentCustomer?.isRegistered &&
!registeredUserChoseGuest
) {
// Save delivery addresses (this would happen in real code)
if (deliveryShipments.length > 0) {
// Address saving would happen here, but we're just testing phone
}

// Persist phone number as phoneHome for all order types (delivery and pickup)
const phoneHome =
deliveryOrder?.billingAddress?.phone ||
contactPhone ||
null
if (phoneHome) {
await mockUseShopperCustomersMutation({
parameters: {customerId},
body: {phoneHome}
})
}
}
}

// Verify updateCustomer was called with phoneHome
expect(mockUseShopperCustomersMutation).toHaveBeenCalledTimes(1)
expect(mockUseShopperCustomersMutation).toHaveBeenCalledWith({
parameters: {customerId: 'new-customer-id'},
body: {phoneHome: '(555) 999-8888'} // Should use billing address phone
})
})
})
Loading