storj/satellite/marketing/service.go
Yingrong Zhao 09b0c2a630
create db implementation for offer table (#2031)
* init marketing service

Fix linting error

Create offerdb implementation

Create offers service

Add update method

Create offer table and migration

Fix linting error

fix conflicts

Insert new data

Change duration to have clear indication to be based on days

add error wrapper

Change from using uuid to int for id field

* Create Marketing service

* make error virable name more readable

* add condition in update service method to check offer status

* generate lock file

Change get to listAllOffers

* Add method for getting current offer

wip

* add check for expires_at in update method

* Fix conflicts

* add copyright header

* Fix linting error

* only allow update to active offers

* add isDefault argument to GetCurrent

* Update lock file

* add migration file

* finish migrate for adding credit_in_cents for both award and invitee

* save 100 years as expiration date for default offers

* create crud test for offers

* add GetCurrent test

* modify doc

* Fix GetCurrent to work with default offer

* fix linting issue

* add more tests and address feedbacks

* fix migration file

* add type column back to match with mockup design

* add type column back to match with mockup design

* move doc changes to new pr

* add comments

* change GetCurrent to GetCurrentByType

* fix typo
2019-06-04 15:17:01 -04:00

95 lines
2.0 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package marketing
import (
"context"
"time"
"github.com/zeebo/errs"
"go.uber.org/zap"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
)
var (
// Error the default offers errs class
Error = errs.Class("marketing error")
mon = monkit.Package()
)
// Service allows access to offers info in the db
type Service struct {
log *zap.Logger
db DB
}
// NewService creates a new offers db
func NewService(log *zap.Logger, db DB) (*Service, error) {
if log == nil {
return nil, Error.New("log can't be nil")
}
return &Service{
log: log,
db: db,
}, nil
}
// ListAllOffers returns all available offers in the db
func (s *Service) ListAllOffers(ctx context.Context) (offers []Offer, err error) {
defer mon.Task()(&ctx)(&err)
offers, err = s.db.Offers().ListAll(ctx)
if err != nil {
return offers, Error.Wrap(err)
}
return offers, nil
}
// GetCurrentOfferByType returns current active offer
func (s *Service) GetCurrentOfferByType(ctx context.Context, offerType OfferType) (offer *Offer, err error) {
defer mon.Task()(&ctx)(&err)
offer, err = s.db.Offers().GetCurrentByType(ctx, offer.Type)
if err != nil {
return nil, Error.Wrap(err)
}
return offer, nil
}
// InsertNewOffer inserts a new offer into the db
func (s *Service) InsertNewOffer(ctx context.Context, offer *NewOffer) (o *Offer, err error) {
defer mon.Task()(&ctx)(&err)
if offer.Status == Default {
offer.ExpiresAt = time.Now().UTC().AddDate(100, 0, 0)
offer.RedeemableCap = 1
}
o, err = s.db.Offers().Create(ctx, offer)
if err != nil {
return nil, Error.Wrap(err)
}
return o, nil
}
// UpdateOffer modifies an existing offer in the db when the offer status is set to NoStatus
func (s *Service) UpdateOffer(ctx context.Context, offer *UpdateOffer) (err error) {
defer mon.Task()(&ctx)(&err)
if offer.Status == Default {
offer.NumRedeemed = 0
}
err = s.db.Offers().Update(ctx, offer)
if err != nil {
return Error.Wrap(err)
}
return nil
}