models

package
v1.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2019 License: MIT Imports: 29 Imported by: 5

Documentation

Index

Constants

View Source
const (
	NumberType = iota
	StringType
	BoolType
)

NumberType | StringType | BoolType are the different types supported in custom data for orders

View Source
const ChargeTransactionType = "charge"

ChargeTransactionType is the charge transaction type.

View Source
const FailedState = "failed"

FailedState is the failed state of an Order

View Source
const PaidState = "paid"

PaidState is the paid state of an Order

View Source
const PendingState = "pending"

PendingState is the pending state of an Order

View Source
const RefundTransactionType = "refund"

RefundTransactionType is the refund transaction type.

View Source
const ShippedState = "shipped"

ShippedState is the shipped state of an Order

View Source
const ShippingState = "shipping"

ShippingState is the shipping state of an order

Variables

View Source
var FulfillmentStates = []string{
	PendingState,
	ShippingState,
	ShippedState,
}

FulfillmentStates are the possible values for the FulfillmentState field

View Source
var Namespace string

Namespace puts all tables names under a common namespace. This is useful if you want to use the same database for several services and don't want table names to collide.

View Source
var PaymentStates = []string{
	PendingState,
	PaidState,
	FailedState,
}

PaymentState are the possible values for the PaymentState field

Functions

func AutoMigrate added in v0.2.0

func AutoMigrate(db *gorm.DB) error

AutoMigrate runs the gorm automigration for all models

func Connect

func Connect(config *conf.GlobalConfiguration, log logrus.FieldLogger) (*gorm.DB, error)

Connect will connect to that storage engine

func CreateInstance added in v1.1.2

func CreateInstance(db *gorm.DB, instance *Instance) error

func DeleteInstance added in v1.1.2

func DeleteInstance(db *gorm.DB, instance *Instance) error

func IsNotFoundError added in v1.1.2

func IsNotFoundError(err error) bool

IsNotFoundError returns whether an error represents a "not found" error.

func LogEvent added in v1.1.0

func LogEvent(db *gorm.DB, ip, userID, orderID string, eventType EventType, changes []string)

LogEvent logs a new event

func NextInvoiceNumber added in v1.1.2

func NextInvoiceNumber(tx *gorm.DB, instanceID string) (int64, error)

NextInvoiceNumber updates and returns the next invoice number for the instance

func RunHooks added in v1.1.0

func RunHooks(db *gorm.DB, log *logrus.Entry)

RunHooks creates a goroutine that triggers stored webhooks every 5 seconds.

func UpdateInstance added in v1.1.2

func UpdateInstance(db *gorm.DB, instance *Instance) error

Types

type AddonItem added in v1.1.0

type AddonItem struct {
	ID int64 `json:"id"`

	Sku         string `json:"sku"`
	Title       string `json:"title"`
	Description string `json:"description"`

	Price uint64 `json:"price"`
}

AddonItem are additional items for a LineItem.

func (AddonItem) TableName added in v1.1.0

func (AddonItem) TableName() string

TableName returns the database table name for the AddonItem model.

type AddonMetaItem added in v1.1.0

type AddonMetaItem struct {
	Sku         string          `json:"sku"`
	Title       string          `json:"title"`
	Description string          `json:"description"`
	Prices      []PriceMetadata `json:"prices"`
}

AddonMetaItem model

type Address

type Address struct {
	AddressRequest

	ID string `json:"id"`

	User   *User  `json:"-"`
	UserID string `json:"-"`

	CreatedAt time.Time  `json:"created_at"`
	DeletedAt *time.Time `json:"deleted_at"`
}

Address is a stored address, reusable with an ID.

func (Address) TableName added in v0.2.0

func (Address) TableName() string

TableName returns the table name used for the Address model

type AddressRequest added in v0.2.0

type AddressRequest struct {
	Name string `json:"name"`

	Company  string `json:"company"`
	Address1 string `json:"address1"`
	Address2 string `json:"address2"`
	City     string `json:"city"`
	Country  string `json:"country"`
	State    string `json:"state"`
	Zip      string `json:"zip"`

	// deprecated
	FirstName string `json:"first_name,omitempty"`
	LastName  string `json:"last_name,omitempty"`
}

AddressRequest is the raw address data

func (*AddressRequest) AfterFind added in v1.1.2

func (a *AddressRequest) AfterFind() (err error)

AfterFind database callback.

func (*AddressRequest) BeforeSave added in v1.1.2

func (a *AddressRequest) BeforeSave() (err error)

BeforeSave database callback.

func (AddressRequest) Validate added in v0.2.0

func (a AddressRequest) Validate() error

Validate validates the AddressRequest model

type CalculationDetail added in v1.6.0

type CalculationDetail struct {
	Subtotal uint64 `json:"subtotal"`

	Discount      uint64         `json:"discount"`
	DiscountItems []DiscountItem `json:"discount_items" gorm:"foreignkey:LineItemID"`

	NetTotal uint64 `json:"net_total"`
	Taxes    uint64 `json:"taxes"`
	Total    int64  `json:"total"`
}

CalculationDetail holds details about pricing for line items

type Coupon added in v1.1.0

type Coupon struct {
	Code string `json:"code"`

	StartDate *time.Time `json:"start_date,omitempty"`
	EndDate   *time.Time `json:"end_date,omitempty"`

	Percentage  uint64         `json:"percentage,omitempty"`
	FixedAmount []*FixedAmount `json:"fixed,omitempty"`

	ProductTypes []string               `json:"product_types,omitempty"`
	Products     []string               `json:"products,omitempty"`
	Claims       map[string]interface{} `json:"claims,omitempty"`
}

Coupon represents a discount redeemable with a code.

func (*Coupon) FixedDiscount added in v1.1.0

func (c *Coupon) FixedDiscount(currency string) uint64

FixedDiscount returns the amount of fixed discount for a Coupon.

func (*Coupon) PercentageDiscount added in v1.1.0

func (c *Coupon) PercentageDiscount() uint64

PercentageDiscount returns the percentage discount of a Coupon.

func (*Coupon) Valid added in v1.1.0

func (c *Coupon) Valid() bool

Valid returns whether a coupon is valid or not.

func (*Coupon) ValidForPrice added in v1.1.0

func (c *Coupon) ValidForPrice(currency string, price uint64) bool

ValidForPrice returns whether a coupon applies to a specific amount.

func (*Coupon) ValidForProduct added in v1.2.0

func (c *Coupon) ValidForProduct(productSku string) bool

ValidForProduct returns whether a coupon applies to a specific product.

func (*Coupon) ValidForType added in v1.1.0

func (c *Coupon) ValidForType(productType string) bool

ValidForType returns whether a coupon applies to a specific product type.

type DBLogger added in v1.7.2

type DBLogger struct {
	logrus.FieldLogger
}

func NewDBLogger added in v1.7.2

func NewDBLogger(log logrus.FieldLogger) *DBLogger

func (*DBLogger) Print added in v1.7.2

func (dbl *DBLogger) Print(params ...interface{})

type DiscountItem added in v1.6.0

type DiscountItem struct {
	ID         int64 `json:"-"`
	LineItemID int64 `json:"-"`

	calculator.DiscountItem `gorm:"embedded"`
}

DiscountItem provides details about a discount that was applied

func (DiscountItem) TableName added in v1.6.0

func (DiscountItem) TableName() string

TableName returns the database table name for the DiscountItem model.

type Download added in v1.1.0

type Download struct {
	ID string `json:"id"`

	OrderID    string `json:"order_id"`
	LineItemID int64  `json:"line_item_id"`

	Title  string `json:"title"`
	Sku    string `json:"sku"`
	Format string `json:"format"`
	URL    string `json:"url"`

	DownloadCount uint64 `json:"downloads"`

	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt time.Time  `json:"updated_at"`
	DeletedAt *time.Time `json:"-" sql:"index"`
}

Download represents a purchased asset download.

func (*Download) SignURL added in v1.1.0

