Repository navigation
feat: add isGift field to StoreOffer type and resolvers - SFS-3040 - #3220
Conversation
- Introduced an optional `isGift` field in the `StoreOffer` type to indicate if an offer is a gift. - Updated GraphQL schema and resolvers to handle the new `isGift` field. - Modified the cart validation mutation to include the `isGift` property for cart items. - Adjusted the `isGift` logic in the cart SDK to utilize the new field when applicable.
WalkthroughAdds an optional Changes
sequenceDiagram
participant Client
participant API as GraphQL API
participant VTEX as VTEX OrderForm
participant Core as Core SDK (cart)
Client->>API: Query cart / offers
API->>VTEX: Fetch order form
VTEX-->>API: OrderForm item (may include isGift)
API->>Core: Resolve CartItem with isGift (respecting experimental flag)
Core-->>API: Computed cart totals, items split by isGift
API-->>Client: Return cart with isGift on StoreOffer
🎯 3 (Moderate) | ⏱️ ~15 minutes
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/core/src/sdk/cart/index.ts`:
- Around line 92-97: The helper function isGift unnecessarily uses optional
chaining on a non-nullable CartItem and its name collides with the
CartItem.isGift field; rename the function to isGiftItem, change the
implementation to return item.isGift ?? false when
storeConfig.experimental?.useIsGiftFromOrderForm is true (and keep the existing
price === 0 fallback), and update all call sites that reference isGift to use
isGiftItem so the code no longer uses item?.isGift and avoids the naming
collision.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (3)
packages/api/src/__generated__/schema.tsis excluded by!**/__generated__/**and included bypackages/**packages/core/@generated/gql.tsis excluded by!**/@generated/**,!**/@generated/**and included bypackages/**packages/core/@generated/graphql.tsis excluded by!**/@generated/**,!**/@generated/**and included bypackages/**
📒 Files selected for processing (4)
packages/api/src/platforms/vtex/resolvers/offer.tspackages/api/src/typeDefs/offer.graphqlpackages/core/discovery.config.default.jspackages/core/src/sdk/cart/index.ts
| const isGift = (item: CartItem) => { | ||
| if (storeConfig.experimental?.useIsGiftFromOrderForm) { | ||
| return item?.isGift ?? false | ||
| } | ||
| return item.price === 0 | ||
| } |
There was a problem hiding this comment.
Two small issues in the isGift helper.
1. Unnecessary optional chain on item: item is typed as CartItem (non-nullable), so item?.isGift is redundant — use item.isGift ?? false directly.
2. Naming collision with the CartItem.isGift field: The local function shares its name with the GraphQL field it reads, which can be confusing during future maintenance. Consider renaming to isGiftItem.
♻️ Proposed fix
-const isGift = (item: CartItem) => {
+const isGiftItem = (item: CartItem) => {
if (storeConfig.experimental?.useIsGiftFromOrderForm) {
- return item?.isGift ?? false
+ return item.isGift ?? false
}
return item.price === 0
}Then update all three call sites:
- totals.totalItems += isGift(curr) ? 0 : quantityMultiplier
+ totals.totalItems += isGiftItem(curr) ? 0 : quantityMultiplier
...
- gifts: cart.items.filter((item) => isGift(item)),
- items: cart.items.filter((item) => !isGift(item)),
+ gifts: cart.items.filter((item) => isGiftItem(item)),
+ items: cart.items.filter((item) => !isGiftItem(item)),🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/core/src/sdk/cart/index.ts` around lines 92 - 97, The helper
function isGift unnecessarily uses optional chaining on a non-nullable CartItem
and its name collides with the CartItem.isGift field; rename the function to
isGiftItem, change the implementation to return item.isGift ?? false when
storeConfig.experimental?.useIsGiftFromOrderForm is true (and keep the existing
price === 0 fallback), and update all call sites that reference isGift to use
isGiftItem so the code no longer uses item?.isGift and avoids the naming
collision.
- Modified the CartItem interface to omit the 'isGift' field from CartItemFragment and added it as an optional property. - This change allows for better flexibility in handling gift items within the cart.
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
There was a problem hiding this comment.
♻️ Duplicate comments (1)
packages/core/src/sdk/cart/index.ts (1)
96-101: SameisGifthelper concerns as earlier review (optional chain + naming collision).
Line 96–101: this duplicates the previously reported issue aboutitem?.isGiftand the helper name colliding with the field.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/core/src/sdk/cart/index.ts` around lines 96 - 101, The helper function isGift duplicates the CartItem.isGift field and uses optional chaining on item (item?.isGift) causing a naming collision and unclear null handling; rename the helper (e.g., isItemGift) and change the logic so you explicitly check the item object and field existence instead of optional chaining: when storeConfig.experimental?.useIsGiftFromOrderForm is enabled, return a boolean based on the explicit presence/value of item.isGift (e.g., item && ('isGift' in item) ? Boolean(item.isGift) : ...), otherwise fall back to the existing price check (item.price === 0); update any callers of isGift to use the new helper name.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@packages/core/src/sdk/cart/index.ts`:
- Around line 96-101: The helper function isGift duplicates the CartItem.isGift
field and uses optional chaining on item (item?.isGift) causing a naming
collision and unclear null handling; rename the helper (e.g., isItemGift) and
change the logic so you explicitly check the item object and field existence
instead of optional chaining: when
storeConfig.experimental?.useIsGiftFromOrderForm is enabled, return a boolean
based on the explicit presence/value of item.isGift (e.g., item && ('isGift' in
item) ? Boolean(item.isGift) : ...), otherwise fall back to the existing price
check (item.price === 0); update any callers of isGift to use the new helper
name.
|
@eduardoformiga I believe we will need a doc to mention this new flag |
What's the purpose of this pull request?
This PR aims to make possible the use of isGift from the orderForm item instead of price = 0 when dealing with gift.For that, you should use the flag
useIsGiftFromOrderFormas true.How it works?
isGiftfield in theStoreOffertype to indicate if an offer is a gift.isGiftfield.isGiftproperty for cart items.isGiftlogic in the cart SDK to utilize the new field when applicable.(see the gift icon and the free tag in the minicart)
How to test it?
...
Starters Deploy Preview
References
Checklist
You may erase this after checking them all 😉
PR Title and Commit Messages
feat,fix,chore,docs,style,refactor,ciandtestPR Description
breaking change,bug,contributing,performance,documentation..Dependencies
pnpm-lock.yamlfile when there were changes to the packagesDocumentation
@Mariana-Caetanoto review and update (Or submit a doc request)Summary by CodeRabbit