forked from crowetic/commerce
normalize cart. list brands (attribute)
This commit is contained in:
parent
dd40b8c604
commit
6a9c6c3bca
@ -73,7 +73,7 @@ export const handler = {
|
|||||||
const itemId = cartData.lineItems[0].id
|
const itemId = cartData.lineItems[0].id
|
||||||
const productId = cartData.lineItems[0].productId
|
const productId = cartData.lineItems[0].productId
|
||||||
const variantId = cartData.lineItems[0].variant.id
|
const variantId = cartData.lineItems[0].variant.id
|
||||||
if (!itemId || !productId || !variantId) {
|
if (!itemId || !productId) {
|
||||||
throw new ValidationError({
|
throw new ValidationError({
|
||||||
message: 'Invalid input used for this operation',
|
message: 'Invalid input used for this operation',
|
||||||
})
|
})
|
||||||
|
@ -10,23 +10,30 @@ export type SwellImage = {
|
|||||||
id: string
|
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 = {
|
export type SwellCart = {
|
||||||
id: string
|
id: string
|
||||||
account_id: number
|
account_id: number
|
||||||
currency: string
|
currency: string
|
||||||
tax_included_total: number
|
tax_included_total: number
|
||||||
sub_total: number
|
sub_total: number
|
||||||
|
grand_total: number
|
||||||
discount_total: number
|
discount_total: number
|
||||||
quantity: number
|
quantity: number
|
||||||
items: {
|
items: CartLineItem[]
|
||||||
id: string
|
|
||||||
product: object
|
|
||||||
price: number
|
|
||||||
variant: boolean
|
|
||||||
quantity: number
|
|
||||||
}
|
|
||||||
date_created: string
|
date_created: string
|
||||||
discounts?: { id: number; amount: number }[]
|
discounts?: { id: number; amount: number }[] | null
|
||||||
// TODO: add missing fields
|
// TODO: add missing fields
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 fetchAllProducts from '../api/utils/fetch-all-products'
|
||||||
import getAllProductVendors from './queries/get-all-product-vendors-query'
|
import getAllProductVendors from './queries/get-all-product-vendors-query'
|
||||||
|
|
||||||
@ -13,18 +14,11 @@ export type BrandEdge = {
|
|||||||
|
|
||||||
export type Brands = BrandEdge[]
|
export type Brands = BrandEdge[]
|
||||||
|
|
||||||
const getVendors = async (config: SwellConfig): Promise<BrandEdge[]> => {
|
const getVendors = async (config: SwellConfig) => {
|
||||||
const vendors = await fetchAllProducts({
|
const vendors =
|
||||||
config,
|
(await config.fetchSwell('attributes', 'get', ['brand']).values) ?? []
|
||||||
query: getAllProductVendors,
|
|
||||||
variables: {
|
|
||||||
first: 250,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
let vendorsStrings = vendors.map(({ node: { vendor } }) => vendor)
|
return [...new Set(vendors)].map((v) => ({
|
||||||
|
|
||||||
return [...new Set(vendorsStrings)].map((v) => ({
|
|
||||||
node: {
|
node: {
|
||||||
entityId: v,
|
entityId: v,
|
||||||
name: v,
|
name: v,
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { Product, Customer } from '@commerce/types'
|
import { Product, Customer } from '@commerce/types'
|
||||||
import { fileURLToPath } from 'node:url'
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Checkout,
|
Checkout,
|
||||||
@ -10,12 +9,14 @@ import {
|
|||||||
|
|
||||||
import type {
|
import type {
|
||||||
Cart,
|
Cart,
|
||||||
LineItem,
|
CartLineItem,
|
||||||
SwellCustomer,
|
SwellCustomer,
|
||||||
SwellProduct,
|
SwellProduct,
|
||||||
SwellImage,
|
SwellImage,
|
||||||
SwellVariant,
|
SwellVariant,
|
||||||
ProductOptionValue,
|
ProductOptionValue,
|
||||||
|
SwellCart,
|
||||||
|
LineItem,
|
||||||
} from '../types'
|
} from '../types'
|
||||||
|
|
||||||
const money = ({ amount, currencyCode }: MoneyV2) => {
|
const money = ({ amount, currencyCode }: MoneyV2) => {
|
||||||
@ -142,20 +143,31 @@ export function normalizeProduct(swellProduct: SwellProduct): Product {
|
|||||||
return product
|
return product
|
||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeCart(cart: Checkout): Cart {
|
export function normalizeCart({
|
||||||
return {
|
id,
|
||||||
id: cart.id,
|
account_id,
|
||||||
customerId: cart.account_id,
|
date_created,
|
||||||
|
currency,
|
||||||
|
tax_included_total,
|
||||||
|
items,
|
||||||
|
sub_total,
|
||||||
|
grand_total,
|
||||||
|
discounts,
|
||||||
|
}: SwellCart) {
|
||||||
|
const cart: Cart = {
|
||||||
|
id: id,
|
||||||
|
customerId: account_id + '',
|
||||||
email: '',
|
email: '',
|
||||||
createdAt: cart.date_created,
|
createdAt: date_created,
|
||||||
currency: cart.currency,
|
currency: { code: currency },
|
||||||
taxesIncluded: cart.tax_included_total,
|
taxesIncluded: tax_included_total > 0,
|
||||||
lineItems: cart.items?.map(normalizeLineItem),
|
lineItems: items?.map(normalizeLineItem),
|
||||||
lineItemsSubtotalPrice: +cart.sub_total,
|
lineItemsSubtotalPrice: +sub_total,
|
||||||
subtotalPrice: +cart.sub_total,
|
subtotalPrice: +sub_total,
|
||||||
totalPrice: cart.grand_total,
|
totalPrice: grand_total,
|
||||||
discounts: cart.discounts,
|
discounts: discounts?.map((discount) => ({ value: discount.amount })),
|
||||||
}
|
}
|
||||||
|
return cart
|
||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeCustomer(customer: SwellCustomer): Customer {
|
export function normalizeCustomer(customer: SwellCustomer): Customer {
|
||||||
@ -173,11 +185,11 @@ function normalizeLineItem({
|
|||||||
price,
|
price,
|
||||||
variant,
|
variant,
|
||||||
quantity,
|
quantity,
|
||||||
}: CheckoutLineItemEdge): LineItem {
|
}: CartLineItem): LineItem {
|
||||||
const item = {
|
const item = {
|
||||||
id,
|
id,
|
||||||
variantId: variant?.id ?? '',
|
variantId: variant?.id,
|
||||||
productId: product?.id ?? '',
|
productId: product.id ?? '',
|
||||||
name: product?.name ?? '',
|
name: product?.name ?? '',
|
||||||
quantity,
|
quantity,
|
||||||
variant: {
|
variant: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user