Skip to content

Commit

Permalink
feat: check for out of stock and shipping method available for shipments
Browse files Browse the repository at this point in the history
  • Loading branch information
malessani authored and acasazza committed Mar 28, 2022
1 parent 2fc6fd5 commit 38000b6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
68 changes: 64 additions & 4 deletions src/components/ShipmentsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,30 @@ import CommerceLayerContext from '#context/CommerceLayerContext'
import components from '#config/components'
import { BaseError } from '#typings/errors'
import { isEmpty } from 'lodash'
import Parent from './utils/Parent'
import { FunctionChildren } from '#typings'

const propTypes = components.ShipmentsContainer.propTypes
const displayName = components.ShipmentsContainer.displayName

type ShipmentsContainerChildrenProps = FunctionChildren<
Omit<ShipmentsContainerProps, 'children'> & {
errors: BaseError[]
}
>

type ShipmentsContainerProps = {
children: ReactNode
children: ShipmentsContainerChildrenProps
}
const ShipmentsContainer: FunctionComponent<ShipmentsContainerProps> = (
props
) => {
const { children } = props
const { children, ...p } = props
const [state, dispatch] = useReducer(shipmentReducer, shipmentInitialState)
const { order, getOrder, include, addResourceToInclude, includeLoaded } =
useContext(OrderContext)
const config = useContext(CommerceLayerContext)
const errors: BaseError[] = []
useEffect(() => {
if (!include?.includes('shipments.available_shipping_methods')) {
addResourceToInclude({
Expand All @@ -55,6 +64,56 @@ const ShipmentsContainer: FunctionComponent<ShipmentsContainerProps> = (
},
})
}
if (!include?.includes('line_items.item')) {
addResourceToInclude({
newResource: ['line_items.item'],
})
} else if (!includeLoaded?.['line_items.item']) {
addResourceToInclude({
newResourceLoaded: {
'line_items.item': true,
},
})
}

if (order && order.shipments && order.shipments.length > 0) {
const hasShippingMethods = order.shipments.map((shipment) => {
return (
shipment.available_shipping_methods &&
shipment.available_shipping_methods.length > 0
)
})
if (hasShippingMethods.includes(false)) {
errors.push({
code: 'NO_SHIPPING_METHODS',
message: 'No shipping methods',
resource: 'shipments',
})
}
}
if (order && order.line_items && order.line_items.length > 0) {
const hasStocks = order.line_items.map((line_item) => {
return (
// @ts-ignore
line_item.item?.do_not_ship || line_item.item?.inventory?.available
)
})
if (hasStocks.includes(false)) {
errors.push({
code: 'OUT_OF_STOCK',
message: 'No stock available',
resource: 'line_items',
})
}
}
if (errors.length > 0) {
dispatch({
type: 'setErrors',
payload: {
errors,
},
})
}
if (order && !isEmpty(config) && order.shipments) {
getShipments({ order, dispatch, config })
}
Expand All @@ -72,14 +131,15 @@ const ShipmentsContainer: FunctionComponent<ShipmentsContainerProps> = (
order,
}),
}

const parentProps = { ...p, errors: state.errors }
return (
<ShipmentContext.Provider value={contextValue}>
{children}
<Parent {...parentProps}>{children}</Parent>
</ShipmentContext.Provider>
)
}

ShipmentsContainer.propTypes = propTypes
ShipmentsContainer.displayName = displayName

export default ShipmentsContainer
1 change: 1 addition & 0 deletions src/reducers/OrderReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export type ResourceIncluded =
| 'billing_address'
| 'shipping_address'
| 'line_items.line_item_options.sku_option'
| 'line_items.item'
| 'available_customer_payment_sources.payment_source'
| 'shipments.available_shipping_methods'
| 'shipments.stock_transfers'
Expand Down
3 changes: 3 additions & 0 deletions src/typings/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export type CodeErrorType =
| 'KEY_ORDER_MISMATCH'
| 'LOCKED'
| 'NOT_ACCEPTABLE'
| 'OUT_OF_STOCK'
| 'PARAM_MISSING'
| 'PARAM_NOT_ALLOWED'
| 'PAYMENT_NOT_APPROVED_FOR_EXECUTION'
| 'PAYMENT_INTENT_AUTHENTICATION_FAILURE'
| 'RECORD_NOT_FOUND'
| 'RECORD_NOT_FOUND'
| 'RELATION_EXISTS'
| 'NO_SHIPPING_METHODS'
| 'SAVE_FAILED'
| 'TYPE_MISMATCH'
| 'UNAUTHORIZED'
Expand All @@ -46,6 +48,7 @@ export type ResourceErrorType =
| 'orders'
| 'payment_methods'
| 'prices'
| 'shipments'
| 'shipping_address'
| 'sku_options'
| 'variant'
Expand Down

0 comments on commit 38000b6

Please sign in to comment.