Skip to content

Commit

Permalink
Validating attachments to know if items should be split (#188)
Browse files Browse the repository at this point in the history
#### 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)

<!--- What is the motivation and context for this change? -->

#### 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

<!--- Add a ✔️ where applicable -->
✔️ | Type of Change
---|---
_ | Bug fix <!-- a non-breaking change which fixes an issue -->
_ | New feature <!-- a non-breaking change which adds functionality -->
_ | Breaking change <!-- fix or feature that would cause existing
functionality to change -->
✔️| Technical improvements <!-- chores, refactors and overall reduction
of technical debt -->
  • Loading branch information
afmejia23 authored Jun 20, 2023
1 parent 717b18b commit 26b4c9e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 16 additions & 2 deletions node/resolvers/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

/**
Expand Down Expand Up @@ -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!)

Expand All @@ -237,7 +251,7 @@ export const mutations = {
const newOrderForm = await clients.checkout.updateItems(
orderFormId!,
cleanItems,
splitItem,
subscriptions ? false : splitItem,
allowedOutdatedData
)

Expand Down
2 changes: 2 additions & 0 deletions node/typings/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ServiceContext, SegmentData, ParamsContext } from '@vtex/api'
import { Attachment } from 'vtex.checkout-graphql'

import { Clients } from '../clients'

Expand Down Expand Up @@ -150,6 +151,7 @@ declare global {
sellingPrices: SellingPrice[]
total: number
}
attachments?: Attachment[]
}

interface SellingPrice {
Expand Down

0 comments on commit 26b4c9e

Please sign in to comment.