diff --git a/framework/swell/cart/use-update-item.tsx b/framework/swell/cart/use-update-item.tsx index 3155778c6..38ad65bf1 100644 --- a/framework/swell/cart/use-update-item.tsx +++ b/framework/swell/cart/use-update-item.tsx @@ -73,7 +73,7 @@ export const handler = { const itemId = cartData.lineItems[0].id const productId = cartData.lineItems[0].productId const variantId = cartData.lineItems[0].variant.id - if (!itemId || !productId || !variantId) { + if (!itemId || !productId) { throw new ValidationError({ message: 'Invalid input used for this operation', }) diff --git a/framework/swell/types.ts b/framework/swell/types.ts index 24362ea38..a4ce4afa8 100644 --- a/framework/swell/types.ts +++ b/framework/swell/types.ts @@ -10,23 +10,30 @@ export type SwellImage = { id: string } +export type CartLineItem = { + id: string + product: SwellProduct + price: number + variant: { + name: string | null + sku: string | null + id: string + } + quantity: number +} + export type SwellCart = { id: string account_id: number currency: string tax_included_total: number sub_total: number + grand_total: number discount_total: number quantity: number - items: { - id: string - product: object - price: number - variant: boolean - quantity: number - } + items: CartLineItem[] date_created: string - discounts?: { id: number; amount: number }[] + discounts?: { id: number; amount: number }[] | null // TODO: add missing fields } diff --git a/framework/swell/utils/get-vendors.ts b/framework/swell/utils/get-vendors.ts index 632f5daaf..d80854f6d 100644 --- a/framework/swell/utils/get-vendors.ts +++ b/framework/swell/utils/get-vendors.ts @@ -1,4 +1,5 @@ -import { SwellConfig } from '../api' +import { swellConfig } from '@framework' +import { getConfig, SwellConfig } from '../api' import fetchAllProducts from '../api/utils/fetch-all-products' import getAllProductVendors from './queries/get-all-product-vendors-query' @@ -13,18 +14,11 @@ export type BrandEdge = { export type Brands = BrandEdge[] -const getVendors = async (config: SwellConfig): Promise => { - const vendors = await fetchAllProducts({ - config, - query: getAllProductVendors, - variables: { - first: 250, - }, - }) +const getVendors = async (config: SwellConfig) => { + const vendors = + (await config.fetchSwell('attributes', 'get', ['brand']).values) ?? [] - let vendorsStrings = vendors.map(({ node: { vendor } }) => vendor) - - return [...new Set(vendorsStrings)].map((v) => ({ + return [...new Set(vendors)].map((v) => ({ node: { entityId: v, name: v, diff --git a/framework/swell/utils/normalize.ts b/framework/swell/utils/normalize.ts index a4c8cb732..63b63feb0 100644 --- a/framework/swell/utils/normalize.ts +++ b/framework/swell/utils/normalize.ts @@ -1,5 +1,4 @@ import { Product, Customer } from '@commerce/types' -import { fileURLToPath } from 'node:url' import { Checkout, @@ -10,12 +9,14 @@ import { import type { Cart, - LineItem, + CartLineItem, SwellCustomer, SwellProduct, SwellImage, SwellVariant, ProductOptionValue, + SwellCart, + LineItem, } from '../types' const money = ({ amount, currencyCode }: MoneyV2) => { @@ -142,20 +143,31 @@ export function normalizeProduct(swellProduct: SwellProduct): Product { return product } -export function normalizeCart(cart: Checkout): Cart { - return { - id: cart.id, - customerId: cart.account_id, +export function normalizeCart({ + id, + account_id, + date_created, + currency, + tax_included_total, + items, + sub_total, + grand_total, + discounts, +}: SwellCart) { + const cart: Cart = { + id: id, + customerId: account_id + '', email: '', - createdAt: cart.date_created, - currency: cart.currency, - taxesIncluded: cart.tax_included_total, - lineItems: cart.items?.map(normalizeLineItem), - lineItemsSubtotalPrice: +cart.sub_total, - subtotalPrice: +cart.sub_total, - totalPrice: cart.grand_total, - discounts: cart.discounts, + createdAt: date_created, + currency: { code: currency }, + taxesIncluded: tax_included_total > 0, + lineItems: items?.map(normalizeLineItem), + lineItemsSubtotalPrice: +sub_total, + subtotalPrice: +sub_total, + totalPrice: grand_total, + discounts: discounts?.map((discount) => ({ value: discount.amount })), } + return cart } export function normalizeCustomer(customer: SwellCustomer): Customer { @@ -173,11 +185,11 @@ function normalizeLineItem({ price, variant, quantity, -}: CheckoutLineItemEdge): LineItem { +}: CartLineItem): LineItem { const item = { id, - variantId: variant?.id ?? '', - productId: product?.id ?? '', + variantId: variant?.id, + productId: product.id ?? '', name: product?.name ?? '', quantity, variant: {