diff --git a/lib/shopify/index.ts b/lib/shopify/index.ts index 5ac64b667..389bf2dcb 100644 --- a/lib/shopify/index.ts +++ b/lib/shopify/index.ts @@ -75,9 +75,8 @@ export async function addToCart( cartId: string, lines: { merchandiseId: string; quantity: number }[] ): Promise { - const prevCart = await payload.findByID('carts', cartId); + const prevCart = await payload.findByID('carts', cartId, { depth: 0 }); const cartItems = await mergeItems(prevCart.lines, lines, true); - const cart = await payload.update('carts', cartId, { lines: cartItems }); @@ -85,7 +84,7 @@ export async function addToCart( } export async function removeFromCart(cartId: string, lineIds: string[]): Promise { - const prevCart = await payload.findByID('carts', cartId); + const prevCart = await payload.findByID('carts', cartId, { depth: 0 }); const lines = prevCart?.lines?.filter((lineItem) => !lineIds.includes(lineItem.id!)) ?? []; const cart = await payload.update('carts', cartId, { lines }); return reshapeCart(cart.doc); @@ -135,9 +134,8 @@ export async function updateCart( cartId: string, lines: { id: string; merchandiseId: string; quantity: number }[] ): Promise { - const prevCart = await payload.findByID('carts', cartId); + const prevCart = await payload.findByID('carts', cartId, { depth: 0 }); const cartItems = await mergeItems(prevCart.lines, lines, false); - const cart = await payload.update('carts', cartId, { lines: cartItems }); return reshapeCart(cart.doc); } @@ -267,7 +265,7 @@ export async function getCollectionProducts({ if (tag) { filters.push({ tags: { - equals: collection + equals: tag } }); } diff --git a/lib/shopify/payload.ts b/lib/shopify/payload.ts index 1279c1fec..45981996a 100644 --- a/lib/shopify/payload.ts +++ b/lib/shopify/payload.ts @@ -51,7 +51,11 @@ type Doc = { doc: T; }; -type FindParams = { +type BaseParams = { + depth?: number; +}; + +type FindParams = BaseParams & { where?: Where; depth?: number; sort?: string; @@ -70,15 +74,15 @@ export class Payload { this.baseUrl = baseUrl; } - find = (collection: Collection, params: FindParams) => { + find = (collection: Collection, params: FindParams = {}) => { const query = qs.stringify(params, { addQueryPrefix: true }); - const url = `${this.baseUrl}/api/${collection}${query}`; return ajax>('GET', url); }; - findByID = (collection: Collection, id: string) => { - const url = `${this.baseUrl}/api/${collection}/${id}`; + findByID = (collection: Collection, id: string, params: BaseParams = {}) => { + const query = qs.stringify(params, { addQueryPrefix: true }); + const url = `${this.baseUrl}/api/${collection}/${id}${query}`; return ajax('GET', url); };