Skip to content

Commit e281913

Browse files
authored
refactor: add helpers for flat fee line creation (#2883)
1 parent b8272d9 commit e281913

File tree

6 files changed

+171
-166
lines changed

6 files changed

+171
-166
lines changed

openmeter/billing/invoiceline.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,70 @@ func (i *Line) DisassociateChildren() {
559559
}
560560
}
561561

562+
// helper functions for generating new lines
563+
type NewFlatFeeLineInput struct {
564+
ID string
565+
CreatedAt time.Time
566+
UpdatedAt time.Time
567+
568+
Namespace string
569+
Period Period
570+
InvoiceAt time.Time
571+
572+
InvoiceID string
573+
574+
Name string
575+
Metadata map[string]string
576+
Description *string
577+
578+
Currency currencyx.Code
579+
580+
ManagedBy InvoiceLineManagedBy
581+
582+
PerUnitAmount alpacadecimal.Decimal
583+
PaymentTerm productcatalog.PaymentTermType
584+
// TODO: Is this needed?
585+
// Category FlatFeeCategory
586+
Quantity alpacadecimal.Decimal
587+
588+
RateCardDiscounts Discounts
589+
}
590+
591+
func NewFlatFeeLine(input NewFlatFeeLineInput) *Line {
592+
return &Line{
593+
LineBase: LineBase{
594+
Namespace: input.Namespace,
595+
ID: input.ID,
596+
CreatedAt: input.CreatedAt,
597+
UpdatedAt: input.UpdatedAt,
598+
599+
Period: input.Period,
600+
InvoiceAt: input.InvoiceAt,
601+
InvoiceID: input.InvoiceID,
602+
603+
Name: input.Name,
604+
Metadata: input.Metadata,
605+
Description: input.Description,
606+
607+
Status: InvoiceLineStatusValid,
608+
609+
Type: InvoiceLineTypeFee,
610+
611+
ManagedBy: lo.CoalesceOrEmpty(input.ManagedBy, SystemManagedLine),
612+
613+
Currency: input.Currency,
614+
RateCardDiscounts: input.RateCardDiscounts,
615+
},
616+
FlatFee: &FlatFeeLine{
617+
PerUnitAmount: input.PerUnitAmount,
618+
PaymentTerm: input.PaymentTerm,
619+
// Category: lo.CoalesceOrEmpty(input.Category, FlatFeeCategoryRegular),
620+
Category: FlatFeeCategoryRegular,
621+
Quantity: input.Quantity,
622+
},
623+
}
624+
}
625+
562626
// TODO[OM-1016]: For events we need a json marshaler
563627
type LineChildren struct {
564628
mo.Option[[]*Line]

openmeter/notification/internal/rule.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -185,32 +185,25 @@ func (t *TestEventGenerator) newTestInvoicePayload(ctx context.Context, namespac
185185
Number: lo.ToPtr("TEST-INV-1"),
186186
Currency: currencyx.Code(currency.USD),
187187
Lines: billing.NewLineChildren([]*billing.Line{
188-
{
189-
LineBase: billing.LineBase{
190-
Namespace: namespace,
191-
ID: ulid.Make().String(),
192-
CreatedAt: now,
193-
UpdatedAt: now,
194-
195-
ManagedBy: billing.ManuallyManagedLine,
196-
197-
Name: "test flat fee",
198-
Type: billing.InvoiceLineTypeFee,
199-
Period: billing.Period{
200-
Start: now.Add(-time.Hour * 24 * 30),
201-
End: now,
202-
},
203-
InvoiceAt: now,
204-
205-
Status: billing.InvoiceLineStatusValid,
206-
},
207-
FlatFee: &billing.FlatFeeLine{
208-
PerUnitAmount: alpacadecimal.NewFromInt(1000),
209-
Quantity: alpacadecimal.NewFromInt(1),
210-
PaymentTerm: productcatalog.InAdvancePaymentTerm,
211-
Category: billing.FlatFeeCategoryRegular,
188+
billing.NewFlatFeeLine(billing.NewFlatFeeLineInput{
189+
Namespace: namespace,
190+
ID: ulid.Make().String(),
191+
CreatedAt: now,
192+
UpdatedAt: now,
193+
194+
ManagedBy: billing.ManuallyManagedLine,
195+
196+
Name: "test flat fee",
197+
Period: billing.Period{
198+
Start: now.Add(-time.Hour * 24 * 30),
199+
End: now,
212200
},
213-
},
201+
InvoiceAt: now,
202+
203+
PerUnitAmount: alpacadecimal.NewFromInt(1000),
204+
Quantity: alpacadecimal.NewFromInt(1),
205+
PaymentTerm: productcatalog.InAdvancePaymentTerm,
206+
}),
214207
}),
215208
})
216209
if err != nil {

test/app/custominvoicing/invocing_test.go

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -182,24 +182,18 @@ func (s *CustomInvoicingTestSuite) TestInvoicingFlowHooksEnabled() {
182182
Customer: customerEntity.GetID(),
183183
Currency: currencyx.Code(currency.HUF),
184184
Lines: []*billing.Line{
185-
{
186-
LineBase: billing.LineBase{
187-
Period: billing.Period{Start: periodStart, End: periodEnd},
185+
billing.NewFlatFeeLine(billing.NewFlatFeeLineInput{
186+
Period: billing.Period{Start: periodStart, End: periodEnd},
188187

189-
InvoiceAt: issueAt,
190-
ManagedBy: billing.ManuallyManagedLine,
188+
InvoiceAt: issueAt,
189+
ManagedBy: billing.ManuallyManagedLine,
191190

192-
Type: billing.InvoiceLineTypeFee,
191+
Name: "Test item - HUF",
193192

194-
Name: "Test item - HUF",
195-
},
196-
FlatFee: &billing.FlatFeeLine{
197-
PerUnitAmount: alpacadecimal.NewFromFloat(200),
198-
Quantity: alpacadecimal.NewFromFloat(3),
199-
Category: billing.FlatFeeCategoryRegular,
200-
PaymentTerm: productcatalog.InAdvancePaymentTerm,
201-
},
202-
},
193+
PerUnitAmount: alpacadecimal.NewFromFloat(200),
194+
Quantity: alpacadecimal.NewFromFloat(3),
195+
PaymentTerm: productcatalog.InAdvancePaymentTerm,
196+
}),
203197
{
204198
LineBase: billing.LineBase{
205199
Period: billing.Period{Start: periodStart, End: periodEnd},
@@ -357,24 +351,18 @@ func (s *CustomInvoicingTestSuite) TestInvoicingFlowPaymentStatusOnly() {
357351
Customer: customerEntity.GetID(),
358352
Currency: currencyx.Code(currency.HUF),
359353
Lines: []*billing.Line{
360-
{
361-
LineBase: billing.LineBase{
362-
Period: billing.Period{Start: periodStart, End: periodEnd},
354+
billing.NewFlatFeeLine(billing.NewFlatFeeLineInput{
355+
Period: billing.Period{Start: periodStart, End: periodEnd},
363356

364-
InvoiceAt: issueAt,
365-
ManagedBy: billing.ManuallyManagedLine,
357+
InvoiceAt: issueAt,
358+
ManagedBy: billing.ManuallyManagedLine,
366359

367-
Type: billing.InvoiceLineTypeFee,
360+
Name: "Test item - HUF",
368361

369-
Name: "Test item - HUF",
370-
},
371-
FlatFee: &billing.FlatFeeLine{
372-
PerUnitAmount: alpacadecimal.NewFromFloat(200),
373-
Quantity: alpacadecimal.NewFromFloat(3),
374-
Category: billing.FlatFeeCategoryRegular,
375-
PaymentTerm: productcatalog.InAdvancePaymentTerm,
376-
},
377-
},
362+
PerUnitAmount: alpacadecimal.NewFromFloat(200),
363+
Quantity: alpacadecimal.NewFromFloat(3),
364+
PaymentTerm: productcatalog.InAdvancePaymentTerm,
365+
}),
378366
},
379367
})
380368
s.NoError(err, "failed to create pending invoice lines")

test/billing/collection_test.go

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -250,21 +250,15 @@ func (s *CollectionTestSuite) TestCollectionFlowWithFlatFeeOnly() {
250250
Customer: customer.GetID(),
251251
Currency: currencyx.Code(currency.USD),
252252
Lines: []*billing.Line{
253-
{
254-
LineBase: billing.LineBase{
255-
Period: billing.Period{Start: periodStart, End: periodEnd},
256-
InvoiceAt: periodStart,
257-
ManagedBy: billing.ManuallyManagedLine,
258-
Type: billing.InvoiceLineTypeFee,
259-
Name: "Flat fee",
260-
},
261-
FlatFee: &billing.FlatFeeLine{
262-
PerUnitAmount: alpacadecimal.NewFromFloat(10),
263-
Quantity: alpacadecimal.NewFromFloat(1),
264-
PaymentTerm: productcatalog.InAdvancePaymentTerm,
265-
Category: billing.FlatFeeCategoryRegular,
266-
},
267-
},
253+
billing.NewFlatFeeLine(billing.NewFlatFeeLineInput{
254+
Period: billing.Period{Start: periodStart, End: periodEnd},
255+
InvoiceAt: periodStart,
256+
Name: "Flat fee",
257+
258+
PerUnitAmount: alpacadecimal.NewFromFloat(10),
259+
Quantity: alpacadecimal.NewFromFloat(1),
260+
PaymentTerm: productcatalog.InAdvancePaymentTerm,
261+
}),
268262
},
269263
})
270264
s.NoError(err)
@@ -362,25 +356,20 @@ func (s *CollectionTestSuite) TestCollectionFlowWithFlatFeeEditing() {
362356
End: periodEnd.Add(time.Hour * 2),
363357
}
364358

365-
invoice.Lines.Append(&billing.Line{
366-
LineBase: billing.LineBase{
359+
invoice.Lines.Append(
360+
billing.NewFlatFeeLine(billing.NewFlatFeeLineInput{
367361
Namespace: namespace,
368362
Currency: currencyx.Code(currency.USD),
369363
InvoiceID: invoice.ID,
370-
Status: billing.InvoiceLineStatusValid,
371364
Period: linePeriod,
372365
InvoiceAt: linePeriod.End,
373-
ManagedBy: billing.ManuallyManagedLine,
374-
Type: billing.InvoiceLineTypeFee,
375366
Name: "Flat fee",
376-
},
377-
FlatFee: &billing.FlatFeeLine{
367+
378368
PerUnitAmount: alpacadecimal.NewFromFloat(10),
379369
Quantity: alpacadecimal.NewFromFloat(1),
380370
PaymentTerm: productcatalog.InArrearsPaymentTerm,
381-
Category: billing.FlatFeeCategoryRegular,
382-
},
383-
})
371+
}),
372+
)
384373
return nil
385374
},
386375
})

0 commit comments

Comments
 (0)