fix: Remove unused code and add hosted checkout url

This commit is contained in:
Alessandro Casazza 2022-05-13 10:53:58 +02:00
parent 8840bb7b21
commit bc9866f7fb
No known key found for this signature in database
GPG Key ID: 3AF41B06C6495D3D
5 changed files with 33 additions and 622 deletions

View File

@ -1,6 +1,6 @@
import type { CheckoutEndpoint } from '.'
import getCredentials from '../../utils/getCredentials'
import { Order } from '@commercelayer/sdk'
import getCredentials, { getOrganizationSlug } from '../../utils/getCredentials'
import CLSdk from '@commercelayer/sdk'
const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({
req,
@ -18,11 +18,17 @@ const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({
const { ENDPOINT } = getCredentials()
if (orderId && accessToken) {
const clOrder = await Order.withCredentials({ endpoint, accessToken })
.includes('lineItems')
.find(orderId as string, { rawResponse: true })
const checkoutUrl = clOrder.data.attributes.checkout_url
const organization = getOrganizationSlug(ENDPOINT).organization
const sdk = CLSdk({
accessToken,
organization,
})
const order = await sdk.orders.retrieve(orderId as string, {
fields: ['checkout_url'],
})
const checkoutUrl = order?.checkout_url
? order?.checkout_url
: `https://${organization}.checkout.commercelayer.app/${order?.id}`
if (checkoutUrl) {
res.redirect(`${checkoutUrl}?accessToken=${accessToken}`)
} else {

View File

@ -1,9 +1,9 @@
import { Price, CommerceLayerClient } from '@commercelayer/sdk'
import { Product } from '@vercel/commerce/types/product'
import { DeepPartialArray } from './contentfulApi'
import type { Products } from './getContentData'
type GetPricesArgs = {
products?: DeepPartialArray<Product>
products?: Products
sdk: CommerceLayerClient
}

View File

@ -1,7 +1,12 @@
import { LineItem } from '@commercelayer/sdk'
import data from '../../data.json'
import type { Products } from './getContentData'
export default function normalizeLineItems(lineItems: LineItem[]) {
type Params = {
lineItems: LineItem[]
products: Products
}
export default function normalizeLineItems({ lineItems, products }: Params) {
return lineItems
.filter((l) => {
return (
@ -10,11 +15,13 @@ export default function normalizeLineItems(lineItems: LineItem[]) {
})
.map((lineItem) => {
const id = lineItem.id
const products = data.products
const [product] = products.filter((p) => p.id === lineItem.reference)
const path = product?.slug
const [image] = product?.images
return {
id,
name: lineItem.name,
path: products.find((p) => p.id === lineItem.reference)?.slug,
path,
productId: lineItem.reference,
variantId: lineItem.reference,
quantity: lineItem.quantity,
@ -24,12 +31,7 @@ export default function normalizeLineItems(lineItems: LineItem[]) {
name: lineItem.name,
sku: lineItem.sku_code,
price: lineItem.unit_amount_float,
image: {
url: `https://data.commercelayer.app/vercel-provider/${lineItem.reference}_FLAT.png`,
altText: lineItem.name,
width: 1000,
height: 1000,
},
image,
},
}
})

View File

@ -6,6 +6,7 @@ import getCredentials, {
getOrganizationSlug,
} from '../api/utils/getCredentials'
import normalizeLineItems from '../api/utils/normalizeLineItems'
import getContentData from '../api/utils/getContentData'
export default useCart as UseCart<typeof handler>
@ -25,8 +26,12 @@ export const handler: SWRHook<any> = {
const order = await sdk.orders.retrieve(id, { include: ['line_items'] })
const orderStatus = order.status
if (orderStatus && ['pending', 'draft'].includes(orderStatus)) {
const contentData = await getContentData()
const lineItems = order.line_items
? normalizeLineItems(order.line_items)
? normalizeLineItems({
lineItems: order.line_items,
products: contentData,
})
: []
return {
id,

View File

@ -1,602 +0,0 @@
{
"products": [
{
"id": "SHIRTWLS000000FFFFFF",
"name": "Black Women Long Sleeve Shirt",
"brandId": "Commerce Layer",
"path": "/black-women-long-sleeve-shirt",
"slug": "black-women-long-sleeve-shirt",
"categoryId": "clothings",
"price": { "value": 50, "currencyCode": "USD" },
"descriptionHtml": "<p><span>This relaxed long sleeve tee combines the best of both worlds, proving that cozy can also be chic. 65/35 polyester/viscose. Drop shoulder. Curved bottom hem.</span></p>",
"images": [
{
"url": "https://data.commercelayer.app/vercel-provider/SHIRTWLS000000FFFFFF_FLAT.png",
"altText": " A Black Women Long Sleeve Shirt",
"width": 1000,
"height": 1000
},
{
"url": "https://data.commercelayer.app/vercel-provider/SHIRTWLS000000FFFFFF_01.png",
"altText": " A Black Women Long Sleeve Shirt",
"width": 1000,
"height": 1000
},
{
"url": "https://data.commercelayer.app/vercel-provider/SHIRTWLS000000FFFFFF_02.png",
"altText": " A Black Women Long Sleeve Shirt",
"width": 1000,
"height": 1000
}
],
"variants": [
{
"id": "SHIRTWLS000000FFFFFFXSXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "SHIRTWLS000000FFFFFFXSXX",
"displayName": "Size",
"values": [
{
"label": "XS"
}
]
}
]
},
{
"id": "SHIRTWLS000000FFFFFFSXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "SHIRTWLS000000FFFFFFSXXX",
"displayName": "Size",
"values": [
{
"label": "S"
}
]
}
]
},
{
"id": "SHIRTWLS000000FFFFFFMXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "SHIRTWLS000000FFFFFFMXXX",
"displayName": "Size",
"values": [
{
"label": "M"
}
]
}
]
},
{
"id": "SHIRTWLS000000FFFFFFLXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "SHIRTWLS000000FFFFFFLXXX",
"displayName": "Size",
"values": [
{
"label": "L"
}
]
}
]
}
],
"options": [
{
"id": "option-color",
"displayName": "Color",
"values": [
{
"label": "Black color",
"hexColors": ["#000"]
}
]
},
{
"id": "option-size",
"displayName": "Size",
"values": [
{
"label": "XS"
},
{
"label": "S"
},
{
"label": "M"
},
{
"label": "L"
}
]
}
]
},
{
"id": "EMUG12OZFFFFFF000000",
"name": "White Enamel Mug",
"brandId": "Commerce Layer",
"path": "/white-enamel-mug",
"slug": "white-enamel-mug",
"categoryId": "household",
"price": { "value": 100, "currencyCode": "USD" },
"descriptionHtml": "<p><span>Lightweight and durable, this mug is a must-have for every devoted camper. Use it for your favorite beverage or a hot meal, and attach it to your bag for easy access on a hike. Ready for your next camping trip?</span></p>",
"images": [
{
"url": "https://data.commercelayer.app/vercel-provider/EMUG12OZFFFFFF000000_FLAT.png",
"altText": " A White Enamel Mug",
"width": 1000,
"height": 1000
},
{
"url": "https://data.commercelayer.app/vercel-provider/EMUG12OZFFFFFF000000_01.png",
"altText": " A White Enamel Mug",
"width": 1000,
"height": 1000
},
{
"url": "https://data.commercelayer.app/vercel-provider/EMUG12OZFFFFFF000000_02.png",
"altText": " A White Enamel Mug",
"width": 1000,
"height": 1000
}
],
"variants": [
{
"id": "EMUG12OZFFFFFF000000XXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "EMUG12OZFFFFFF000000XXXX",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
}
],
"options": [
{
"id": "option-color",
"displayName": "Color",
"values": [
{
"label": "White color",
"hexColors": ["#fff"]
}
]
},
{
"id": "option-size",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
},
{
"id": "VFLEXFIT000000FFFFFF",
"name": "Black Visor Flexfit",
"brandId": "Commerce Layer",
"path": "/black-visor-flexfit",
"slug": "black-visor-flexfit",
"categoryId": "clothings",
"price": { "value": 39.5, "currencyCode": "USD" },
"descriptionHtml": "<p><span>A visor is perfect for the days when youre not in the mood for a cap, but you still want to protect your peepers from the blinding sun. Made of a quick-drying fabric, itll be right for sports activities or just a casual stroll around.</span></p>",
"images": [
{
"url": "https://data.commercelayer.app/vercel-provider/VFLEXFIT000000FFFFFF_FLAT.png",
"altText": " A Black Visor Flexfit",
"width": 1000,
"height": 1000
},
{
"url": "https://data.commercelayer.app/vercel-provider/VFLEXFIT000000FFFFFF_01.png",
"altText": " A Black Visor Flexfit",
"width": 1000,
"height": 1000
},
{
"url": "https://data.commercelayer.app/vercel-provider/VFLEXFIT000000FFFFFF_02.png",
"altText": " A Black Visor Flexfit",
"width": 1000,
"height": 1000
},
{
"url": "https://data.commercelayer.app/vercel-provider/VFLEXFIT000000FFFFFF_03.png",
"altText": " A Black Visor Flexfit",
"width": 1000,
"height": 1000
}
],
"variants": [
{
"id": "VFLEXFIT000000FFFFFFXXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "VFLEXFIT000000FFFFFFXXXX",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
}
],
"options": [
{
"id": "option-color",
"displayName": "Color",
"values": [
{
"label": "Black color",
"hexColors": ["#000"]
}
]
},
{
"id": "option-size",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
},
{
"id": "FACEMASK000000FFFFFF",
"name": "Black Face Mask",
"brandId": "Commerce Layer",
"path": "/black-face-mask",
"slug": "black-face-mask",
"price": { "value": 15, "currencyCode": "USD" },
"categoryId": "clothings",
"descriptionHtml": "<p><span>Durable double-layer, washable cloth face mask. The center seam and elastic ear loops give the mask a close fit, and the treated fabric ensures that it will serve you well.</span></p>",
"images": [
{
"url": "https://data.commercelayer.app/vercel-provider/FACEMASK000000FFFFFF_FLAT.png",
"altText": " A Black Face Mask",
"width": 1000,
"height": 1000
}
],
"variants": [
{
"id": "FACEMASK000000FFFFFFXXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "FACEMASK000000FFFFFFXXXX",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
}
],
"options": [
{
"id": "option-color",
"displayName": "Color",
"values": [
{
"label": "Black color",
"hexColors": ["#000"]
}
]
},
{
"id": "option-size",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
},
{
"id": "SHIRTWLS9397A0000000",
"name": "Gray Women Long Sleeve Shirt",
"brandId": "Commerce Layer",
"path": "/gray-women-long-sleeve-shirt",
"slug": "gray-women-long-sleeve-shirt",
"categoryId": "clothings",
"price": { "value": 50, "currencyCode": "USD" },
"descriptionHtml": "<p><span>This relaxed long sleeve tee combines the best of both worlds, proving that cozy can also be chic. 65/35 polyester/viscose. Drop shoulder. Curved bottom hem.</span></p>",
"images": [
{
"url": "https://data.commercelayer.app/vercel-provider/SHIRTWLS9397A0000000_FLAT.png",
"altText": " A Gray Women Long Sleeve Shirt",
"width": 1000,
"height": 1000
}
],
"variants": [
{
"id": "SHIRTWLS9397A0000000XSXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "SHIRTWLS9397A0000000XSXX",
"displayName": "Size",
"values": [
{
"label": "XS"
}
]
}
]
},
{
"id": "SHIRTWLS9397A0000000SXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "SHIRTWLS9397A0000000SXXX",
"displayName": "Size",
"values": [
{
"label": "S"
}
]
}
]
},
{
"id": "SHIRTWLS9397A0000000MXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "SHIRTWLS9397A0000000MXXX",
"displayName": "Size",
"values": [
{
"label": "M"
}
]
}
]
},
{
"id": "SHIRTWLS9397A0000000LXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "SHIRTWLS9397A0000000LXXX",
"displayName": "Size",
"values": [
{
"label": "L"
}
]
}
]
}
],
"options": [
{
"id": "option-color",
"displayName": "Color",
"values": [
{
"label": "Gray color",
"hexColors": ["#bbbbbb"]
}
]
},
{
"id": "option-size",
"displayName": "Size",
"values": [
{
"label": "XS"
},
{
"label": "S"
},
{
"label": "M"
},
{
"label": "L"
}
]
}
]
},
{
"id": "FACEMASKFFFFFF000000",
"name": "White Face Mask",
"brandId": "Commerce Layer",
"path": "/white-face-mask",
"slug": "white-face-mask",
"categoryId": "clothings",
"price": { "value": 15, "currencyCode": "USD" },
"descriptionHtml": "<p><span>Durable double-layer, washable cloth face mask. The center seam and elastic ear loops give the mask a close fit, and the treated fabric ensures that it will serve you well.</span></p>",
"images": [
{
"url": "https://data.commercelayer.app/vercel-provider/FACEMASKFFFFFF000000_FLAT.png",
"altText": " A White Face Mask",
"width": 1000,
"height": 1000
}
],
"variants": [
{
"id": "FACEMASKFFFFFF000000XXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "FACEMASKFFFFFF000000XXXX",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
}
],
"options": [
{
"id": "option-color",
"displayName": "Color",
"values": [
{
"label": "White color",
"hexColors": ["#fff"]
}
]
},
{
"id": "option-size",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
},
{
"id": "BEANIEXXFFFFFF000000",
"name": "White Beanie",
"brandId": "Commerce Layer",
"path": "/white-beanie",
"slug": "white-beanie",
"categoryId": "clothings",
"price": { "value": 20, "currencyCode": "USD" },
"descriptionHtml": "<p><span>Soft double-layered customizable beanie. 95% polyester, 5% spandex. Regular fit. Accurately printed, cut, and hand-sewn.</span></p>",
"images": [
{
"url": "https://data.commercelayer.app/vercel-provider/BEANIEXXFFFFFF000000_FLAT.png",
"altText": " A White Beanie",
"width": 1000,
"height": 1000
}
],
"variants": [
{
"id": "BEANIEXXFFFFFF000000XXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "BEANIEXXFFFFFF000000XXXX",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
}
],
"options": [
{
"id": "option-color",
"displayName": "Color",
"values": [
{
"label": "White color",
"hexColors": ["#fff"]
}
]
},
{
"id": "option-size",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
},
{
"id": "DUFFLBAGFFFFFF000000",
"name": "White Duffle Bag",
"brandId": "Commerce Layer",
"path": "/white-duffle-bag",
"slug": "white-duffle-bag",
"categoryId": "featured",
"price": { "value": 250, "currencyCode": "USD" },
"descriptionHtml": "<p><span>The perfect spacious bag no matter the occasion. Its great for packing exercise gear when heading to the gym, or throwing in necessities and going on an adventure.</span></p>",
"images": [
{
"url": "https://data.commercelayer.app/vercel-provider/DUFFLBAGFFFFFF000000_FLAT.png",
"altText": " A White Face Mask",
"width": 1000,
"height": 1000
}
],
"variants": [
{
"id": "DUFFLBAGFFFFFF000000XXXX",
"options": [
{
"__typename": "MultipleChoiceOption",
"id": "DUFFLBAGFFFFFF000000XXXX",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
}
],
"options": [
{
"id": "option-color",
"displayName": "Color",
"values": [
{
"label": "White color",
"hexColors": ["#fff"]
}
]
},
{
"id": "option-size",
"displayName": "Size",
"values": [
{
"label": "Size One"
}
]
}
]
}
]
}