func (d *Download) SignURL(store assetstores.Store) error

SignURL signs a download URL using the provided asset store.

func (Download) TableName added in v1.1.0

func (Download) TableName() string

TableName returns the database table name for the Download model.

type Event added in v1.1.0

type Event struct {
	ID uint64 `json:"id"`

	IP string `json:"ip"`

	User   *User  `json:"user,omitempty"`
	UserID string `json:"user_id,omitempty"`

	Order   *Order `json:"order,omitempty"`
	OrderID string `json:"order_id,omitempty"`

	Type    string `json:"type"`
	Changes string `json:"data"`

	CreatedAt time.Time `json:"created_at"`
}

Event represents a change to an order.

func (Event) TableName added in v1.1.0

func (Event) TableName() string

TableName returns the database table name for the Event model.

type EventType added in v1.1.0

type EventType string

EventType is the type of change that occurred.

const (
	// EventCreated is the EventType when an order is created.
	EventCreated EventType = "created"
	// EventUpdated is the EventType when an order is updated.
	EventUpdated EventType = "updated"
	// EventDeleted is the EventType when an order is deleted.
	EventDeleted EventType = "deleted"
)

type FixedAmount added in v1.1.0

type FixedAmount struct {
	Amount   string `json:"amount"`
	Currency string `json:"currency"`
}

FixedAmount represents an amount and currency pair

type HackyNullTime added in v1.5.0

type HackyNullTime struct {
	Time  time.Time
	Valid bool
}

@todo: replace with mysql.NullTime once the tests no longer use SQLite

func (*HackyNullTime) MarshalJSON added in v1.5.0

func (t *HackyNullTime) MarshalJSON() ([]byte, error)

func (*HackyNullTime) Scan added in v1.5.0

func (t *HackyNullTime) Scan(value interface{}) (err error)

func (*HackyNullTime) UnmarshalJSON added in v1.5.0

func (t *HackyNullTime) UnmarshalJSON(data []byte) error

type Hook added in v1.1.0

type Hook struct {
	ID uint64

	UserID string

	Type string

	Done   bool
	Failed bool

	URL     string
	Payload string `sql:"type:text"`
	Secret  string

	ResponseStatus  string
	ResponseHeaders string  `sql:"type:text"`
	ResponseBody    string  `sql:"type:text"`
	ErrorMessage    *string `sql:"type:text"`

	Tries int

	CreatedAt   time.Time
	RunAfter    *time.Time
	LockedAt    *time.Time
	LockedBy    *string
	CompletedAt *time.Time
}

Hook represents a webhook.

func NewHook added in v1.1.0

func NewHook(hookType, siteURL, hookURL, userID, secret string, payload interface{}) (*Hook, error)

NewHook creates a Hook model.

func (Hook) TableName added in v1.1.0

func (Hook) TableName() string

TableName returns the database table name for the Hook model.

func (*Hook) Trigger added in v1.1.0

func (h *Hook) Trigger(client *http.Client, log *logrus.Entry) (*http.Response, error)

Trigger creates and executes the HTTP request for a Hook.

type Instance added in v1.1.2

type Instance struct {
	ID string `json:"id"`
	// Netlify UUID
	UUID string `json:"uuid,omitempty"`

	RawBaseConfig string              `json:"-" sql:"type:text"`
	BaseConfig    *conf.Configuration `json:"config"`

	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt time.Time  `json:"updated_at"`
	DeletedAt *time.Time `json:"deleted_at"`
}

func GetInstance added in v1.1.2

func GetInstance(db *gorm.DB, instanceID string) (*Instance, error)

GetInstance finds an instance by ID

func GetInstanceByUUID added in v1.1.2

func GetInstanceByUUID(db *gorm.DB, uuid string) (*Instance, error)

func (*Instance) AfterFind added in v1.1.2

func (i *Instance) AfterFind() error

AfterFind database callback.

func (*Instance) BeforeDelete added in v1.2.1

func (i *Instance) BeforeDelete(tx *gorm.DB) error

