Repository navigation
feat: remove PrepareForCreate from line service - #3823
Conversation
We are removing the PrepareForCreate from the line service as these defaulting/time truncating logic are related to the line entity itself. The line service is to be made a pure calculation-only package.
📝 WalkthroughWalkthroughThe PR removes per-line PrepareForCreate processing and instead adds per-line and batch normalization methods ( Changes
Sequence Diagram(s)mermaid Client->>BillingSvc: CreateGatheringInvoice(request with lines) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 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
🤖 Fix all issues with AI agents
In `@openmeter/billing/service/stdinvoiceline.go`:
- Around line 92-107: The error returned by slicesx.MapWithErr when building
linesToCreate is never checked and gets overwritten by the next call; after
calling slicesx.MapWithErr on input.Lines (creating linesToCreate and err) add
an immediate nil check (if err != nil) and return a wrapped error (e.g.,
fmt.Errorf("normalizing lines: %w", err)) before calling
s.lineService.FromEntities so any failure from WithNormalizedValues() on
billing.StandardLine is propagated.
🧹 Nitpick comments (2)
openmeter/billing/stdinvoiceline.go (1)
325-349: Nice addition for centralizing normalization logic!The method looks solid overall. One thing to consider: there's no nil check before accessing
out.UsageBased.Priceat line 335. IfUsageBasedorPriceis ever nil, this will panic.I noticed the rest of the codebase (like
DependsOnMeteredQuantityandValidateUsageBased) follows the same pattern, so it seems likeUsageBasedbeing non-nil is an implicit invariant. Just wanted to flag this in case you'd like to add a defensive check here since this is new code:🛡️ Optional defensive check
func (i StandardLine) WithNormalizedValues() (*StandardLine, error) { out := i.Clone() out.Period = out.Period.Truncate(streaming.MinimumWindowSizeDuration) out.InvoiceAt = out.InvoiceAt.Truncate(streaming.MinimumWindowSizeDuration) + if out.UsageBased == nil || out.UsageBased.Price == nil { + return out, nil + } + if out.UsageBased.Price.Type() == productcatalog.FlatPriceType {openmeter/billing/service/stdinvoiceline.go (1)
60-68: Minor: Consider moving the empty check beforeresolveFeatureMeters.The empty lines check at line 66 comes after
resolveFeatureMetersat line 61. Ifinput.Linesis empty, you're doing unnecessary work resolving feature meters. Could be a small optimization to check first:♻️ Optional reorder
return transcationForInvoiceManipulation(ctx, s, input.Customer, func(ctx context.Context) (*billing.CreatePendingInvoiceLinesResult, error) { + if len(input.Lines) == 0 { + return nil, nil + } + featureMeters, err := s.resolveFeatureMeters(ctx, input.Lines) if err != nil { return nil, fmt.Errorf("resolving feature meters: %w", err) } - if len(input.Lines) == 0 { - return nil, nil - }
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@openmeter/billing/service/stdinvoiceline.go`:
- Around line 61-62: CreatePendingInvoiceLines currently returns nil for empty
input.Lines, but the HTTP handler that calls CreatePendingInvoiceLines
dereferences the returned result (accessing res.IsInvoiceNew and res.Invoice)
without checking for nil, causing a potential panic. Fix by either making
CreatePendingInvoiceLines return a descriptive error for empty input or update
the HTTP handler that calls CreatePendingInvoiceLines to check if res == nil
before using res.IsInvoiceNew/res.Invoice and return an appropriate HTTP
response (e.g., 400 Bad Request or a clear error) when lines are empty;
reference CreatePendingInvoiceLines and the HTTP handler which reads
res.IsInvoiceNew and res.Invoice to locate the code to change.
| if len(input.Lines) == 0 { | ||
| return nil, nil |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Find and display the file to see the context around lines 61-62
fd -t f stdinvoiceline.go --exec cat -n {} \;Repository: openmeterio/openmeter
Length of output: 36064
🏁 Script executed:
#!/bin/bash
# Search for all call sites of CreatePendingInvoiceLines
rg -n --type=go 'CreatePendingInvoiceLines' -C 3Repository: openmeterio/openmeter
Length of output: 30598
🏁 Script executed:
#!/bin/bash
# Check the actual usage pattern at a few call sites
# Focus on openmeter code (not tests)
# invoiceupdate.go call
sed -n '155,170p' openmeter/billing/worker/subscriptionsync/service/invoiceupdate.go
# httpdriver call
sed -n '85,100p' openmeter/billing/httpdriver/invoiceline.goRepository: openmeterio/openmeter
Length of output: 1008
Callers don't consistently handle nil result—HTTP handler needs nil check
The interface documents that CreatePendingInvoiceLines returns nil when input lines are empty, but the HTTP handler in openmeter/billing/httpdriver/invoiceline.go (lines 85–100) doesn't guard against this. It directly accesses res.IsInvoiceNew and res.Invoice after only checking the error, which will panic if the result is nil. Add a nil check before dereferencing the result, or return an error when the request contains empty lines.
🤖 Prompt for AI Agents
In `@openmeter/billing/service/stdinvoiceline.go` around lines 61 - 62,
CreatePendingInvoiceLines currently returns nil for empty input.Lines, but the
HTTP handler that calls CreatePendingInvoiceLines dereferences the returned
result (accessing res.IsInvoiceNew and res.Invoice) without checking for nil,
causing a potential panic. Fix by either making CreatePendingInvoiceLines return
a descriptive error for empty input or update the HTTP handler that calls
CreatePendingInvoiceLines to check if res == nil before using
res.IsInvoiceNew/res.Invoice and return an appropriate HTTP response (e.g., 400
Bad Request or a clear error) when lines are empty; reference
CreatePendingInvoiceLines and the HTTP handler which reads res.IsInvoiceNew and
res.Invoice to locate the code to change.
Overview
We are removing the PrepareForCreate from the line service as these defaulting/time truncating logic are related to the line entity itself.
The line service is to be made a pure calculation-only package.
Notes for reviewer
Summary by CodeRabbit
Refactor
New Features
Chores
✏️ Tip: You can customize this high-level summary in your review settings.