Products can be added at max stock amount - #35
Conversation
… through and add whenRequestSent attribute. Add cartItem create and update methods
…n /api/cart/index.ts
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
| createdAt: true, | ||
| updatedAt: true, | ||
| uuid: true, | ||
| stock: true, |
There was a problem hiding this comment.
Omit stock from request sent to frontend for now
| return res.status(200).json(cartItems); | ||
| const clonedCartItems = structuredClone(cartItems); | ||
| const newCartItems = clonedCartItems.map((clonedCartItem) => { | ||
| return { ...clonedCartItem, whenRequestSent: new Date() }; | ||
| }); | ||
|
|
||
| return res.status(200).json(newCartItems); |
There was a problem hiding this comment.
StructureClone the cartItems and add newCartItems. Loop them through and add whenRequsetSent attribute. This will make GET requests differ from each other so useEffect will "grab" them
| const upsertedItem = await prisma.cartItem.upsert({ | ||
| const cartItem = await prisma.cartItem.findFirst({ | ||
| where: { | ||
| cartUUID_productUUID: { | ||
| productUUID: product.uuid, | ||
| cartUUID: cart.uuid, | ||
| }, | ||
| }, | ||
| create: { | ||
| cartUUID: cart.uuid, | ||
| productUUID: product.uuid, | ||
| amount: productToBeAdded.amount, | ||
| }, | ||
| update: { | ||
| amount: { | ||
| increment: productToBeAdded.amount, | ||
| }); | ||
|
|
||
| // cartItem does not exist, create a new one | ||
| if (!cartItem) { | ||
| const createdItem = await prisma.cartItem.create({ | ||
| data: { | ||
| amount: productToBeAdded.amount, | ||
| cartUUID: cart.uuid, | ||
| productUUID: product.uuid, | ||
| }, | ||
| }); | ||
| return res.status(200).json(createdItem); | ||
| } | ||
|
|
||
| if (cartItem.amount + 1 > product.stock) { | ||
| throw new HttpError('Maximum amount of products reached!', 400); | ||
| } | ||
|
|
||
| const newAmount = | ||
| cartItem.amount >= product.stock ? product.stock : cartItem.amount + 1; | ||
|
|
||
| const updatedItem = await prisma.cartItem.update({ | ||
| where: { | ||
| cartUUID_productUUID: { | ||
| cartUUID: cart.uuid, | ||
| productUUID: product.uuid, | ||
| }, | ||
| }, | ||
| data: { | ||
| amount: newAmount, | ||
| }, | ||
| }); | ||
| return res.status(200).json(upsertedItem); | ||
| return res.status(200).json(updatedItem); |
There was a problem hiding this comment.
Change upsertedItem to createdItem/updatedItem. Uses "old" method due to need to know the amount of products in cart
| // this will prevent request sent to backend if amount of products were same | ||
| // so for example if user just click the input and clicks out without changing anything | ||
| if (amountOfProductBeforeBlur === amountOfProduct) return; | ||
| await mutateAsync(); | ||
| await queryClient.invalidateQueries({ | ||
| queryKey: QueryAndMutationKeys.CartProducts, | ||
| }); | ||
| try { | ||
| // this will prevent request sent to backend if amount of products were same | ||
| // so for example if user just click the input and clicks out without changing anything | ||
| if (amountOfProductBeforeBlur === amountOfProduct) return; | ||
| await mutateAsync(); | ||
| await queryClient.invalidateQueries({ | ||
| queryKey: QueryAndMutationKeys.CartProducts, | ||
| }); | ||
| } catch (e) { | ||
| handleError(e); | ||
| } |
| export type GetCart = { amount: number; createdAt: Date; updatedAt: Date } & { | ||
| export type GetCart = { | ||
| amount: number; | ||
| createdAt: Date; | ||
| updatedAt: Date; | ||
| whenRequestSent?: Date; | ||
| } & { |
There was a problem hiding this comment.
whenRequestSent's idea is to tell when the request was sent. Just to make GET request of products change every time so useEffect will "grab" them
| console.error(e); | ||
| handleError(e); |
There was a problem hiding this comment.
handleError is better here
| await queryClient.invalidateQueries({ | ||
| queryKey: QueryAndMutationKeys.CartProducts, | ||
| }); | ||
| await refetchCartProducts(queryClient); |
There was a problem hiding this comment.
Add this new function to replace the old queryClient.invalidateQueries etc. Might make the function generic later on
Products can be added to cart until max stock amount is reached