diff --git a/openmeter/billing/events.go b/openmeter/billing/events.go index 71f86f701..37c675417 100644 --- a/openmeter/billing/events.go +++ b/openmeter/billing/events.go @@ -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 { @@ -32,5 +46,5 @@ func (e InvoiceCreatedEvent) EventMetadata() metadata.EventMetadata { } func (e InvoiceCreatedEvent) Validate() error { - return e.Invoice.Validate() + return e.EventInvoice.Validate() } diff --git a/openmeter/billing/worker/subscription/sync.go b/openmeter/billing/worker/subscription/sync.go index 48b2f6b6a..c7ac813f6 100644 --- a/openmeter/billing/worker/subscription/sync.go +++ b/openmeter/billing/worker/subscription/sync.go @@ -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 } diff --git a/openmeter/billing/worker/worker.go b/openmeter/billing/worker/worker.go index 6e0b981a1..01471b83a 100644 --- a/openmeter/billing/worker/worker.go +++ b/openmeter/billing/worker/worker.go @@ -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 {