storj/satellite/rewards/rewards.go

154 lines
3.6 KiB
Go
Raw Normal View History

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package rewards
import (
"context"
"time"
satellite/marketing: Create New Offer (#2186) * update UI to reflect final mockups * implement create handler and render offers table data to UI * fix line-height unit and remove important from selectors * update file names and ids for clarity * shorten 'label' in ids * localize global vars, fix endpoint names, remove unnecessary receiver, fix comments * fix unnecessary initialization of pointer * correct file-naming conventions * register timeConverter in an init func for safety and remove unnecessary important from css * consolidate create endpoints and add comments * register timeConverter in init func * add copyright to files * introduce require pkg * add proper http server unit test * update linting and create offers concurrently in unit test * fix getOffers comment * add copy-right to unit-test * fix data-races * fix linting * remove converter in NewServer * requested changes in progress * add require for checking status code * renamed template file * fix 400 handler * fix missing copyright and remove extra line * fix build * run goroutine for testing parallel * evaluate reqType with switch stmt and promp for credit amount in cents * fix lint issue * add default case * remove unnecessary var * fix range scope error * remove empty lines and use long form for struct field * fix merge conflicts * fix template reference * fix modal id * not delete package * add currency formatting and requested changes * markup formatting * lean out currency logic and move wait outside loop * pass ToDollars func to home template * fix lint
2019-06-28 15:34:10 +01:00
satellite/satellitedb: add updateEarnedCredits method for user_credits table (#2609) * parent 13dd501042d0fa6eb0142b6f737704985c17f5bc author Yingrong Zhao <yingrong.zhao@gmail.com> 1563560530 -0400 committer Yingrong Zhao <yingrong.zhao@gmail.com> 1563581673 -0400 parent 13dd501042d0fa6eb0142b6f737704985c17f5bc author Yingrong Zhao <yingrong.zhao@gmail.com> 1563560530 -0400 committer Yingrong Zhao <yingrong.zhao@gmail.com> 1563581428 -0400 satellite/console: add referral link logic (#2576) * setup referral route * referredBy * add user id * modify user query * separate optional field from userInfo * get current reward on init of satellite gui * remove unsed code * fix format * only apply 0 credit on registration * only pass required information for rewards * fix time parsing * fix test and linter * rename method * add todo * remove user referral logic * add null check and fix format * get current offer * remove partnerID on CreateUser struct * fix storj-sim user creation * only redeem credit when there's an offer * fix default offer configuration * fix migration * Add helper function for get correct credit duration * add comment * only store userid into user_credit table * add check for partner id to set correct offer type * change free credit to use invitee credits * remove unecessary code * add credit update in activateAccount * remove unused code * fix format * close reader and fix front-end build * move create credit logic into CreateUser method * when there's no offer set, user flow shouldn't be interrupted by referral program * add appropriate error messages * remove unused code * add comment * add error class for no current offer error * add error class for credits update * add comment for migration * only log secret when it's in debug level * fix typo * add testdata
2019-07-30 14:21:00 +01:00
"github.com/zeebo/errs"
"storj.io/storj/internal/currency"
)
satellite/marketing: Create New Offer (#2186) * update UI to reflect final mockups * implement create handler and render offers table data to UI * fix line-height unit and remove important from selectors * update file names and ids for clarity * shorten 'label' in ids * localize global vars, fix endpoint names, remove unnecessary receiver, fix comments * fix unnecessary initialization of pointer * correct file-naming conventions * register timeConverter in an init func for safety and remove unnecessary important from css * consolidate create endpoints and add comments * register timeConverter in init func * add copyright to files * introduce require pkg * add proper http server unit test * update linting and create offers concurrently in unit test * fix getOffers comment * add copy-right to unit-test * fix data-races * fix linting * remove converter in NewServer * requested changes in progress * add require for checking status code * renamed template file * fix 400 handler * fix missing copyright and remove extra line * fix build * run goroutine for testing parallel * evaluate reqType with switch stmt and promp for credit amount in cents * fix lint issue * add default case * remove unnecessary var * fix range scope error * remove empty lines and use long form for struct field * fix merge conflicts * fix template reference * fix modal id * not delete package * add currency formatting and requested changes * markup formatting * lean out currency logic and move wait outside loop * pass ToDollars func to home template * fix lint
2019-06-28 15:34:10 +01:00
satellite/satellitedb: add updateEarnedCredits method for user_credits table (#2609) * parent 13dd501042d0fa6eb0142b6f737704985c17f5bc author Yingrong Zhao <yingrong.zhao@gmail.com> 1563560530 -0400 committer Yingrong Zhao <yingrong.zhao@gmail.com> 1563581673 -0400 parent 13dd501042d0fa6eb0142b6f737704985c17f5bc author Yingrong Zhao <yingrong.zhao@gmail.com> 1563560530 -0400 committer Yingrong Zhao <yingrong.zhao@gmail.com> 1563581428 -0400 satellite/console: add referral link logic (#2576) * setup referral route * referredBy * add user id * modify user query * separate optional field from userInfo * get current reward on init of satellite gui * remove unsed code * fix format * only apply 0 credit on registration * only pass required information for rewards * fix time parsing * fix test and linter * rename method * add todo * remove user referral logic * add null check and fix format * get current offer * remove partnerID on CreateUser struct * fix storj-sim user creation * only redeem credit when there's an offer * fix default offer configuration * fix migration * Add helper function for get correct credit duration * add comment * only store userid into user_credit table * add check for partner id to set correct offer type * change free credit to use invitee credits * remove unecessary code * add credit update in activateAccount * remove unused code * fix format * close reader and fix front-end build * move create credit logic into CreateUser method * when there's no offer set, user flow shouldn't be interrupted by referral program * add appropriate error messages * remove unused code * add comment * add error class for no current offer error * add error class for credits update * add comment for migration * only log secret when it's in debug level * fix typo * add testdata
2019-07-30 14:21:00 +01:00
var (
// MaxRedemptionErr is the error class used when an offer has reached its redemption capacity
MaxRedemptionErr = errs.Class("offer redemption has reached its capacity")
// NoCurrentOfferErr is the error class used when no current offer is set
NoCurrentOfferErr = errs.Class("no current offer")
)
// DB holds information about offer
type DB interface {
ListAll(ctx context.Context) (Offers, error)
GetActiveOffersByType(ctx context.Context, offerType OfferType) (Offers, error)
Create(ctx context.Context, offer *NewOffer) (*Offer, error)
Finish(ctx context.Context, offerID int) error
}
// NewOffer holds information that's needed for creating a new offer
type NewOffer struct {
Name string
Description string
AwardCredit currency.USD
InviteeCredit currency.USD
RedeemableCap int
AwardCreditDurationDays int
InviteeCreditDurationDays int
ExpiresAt time.Time
Status OfferStatus
Type OfferType
}
// UpdateOffer holds fields needed for update an offer
type UpdateOffer struct {
ID int
Status OfferStatus
ExpiresAt time.Time
}
// RedeemOffer holds field needed for redeem an offer
type RedeemOffer struct {
RedeemableCap int
Status OfferStatus
Type OfferType
}
// Offers contains a slice of offers.
type Offers []Offer
// OfferType indicates the type of an offer
type OfferType int
const (
// Invalid is a default value for offers that don't have correct type associated with it
Invalid = OfferType(0)
// FreeCredit is a type of offers used for Free Credit Program
FreeCredit = OfferType(1)
// Referral is a type of offers used for Referral Program
Referral = OfferType(2)
// Partner is an OfferType used be the Open Source Partner Program
Partner = OfferType(3)
)
// OfferStatus represents the different stage an offer can have in its life-cycle.
type OfferStatus int
const (
// Done is the status of an offer that is no longer in use.
Done = OfferStatus(iota)
// Default is the status of an offer when there is no active offer.
Default
// Active is the status of an offer that is currently in use.
Active
)
// Offer contains info needed for giving users free credits through different offer programs
type Offer struct {
ID int
Name string
Description string
AwardCredit currency.USD
InviteeCredit currency.USD
AwardCreditDurationDays int
InviteeCreditDurationDays int
RedeemableCap int
ExpiresAt time.Time
CreatedAt time.Time
Status OfferStatus
Type OfferType
}
satellite/marketing: Create New Offer (#2186) * update UI to reflect final mockups * implement create handler and render offers table data to UI * fix line-height unit and remove important from selectors * update file names and ids for clarity * shorten 'label' in ids * localize global vars, fix endpoint names, remove unnecessary receiver, fix comments * fix unnecessary initialization of pointer * correct file-naming conventions * register timeConverter in an init func for safety and remove unnecessary important from css * consolidate create endpoints and add comments * register timeConverter in init func * add copyright to files * introduce require pkg * add proper http server unit test * update linting and create offers concurrently in unit test * fix getOffers comment * add copy-right to unit-test * fix data-races * fix linting * remove converter in NewServer * requested changes in progress * add require for checking status code * renamed template file * fix 400 handler * fix missing copyright and remove extra line * fix build * run goroutine for testing parallel * evaluate reqType with switch stmt and promp for credit amount in cents * fix lint issue * add default case * remove unnecessary var * fix range scope error * remove empty lines and use long form for struct field * fix merge conflicts * fix template reference * fix modal id * not delete package * add currency formatting and requested changes * markup formatting * lean out currency logic and move wait outside loop * pass ToDollars func to home template * fix lint
2019-06-28 15:34:10 +01:00
// IsEmpty evaluates whether or not an on offer is empty
func (o Offer) IsEmpty() bool {
return o.Name == ""
satellite/marketing: Create New Offer (#2186) * update UI to reflect final mockups * implement create handler and render offers table data to UI * fix line-height unit and remove important from selectors * update file names and ids for clarity * shorten 'label' in ids * localize global vars, fix endpoint names, remove unnecessary receiver, fix comments * fix unnecessary initialization of pointer * correct file-naming conventions * register timeConverter in an init func for safety and remove unnecessary important from css * consolidate create endpoints and add comments * register timeConverter in init func * add copyright to files * introduce require pkg * add proper http server unit test * update linting and create offers concurrently in unit test * fix getOffers comment * add copy-right to unit-test * fix data-races * fix linting * remove converter in NewServer * requested changes in progress * add require for checking status code * renamed template file * fix 400 handler * fix missing copyright and remove extra line * fix build * run goroutine for testing parallel * evaluate reqType with switch stmt and promp for credit amount in cents * fix lint issue * add default case * remove unnecessary var * fix range scope error * remove empty lines and use long form for struct field * fix merge conflicts * fix template reference * fix modal id * not delete package * add currency formatting and requested changes * markup formatting * lean out currency logic and move wait outside loop * pass ToDollars func to home template * fix lint
2019-06-28 15:34:10 +01:00
}
// GetActiveOffer returns an offer that is active based on its type
func (offers Offers) GetActiveOffer(offerType OfferType, partnerID string) (offer *Offer, err error) {
if len(offers) < 1 {
return nil, NoCurrentOfferErr.New("no active offers")
}
switch offerType {
case Partner:
if partnerID == "" {
return nil, errs.New("partner ID is empty")
}
partnerInfo, ok := LoadPartnerInfos()[partnerID]
if !ok {
return nil, NoMatchPartnerIDErr.New("no partnerInfo found")
}
for i := range offers {
if offers[i].Name == partnerInfo.Name {
offer = &offers[i]
}
}
default:
if len(offers) > 1 {
return nil, errs.New("multiple active offers found")
}
offer = &offers[0]
}
return offer, nil
}
// IsDefault checks if a offer's status is default
func (status OfferStatus) IsDefault() bool {
return status == Default
}