2019-06-04 20:17:01 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
package rewards
|
2019-06-04 20:17:01 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
2019-06-28 15:34:10 +01:00
|
|
|
|
2019-07-01 20:16:49 +01:00
|
|
|
"storj.io/storj/internal/currency"
|
|
|
|
)
|
2019-06-28 15:34:10 +01:00
|
|
|
|
2019-06-24 21:51:54 +01:00
|
|
|
// DB holds information about offer
|
|
|
|
type DB interface {
|
2019-06-04 20:17:01 +01:00
|
|
|
ListAll(ctx context.Context) ([]Offer, error)
|
|
|
|
GetCurrentByType(ctx context.Context, offerType OfferType) (*Offer, error)
|
|
|
|
Create(ctx context.Context, offer *NewOffer) (*Offer, error)
|
2019-06-24 21:51:54 +01:00
|
|
|
Redeem(ctx context.Context, offerID int, isDefault bool) error
|
2019-06-12 16:53:19 +01:00
|
|
|
Finish(ctx context.Context, offerID int) error
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOffer holds information that's needed for creating a new offer
|
|
|
|
type NewOffer struct {
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
|
2019-07-01 20:16:49 +01:00
|
|
|
AwardCredit currency.USD
|
|
|
|
InviteeCredit currency.USD
|
2019-06-04 20:17:01 +01:00
|
|
|
|
|
|
|
RedeemableCap int
|
|
|
|
|
|
|
|
AwardCreditDurationDays int
|
|
|
|
InviteeCreditDurationDays int
|
|
|
|
|
|
|
|
ExpiresAt time.Time
|
|
|
|
|
|
|
|
Status OfferStatus
|
|
|
|
Type OfferType
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateOffer holds fields needed for update an offer
|
|
|
|
type UpdateOffer struct {
|
2019-06-12 16:53:19 +01:00
|
|
|
ID int
|
|
|
|
Status OfferStatus
|
|
|
|
ExpiresAt time.Time
|
2019-06-04 20:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// OfferType indicates the type of an offer
|
|
|
|
type OfferType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// FreeCredit is a type of offers used for Free Credit Program
|
|
|
|
FreeCredit = OfferType(iota)
|
|
|
|
// Referral is a type of offers used for Referral Program
|
|
|
|
Referral
|
|
|
|
)
|
|
|
|
|
|
|
|
// OfferStatus indicates the status of an offer
|
|
|
|
type OfferStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Done is a default offer status when an offer is not being used currently
|
|
|
|
Done = OfferStatus(iota)
|
|
|
|
// Default is a offer status when an offer is used as a default offer
|
|
|
|
Default
|
|
|
|
// Active is a offer status when an offer is currently being used
|
|
|
|
Active
|
|
|
|
)
|
|
|
|
|
|
|
|
// Offer contains info needed for giving users free credits through different offer programs
|
|
|
|
type Offer struct {
|
|
|
|
ID int
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
|
2019-07-01 20:16:49 +01:00
|
|
|
AwardCredit currency.USD
|
|
|
|
InviteeCredit currency.USD
|
2019-06-04 20:17:01 +01:00
|
|
|
|
|
|
|
AwardCreditDurationDays int
|
|
|
|
InviteeCreditDurationDays int
|
|
|
|
|
|
|
|
RedeemableCap int
|
|
|
|
NumRedeemed int
|
|
|
|
|
|
|
|
ExpiresAt time.Time
|
|
|
|
CreatedAt time.Time
|
|
|
|
|
|
|
|
Status OfferStatus
|
|
|
|
Type OfferType
|
|
|
|
}
|
2019-06-28 15:34:10 +01:00
|
|
|
|
|
|
|
// IsDefault evaluates the default status of offers for templates.
|
|
|
|
func (o Offer) IsDefault() bool {
|
|
|
|
if o.Status == Default {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsCurrent evaluates the current status of offers for templates.
|
|
|
|
func (o Offer) IsCurrent() bool {
|
|
|
|
if o.Status == Active {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDone evaluates the done status of offers for templates.
|
|
|
|
func (o Offer) IsDone() bool {
|
|
|
|
if o.Status == Done {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Offers holds a set of organized offers.
|
|
|
|
type Offers struct {
|
|
|
|
Set []Offer
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentFromSet returns the current offer from an organized set.
|
|
|
|
func (offers Offers) GetCurrentFromSet() Offer {
|
|
|
|
var o Offer
|
|
|
|
for _, offer := range offers.Set {
|
|
|
|
if offer.IsCurrent() {
|
|
|
|
o = offer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefaultFromSet returns the current offer from an organized set.
|
|
|
|
func (offers Offers) GetDefaultFromSet() Offer {
|
|
|
|
var o Offer
|
|
|
|
for _, offer := range offers.Set {
|
|
|
|
if offer.IsDefault() {
|
|
|
|
o = offer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return o
|
|
|
|
}
|