| package notification |
|
|
| import ( |
| "encoding/json" |
| "errors" |
| "fmt" |
|
|
| "github.com/openmeterio/openmeter/pkg/models" |
| ) |
|
|
| type RawPayload map[string]any |
|
|
| func AsRawPayload(t any) (RawPayload, error) { |
| b, err := json.Marshal(t) |
| if err != nil { |
| return nil, err |
| } |
|
|
| var m RawPayload |
| if err = json.Unmarshal(b, &m); err != nil { |
| return nil, err |
| } |
|
|
| return m, nil |
| } |
|
|
| type EventPayloadMeta struct { |
| Type EventType `json:"type"` |
| Version int `json:"version,omitempty"` |
| } |
|
|
| func (m EventPayloadMeta) Validate() error { |
| return m.Type.Validate() |
| } |
|
|
| const ( |
| |
| EventPayloadVersionCurrent int = 1 |
| ) |
|
|
| |
| type EventPayload struct { |
| EventPayloadMeta |
|
|
| |
| BalanceThreshold *BalanceThresholdPayload `json:"balanceThreshold,omitempty"` |
| EntitlementReset *EntitlementResetPayload `json:"entitlementReset,omitempty"` |
|
|
| |
| Invoice *InvoicePayload `json:"invoice,omitempty"` |
| } |
|
|
| func (p EventPayload) Validate() error { |
| switch p.Type { |
| case EventTypeBalanceThreshold: |
| if p.BalanceThreshold == nil { |
| return models.NewGenericValidationError(errors.New("missing balance threshold payload")) |
| } |
|
|
| return p.BalanceThreshold.Validate() |
| case EventTypeEntitlementReset: |
| if p.EntitlementReset == nil { |
| return models.NewGenericValidationError(errors.New("missing entitlement reset payload")) |
| } |
|
|
| return p.EntitlementReset.Validate() |
| case EventTypeInvoiceCreated, EventTypeInvoiceUpdated: |
| if p.Invoice == nil { |
| return models.NewGenericValidationError(errors.New("missing invoice payload")) |
| } |
|
|
| return p.Invoice.Validate() |
| default: |
| return models.NewGenericValidationError(fmt.Errorf("invalid event type: %s", p.Type)) |
| } |
| } |
|
|