func (*Instance) BeforeSave added in v1.1.2

func (i *Instance) BeforeSave() error

BeforeSave database callback.

func (*Instance) Config added in v1.1.2

func (i *Instance) Config() (*conf.Configuration, error)

Config loads the configuration and applies defaults.

func (*Instance) TableName added in v1.1.2

func (i *Instance) TableName() string

TableName returns the table name used for the Instance model

type InvoiceNumber added in v1.1.2

type InvoiceNumber struct {
	InstanceID string `gorm:"primary_key"`
	Number     int64
}

func (InvoiceNumber) TableName added in v1.1.2

func (InvoiceNumber) TableName() string

TableName returns the database table name for the LineItem model.

type LineItem

type LineItem struct {
	ID      int64  `json:"id"`
	OrderID string `json:"-"`

	Title       string `json:"title"`
	Sku         string `json:"sku"`
	Type        string `json:"type"`
	Description string `json:"description" sql:"type:text"`

	Path string `json:"path"`

	Price uint64 `json:"price"`
	VAT   uint64 `json:"vat"`

	*CalculationDetail `json:"calculation" gorm:"embedded;embedded_prefix:calculation_"`

	PriceItems []*PriceItem `json:"price_items"`
	AddonItems []*AddonItem `json:"addons"`
	AddonPrice uint64       `json:"addon_price"`

	Quantity uint64 `json:"quantity"`

	MetaData    map[string]interface{} `sql:"-" json:"meta"`
	RawMetaData string                 `json:"-" sql:"type:text"`

	CreatedAt time.Time  `json:"-"`
	DeletedAt *time.Time `json:"-"`
}

LineItem is a single item in an Order.

func (*LineItem) AfterFind added in v1.1.0

func (i *LineItem) AfterFind() error

AfterFind database callback.

func (*LineItem) BeforeDelete added in v1.2.1

func (i *LineItem) BeforeDelete(tx *gorm.DB) error

func (*LineItem) BeforeSave added in v1.1.2

func (i *LineItem) BeforeSave() error

BeforeSave database callback.

func (*LineItem) FetchMeta added in v1.7.2

func (i *LineItem) FetchMeta(siteURL string) (*LineItemMetadata, error)

FetchMeta determines the product metadata for the item based on its path

func (*LineItem) FixedVAT added in v1.1.0

func (i *LineItem) FixedVAT() uint64

FixedVAT implements part of the calculator.Item interface.

func (*LineItem) GetQuantity added in v1.1.0

func (i *LineItem) GetQuantity() uint64

GetQuantity implements part of the calculator.Item interface.

func (*LineItem) MissingDownloads added in v1.7.2

func (i *LineItem) MissingDownloads(order *Order, meta *LineItemMetadata) []Download

MissingDownloads returns all downloads that are not yet listed in the order

func (*LineItem) PriceInLowestUnit added in v1.1.0

func (i *LineItem) PriceInLowestUnit() uint64

PriceInLowestUnit implements part of the calculator.Item interface.

func (*LineItem) Process

func (i *LineItem) Process(config *conf.Configuration, userClaims map[string]interface{}, order *Order) error

Process calculates the price of a LineItem.

func (*LineItem) ProductSku added in v1.2.0

func (i *LineItem) ProductSku() string

ProductSku returns the Sku of the line item to match the calculator.Item interface

func (*LineItem) ProductType added in v1.1.0

func (i *LineItem) ProductType() string

ProductType implements part of the calculator.Item interface.

func (LineItem) TableName added in v0.2.0

func (LineItem) TableName() string

TableName returns the database table name for the LineItem model.

func (*LineItem) TaxableItems added in v1.1.0

func (i *LineItem) TaxableItems() []calculator.Item

TaxableItems implements part of the calculator.Item interface.

type LineItemMetadata

type LineItemMetadata struct {
	Sku         string          `json:"sku"`
	Title       string          `json:"title"`
	Description string          `json:"description"`
	VAT         uint64          `json:"vat"`
	Prices      []PriceMetadata `json:"prices"`
	Type        string          `json:"type"`

	Downloads []Download      `json:"downloads"`
	Addons    []AddonMetaItem `json:"addons"`

	Webhook string `json:"webhook"`
}

