satellite/console: add get current reward offer to console (#2341)

* expose reward offer to satellite gui

* named return
This commit is contained in:
Yingrong Zhao 2019-07-02 10:36:54 -04:00 committed by Faris Huskovic
parent 9e8ecb6303
commit f56cb1c612
7 changed files with 103 additions and 4 deletions

View File

@ -52,6 +52,7 @@ func TestGrapqhlMutation(t *testing.T) {
log,
&consoleauth.Hmac{Secret: []byte("my-suppa-secret-key")},
db.Console(),
db.Rewards(),
localpayments.NewService(nil),
console.TestPasswordCost,
)

View File

@ -13,6 +13,7 @@ import (
"storj.io/storj/internal/post"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/mailservice"
"storj.io/storj/satellite/rewards"
)
const (
@ -24,6 +25,8 @@ const (
ProjectQuery = "project"
// MyProjectsQuery is a query name for projects related to account
MyProjectsQuery = "myProjects"
// ActiveRewardQuery is a query name for current active reward offer
ActiveRewardQuery = "activeReward"
// CreditUsageQuery is a query name for credit usage related to an user
CreditUsageQuery = "creditUsage"
// TokenQuery is a query name for token
@ -83,6 +86,19 @@ func rootQuery(service *console.Service, mailService *mailservice.Service, types
return service.GetUsersProjects(p.Context)
},
},
ActiveRewardQuery: &graphql.Field{
Type: types.reward,
Args: graphql.FieldConfigArgument{
FieldType: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.Int),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
rewardType, _ := p.Args[FieldType].(int)
return service.GetCurrentRewardByType(p.Context, rewards.OfferType(rewardType))
},
},
CreditUsageQuery: &graphql.Field{
Type: types.creditUsage,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {

View File

@ -35,6 +35,7 @@ func TestGraphqlQuery(t *testing.T) {
log,
&consoleauth.Hmac{Secret: []byte("my-suppa-secret-key")},
db.Console(),
db.Rewards(),
localpayments.NewService(nil),
console.TestPasswordCost,
)

View File

@ -0,0 +1,59 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package consoleql
import (
"github.com/graphql-go/graphql"
)
const (
// RewardType is a graphql type for reward
RewardType = "reward"
// FieldAwardCreditInCent is a field name for award credit amount for referrers
FieldAwardCreditInCent = "awardCreditInCent"
// FieldInviteeCreditInCents is a field name for credit amount rewarded to invitees
FieldInviteeCreditInCents = "referred"
// FieldRedeemableCap is a field name for the total redeemable amount of the reward offer
FieldRedeemableCap = "redeemableCap"
// FieldAwardCreditDurationDays is a field name for the valid time frame of current award credit
FieldAwardCreditDurationDays = "awardCreditDurationDays"
// FieldInviteeCreditDurationDays is a field name for the valid time frame of current invitee credit
FieldInviteeCreditDurationDays = "inviteeCreditDurationDays"
// FieldExpiresAt is a field name for the expiration time of a reward offer
FieldExpiresAt = "expiresAt"
// FieldType is a field name for the type of reward offers
FieldType = "type"
)
func graphqlReward() *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: RewardType,
Fields: graphql.Fields{
FieldName: &graphql.Field{
Type: graphql.String,
},
FieldDescription: &graphql.Field{
Type: graphql.String,
},
FieldAwardCreditInCent: &graphql.Field{
Type: graphql.Int,
},
FieldInviteeCreditInCents: &graphql.Field{
Type: graphql.Int,
},
FieldRedeemableCap: &graphql.Field{
Type: graphql.Int,
},
FieldAwardCreditDurationDays: &graphql.Field{
Type: graphql.Int,
},
FieldInviteeCreditDurationDays: &graphql.Field{
Type: graphql.Int,
},
FieldExpiresAt: &graphql.Field{
Type: graphql.DateTime,
},
},
})
}

View File

@ -19,6 +19,7 @@ type TypeCreator struct {
token *graphql.Object
user *graphql.Object
reward *graphql.Object
creditUsage *graphql.Object
project *graphql.Object
projectUsage *graphql.Object
@ -57,6 +58,11 @@ func (c *TypeCreator) Create(log *zap.Logger, service *console.Service, mailServ
return err
}
c.reward = graphqlReward()
if err := c.reward.Error(); err != nil {
return err
}
c.creditUsage = graphqlCreditUsage()
if err := c.creditUsage.Error(); err != nil {
return err

View File

@ -19,6 +19,7 @@ import (
"storj.io/storj/pkg/macaroon"
"storj.io/storj/satellite/console/consoleauth"
"storj.io/storj/satellite/payments"
"storj.io/storj/satellite/rewards"
)
var mon = monkit.Package()
@ -57,15 +58,16 @@ const (
type Service struct {
Signer
log *zap.Logger
pm payments.Service
store DB
log *zap.Logger
pm payments.Service
store DB
rewards rewards.DB
passwordCost int
}
// NewService returns new instance of Service
func NewService(log *zap.Logger, signer Signer, store DB, pm payments.Service, passwordCost int) (*Service, error) {
func NewService(log *zap.Logger, signer Signer, store DB, rewards rewards.DB, pm payments.Service, passwordCost int) (*Service, error) {
if signer == nil {
return nil, errs.New("signer can't be nil")
}
@ -83,6 +85,7 @@ func NewService(log *zap.Logger, signer Signer, store DB, pm payments.Service, p
log: log,
Signer: signer,
store: store,
rewards: rewards,
pm: pm,
passwordCost: passwordCost,
}, nil
@ -455,6 +458,18 @@ func (s *Service) GetUsersProjects(ctx context.Context) (ps []Project, err error
return
}
// GetCurrentRewardByType is a method for querying current active reward offer based on its type
func (s *Service) GetCurrentRewardByType(ctx context.Context, offerType rewards.OfferType) (reward *rewards.Offer, err error) {
defer mon.Task()(&ctx)(&err)
reward, err = s.rewards.GetCurrentByType(ctx, offerType)
if err != nil {
return nil, errs.New(internalErrMsg)
}
return reward, nil
}
// GetUserCreditUsage is a method for querying users' credit information up until now
func (s *Service) GetUserCreditUsage(ctx context.Context) (usage *UserCreditUsage, err error) {
defer mon.Task()(&ctx)(&err)

View File

@ -573,6 +573,7 @@ func New(log *zap.Logger, full *identity.FullIdentity, db DB, config *Config, ve
peer.Log.Named("console:service"),
&consoleauth.Hmac{Secret: []byte(consoleConfig.AuthTokenSecret)},
peer.DB.Console(),
peer.DB.Rewards(),
pmService,
consoleConfig.PasswordCost,
)