fix: Clean order if it is placed

This commit is contained in:
Alessandro Casazza 2022-01-05 10:43:42 +01:00
parent 217a8f34c0
commit 0f5cdf99fb
No known key found for this signature in database
GPG Key ID: 3AF41B06C6495D3D
2 changed files with 42 additions and 33 deletions

View File

@ -1,26 +1,30 @@
export default function normalizeLineItems(lineItems: any[]) { export default function normalizeLineItems(lineItems: any[]) {
return lineItems.map((lineItem) => { return lineItems
const id = lineItem.id .filter((l) => {
const attributes = lineItem.attributes return !['shipment', 'paymentMethod'].includes(l.itemType)
return { })
id, .map((lineItem) => {
name: attributes.name, const id = lineItem.id
productId: attributes.reference, const attributes = lineItem.attributes
variantId: attributes.reference, return {
quantity: attributes.quantity,
price: attributes.unit_amount_float,
variant: {
id, id,
name: attributes.name, name: attributes.name,
sku: attributes.sku_code, productId: attributes.reference,
variantId: attributes.reference,
quantity: attributes.quantity,
price: attributes.unit_amount_float, price: attributes.unit_amount_float,
image: { variant: {
url: `/commercelayer_assets/${attributes.reference}_FLAT.png`, id,
altText: 'Black Women Long Sleeve Shirt', name: attributes.name,
width: 1000, sku: attributes.sku_code,
height: 1000, price: attributes.unit_amount_float,
image: {
url: `/commercelayer_assets/${attributes.reference}_FLAT.png`,
altText: 'Black Women Long Sleeve Shirt',
width: 1000,
height: 1000,
},
}, },
}, }
} })
})
} }

View File

@ -18,19 +18,24 @@ export const handler: SWRHook<any> = {
const clOrder = await Order.withCredentials(credentials) const clOrder = await Order.withCredentials(credentials)
.includes('lineItems') .includes('lineItems')
.find(id, { rawResponse: true }) .find(id, { rawResponse: true })
const attributes = clOrder.data.attributes const orderStatus = clOrder.status
const lineItems = clOrder?.included if (['pending', 'draft'].includes(orderStatus)) {
? normalizeLineItems(clOrder?.included) const attributes = clOrder.data.attributes
: [] const lineItems = clOrder?.included
return { ? normalizeLineItems(clOrder?.included)
id, : []
createdAt: attributes.created_at, return {
currency: { code: attributes.currency_code }, id,
taxesIncluded: '', createdAt: attributes.created_at,
lineItems, currency: { code: attributes.currency_code },
lineItemsSubtotalPrice: '', taxesIncluded: '',
subtotalPrice: attributes.subtotal_amount_float, lineItems,
totalPrice: attributes.total_amount_float, lineItemsSubtotalPrice: '',
subtotalPrice: attributes.subtotal_amount_float,
totalPrice: attributes.total_amount_float,
}
} else {
localStorage.removeItem('CL_ORDER_ID')
} }
} }
return { return {