Repository navigation
Feat: Customer create charges - #4503
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThis PR adds a complete customer charge creation endpoint at POST /openmeter/customers/{customerId}/charges, supporting flat-fee and usage-based variants with proration, amount-before-proration, discounts, tax parsing, generated TypeScript client/SDK/types/schemas, and Go handler/converter and route wiring. ChangesCustomer Charge Creation Feature
Sequence DiagramsequenceDiagram
participant Client
participant Server
participant ChargesHandler
participant BillingChargeService
Client->>Server: POST /openmeter/customers/{customerId}/charges (createCharge)
Server->>ChargesHandler: invoke CreateCustomerCharge handler (parsed body)
ChargesHandler->>BillingChargeService: Create(namespace, ChargeIntent)
BillingChargeService-->>ChargesHandler: created Charge
ChargesHandler-->>Server: respond 201 with BillingCharge
Server-->>Client: HTTP 201 BillingCharge
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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 |
6ef7ba0 to
5f36f3e
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
api/v3/handlers/customers/charges/create.go (1)
124-136: ⚡ Quick winConsider using structured errors for service validation.
The validation checks on lines 129-134 use plain
errors.New(), but other validation errors in the request parser useapierrors.NewBadRequestError(). For consistency and better error responses to clients, consider wrapping these in structured API errors too.These cases represent contract violations from the service layer (unexpected result cardinality), so they could be Internal Server Errors rather than Bad Requests, but structured errors would still be clearer.
♻️ Proposed refactor
res, err := h.service.Create(ctx, request) if err != nil { return CreateCustomerChargesResponse{}, err } if len(res) < 1 { - return CreateCustomerChargesResponse{}, errors.New("no charge created") + return CreateCustomerChargesResponse{}, apierrors.NewInternalServerError(ctx, errors.New("no charge created")) } if len(res) > 1 { - return CreateCustomerChargesResponse{}, errors.New("too many results") + return CreateCustomerChargesResponse{}, apierrors.NewInternalServerError(ctx, errors.New("too many results")) } return convertChargeToAPI(res[0])🤖 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 `@api/v3/handlers/customers/charges/create.go` around lines 124 - 136, Replace the plain errors.New calls used for unexpected result cardinality in the Create handler with structured API errors (use apierrors.NewInternalServerError with a clear message and wrap the original context), i.e., where the anonymous handler checks len(res) < 1 and len(res) > 1 after calling h.service.Create, return apierrors.NewInternalServerError(...) instead of errors.New so the client receives a consistent structured error; keep the rest of the flow (calling convertChargeToAPI) unchanged.
🤖 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 `@api/v3/handlers/customers/charges/create.go`:
- Around line 102-109: The default switch branch returns
CreateCustomerChargesRequest{} and calls apierrors.NewBadRequestError(ctx, err,
...), but err is nil there; replace the second argument with either nil or a
newly constructed descriptive error (e.g., errors.New("unexpected charge type
discriminator: <value>")) so the BadRequestError contains meaningful context;
update the default path that produces the apierrors.NewBadRequestError(...) to
pass a proper error value and include the actual discriminator value in the
message for apierrors.NewBadRequestError, referencing the same
switch/discriminator variable used in this function.
---
Nitpick comments:
In `@api/v3/handlers/customers/charges/create.go`:
- Around line 124-136: Replace the plain errors.New calls used for unexpected
result cardinality in the Create handler with structured API errors (use
apierrors.NewInternalServerError with a clear message and wrap the original
context), i.e., where the anonymous handler checks len(res) < 1 and len(res) > 1
after calling h.service.Create, return apierrors.NewInternalServerError(...)
instead of errors.New so the client receives a consistent structured error; keep
the rest of the flow (calling convertChargeToAPI) unchanged.
🪄 Autofix (Beta)
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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 2ebf7386-9c98-4c37-8924-e5d5cc07b837
⛔ Files ignored due to path filters (1)
api/v3/openapi.yamlis excluded by!**/openapi.yaml
📒 Files selected for processing (16)
api/spec/packages/aip-client-javascript/README.mdapi/spec/packages/aip-client-javascript/src/funcs/customers.tsapi/spec/packages/aip-client-javascript/src/index.tsapi/spec/packages/aip-client-javascript/src/models/operations/customers.tsapi/spec/packages/aip-client-javascript/src/models/operations/subscriptions.tsapi/spec/packages/aip-client-javascript/src/models/schemas.tsapi/spec/packages/aip-client-javascript/src/models/types.tsapi/spec/packages/aip-client-javascript/src/sdk/customers.tsapi/spec/packages/aip/src/customers/charges/charges.tspapi/spec/packages/aip/src/customers/charges/operations.tspapi/spec/packages/aip/src/productcatalog/ratecard.tspapi/v3/api.gen.goapi/v3/handlers/customers/charges/convert.goapi/v3/handlers/customers/charges/create.goapi/v3/handlers/customers/charges/handler.goapi/v3/server/routes.go
👮 Files not reviewed due to content moderation or server errors (6)
- api/spec/packages/aip-client-javascript/src/models/schemas.ts
- api/spec/packages/aip/src/customers/charges/charges.tsp
- api/spec/packages/aip/src/productcatalog/ratecard.tsp
- api/spec/packages/aip-client-javascript/src/models/operations/subscriptions.ts
- api/spec/packages/aip-client-javascript/src/index.ts
- api/v3/handlers/customers/charges/convert.go
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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 `@api/spec/packages/aip-client-javascript/src/models/schemas.ts`:
- Around line 4924-4927: The operation request type for the Create Customer
Charges endpoint is still using Record<string, never> even though
createCustomerChargesBody is mapped to createChargeRequest; regenerate or resync
the operation types so CreateCustomerChargesRequest.body references the
create-charge schema (createChargeRequest) instead of Record<string, never>.
Locate the operations file that defines CreateCustomerChargesRequest
(customers.ts) and either re-run the OpenAPI/typegen to update the generated
types or replace the body type to the createChargeRequest schema type used in
models/schemas.ts so CustomersCharges.create() accepts the new payload shape.
In `@api/spec/packages/aip-client-javascript/src/models/types.ts`:
- Around line 2820-2860: The operation request body type for
create-customer-charges is still typed as Record<string, never>, which prevents
using the new charge models; update the CreateCustomerChargesRequest.body (in
customers.ts) to accept the appropriate create-charge types such as
CreateFlatFeeChargeRequest (and the analogous create types for other charge
kinds you added) or a discriminated union of those types so the SDK contract
reflects the new models; ensure the union uses the same discriminator fields
(e.g., type and managed_by) as defined on CreateFlatFeeChargeRequest so existing
callers compile against CreateCustomerChargesRequest.body.
- Around line 2763-2765: The JSDoc for the amount_before_proration field is
incorrect (it currently repeats "amount after proration"); update the comment
for the amount_before_proration property (type CurrencyAmount) to read "The
amount before proration of the charge." and make the same correction for the
other duplicate occurrence of amount_before_proration around the second block
(the one near lines 2857-2858) so generated SDK docs correctly reflect it is the
pre-proration amount.
🪄 Autofix (Beta)
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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 3f5c01ef-46d6-4b03-b458-27fd72ae2345
⛔ Files ignored due to path filters (1)
api/v3/openapi.yamlis excluded by!**/openapi.yaml
📒 Files selected for processing (15)
api/spec/packages/aip-client-javascript/README.mdapi/spec/packages/aip-client-javascript/src/funcs/customers.tsapi/spec/packages/aip-client-javascript/src/index.tsapi/spec/packages/aip-client-javascript/src/models/operations/customers.tsapi/spec/packages/aip-client-javascript/src/models/schemas.tsapi/spec/packages/aip-client-javascript/src/models/types.tsapi/spec/packages/aip-client-javascript/src/sdk/customers.tsapi/spec/packages/aip/src/customers/charges/charges.tspapi/spec/packages/aip/src/customers/charges/operations.tspapi/spec/packages/aip/src/productcatalog/ratecard.tspapi/v3/api.gen.goapi/v3/handlers/customers/charges/convert.goapi/v3/handlers/customers/charges/create.goapi/v3/handlers/customers/charges/handler.goapi/v3/server/routes.go
✅ Files skipped from review due to trivial changes (1)
- api/spec/packages/aip-client-javascript/README.md
🚧 Files skipped from review as they are similar to previous changes (10)
- api/v3/handlers/customers/charges/handler.go
- api/spec/packages/aip/src/productcatalog/ratecard.tsp
- api/spec/packages/aip-client-javascript/src/models/operations/customers.ts
- api/spec/packages/aip-client-javascript/src/sdk/customers.ts
- api/spec/packages/aip/src/customers/charges/operations.tsp
- api/spec/packages/aip/src/customers/charges/charges.tsp
- api/v3/handlers/customers/charges/create.go
- api/spec/packages/aip-client-javascript/src/funcs/customers.ts
- api/v3/server/routes.go
- api/v3/handlers/customers/charges/convert.go
9bee8b5 to
7bd7ecc
Compare
Greptile SummaryThis PR adds a
Confidence Score: 4/5The Go handler and converter are well-structured and address previous review findings, but the TypeScript SDK ships with amount_before_proration as a required field on the read-response type when the API never returns it. The flatFeeCharge Zod schema and FlatFeeCharge TypeScript interface both declare amount_before_proration as required, yet neither the OpenAPI response schema nor the Go handler ever populate this field. Any consumer that performs runtime Zod validation or relies on TypeScript's type guarantees for flat fee charge reads will encounter failures or undefined values for a field the types claim is always present. api/spec/packages/aip-client-javascript/src/models/schemas.ts and api/spec/packages/aip-client-javascript/src/models/types.ts — the amount_before_proration field needs to be removed from or made optional on the flatFeeCharge / FlatFeeCharge response type. Important Files Changed
|
…nly version in the flat fee, also fix the proration_configuration.mode result
7bd7ecc to
19a65ae
Compare
… based price calculation payment term in arrears. delete managed by
Overview
Notes for reviewer
Summary by CodeRabbit
New Features
Documentation