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[]) {
return lineItems.map((lineItem) => {
const id = lineItem.id
const attributes = lineItem.attributes
return {
id,
name: attributes.name,
productId: attributes.reference,
variantId: attributes.reference,
quantity: attributes.quantity,
price: attributes.unit_amount_float,
variant: {
return lineItems
.filter((l) => {
return !['shipment', 'paymentMethod'].includes(l.itemType)
})
.map((lineItem) => {
const id = lineItem.id
const attributes = lineItem.attributes
return {
id,
name: attributes.name,
sku: attributes.sku_code,
productId: attributes.reference,
variantId: attributes.reference,
quantity: attributes.quantity,
price: attributes.unit_amount_float,
image: {
url: `/commercelayer_assets/${attributes.reference}_FLAT.png`,
altText: 'Black Women Long Sleeve Shirt',
width: 1000,
height: 1000,
variant: {
id,
name: attributes.name,
sku: attributes.sku_code,
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)
.includes('lineItems')
.find(id, { rawResponse: true })
const attributes = clOrder.data.attributes
const lineItems = clOrder?.included
? normalizeLineItems(clOrder?.included)
: []
return {
id,
createdAt: attributes.created_at,
currency: { code: attributes.currency_code },
taxesIncluded: '',
lineItems,
lineItemsSubtotalPrice: '',
subtotalPrice: attributes.subtotal_amount_float,
totalPrice: attributes.total_amount_float,
const orderStatus = clOrder.status
if (['pending', 'draft'].includes(orderStatus)) {
const attributes = clOrder.data.attributes
const lineItems = clOrder?.included
? normalizeLineItems(clOrder?.included)
: []
return {
id,
createdAt: attributes.created_at,
currency: { code: attributes.currency_code },
taxesIncluded: '',
lineItems,
lineItemsSubtotalPrice: '',
subtotalPrice: attributes.subtotal_amount_float,
totalPrice: attributes.total_amount_float,
}
} else {
localStorage.removeItem('CL_ORDER_ID')
}
}
return {