Skip to content

Commit 7d06be8

Browse files
committed
chore: address tax code review feedback
1 parent 34b8f67 commit 7d06be8

17 files changed

Lines changed: 97 additions & 25 deletions

File tree

api/v3/handlers/customers/charges/convert.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,15 @@ func convertTaxCodeConfigToAPI(cfg productcatalog.TaxCodeConfig) *api.BillingTax
375375
return nil
376376
}
377377

378-
return &api.BillingTaxConfig{
379-
Behavior: (*api.BillingTaxBehavior)(cfg.Behavior),
380-
TaxCode: &api.TaxCodeReference{Id: cfg.TaxCodeID},
381-
TaxCodeId: lo.ToPtr(cfg.TaxCodeID),
378+
out := &api.BillingTaxConfig{
379+
Behavior: (*api.BillingTaxBehavior)(cfg.Behavior),
382380
}
381+
if cfg.TaxCodeID != "" {
382+
out.TaxCode = &api.TaxCodeReference{Id: cfg.TaxCodeID}
383+
out.TaxCodeId = lo.ToPtr(cfg.TaxCodeID)
384+
}
385+
386+
return out
383387
}
384388

385389
// convertAPIChargeStatus maps an API status string to its domain equivalent.

api/v3/handlers/customers/charges/convert_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ func TestConvertTaxCodeConfigToAPI(t *testing.T) {
4343
TaxCodeId: lo.ToPtr(api.ULID("01JTEST00000000000000000002")),
4444
},
4545
},
46+
{
47+
name: "behavior only",
48+
input: productcatalog.TaxCodeConfig{
49+
Behavior: lo.ToPtr(productcatalog.InclusiveTaxBehavior),
50+
},
51+
want: &api.BillingTaxConfig{
52+
Behavior: lo.ToPtr(api.BillingTaxBehaviorInclusive),
53+
},
54+
},
4655
}
4756

