mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
initial provider (#2)
This commit is contained in:
committed by
GitHub
parent
5b4d1b57d4
commit
3342d9d1bb
77
framework/medusa/utils/normalizers/normalize-cart.ts
Normal file
77
framework/medusa/utils/normalizers/normalize-cart.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { Cart, LineItem, ProductVariant } from '../../types/cart'
|
||||
import {
|
||||
Cart as MedusaCart,
|
||||
Discount as MedusaDiscount,
|
||||
LineItem as MedusaLineItem,
|
||||
ProductVariant as MedusaProductVariant,
|
||||
} from '@medusajs/medusa-js/lib/types'
|
||||
import { Discount } from '@commerce/types/common'
|
||||
|
||||
export function normalizeProductVariant(
|
||||
{ id, title: name, sku }: MedusaProductVariant,
|
||||
price: number,
|
||||
thumbnail: string
|
||||
): ProductVariant {
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
price: price / 100,
|
||||
sku: sku || id,
|
||||
listPrice: price / 100,
|
||||
requiresShipping: true,
|
||||
image: { url: thumbnail, altText: name },
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizeLineItem({
|
||||
id,
|
||||
title: name,
|
||||
variant,
|
||||
quantity,
|
||||
unit_price,
|
||||
thumbnail,
|
||||
}: MedusaLineItem): LineItem {
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
path: variant?.product.handle || name.replace(' ', '-'),
|
||||
variant: normalizeProductVariant(variant!, unit_price, thumbnail!),
|
||||
variantId: variant!.id,
|
||||
productId: variant!.product.id,
|
||||
discounts: [],
|
||||
quantity,
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizeDiscount(discount: MedusaDiscount): Discount {
|
||||
return {
|
||||
value: discount.rule.value,
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizeCart({
|
||||
id,
|
||||
email,
|
||||
created_at,
|
||||
region,
|
||||
items,
|
||||
subtotal,
|
||||
total,
|
||||
tax_total,
|
||||
customer_id,
|
||||
discounts,
|
||||
}: MedusaCart): Cart {
|
||||
return {
|
||||
id,
|
||||
email,
|
||||
customerId: customer_id,
|
||||
discounts: discounts.map((discount) => normalizeDiscount(discount)),
|
||||
createdAt: `${created_at}`,
|
||||
currency: { code: region.currency_code },
|
||||
lineItems: items.map((item) => normalizeLineItem(item)),
|
||||
subtotalPrice: subtotal / 100,
|
||||
totalPrice: total / 100,
|
||||
taxesIncluded: tax_total > 0,
|
||||
lineItemsSubtotalPrice: subtotal / 100,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user