Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invoice event unmarshaling #2162

Merged
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
20 changes: 17 additions & 3 deletions openmeter/billing/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@ const (
EventSubsystem metadata.EventSubsystem = "billing"
)

type EventInvoice Invoice

func NewEventInvoice(invoice Invoice) EventInvoice {
// This causes a stack overflow
payload := invoice.RemoveCircularReferences()

// Remove the Apps from the payload, as they are not json unmarshallable
// but either ways, the apps service should be used in workers to acquire
// an up-to-date app based on the payload.Workflow.AppReferences
payload.Workflow.Apps = nil

return EventInvoice(payload)
}

type InvoiceCreatedEvent struct {
Invoice `json:",inline"`
EventInvoice `json:",inline"`
}

func NewInvoiceCreatedEvent(invoice Invoice) InvoiceCreatedEvent {
return InvoiceCreatedEvent{invoice.RemoveCircularReferences()}
return InvoiceCreatedEvent{NewEventInvoice(invoice)}
}

func (e InvoiceCreatedEvent) EventName() string {
Expand All @@ -32,5 +46,5 @@ func (e InvoiceCreatedEvent) EventMetadata() metadata.EventMetadata {
}

func (e InvoiceCreatedEvent) Validate() error {
return e.Invoice.Validate()
return e.EventInvoice.Validate()
}
2 changes: 1 addition & 1 deletion openmeter/billing/worker/subscription/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ func (h *Handler) cloneLineForUpsert(line *billing.Line) *billing.Line {

// HandleInvoiceCreation is a handler for the invoice creation event, it will make sure that
// we are backfilling the items consumed by invoice creation into the gathering invoice.
func (h *Handler) HandleInvoiceCreation(ctx context.Context, invoice billing.Invoice) error {
func (h *Handler) HandleInvoiceCreation(ctx context.Context, invoice billing.EventInvoice) error {
if invoice.Status == billing.InvoiceStatusGathering {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion openmeter/billing/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (w *Worker) eventHandler(opts WorkerOptions) (message.NoPublishHandlerFunc,
return w.subscriptionHandler.SyncronizeSubscription(ctx, event.UpdatedView, time.Now())
}),
grouphandler.NewGroupEventHandler(func(ctx context.Context, event *billing.InvoiceCreatedEvent) error {
return w.subscriptionHandler.HandleInvoiceCreation(ctx, event.Invoice)
return w.subscriptionHandler.HandleInvoiceCreation(ctx, event.EventInvoice)
}),
)
if err != nil {
Expand Down
Loading