File size: 3,499 Bytes
429334c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | package app
import (
"errors"
"fmt"
"github.com/openmeterio/openmeter/pkg/models"
)
// AppType represents the type of an app
type AppType string
const (
AppTypeStripe AppType = "stripe"
AppTypeSandbox AppType = "sandbox"
AppTypeCustomInvoicing AppType = "custom_invoicing"
)
func (t AppType) Validate() error {
switch t {
case AppTypeStripe, AppTypeSandbox, AppTypeCustomInvoicing:
return nil
default:
return models.NewGenericValidationError(fmt.Errorf("invalid app type: %s", t))
}
}
// AppStatus represents the status of an app
type AppStatus string
const (
AppStatusReady AppStatus = "ready"
AppStatusUnauthorized AppStatus = "unauthorized"
)
type CapabilityType string
const (
CapabilityTypeReportUsage CapabilityType = "reportUsage"
CapabilityTypeReportEvents CapabilityType = "reportEvents"
CapabilityTypeCalculateTax CapabilityType = "calculateTax"
CapabilityTypeInvoiceCustomers CapabilityType = "invoiceCustomers"
CapabilityTypeCollectPayments CapabilityType = "collectPayments"
)
// AppBase represents an abstract with the base fields of an app
type AppBase struct {
models.ManagedResource
Type AppType `json:"type"`
Status AppStatus `json:"status"`
Listing MarketplaceListing `json:"listing"`
Metadata models.Metadata `json:"metadata,omitempty"`
}
func (a AppBase) GetAppBase() AppBase {
return a
}
func (a AppBase) GetID() AppID {
return AppID{
Namespace: a.Namespace,
ID: a.ID,
}
}
func (a AppBase) GetType() AppType {
return a.Type
}
func (a AppBase) GetName() string {
return a.Name
}
func (a AppBase) GetDescription() *string {
return a.Description
}
func (a AppBase) GetStatus() AppStatus {
return a.Status
}
func (a AppBase) GetListing() MarketplaceListing {
return a.Listing
}
func (a AppBase) GetMetadata() models.Metadata {
return a.Metadata
}
// ValidateCapabilities validates if the app can run for the given capabilities
func (a AppBase) ValidateCapabilities(capabilities ...CapabilityType) error {
for _, capability := range capabilities {
found := false
for _, c := range a.Listing.Capabilities {
if c.Type == capability {
found = true
break
}
}
if !found {
return fmt.Errorf("capability %s is not supported by %s app type", capability, a.Type)
}
}
return nil
}
// ValidateCustomer validates if the app can run for the given customer
// func (a AppBase) ValidateCustomer(c customerentity.Customer, capabilities []CapabilityType) error {
// return fmt.Errorf("each app must implement its own ValidateCustomer method")
// }
// Validate validates the app base
func (a AppBase) Validate() error {
if err := a.ManagedResource.Validate(); err != nil {
return fmt.Errorf("error validating managed resource: %w", err)
}
if a.ID == "" {
return errors.New("id is required")
}
if a.Namespace == "" {
return errors.New("namespace is required")
}
if a.Name == "" {
return errors.New("name is required")
}
if a.Status == "" {
return errors.New("status is required")
}
if err := a.Listing.Validate(); err != nil {
return fmt.Errorf("error validating listing: %w", err)
}
return nil
}
// AppID represents the unique identifier for an installed app
type AppID struct {
Namespace string
ID string
}
func (i AppID) Validate() error {
if i.Namespace == "" {
return errors.New("namespace is required")
}
if i.ID == "" {
return errors.New("id is required")
}
return nil
}
|