LineItemMetadata model

type ModelNotFoundError added in v1.1.2

type ModelNotFoundError struct {
	// contains filtered or unexported fields
}

ModelNotFoundError represents when an instance is not found.

func (ModelNotFoundError) Error added in v1.1.2

func (e ModelNotFoundError) Error() string

type Order

type Order struct {
	InstanceID    string `json:"-" sql:"index"`
	ID            string `json:"id"`
	InvoiceNumber int64  `json:"invoice_number,omitempty"`

	IP string `json:"ip"`

	User      *User  `json:"user,omitempty"`
	UserID    string `json:"user_id,omitempty"`
	SessionID string `json:"-"`

	Email string `json:"email"`

	LineItems []*LineItem `json:"line_items"`

	Downloads []Download `json:"downloads"`

	Currency string `json:"currency"`
	Taxes    uint64 `json:"taxes"`
	Shipping uint64 `json:"shipping"`
	SubTotal uint64 `json:"subtotal"`
	Discount uint64 `json:"discount"`
	NetTotal uint64 `json:"net_total"`

	Total uint64 `json:"total"`

	PaymentState     string `json:"payment_state"`
	FulfillmentState string `json:"fulfillment_state"`
	State            string `json:"state"`

	PaymentProcessor string `json:"payment_processor"`

	Transactions []*Transaction `json:"transactions"`
	Notes        []*OrderNote   `json:"notes"`

	ShippingAddress   Address `json:"shipping_address" gorm:"ForeignKey:ShippingAddressID"`
	ShippingAddressID string  `json:"shipping_address_id"`

	BillingAddress   Address `json:"billing_address" gorm:"ForeignKey:BillingAddressID"`
	BillingAddressID string  `json:"billing_address_id"`

	VATNumber string `json:"vatnumber"`

	MetaData    map[string]interface{} `sql:"-" json:"meta"`
	RawMetaData string                 `json:"-" sql:"type:text"`

	CouponCode string `json:"coupon_code,omitempty"`

	Coupon    *Coupon `json:"coupon,omitempty" sql:"-"`
	RawCoupon string  `json:"-" sql:"type:text"`

	CreatedAt time.Time  `json:"created_at" sql:"index"`
	UpdatedAt time.Time  `json:"updated_at"`
	DeletedAt *time.Time `json:"-" sql:"index"`
}

Order model

func NewOrder

func NewOrder(instanceID, sessionID, email, currency string) *Order

NewOrder creates a new pending Order.

func (*Order) AfterFind added in v1.1.0

func (o *Order) AfterFind() error

AfterFind database callback.

func (*Order) BeforeDelete added in v1.2.1

func (o *Order) BeforeDelete(tx *gorm.DB) error

func (*Order) BeforeSave added in v1.1.2

func (o *Order) BeforeSave() error

BeforeSave database callback.

func (*Order) CalculateTotal

func (o *Order) CalculateTotal(settings *calculator.Settings, claims map[string]interface{}, log logrus.FieldLogger)

CalculateTotal calculates the total price of an Order.

func (Order) TableName added in v0.2.0

func (Order) TableName() string

TableName returns the database table name for the Order model.

func (*Order) UpdateDownloads added in v1.7.2

func (o *Order) UpdateDownloads(config *conf.Configuration, log logrus.FieldLogger) error

UpdateDownloads will refetch downloads for all line items in the order and update the downloads in the order

type OrderNote

type OrderNote struct {
	ID int64 `json:"-"`

	UserID string `json:"user_id"`

	Text string `json:"text" sql:"type:text"`

	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt time.Time  `json:"updated_at"`
	DeletedAt *time.Time `json:"-"`
}

OrderNote model which represent notes on a model.

func (OrderNote) TableName added in v0.2.0

func (OrderNote) TableName() string

TableName returns the database table name for the OrderNote model.

