mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 15:36:58 +00:00
wip
This commit is contained in:
parent
0f2485a010
commit
164833d96f
@ -7,7 +7,7 @@ import { ensureStartsWith } from 'lib/utils';
|
|||||||
import { revalidateTag } from 'next/cache';
|
import { revalidateTag } from 'next/cache';
|
||||||
import { headers } from 'next/headers';
|
import { headers } from 'next/headers';
|
||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
import { editCartItemsMutation, removeFromCartMutation } from './mutations/cart';
|
import { removeFromCartMutation } from './mutations/cart';
|
||||||
import { getPageQuery } from './queries/page';
|
import { getPageQuery } from './queries/page';
|
||||||
import {
|
import {
|
||||||
CartItem,
|
CartItem,
|
||||||
@ -15,7 +15,6 @@ import {
|
|||||||
Connection,
|
Connection,
|
||||||
Cart as ExCart,
|
Cart as ExCart,
|
||||||
Product as ExProduct,
|
Product as ExProduct,
|
||||||
Image,
|
|
||||||
Menu,
|
Menu,
|
||||||
Money,
|
Money,
|
||||||
Page,
|
Page,
|
||||||
@ -23,8 +22,7 @@ import {
|
|||||||
ProductVariant,
|
ProductVariant,
|
||||||
ShopifyCart,
|
ShopifyCart,
|
||||||
ShopifyPageOperation,
|
ShopifyPageOperation,
|
||||||
ShopifyRemoveFromCartOperation,
|
ShopifyRemoveFromCartOperation
|
||||||
ShopifyUpdateCartOperation
|
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
const domain = process.env.SHOPIFY_STORE_DOMAIN
|
const domain = process.env.SHOPIFY_STORE_DOMAIN
|
||||||
@ -109,8 +107,8 @@ const reshapeCart = (cart: ShopifyCart): ExCart => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const reshapeCartItems = (cartItems: Cart['lines']): CartItem[] => {
|
const reshapeCartItems = (lines: Cart['lines']): CartItem[] => {
|
||||||
return (cartItems ?? []).map((item) => {
|
return (lines ?? []).map((item) => {
|
||||||
const product = item.product as Product;
|
const product = item.product as Product;
|
||||||
const variant = product.variants.find((v) => v.id === item.variant);
|
const variant = product.variants.find((v) => v.id === item.variant);
|
||||||
|
|
||||||
@ -162,27 +160,12 @@ export async function addToCart(
|
|||||||
cartId: string,
|
cartId: string,
|
||||||
lines: { merchandiseId: string; quantity: number }[]
|
lines: { merchandiseId: string; quantity: number }[]
|
||||||
): Promise<ExCart> {
|
): Promise<ExCart> {
|
||||||
const products = await find<Product>('products', {
|
const prevCart = await findByID<Cart>('carts', cartId);
|
||||||
where: {
|
const cartItems = await mergeItems(prevCart.lines, lines, true);
|
||||||
'variants.id': {
|
console.log('ADD');
|
||||||
in: lines.map((line) => line.merchandiseId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const cart = await update<Cart>('carts', cartId, {
|
const cart = await update<Cart>('carts', cartId, {
|
||||||
lines: lines.map((line) => {
|
lines: cartItems
|
||||||
const product = products.docs.find((p) =>
|
|
||||||
p.variants.some((variant) => variant.id === line.merchandiseId)
|
|
||||||
);
|
|
||||||
return {
|
|
||||||
product: product?.id!,
|
|
||||||
variant: line.merchandiseId,
|
|
||||||
quantity: line.quantity
|
|
||||||
};
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return reshapeC(cart.doc);
|
return reshapeC(cart.doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,21 +182,57 @@ export async function removeFromCart(cartId: string, lineIds: string[]): Promise
|
|||||||
return reshapeCart(res.body.data.cartLinesRemove.cart);
|
return reshapeCart(res.body.data.cartLinesRemove.cart);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mergeItems = async (
|
||||||
|
cartItems: Cart['lines'],
|
||||||
|
lines: { merchandiseId: string; quantity: number }[],
|
||||||
|
add: boolean
|
||||||
|
): Promise<Cart['lines']> => {
|
||||||
|
const map = new Map((cartItems ?? []).map((item) => [item.variant, item]));
|
||||||
|
|
||||||
|
const products = await find<Product>('products', {
|
||||||
|
where: {
|
||||||
|
'variants.id': {
|
||||||
|
in: lines.map((line) => line.merchandiseId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
lines.forEach((line) => {
|
||||||
|
const product = products.docs.find((p) =>
|
||||||
|
p.variants.some((variant) => variant.id === line.merchandiseId)
|
||||||
|
);
|
||||||
|
|
||||||
|
let item = {
|
||||||
|
product: product?.id!,
|
||||||
|
variant: line.merchandiseId,
|
||||||
|
quantity: line.quantity
|
||||||
|
};
|
||||||
|
if (add && map.has(line.merchandiseId)) {
|
||||||
|
const added = map.get(line.merchandiseId);
|
||||||
|
if (added) {
|
||||||
|
item = {
|
||||||
|
...item,
|
||||||
|
quantity: item.quantity + added.quantity
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
map.set(line.merchandiseId, item);
|
||||||
|
});
|
||||||
|
|
||||||
|
return Array.from(map).map(([, item]) => item);
|
||||||
|
};
|
||||||
|
|
||||||
export async function updateCart(
|
export async function updateCart(
|
||||||
cartId: string,
|
cartId: string,
|
||||||
lines: { id: string; merchandiseId: string; quantity: number }[]
|
lines: { id: string; merchandiseId: string; quantity: number }[]
|
||||||
): Promise<ExCart> {
|
): Promise<ExCart> {
|
||||||
console.log('TEST');
|
const cart = await findByID<Cart>('carts', cartId);
|
||||||
const res = await shopifyFetch<ShopifyUpdateCartOperation>({
|
const cartItems = await mergeItems(cart.lines, lines, false);
|
||||||
query: editCartItemsMutation,
|
|
||||||
variables: {
|
|
||||||
cartId,
|
|
||||||
lines
|
|
||||||
},
|
|
||||||
cache: 'no-store'
|
|
||||||
});
|
|
||||||
|
|
||||||
return reshapeCart(res.body.data.cartLinesUpdate.cart);
|
console.log(cartItems);
|
||||||
|
|
||||||
|
const c = await update<Cart>('carts', cartId, { lines: cartItems });
|
||||||
|
return reshapeC(c.doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCart(cartId: string): Promise<ExCart | undefined> {
|
export async function getCart(cartId: string): Promise<ExCart | undefined> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user