Skip to content

fix: add function to unify order ledger on details and invoice - #307

Open
tomrndom wants to merge 3 commits into
mainfrom
fix/util-order-ledger
Open

fix: add function to unify order ledger on details and invoice#307
tomrndom wants to merge 3 commits into
mainfrom
fix/util-order-ledger

Conversation

@tomrndom

@tomrndom tomrndom commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

*adjust pdf params, add tests

ref: https://app.clickup.com/t/9014802374/86bb67u52

Signed-off-by: Tomás Castillo tcastilloboireau@gmail.com

Summary by CodeRabbit

  • New Features

    • Added consistent order processing across sponsor order grids and invoice PDFs.
    • Order items, discounts, fees, payments, refunds, and notes now appear in a consistent order with accurate running balances.
    • Improved handling of quantities, cancellations, zero-value discounts, and multiple fees.
  • Bug Fixes

    • Preserved discount formatting and prevented duplicate row identifiers.
    • Corrected balance and display inconsistencies between order grids and invoice PDFs.

…t pdf params, add tests

Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 39447a87-026a-48be-8cab-a8d32daf2242

📥 Commits

Reviewing files that changed from the base of the PR and between 9e4eb58 and 8cf0bd5.

📒 Files selected for processing (4)
  • src/components/mui/SponsorOrderGrid/__tests__/SponsorOrderGrid.test.js
  • src/components/mui/SponsorOrderGrid/index.js
  • src/utils/__tests__/order-ledger.test.js
  • src/utils/order-ledger.js
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/utils/order-ledger.js
  • src/utils/tests/order-ledger.test.js
  • src/components/mui/SponsorOrderGrid/index.js

📝 Walkthrough

Walkthrough

The change adds buildOrderLedger as the shared source for order entries and balances. Invoice PDF rows and SponsorOrderGrid rows now map ledger entries. Tests cover ledger behavior and rendering consistency.

Changes

Order ledger rendering

