Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core-flows, medusa): add shipping methods to cart API #7150

Merged
merged 11 commits into from Apr 29, 2024

Conversation

riqwan
Copy link
Contributor

@riqwan riqwan commented Apr 25, 2024

what:

  • adds API to add shipping methods to cart
  • refreshes shipping methods on cart mutations

RESOLVES CORE-2007

Copy link

changeset-bot bot commented Apr 25, 2024

🦋 Changeset detected

Latest commit: cdc07c1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@medusajs/core-flows Patch
@medusajs/medusa Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Apr 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
medusa-dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 26, 2024 2:52pm
3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
api-reference ⬜️ Ignored (Inspect) Apr 26, 2024 2:52pm
docs-ui ⬜️ Ignored (Inspect) Visit Preview Apr 26, 2024 2:52pm
medusa-docs ⬜️ Ignored (Inspect) Visit Preview Apr 26, 2024 2:52pm

Copy link
Contributor

@olivermrbl olivermrbl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! A couple of comments.

const optionIds = transform({ input }, (data) => {
return (data.input.options ?? []).map((i) => i.id)
})

validateCartShippingOptionsStep({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I wonder if we should just remove the method, if it's no longer applicable? We've talked about doing that previously.

That would be a slightly better DX than telling users to handle this themselves in the client.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it could be a config passed to the step?

Copy link
Contributor Author

@riqwan riqwan Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a new step called refreshCartShippingMethodsStep to do this, feels cleaner that way as the inputs are slightly different:

refreshCartShippingMethodsStep({ cart }) - removes shipping methods if no longer applicable
validateCartShippingOptionsStep({ cart, option_ids }) - throws error

should we merge them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine to keep the two separated. In that case, I would probably unify the validation logic in a helper we can use in both methods, as this is almost identical right?

Update: I looked at the commit, and it looks like there are slight differences, so I am not sure if this is possible. I'll let you decide on how to proceed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a similar concern, maybe refresh can just return if a refresh was performed or not, and that can serve for the cases when we do want to validate? Just something that seemed confusing, but if you can't think of anything better than this is fine I guess

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inputs are different and the outcome is different. They serve 2 different purposes even though they're doing similar things. If you both feel strongly enough, I'll merge them in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine keeping it as is, so we can get it merged.

shippingMethodsToDelete.push(...diff)
}

const validShippingOptions =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit confused by this workflow as it lists shipping options twice and deletes, maybe we can make the code more obvious why we are doing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be outdated, take a look again 🤞🏻

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riqwan yup it was outdated, looks good 👍

@@ -22,6 +23,19 @@ import { prepareConfirmInventoryInput } from "../utils/prepare-confirm-inventory
import { prepareLineItemData } from "../utils/prepare-line-item-data"
import { refreshPaymentCollectionForCartStep } from "./refresh-payment-collection"

const cartFields = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot of data, do we really need everything? I'm afraid this might have a toll on performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These actions are performed on a cart context, depending on what the rules are, they could be any of these. We can optimize this by whitelisting what we allow as "rules", but that can come later.

const optionIds = transform({ input }, (data) => {
return (data.input.options ?? []).map((i) => i.id)
})

validateCartShippingOptionsStep({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a similar concern, maybe refresh can just return if a refresh was performed or not, and that can serve for the cases when we do want to validate? Just something that seemed confusing, but if you can't think of anything better than this is fine I guess

.filter((sm) => invalidShippingOptionIds.includes(sm.shipping_option_id!))
.map((sm) => sm.id)

await cartModule.softDeleteShippingMethods(shippingMethodsToDelete)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Do we want to do a soft or hard delete in this case, if a shipping method doesn't exist anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can only soft delete here as we have to revert this if any other steps fail

@riqwan riqwan merged commit d2393f0 into develop Apr 29, 2024
24 checks passed
@riqwan riqwan deleted the feat/shipping-methods branch April 29, 2024 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants