storj/satellite/satellitedb/consoledb.go
Yingrong Zhao efa2c776b7
satellite/satellitedb: add updateEarnedCredits method for user_credits table (#2609)
* parent 13dd501042
author Yingrong Zhao <yingrong.zhao@gmail.com> 1563560530 -0400
committer Yingrong Zhao <yingrong.zhao@gmail.com> 1563581673 -0400

parent 13dd501042
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 09:21:00 -04:00

127 lines
3.2 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"github.com/zeebo/errs"
"storj.io/storj/satellite/accounting"
"storj.io/storj/satellite/console"
dbx "storj.io/storj/satellite/satellitedb/dbx"
)
// ConsoleDB contains access to different satellite databases
type ConsoleDB struct {
db *dbx.DB
tx *dbx.Tx
methods dbx.Methods
}
// Users is getter a for Users repository
func (db *ConsoleDB) Users() console.Users {
return &users{db.methods}
}
// Projects is a getter for Projects repository
func (db *ConsoleDB) Projects() console.Projects {
return &projects{db.methods}
}
// ProjectMembers is a getter for ProjectMembers repository
func (db *ConsoleDB) ProjectMembers() console.ProjectMembers {
return &projectMembers{db.methods, db.db}
}
// APIKeys is a getter for APIKeys repository
func (db *ConsoleDB) APIKeys() console.APIKeys {
return &apikeys{db.methods}
}
// BucketUsage is a getter for accounting.BucketUsage repository
func (db *ConsoleDB) BucketUsage() accounting.BucketUsage {
return &bucketusage{db.methods}
}
// RegistrationTokens is a getter for RegistrationTokens repository
func (db *ConsoleDB) RegistrationTokens() console.RegistrationTokens {
return &registrationTokens{db.methods}
}
// ResetPasswordTokens is a getter for ResetPasswordTokens repository
func (db *ConsoleDB) ResetPasswordTokens() console.ResetPasswordTokens {
return &resetPasswordTokens{db.methods}
}
// UsageRollups is a getter for console.UsageRollups repository
func (db *ConsoleDB) UsageRollups() console.UsageRollups {
return &usagerollups{db.db}
}
// UserCredits is a getter for console.UserCredits repository
func (db *ConsoleDB) UserCredits() console.UserCredits {
return &usercredits{db.db, db.tx}
}
// UserPayments is a getter for console.UserPayments repository
func (db *ConsoleDB) UserPayments() console.UserPayments {
return &userpayments{db.methods}
}
// ProjectPayments is a getter for console.ProjectPayments repository
func (db *ConsoleDB) ProjectPayments() console.ProjectPayments {
return &projectPayments{db.db, db.methods}
}
// ProjectInvoiceStamps is a getter for console.ProjectInvoiceStamps repository
func (db *ConsoleDB) ProjectInvoiceStamps() console.ProjectInvoiceStamps {
return &projectinvoicestamps{db.methods}
}
// BeginTx is a method for opening transaction
func (db *ConsoleDB) BeginTx(ctx context.Context) (console.DBTx, error) {
if db.db == nil {
return nil, errs.New("DB is not initialized!")
}
tx, err := db.db.Open(ctx)
if err != nil {
return nil, err
}
return &DBTx{
ConsoleDB: &ConsoleDB{
// Need to expose dbx.DB for when database methods need access to check database driver type
db: db.db,
tx: tx,
methods: tx,
},
}, nil
}
// DBTx extends Database with transaction scope
type DBTx struct {
*ConsoleDB
}
// Commit is a method for committing and closing transaction
func (db *DBTx) Commit() error {
if db.tx == nil {
return errs.New("begin transaction before commit it!")
}
return db.tx.Commit()
}
// Rollback is a method for rollback and closing transaction
func (db *DBTx) Rollback() error {
if db.tx == nil {
return errs.New("begin transaction before rollback it!")
}
return db.tx.Rollback()
}