Skip to content

Commit

Permalink
fix: Check if there is do_not_ship set to true and the order does…
Browse files Browse the repository at this point in the history
…n't have the shipping address
  • Loading branch information
acasazza committed Apr 8, 2022
1 parent 4b5ec96 commit c282728
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
10 changes: 8 additions & 2 deletions src/components/LineItemsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ const LineItemsContainer: FunctionComponent<LineItemsContainer> = (props) => {
useEffect(() => {
if (!include?.includes('line_items.line_item_options.sku_option')) {
addResourceToInclude({
newResource: 'line_items.line_item_options.sku_option',
newResource: [
'line_items.line_item_options.sku_option',
'line_items.item',
],
})
} else if (!includeLoaded?.['line_items.line_item_options.sku_option']) {
addResourceToInclude({
newResourceLoaded: { 'line_items.line_item_options.sku_option': true },
newResourceLoaded: {
'line_items.line_item_options.sku_option': true,
'line_items.item': true,
},
})
}
if (!isEmpty(order) && order?.line_items) {
Expand Down
29 changes: 15 additions & 14 deletions src/reducers/PlaceOrderReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseError } from '#typings/errors'
import { CommerceLayerConfig } from '#context/CommerceLayerContext'
import { Order, OrderUpdate } from '@commercelayer/sdk'
import isEmpty from 'lodash/isEmpty'
import { shipmentsFilled } from '#utils/shipments'
import { isDoNotShip, shipmentsFilled } from '#utils/shipments'
import { PaymentResource } from './PaymentMethodReducer'
import { PaymentSourceType } from './PaymentMethodReducer'
import {
Expand Down Expand Up @@ -89,7 +89,8 @@ export const placeOrderPermitted: PlaceOrderPermitted = async ({
const billingAddress = order.billing_address
if (isEmpty(billingAddress)) isPermitted = false
const shippingAddress = order.shipping_address
if (isEmpty(shippingAddress)) isPermitted = false
const doNotShip = isDoNotShip(order.line_items)
if (isEmpty(shippingAddress) && !doNotShip) isPermitted = false
const shipments = order.shipments
const shipment = shipments && shipmentsFilled(shipments)
if (!isEmpty(shipments) && !shipment) isPermitted = false
Expand Down Expand Up @@ -139,10 +140,10 @@ export const setPlaceOrder: SetPlaceOrder = async ({
const response = {
placed: false,
}
try {
if (state && order && config) {
const sdk = getSdk(config)
const { options, paymentType } = state
if (state && config && order) {
const sdk = getSdk(config)
const { options, paymentType } = state
try {
if (paymentType === 'paypal_payments' && paymentSource) {
if (!options?.paypalPayerId && paymentSource?.approval_url) {
window.location.href = paymentSource?.approval_url as string
Expand Down Expand Up @@ -228,16 +229,16 @@ export const setPlaceOrder: SetPlaceOrder = async ({
}
}
}
}
return response
} catch (error) {
const errors = getErrors(error, 'orders')
setOrderErrors && setOrderErrors(errors)
return {
...response,
errors,
} catch (error) {
const errors = getErrors(error, 'orders', paymentType)
setOrderErrors && setOrderErrors(errors)
return {
...response,
errors,
}
}
}
return response
}

const type: PlaceOrderActionType[] = ['setErrors', 'setPlaceOrderPermitted']
Expand Down
17 changes: 16 additions & 1 deletion src/utils/shipments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Shipment } from '@commercelayer/sdk'
import { LineItem, Shipment } from '@commercelayer/sdk'
import compact from 'lodash/compact'
import isEmpty from 'lodash/isEmpty'

Expand All @@ -8,3 +8,18 @@ export function shipmentsFilled(shipments: Shipment[]) {
)
return !isEmpty(filled)
}

export function isDoNotShip(lineItems?: LineItem[]) {
const itemDoNotShip: boolean[] = []
const items = lineItems
? lineItems
.filter(({ item_type }) => item_type === 'skus')
.map((lineItem) => {
// @ts-ignore
if (lineItem.item && lineItem.item?.do_not_ship)
itemDoNotShip.push(true)
return lineItem
})
: []
return itemDoNotShip.length > 0 && itemDoNotShip.length === items.length
}

0 comments on commit c282728

Please sign in to comment.