Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion openmeter/billing/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ type CustomerSynchronizationAdapter interface {
type InvoiceLineAdapter interface {
UpsertInvoiceLines(ctx context.Context, input UpsertInvoiceLinesAdapterInput) ([]*StandardLine, error)
ListInvoiceLines(ctx context.Context, input ListInvoiceLinesAdapterInput) ([]*StandardLine, error)
GetLinesForSubscription(ctx context.Context, input GetLinesForSubscriptionInput) ([]LineOrHierarchy, error)
}

type InvoiceAdapter interface {
Expand All @@ -78,6 +77,7 @@ type StandardInvoiceAdapter interface {
UpdateStandardInvoice(ctx context.Context, input UpdateStandardInvoiceAdapterInput) (StandardInvoice, error)
ListStandardInvoicesPendingAdvancement(ctx context.Context, input ListStandardInvoicesPendingAdvancementInput) ([]StandardInvoice, error)
CountStandardInvoicesPendingAdvancement(ctx context.Context, input CountStandardInvoicesPendingAdvancementInput) (int64, error)
GetStandardLinesForSubscription(ctx context.Context, input GetStandardLinesForSubscriptionInput) ([]LineOrHierarchy, error)
}

type GatheringInvoiceAdapter interface {
Expand All @@ -86,6 +86,7 @@ type GatheringInvoiceAdapter interface {
DeleteGatheringInvoice(ctx context.Context, input DeleteGatheringInvoiceAdapterInput) error
GetGatheringInvoiceById(ctx context.Context, input GetGatheringInvoiceByIdInput) (GatheringInvoice, error)
ListGatheringInvoices(ctx context.Context, input ListGatheringInvoicesInput) (pagination.Result[GatheringInvoice], error)
GetGatheringLinesForSubscription(ctx context.Context, input GetGatheringLinesForSubscriptionInput) (GatheringLines, error)

HardDeleteGatheringInvoiceLines(ctx context.Context, invoiceID InvoiceID, lineIDs []string) error
}
Expand Down
24 changes: 22 additions & 2 deletions openmeter/billing/adapter/gatheringinvoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/openmeterio/openmeter/api"
"github.com/openmeterio/openmeter/openmeter/billing"
"github.com/openmeterio/openmeter/openmeter/ent/db"
"github.com/openmeterio/openmeter/openmeter/ent/db/billinggatheringinvoiceline"
"github.com/openmeterio/openmeter/openmeter/ent/db/billinginvoice"
"github.com/openmeterio/openmeter/openmeter/ent/db/billinginvoiceline"
"github.com/openmeterio/openmeter/pkg/clock"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/openmeterio/openmeter/pkg/framework/entutils"
"github.com/openmeterio/openmeter/pkg/models"
"github.com/openmeterio/openmeter/pkg/pagination"
"github.com/openmeterio/openmeter/pkg/slicesx"
"github.com/openmeterio/openmeter/pkg/sortx"
"github.com/openmeterio/openmeter/pkg/timeutil"
)
Expand Down Expand Up @@ -343,7 +345,7 @@ func (a *adapter) DeleteGatheringInvoice(ctx context.Context, input billing.Dele
}

func (a *adapter) expandGatheringInvoiceLines(q *db.BillingInvoiceQuery, expand billing.GatheringInvoiceExpands) *db.BillingInvoiceQuery {
return q.WithBillingInvoiceLines(func(q *db.BillingInvoiceLineQuery) {
q = q.WithBillingInvoiceLines(func(q *db.BillingInvoiceLineQuery) {
if !expand.Has(billing.GatheringInvoiceExpandDeletedLines) {
q = q.Where(billinginvoiceline.DeletedAtIsNil())
}
Expand All @@ -354,6 +356,14 @@ func (a *adapter) expandGatheringInvoiceLines(q *db.BillingInvoiceQuery, expand
WithUsageBasedLine().
WithTaxCode()
})

return q.WithBillingGatheringInvoiceLines(func(q *db.BillingGatheringInvoiceLineQuery) {
if !expand.Has(billing.GatheringInvoiceExpandDeletedLines) {
q.Where(billinggatheringinvoiceline.DeletedAtIsNil())
}

q.WithTaxCode()
})
}

func (a *adapter) GetGatheringInvoiceById(ctx context.Context, input billing.GetGatheringInvoiceByIdInput) (billing.GatheringInvoice, error) {
Expand Down Expand Up @@ -428,7 +438,17 @@ func (a *adapter) mapGatheringInvoiceFromDB(ctx context.Context, invoice *db.Bil
}

if expand.Has(billing.GatheringInvoiceExpandLines) {
mappedLines, err := a.mapGatheringInvoiceLinesFromDB(invoice.SchemaLevel, invoice.Edges.BillingInvoiceLines)
legacyLines, err := slicesx.MapWithErr(invoice.Edges.BillingInvoiceLines, a.fromDBBillingInvoiceLine)
if err != nil {
return billing.GatheringInvoice{}, err
}

gatheringLines, err := slicesx.MapWithErr(invoice.Edges.BillingGatheringInvoiceLines, a.fromDBBillingGatheringInvoiceLine)
if err != nil {
return billing.GatheringInvoice{}, err
}

mappedLines, err := mergeGatheringLines(legacyLines, gatheringLines)
if err != nil {
return billing.GatheringInvoice{}, err
}
Expand Down
Loading
Loading