diff --git a/lib/shopify/ajax.ts b/lib/shopify/ajax.ts index 9260684e4..005bfb2bb 100644 --- a/lib/shopify/ajax.ts +++ b/lib/shopify/ajax.ts @@ -13,7 +13,7 @@ export class AjaxError extends Error { } } -type AjaxMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'; +type AjaxMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; export const ajax = async (method: AjaxMethod, url: string, data?: object): Promise => { const response = await fetch(url, { diff --git a/lib/shopify/index.ts b/lib/shopify/index.ts index b7a8a584e..997e944af 100644 --- a/lib/shopify/index.ts +++ b/lib/shopify/index.ts @@ -1,5 +1,6 @@ import { SHOPIFY_GRAPHQL_API_ENDPOINT, TAGS } from 'lib/constants'; -import { Where, create, find, findByID } from 'lib/shopify/payload'; +import { AjaxError } from 'lib/shopify/ajax'; +import { Where, create, find, findByID, update } from 'lib/shopify/payload'; import { Cart, Category, Media, Option, Product } from 'lib/shopify/payload-types'; import { isShopifyError } from 'lib/type-guards'; import { ensureStartsWith } from 'lib/utils'; @@ -7,7 +8,6 @@ import { revalidateTag } from 'next/cache'; import { headers } from 'next/headers'; import { NextRequest, NextResponse } from 'next/server'; import { editCartItemsMutation, removeFromCartMutation } from './mutations/cart'; -import { getCartQuery } from './queries/cart'; import { getPageQuery } from './queries/page'; import { CartItem, @@ -22,7 +22,6 @@ import { ProductOption, ProductVariant, ShopifyCart, - ShopifyCartOperation, ShopifyPageOperation, ShopifyRemoveFromCartOperation, ShopifyUpdateCartOperation @@ -116,7 +115,7 @@ const reshapeCartItems = (cartItems: Cart['lines']): CartItem[] => { const variant = product.variants.find((v) => v.id === item.variant); return { - id: item.variant, + id: item.id!, quantity: item.quantity, merchandise: { id: item.variant, @@ -156,23 +155,35 @@ const reshapeC = (cart: Cart): ExCart => { export async function createCart(): Promise { const cart = await create('carts', { lines: [] }); - return reshapeC(cart); + return reshapeC(cart.doc); } export async function addToCart( cartId: string, lines: { merchandiseId: string; quantity: number }[] ): Promise { - console.log('TEST'); - // const res = await shopifyFetch({ - // query: addToCartMutation, - // variables: { - // cartId, - // lines - // }, - // cache: 'no-store' - // }); - // return reshapeCart(res.body.data.cartLinesAdd.cart); + const products = await find('products', { + where: { + 'variants.id': { + in: lines.map((line) => line.merchandiseId) + } + } + }); + + const cart = await update('carts', cartId, { + lines: lines.map((line) => { + 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); } export async function removeFromCart(cartId: string, lineIds: string[]): Promise { @@ -192,6 +203,7 @@ export async function updateCart( cartId: string, lines: { id: string; merchandiseId: string; quantity: number }[] ): Promise { + console.log('TEST'); const res = await shopifyFetch({ query: editCartItemsMutation, variables: { @@ -205,19 +217,18 @@ export async function updateCart( } export async function getCart(cartId: string): Promise { - const res = await shopifyFetch({ - query: getCartQuery, - variables: { cartId }, - tags: [TAGS.cart], - cache: 'no-store' - }); + try { + const cart = await findByID('carts', cartId); + return reshapeC(cart); + } catch (error: unknown) { + if (error instanceof AjaxError) { + if (error.statusCode === 404) { + return undefined; + } + } - // Old carts becomes `null` when you checkout. - if (!res.body.data.cart) { - return undefined; + throw error; } - - return reshapeCart(res.body.data.cart); } export async function getCollection(handle: string): Promise { diff --git a/lib/shopify/payload.ts b/lib/shopify/payload.ts index 61a57250f..31c3b9c3f 100644 --- a/lib/shopify/payload.ts +++ b/lib/shopify/payload.ts @@ -45,6 +45,11 @@ export type PaginatedDocs = { totalPages: number; }; +type Doc = { + message: string; + doc: T; +}; + type FindParams = { where?: Where; depth?: number; @@ -67,5 +72,10 @@ export const findByID = (collection: string, id: string) => { export const create = (collection: string, body: Partial) => { const url = `${process.env.CMS_URL}/api/${collection}`; - return ajax('POST', url, body); + return ajax>('POST', url, body); +}; + +export const update = (collection: string, id: string, body: Partial) => { + const url = `${process.env.CMS_URL}/api/${collection}/${id}`; + return ajax>('PATCH', url, body); };