4857
for _, tt := range tests {

openmeter/billing/charges/charge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func (i ChargeIntent) Meta() (meta.Intent, error) {
412412
}
413413

414414
// WithTaxCodeID returns a copy of the intent with TaxCodeID set to id.
415-
// If TaxConfig is nil a new one is created; all other fields are preserved.
415+
// Existing tax behavior and other intent fields are preserved.
416416
func (i ChargeIntent) WithTaxCodeID(id string) (ChargeIntent, error) {
417417
switch i.t {
418418
case meta.ChargeTypeFlatFee:

openmeter/billing/charges/models/chargemeta/mixin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func (metaMixin) Fields() []ent.Field {
8787
Optional().
8888
Nillable(),
8989
field.String("tax_code_id").
90+
NotEmpty().
9091
SchemaType(map[string]string{
9192
dialect.Postgres: "char(26)",
9293
}),

openmeter/billing/charges/service/taxcode_test.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ func (s *TaxCodePersistenceTestSuite) TestFlatFeeChargePersistsTaxConfig() {
9898
flatFee, err := readBack.AsFlatFeeCharge()
9999
s.NoError(err)
100100

101-
s.Require().NotNil(flatFee.Intent.TaxConfig, "TaxConfig must be populated on read")
102101
s.Require().NotNil(flatFee.Intent.TaxConfig.Behavior, "TaxBehavior must be persisted")
103102
s.Equal(productcatalog.InclusiveTaxBehavior, *flatFee.Intent.TaxConfig.Behavior)
104-
s.Require().NotNil(flatFee.Intent.TaxConfig.TaxCodeID, "TaxCodeID must be persisted as FK")
103+
s.Require().NotEmpty(flatFee.Intent.TaxConfig.TaxCodeID, "TaxCodeID must be persisted as FK")
105104
s.Equal(tc.ID, flatFee.Intent.TaxConfig.TaxCodeID)
106105
})
107106

@@ -135,7 +134,6 @@ func (s *TaxCodePersistenceTestSuite) TestFlatFeeChargePersistsTaxConfig() {
135134
s.NoError(err)
136135

137136
// nil TaxConfig intents get the namespace default invoicing TaxCodeID stamped.
138-
s.Require().NotNil(flatFee.Intent.TaxConfig, "TaxConfig must be stamped with the default invoicing tax code")
139137
s.Require().NotEmpty(flatFee.Intent.TaxConfig.TaxCodeID, "default invoicing TaxCodeID must be stamped")
140138
s.Equal(defaults.InvoicingTaxCodeID, flatFee.Intent.TaxConfig.TaxCodeID)
141139
s.Nil(flatFee.Intent.TaxConfig.Behavior, "Behavior must remain nil when only default TaxCodeID is stamped")
@@ -196,10 +194,9 @@ func (s *TaxCodePersistenceTestSuite) TestUsageBasedChargePersistsTaxConfig() {
196194
usageBased, err := readBack.AsUsageBasedCharge()
197195
s.NoError(err)
198196

199-
s.Require().NotNil(usageBased.Intent.TaxConfig, "TaxConfig must be populated on read")
200197
s.Require().NotNil(usageBased.Intent.TaxConfig.Behavior, "TaxBehavior must be persisted")
201198
s.Equal(productcatalog.ExclusiveTaxBehavior, *usageBased.Intent.TaxConfig.Behavior)
202-
s.Require().NotNil(usageBased.Intent.TaxConfig.TaxCodeID, "TaxCodeID must be persisted as FK")
199+
s.Require().NotEmpty(usageBased.Intent.TaxConfig.TaxCodeID, "TaxCodeID must be persisted as FK")
203200
s.Equal(tc.ID, usageBased.Intent.TaxConfig.TaxCodeID)
204201
})
205202

@@ -233,7 +230,6 @@ func (s *TaxCodePersistenceTestSuite) TestUsageBasedChargePersistsTaxConfig() {
233230
s.NoError(err)
234231

235232
// nil TaxConfig intents get the namespace default invoicing TaxCodeID stamped.
236-
s.Require().NotNil(usageBased.Intent.TaxConfig, "TaxConfig must be stamped with the default invoicing tax code")
237233
s.Require().NotEmpty(usageBased.Intent.TaxConfig.TaxCodeID, "default invoicing TaxCodeID must be stamped")
238234
s.Equal(defaults.InvoicingTaxCodeID, usageBased.Intent.TaxConfig.TaxCodeID)
239235
s.Nil(usageBased.Intent.TaxConfig.Behavior, "Behavior must remain nil when only default TaxCodeID is stamped")
@@ -293,10 +289,9 @@ func (s *TaxCodePersistenceTestSuite) TestCreditPurchaseChargePersistsTaxConfig(
293289
cp, err := readBack.AsCreditPurchaseCharge()
294290
s.NoError(err)
295291

296-
s.Require().NotNil(cp.Intent.TaxConfig, "TaxConfig must be populated on read")
297292
s.Require().NotNil(cp.Intent.TaxConfig.Behavior, "TaxBehavior must be persisted")
298293
s.Equal(productcatalog.InclusiveTaxBehavior, *cp.Intent.TaxConfig.Behavior)
299-
s.Require().NotNil(cp.Intent.TaxConfig.TaxCodeID, "TaxCodeID must be persisted as FK")
294+
s.Require().NotEmpty(cp.Intent.TaxConfig.TaxCodeID, "TaxCodeID must be persisted as FK")
300295
s.Equal(tc.ID, cp.Intent.TaxConfig.TaxCodeID)
301296
})
302297

@@ -333,7 +328,6 @@ func (s *TaxCodePersistenceTestSuite) TestCreditPurchaseChargePersistsTaxConfig(
333328
s.NoError(err)
334329

335330
// nil TaxConfig intents get the namespace default credit-grant TaxCodeID stamped.
336-
s.Require().NotNil(cp.Intent.TaxConfig, "TaxConfig must be stamped with the default credit grant tax code")
337331
s.Require().NotEmpty(cp.Intent.TaxConfig.TaxCodeID, "default credit grant TaxCodeID must be stamped")
338332
s.Equal(defaults.CreditGrantTaxCodeID, cp.Intent.TaxConfig.TaxCodeID)
339333
s.Nil(cp.Intent.TaxConfig.Behavior, "Behavior must remain nil when only default TaxCodeID is stamped")
@@ -471,7 +465,7 @@ func (s *TaxCodePersistenceTestSuite) TestCreditPurchaseInvoiceSettlementPropaga
471465
}
472466

473467
// TestCreditPurchaseInvoiceSettlementNilTaxConfigGetsDefaultCreditGrantTaxCodeStamped verifies that
474-
// when Intent.TaxConfig is nil the gathering line's TaxConfig is populated with the namespace
468+
// when Intent.TaxConfig is zero the gathering line's TaxConfig is populated with the namespace
475469
// default credit-grant tax code ID stamped by applyDefaultTaxCodes.
476470
func (s *TaxCodePersistenceTestSuite) TestCreditPurchaseInvoiceSettlementNilTaxConfigGetsDefaultCreditGrantTaxCodeStamped() {
477471
ctx := s.T().Context()
@@ -599,7 +593,6 @@ func (s *TaxCodePersistenceTestSuite) TestFlatFeeCreditOnlyHandlerReceivesTaxCon
599593
s.NoError(err)
600594
s.Require().Len(advancedCharges, 1)
601595

602-
s.Require().NotNil(capturedInput.Charge.Intent.TaxConfig, "handler must receive TaxConfig after DB roundtrip")
603596
s.Require().NotNil(capturedInput.Charge.Intent.TaxConfig.Behavior)
604597
s.Equal(productcatalog.InclusiveTaxBehavior, *capturedInput.Charge.Intent.TaxConfig.Behavior)
605598
s.Require().NotEmpty(capturedInput.Charge.Intent.TaxConfig.TaxCodeID)
@@ -682,7 +675,6 @@ func (s *TaxCodePersistenceTestSuite) TestUsageBasedCreditOnlyHandlerReceivesTax
682675
_, err = s.Charges.AdvanceCharges(ctx, charges.AdvanceChargesInput{Customer: cust.GetID()})
683676
s.NoError(err)
684677

685-
s.Require().NotNil(capturedInput.Charge.Intent.TaxConfig, "handler must receive TaxConfig after DB roundtrip")
686678
s.Require().NotNil(capturedInput.Charge.Intent.TaxConfig.Behavior)
687679
s.Equal(productcatalog.ExclusiveTaxBehavior, *capturedInput.Charge.Intent.TaxConfig.Behavior)
688680
s.Require().NotEmpty(capturedInput.Charge.Intent.TaxConfig.TaxCodeID)
@@ -893,12 +885,10 @@ func (s *TaxCodePersistenceTestSuite) TestTaxConfigInListCharges() {
893885

894886
if ff.Intent.Intent.UniqueReferenceID != nil && *ff.Intent.Intent.UniqueReferenceID == "flat-fee-list-no-taxcode" {
895887
// nil TaxConfig intents get the default invoicing TaxCodeID stamped.
896-
s.Require().NotNil(ff.Intent.TaxConfig, "flat fee charge without explicit tax config must have default invoicing TaxCodeID stamped")
897888
s.Require().NotEmpty(ff.Intent.TaxConfig.TaxCodeID)
898889
s.Equal(defaults.InvoicingTaxCodeID, ff.Intent.TaxConfig.TaxCodeID)
899890
s.Nil(ff.Intent.TaxConfig.Behavior)
900891
} else {
901-
s.Require().NotNil(ff.Intent.TaxConfig, "flat fee charge must carry TaxConfig in list response")
902892
s.Require().NotNil(ff.Intent.TaxConfig.Behavior)
903893
s.Equal(productcatalog.InclusiveTaxBehavior, *ff.Intent.TaxConfig.Behavior)
904894
s.Require().NotEmpty(ff.Intent.TaxConfig.TaxCodeID)
@@ -908,7 +898,6 @@ func (s *TaxCodePersistenceTestSuite) TestTaxConfigInListCharges() {
908898
case meta.ChargeTypeUsageBased:
909899
ub, err := charge.AsUsageBasedCharge()
910900
s.Require().NoError(err)
911-
s.Require().NotNil(ub.Intent.TaxConfig, "usage-based charge must carry TaxConfig in list response")
912901
s.Require().NotNil(ub.Intent.TaxConfig.Behavior)
913902
s.Equal(productcatalog.InclusiveTaxBehavior, *ub.Intent.TaxConfig.Behavior)
914903
s.Require().NotEmpty(ub.Intent.TaxConfig.TaxCodeID)
@@ -993,7 +982,7 @@ func (s *TaxCodePersistenceTestSuite) TestFlatFeeInvoiceSettlementPropagatesTaxC
993982
}
994983

995984
// TestFlatFeeInvoiceSettlementNilTaxConfigGetsDefaultInvoicingTaxCodeStampedOnGatheringLine verifies
996-
// that when Intent.TaxConfig is nil, the flat-fee CreditThenInvoice gathering line's TaxConfig is
985+
// that when Intent.TaxConfig is zero, the flat-fee CreditThenInvoice gathering line's TaxConfig is
997986
// populated with the namespace default invoicing tax code ID stamped by applyDefaultTaxCodes.
998987
func (s *TaxCodePersistenceTestSuite) TestFlatFeeInvoiceSettlementNilTaxConfigGetsDefaultInvoicingTaxCodeStampedOnGatheringLine() {
999988
ctx := s.T().Context()

openmeter/ent/db/chargecreditpurchase/chargecreditpurchase.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openmeter/ent/db/chargecreditpurchase_create.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openmeter/ent/db/chargecreditpurchase_update.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openmeter/ent/db/chargeflatfee/chargeflatfee.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openmeter/ent/db/chargeflatfee_create.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)