From 26b4c9e1cb82dadea96e219de066dacad80f4371 Mon Sep 17 00:00:00 2001 From: afmejia23 <75274040+afmejia23@users.noreply.github.com> Date: Tue, 20 Jun 2023 09:20:23 -0500 Subject: [PATCH] Validating attachments to know if items should be split (#188) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What problem is this solving? When we update one item in cart from minicart, and if that item has attachments, currently the item will be duplicated in the list. With this fix the quantity on those items will be increased instead of duplicating the item. More details [here](https://projects-northlatam.atlassian.net/browse/LRP-330) #### How should this be manually tested? 1. Visit our [Test Workspace](https://web2--sebastiancervantes.myvtex.com) 2. Go to a PDP of a product that has subscriptions created ([Arroz Superior x 2 kg](https://web2--sebastiancervantes.myvtex.com/arroz-superior-x-2kg/p) is a good match) 3. Select more than one item and add subscription. ![image](https://github.com/vtex-apps/checkout-graphql/assets/75274040/4632b07b-88e6-4d06-b323-99446e667ab1) 4. Select frequency ![image](https://github.com/vtex-apps/checkout-graphql/assets/75274040/60bce025-17ea-45de-b1af-dcd97f3e5ee7) 5. Add to cart 6. In minicart, increase the item quantity ![image](https://github.com/vtex-apps/checkout-graphql/assets/75274040/9e7cebb8-03b9-48da-97eb-c64c09ffaaf3) **Result** Now we can see that the quantity increases correctly and the item is not duplicated as it was before the fix. ![image](https://github.com/vtex-apps/checkout-graphql/assets/75274040/dc53d501-ca4b-4725-85df-aa8a37cfede2) [Workspace](https://web2--sebastiancervantes.myvtex.com/arroz-superior-x-2kg/p) #### Checklist/Reminders - [ ] Updated `README.md`. - [ ] Updated `CHANGELOG.md`. - [ ✔️] Linked this PR to a Jira story (if applicable). - [ ] Updated/created tests (important for bug fixes). - [ ] Deleted the workspace after merging this PR (if applicable). #### Type of changes ✔️ | Type of Change ---|--- _ | Bug fix _ | New feature _ | Breaking change ✔️| Technical improvements --- node/resolvers/items.ts | 18 ++++++++++++++++-- node/typings/global.d.ts | 2 ++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/node/resolvers/items.ts b/node/resolvers/items.ts index adf49e7..fc0ec70 100644 --- a/node/resolvers/items.ts +++ b/node/resolvers/items.ts @@ -116,7 +116,10 @@ export const mutations = { ) const withOptions = items - .map((item, currentIndex) => ({ ...item, index: item.index ?? currentIndex })) + .map((item, currentIndex) => ({ + ...item, + index: item.index ?? currentIndex, + })) .filter(({ options }) => !!options && options.length > 0) /** @@ -214,6 +217,17 @@ export const mutations = { const cleanItems = orderItems.map(({ id, ...rest }) => rest) + // Validating subscriptions + let subscriptions + if (orderItems.length === 1 && orderItems[0].index !== undefined) { + const { items } = await checkout.orderForm(orderFormId!) + const itemToUpdate = items[orderItems[0].index] + + subscriptions = itemToUpdate.attachments?.some(attachment => + attachment.name?.includes('vtex.subscription') + ) + } + if (cleanItems.some((item: OrderFormItemInput) => !item.index)) { const orderForm = await checkout.orderForm(orderFormId!) @@ -237,7 +251,7 @@ export const mutations = { const newOrderForm = await clients.checkout.updateItems( orderFormId!, cleanItems, - splitItem, + subscriptions ? false : splitItem, allowedOutdatedData ) diff --git a/node/typings/global.d.ts b/node/typings/global.d.ts index 212e09a..8bb499d 100644 --- a/node/typings/global.d.ts +++ b/node/typings/global.d.ts @@ -1,4 +1,5 @@ import { ServiceContext, SegmentData, ParamsContext } from '@vtex/api' +import { Attachment } from 'vtex.checkout-graphql' import { Clients } from '../clients' @@ -150,6 +151,7 @@ declare global { sellingPrices: SellingPrice[] total: number } + attachments?: Attachment[] } interface SellingPrice {