Skip to content

Commit

Permalink
fix: invoice event unmarshaling
Browse files Browse the repository at this point in the history
We need to remove the invoice.Workflow.Apps member as it's not json serializable.

Either ways, the apps service should be used in workers to acquire
an up-to-date app based on the payload.Workflow.AppReferences
  • Loading branch information
turip committed Jan 28, 2025
1 parent e6da6e4 commit b52791e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
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

0 comments on commit b52791e

Please sign in to comment.