type PriceItem added in v1.0.0

type PriceItem struct {
	ID int64 `json:"id"`

	Amount uint64 `json:"amount"`
	Type   string `json:"type"`
	VAT    uint64 `json:"vat"`
}

PriceItem represent the subcomponent price items of a LineItem.

func (*PriceItem) FixedVAT added in v1.1.0

func (i *PriceItem) FixedVAT() uint64

FixedVAT implements part of the calculator.Item interface.

func (*PriceItem) GetQuantity added in v1.1.0

func (i *PriceItem) GetQuantity() uint64

GetQuantity implements part of the calculator.Item interface.

func (*PriceItem) PriceInLowestUnit added in v1.1.0

func (i *PriceItem) PriceInLowestUnit() uint64

PriceInLowestUnit implements part of the calculator.Item interface.

func (*PriceItem) ProductSku added in v1.2.0

func (i *PriceItem) ProductSku() string

ProductSku returns the Sku of the line item to match the calculator.Item interface

func (*PriceItem) ProductType added in v1.1.0

func (i *PriceItem) ProductType() string

ProductType implements part of the calculator.Item interface.

func (PriceItem) TableName added in v1.1.0

func (PriceItem) TableName() string

TableName returns the database table name for the PriceItem model.

func (*PriceItem) TaxableItems added in v1.1.0

func (i *PriceItem) TaxableItems() []calculator.Item

TaxableItems implements part of the calculator.Item interface.

type PriceMetaItem added in v1.0.0

type PriceMetaItem struct {
	Amount string `json:"amount"`
	Type   string `json:"type"`
	VAT    uint64 `json:"vat"`
}

PriceMetaItem model

type PriceMetadata

type PriceMetadata struct {
	Amount   string            `json:"amount"`
	Currency string            `json:"currency"`
	VAT      string            `json:"vat"`
	Items    []PriceMetaItem   `json:"items"`
	Claims   map[string]string `json:"claims"`
	// contains filtered or unexported fields
}

PriceMetadata model

type Transaction

type Transaction struct {
	InstanceID    string `json:"-"`
	ID            string `json:"id"`
	Order         *Order `json:"-"`
	OrderID       string `json:"order_id"`
	InvoiceNumber int64  `json:"invoice_number"`

	ProcessorID string `json:"processor_id"`

	User   *User  `json:"-"`
	UserID string `json:"user_id,omitempty"`

	Amount   uint64 `json:"amount"`
	Currency string `json:"currency"`

	FailureCode        string `json:"failure_code,omitempty"`
	FailureDescription string `json:"failure_description,omitempty" sql:"type:text"`

	Status string `json:"status"`
	Type   string `json:"type"`

	CreatedAt time.Time  `json:"created_at"`
	DeletedAt *time.Time `json:"-"`

	ProviderMetadata map[string]interface{} `json:"provider_metadata,omitempty" sql:"-"`
}

Transaction is an transaction with a payment provider

func GetTransaction added in v1.1.2

func GetTransaction(db *gorm.DB, id string) (*Transaction, error)

func NewTransaction

func NewTransaction(order *Order) *Transaction

NewTransaction returns a new transaction for an order

func (Transaction) TableName added in v0.2.0

func (Transaction) TableName() string

TableName returns the database table name for the Transaction model.

type User

type User struct {
	InstanceID string `json:"-"`
	ID         string `json:"id"`
	Email      string `json:"email"`
	Name       string `json:"name"`

	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt time.Time  `json:"updated_at"`
	DeletedAt *time.Time `json:"-"`

	OrderCount  int64          `json:"order_count" gorm:"-"`
	LastOrderAt *HackyNullTime `json:"last_order_at" gorm:"-"`
}

User model

func GetUser added in v1.1.2

func GetUser(db *gorm.DB, userID string) (*User, error)

func (*User) BeforeDelete added in v1.2.1

func (u *User) BeforeDelete(tx *gorm.DB) error

func (User) TableName added in v0.2.0

func (User) TableName() string

TableName returns the database table name for the User model.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL