-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Round percentage sale commissions to the nearest cent #4366
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8100a09
round percentage sale commissions to the nearest cent
pepeladeira 8e09dcf
code improvements
pepeladeira afa411b
playwright tests
pepeladeira 443cfbb
Merge branch 'main' into sale-commission-rounding
steven-tey a80e4d0
finalize tests
steven-tey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings"; | ||
| import { describe, expect, test } from "vitest"; | ||
|
|
||
| const percentageEarnings = ( | ||
| saleAmount: number, | ||
| percent: number, | ||
| quantity = 1, | ||
| ) => | ||
| calculateSaleEarnings({ | ||
| reward: { | ||
| type: "percentage", | ||
| amountInCents: null, | ||
| amountInPercentage: percent, | ||
| }, | ||
| sale: { amount: saleAmount, quantity }, | ||
| }); | ||
|
|
||
| describe("calculateSaleEarnings", () => { | ||
| describe("percentage – same as the old truncate path", () => { | ||
| test.each([ | ||
| { | ||
| label: "whole cents: $10 sale at 20%", | ||
| saleAmount: 1000, | ||
| percent: 20, | ||
| expected: 200, | ||
| }, | ||
| { | ||
| label: "whole cents: $10 sale at 50%", | ||
| saleAmount: 1000, | ||
| percent: 50, | ||
| expected: 500, | ||
| }, | ||
| { | ||
| label: "whole cents: $19 sale at 10%", | ||
| saleAmount: 1900, | ||
| percent: 10, | ||
| expected: 190, | ||
| }, | ||
| { | ||
| label: "1.4¢ truncates and rounds to the same value", | ||
| saleAmount: 7, | ||
| percent: 20, | ||
| expected: 1, | ||
| }, | ||
| { | ||
| label: "0.4¢ stays 0 (below half a cent)", | ||
| saleAmount: 2, | ||
| percent: 20, | ||
| expected: 0, | ||
| }, | ||
| { | ||
| label: "zero sale amount", | ||
| saleAmount: 0, | ||
| percent: 20, | ||
| expected: 0, | ||
| }, | ||
| { | ||
| label: "zero percent", | ||
| saleAmount: 1000, | ||
| percent: 0, | ||
| expected: 0, | ||
| }, | ||
| ])("$label → $expected", ({ saleAmount, percent, expected }) => { | ||
| expect(percentageEarnings(saleAmount, percent)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("percentage – half-up (new vs old truncate)", () => { | ||
| test.each([ | ||
| { | ||
| label: "3¢ sale at 20% (ticket: 0.6¢, used to store 0)", | ||
| saleAmount: 3, | ||
| percent: 20, | ||
| expected: 1, | ||
| }, | ||
| { | ||
| label: "15¢ sale at 10% (1.5¢, used to store 1)", | ||
| saleAmount: 15, | ||
| percent: 10, | ||
| expected: 2, | ||
| }, | ||
| { | ||
| label: "1¢ sale at 50% (0.5¢, used to store 0)", | ||
| saleAmount: 1, | ||
| percent: 50, | ||
| expected: 1, | ||
| }, | ||
| ])("$label → $expected", ({ saleAmount, percent, expected }) => { | ||
| expect(percentageEarnings(saleAmount, percent)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| test("percentage ignores quantity (uses sale amount only)", () => { | ||
| expect(percentageEarnings(1000, 20, 5)).toBe(200); | ||
| }); | ||
|
|
||
| describe("flat", () => { | ||
| test.each([ | ||
| { | ||
| label: "single sale", | ||
| amountInCents: 5000, | ||
| quantity: 1, | ||
| expected: 5000, | ||
| }, | ||
| { | ||
| label: "quantity multiplies the flat amount", | ||
| amountInCents: 500, | ||
| quantity: 2, | ||
| expected: 1000, | ||
| }, | ||
| { | ||
| label: "zero quantity", | ||
| amountInCents: 500, | ||
| quantity: 0, | ||
| expected: 0, | ||
| }, | ||
| ])("$label → $expected", ({ amountInCents, quantity, expected }) => { | ||
| expect( | ||
| calculateSaleEarnings({ | ||
| reward: { | ||
| type: "flat", | ||
| amountInCents, | ||
| amountInPercentage: null, | ||
| }, | ||
| sale: { amount: 1000, quantity }, | ||
| }), | ||
| ).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| test("returns 0 when reward type is neither flat nor percentage", () => { | ||
| expect( | ||
| calculateSaleEarnings({ | ||
| reward: { | ||
| type: "unknown" as "flat", | ||
| amountInCents: 500, | ||
| amountInPercentage: 20, | ||
| }, | ||
| sale: { amount: 1000, quantity: 1 }, | ||
| }), | ||
| ).toBe(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.