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

fix: Disallow creating duplicate prices #7866

Open
wants to merge 1 commit into
base: feat/revamp-pricing-module
Choose a base branch
from

Conversation

sradevski
Copy link
Member

This PR is targeting the pricing revamp as it depends on the cleanup there. Will change base branch to develop once that is merged.

We do two things here:

  1. Sort by rules count first, amount later
  2. Disallow creating equivalent rules

@sradevski sradevski requested a review from a team as a code owner June 28, 2024 11:43
@sradevski sradevski requested review from riqwan, fPolic and olivermrbl and removed request for a team June 28, 2024 11:43
Copy link

vercel bot commented Jun 28, 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 Jun 29, 2024 11:08am
3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
api-reference ⬜️ Ignored (Inspect) Jun 29, 2024 11:08am
docs-ui ⬜️ Ignored (Inspect) Visit Preview Jun 29, 2024 11:08am
medusa-docs ⬜️ Ignored (Inspect) Visit Preview Jun 29, 2024 11:08am

Copy link

changeset-bot bot commented Jun 28, 2024

⚠️ No Changeset found

Latest commit: e31a2d3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

const pricesToCreate = data
.map((addPrice) =>
this.normalizePrices(
addPrice.prices as any,
Copy link
Member Author

Choose a reason for hiding this comment

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

The typings can be improve a lot more, but I decided not to do it as part of this PR

@@ -163,6 +163,7 @@ export class PricingRepository
pl_rules_count: "price.pl_rules_count",
price_list_type: "price.pl_type",
price_list_id: "price.price_list_id",
all_rules_count: knex.raw("price.rules_count + price.pl_rules_count"),
Copy link
Contributor

Choose a reason for hiding this comment

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

don't we need a COALESCE here? "pl_rules_count" could be NULL no?

Copy link
Member Author

Choose a reason for hiding this comment

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

Both rules count are non-nullable and default to 0, so we should be good

Copy link
Contributor

Choose a reason for hiding this comment

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

At a first glance price.pl_rules_count apparently comes from pl_rules_count: "pl.rules_count", and that is a left join with price_list.
that why I asked if that can be null.

Copy link
Member

@adrien2p adrien2p Jun 29, 2024

Choose a reason for hiding this comment

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

it seams that the pl_price_count is indeed retrieved through a left join and it means by default that the right side is optional and therefore if you dont have a matching pl tuple the value will not be present which is why carlos is suggesting to use a coalescing (either in the original sub query or before using it, i would suggest to coalesce in the original select to be always defaulted) as it can be null. The only time it cant be null is when the left join as a match and therefore the default value at least will take place or if you were in the case of an inner join because both side requires to be present to be returned.

Feel free to let us know if we are wrong but could you run a little test query (just one select with one left join) where you select a price that does have a matching price list and let us know what the select returns you, i believe the count will be null 🙏

Maybe we are missing some context and in that case feel free to complement.

entry.prices = this.normalizePrices(
priceListData.prices,
[],
priceListData.prices.price_set_id
Copy link
Contributor

Choose a reason for hiding this comment

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

The price_set_id should come from individual prices in a price list as they can be from multiple price sets. This might not be needed here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah you are right, let me try to clean this up a bit more 👍

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

4 participants