Layer / File(s) Summary
Ledger derivation and coverage
src/utils/order-ledger.js, src/utils/__tests__/order-ledger.test.js
Added buildOrderLedger to derive ordered entries and running balances. Tests cover item, discount, fee, payment, refund, note, quantity, cancellation, key, and ordering behavior.
Invoice PDF ledger mapping
src/components/order-invoice-pdf/helpers.js, src/components/order-invoice-pdf/index.js, src/components/order-invoice-pdf/__tests__/*
buildRows maps ledger entries to invoice rows. OrderPdf passes only the order. Tests validate the revised API and ledger consistency.
Sponsor order grid ledger rendering
src/components/mui/SponsorOrderGrid/index.js, src/components/mui/SponsorOrderGrid/__tests__/SponsorOrderGrid.test.js
SponsorOrderGrid renders ledger entries and uses ledger balances, keys, quantities, and cancellation metadata. Regression tests cover missing quantities, fee keys, and zero-value discounts.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Order
  participant buildOrderLedger
  participant OrderPdf
  participant SponsorOrderGrid

  Order->>buildOrderLedger: provide raw order data
  buildOrderLedger-->>OrderPdf: return typed entries and balances
  buildOrderLedger-->>SponsorOrderGrid: return typed entries and balances
  OrderPdf->>OrderPdf: map entries to invoice rows
  SponsorOrderGrid->>SponsorOrderGrid: map entries to grid rows
Loading

Possibly related PRs

Suggested reviewers: santipalenque, smarcet

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes adding a shared order ledger function for details and invoices, which is the main change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/util-order-ledger

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/components/mui/SponsorOrderGrid/__tests__/SponsorOrderGrid.test.js (1)

179-186: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert the rendered quantity, not only the amount.

The test name states that quantity defaults to 1, but the assertion checks the $100.00 text. That text also comes from the balance cell, so it does not confirm the quantity value. The details column renders the quantity, so assert it directly.

♻️ Proposed refinement
     render(<SponsorOrderGrid order={order} />);
-    expect(screen.getAllByText("$100.00").length).toBeGreaterThan(0);
+    expect(
+      screen.getByText(/Booth - sponsor_order_grid\.total: 1/)
+    ).toBeInTheDocument();
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/mui/SponsorOrderGrid/__tests__/SponsorOrderGrid.test.js`
around lines 179 - 186, Update the test for missing quantity in SponsorOrderGrid
to assert the details column renders quantity 1 directly, rather than relying on
the ambiguous "$100.00" amount text. Keep the existing order setup and render
flow unchanged.
src/components/order-invoice-pdf/__tests__/ledger-consistency.test.js (1)

77-79: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Scope the row query to exclude the appended total row.

SponsorOrderGrid appends a TotalRow inside the same <TableBody> when withReconciliation is false, which is the default here. The index-based loop is safe today only because that row comes after the ledger rows. Add a length assertion so a future reordering fails loudly instead of comparing the wrong cells.

💚 Proposed hardening
     const { container } = render(<SponsorOrderGrid order={purchaseV2Fixture} />);
     const rows = container.querySelectorAll("tbody tr");
+    expect(rows.length).toBeGreaterThanOrEqual(ledger.length);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/order-invoice-pdf/__tests__/ledger-consistency.test.js` around
lines 77 - 79, Update the SponsorOrderGrid test’s row selection and assertions
to explicitly account for the appended TotalRow, asserting the expected
ledger-row count before the index-based comparison. Keep the
running-balance/order comparison scoped to ledger rows and make any future row
reordering fail clearly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/components/mui/SponsorOrderGrid/index.js`:
- Line 314: Update the empty-state condition near hasNoForms in SponsorOrderGrid
to derive from the ledger’s rendered rows rather than order.forms.length. Show
the “no_items” row only when the ledger contains no visible form, fee, payment,
refund, or note rows, including when all forms are removed by the ledger
quantity filter.
- Around line 249-258: Update the "discount" case in SponsorOrderGrid to derive
the description with formatDiscount(entry.form.discount_amount,
entry.form.discount_type) instead of entry.form.discount, and pass the formatted
value to DiscountRow's discount prop. Preserve the existing amount, balance,
trailing, and key props.

---

Nitpick comments:
In `@src/components/mui/SponsorOrderGrid/__tests__/SponsorOrderGrid.test.js`:
- Around line 179-186: Update the test for missing quantity in SponsorOrderGrid
to assert the details column renders quantity 1 directly, rather than relying on
the ambiguous "$100.00" amount text. Keep the existing order setup and render
flow unchanged.

In `@src/components/order-invoice-pdf/__tests__/ledger-consistency.test.js`:
- Around line 77-79: Update the SponsorOrderGrid test’s row selection and
assertions to explicitly account for the appended TotalRow, asserting the
expected ledger-row count before the index-based comparison. Keep the
running-balance/order comparison scoped to ledger rows and make any future row
reordering fail clearly.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 6456ee60-09a2-456a-9cbc-c9bddfe6e4b2

📥 Commits

Reviewing files that changed from the base of the PR and between 43f0d8c and 9e4eb58.

📒 Files selected for processing (8)
  • src/components/mui/SponsorOrderGrid/__tests__/SponsorOrderGrid.test.js
  • src/components/mui/SponsorOrderGrid/index.js
  • src/components/order-invoice-pdf/__tests__/ledger-consistency.test.js
  • src/components/order-invoice-pdf/__tests__/order-invoice-pdf.test.js
  • src/components/order-invoice-pdf/helpers.js
  • src/components/order-invoice-pdf/index.js
  • src/utils/__tests__/order-ledger.test.js
  • src/utils/order-ledger.js

Comment thread src/components/mui/SponsorOrderGrid/index.js
Comment thread src/components/mui/SponsorOrderGrid/index.js Outdated
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR centralizes the derivation of an order “ledger” (row ordering, row keys, and running balances) into a shared utility so the SponsorOrderGrid (MUI) and invoice PDF stay consistent.

Changes:

  • Added buildOrderLedger as a single source of truth for ledger entry ordering, row keys, and running balance computation.
  • Refactored invoice PDF row building (buildRows) and SponsorOrderGrid rendering to consume the shared ledger.
  • Added unit + integration/regression tests to prevent the PDF/grid from drifting again.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/utils/order-ledger.js Introduces shared ledger derivation (entries + running balance).
src/utils/tests/order-ledger.test.js Unit tests for ledger behavior (keys, filtering, ordering, balances).
src/components/order-invoice-pdf/index.js Adjusts PDF to call buildRows(order) with the updated signature.
src/components/order-invoice-pdf/helpers.js Refactors PDF buildRows into a thin mapper over buildOrderLedger.
src/components/order-invoice-pdf/tests/order-invoice-pdf.test.js Updates/relocates coverage to focus on presentational mapping rather than ledger math.
src/components/order-invoice-pdf/tests/ledger-consistency.test.js Adds cross-consumer regression test to ensure PDF/grid match ledger ordering/balances.
src/components/mui/SponsorOrderGrid/index.js Refactors grid rendering to iterate over buildOrderLedger(order) and use ledger balances/keys.
src/components/mui/SponsorOrderGrid/tests/SponsorOrderGrid.test.js Adds regression coverage for previously divergent behaviors now driven by the ledger.
Suppressed comments (1)

src/components/mui/SponsorOrderGrid/index.js:284

  • For refunds where reason/status are missing (a case already handled in the invoice PDF), the grid will render an empty reason/status. Normalize these fields before passing the refund to RefundRow (fallback reason to mui_table.refund, status to empty string) to avoid blank rows and keep PDF/grid behavior aligned.
                      refund={entry.refund}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -79,26 +74,24 @@ const SponsorOrderGrid = ({

const {
forms = [],
Comment on lines +270 to +278
case "payment":
return (
<PaymentRow
key={entry.rowKey}
payment={entry.payment}
balance={entry.balanceCents}
trailing={trailingCols}
/>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants