satellite/payments: dbx scheme renamed, userID placed on Account level (#3281)

This commit is contained in:
Yehor Butko 2019-10-15 21:05:45 +03:00 committed by GitHub
parent 4962c6843e
commit a5f4bbee22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 87 additions and 88 deletions

View File

@ -5,17 +5,15 @@ package payments
import ( import (
"context" "context"
"github.com/skyrings/skyring-common/tools/uuid"
) )
// Accounts exposes all needed functionality to manage payment accounts. // Accounts exposes all needed functionality to manage payment accounts.
type Accounts interface { type Accounts interface {
// Setup creates a payment account for the user. // Setup creates a payment account for the user.
Setup(ctx context.Context, userID uuid.UUID, email string) error Setup(ctx context.Context, email string) error
// Balance returns an integer amount in cents that represents the current balance of payment account. // Balance returns an integer amount in cents that represents the current balance of payment account.
Balance(ctx context.Context, userID uuid.UUID) (int64, error) Balance(ctx context.Context) (int64, error)
// CreditCards exposes all needed functionality to manage account credit cards. // CreditCards exposes all needed functionality to manage account credit cards.
CreditCards() CreditCards CreditCards() CreditCards

View File

@ -5,17 +5,15 @@ package payments
import ( import (
"context" "context"
"github.com/skyrings/skyring-common/tools/uuid"
) )
// CreditCards exposes all needed functionality to manage account credit cards. // CreditCards exposes all needed functionality to manage account credit cards.
type CreditCards interface { type CreditCards interface {
// List returns a list of PaymentMethods for a given account. // List returns a list of PaymentMethods for a given account.
List(ctx context.Context, userID uuid.UUID) ([]CreditCard, error) List(ctx context.Context) ([]CreditCard, error)
// Add is used to save new credit card and attach it to payment account. // Add is used to save new credit card and attach it to payment account.
Add(ctx context.Context, userID uuid.UUID, cardToken string) error Add(ctx context.Context, cardToken string) error
} }
// CreditCard holds all public information about credit card. // CreditCard holds all public information about credit card.

View File

@ -15,6 +15,7 @@ import (
// accounts is an implementation of payments.Accounts. // accounts is an implementation of payments.Accounts.
type accounts struct { type accounts struct {
service *Service service *Service
userID uuid.UUID
} }
// CreditCards exposes all needed functionality to manage account credit cards. // CreditCards exposes all needed functionality to manage account credit cards.
@ -23,8 +24,8 @@ func (accounts *accounts) CreditCards() payments.CreditCards {
} }
// Setup creates a payment account for the user. // Setup creates a payment account for the user.
func (accounts *accounts) Setup(ctx context.Context, userID uuid.UUID, email string) (err error) { func (accounts *accounts) Setup(ctx context.Context, email string) (err error) {
defer mon.Task()(&ctx, userID, email)(&err) defer mon.Task()(&ctx, accounts.userID, email)(&err)
params := &stripe.CustomerParams{ params := &stripe.CustomerParams{
Email: stripe.String(email), Email: stripe.String(email),
@ -35,14 +36,14 @@ func (accounts *accounts) Setup(ctx context.Context, userID uuid.UUID, email str
} }
// TODO: delete customer from stripe, if db insertion fails // TODO: delete customer from stripe, if db insertion fails
return accounts.service.customers.Insert(ctx, userID, email) return accounts.service.customers.Insert(ctx, accounts.userID, email)
} }
// Balance returns an integer amount in cents that represents the current balance of payment account. // Balance returns an integer amount in cents that represents the current balance of payment account.
func (accounts *accounts) Balance(ctx context.Context, userID uuid.UUID) (_ int64, err error) { func (accounts *accounts) Balance(ctx context.Context) (_ int64, err error) {
defer mon.Task()(&ctx, userID)(&err) defer mon.Task()(&ctx, accounts.userID)(&err)
customerID, err := accounts.service.customers.GetCustomerID(ctx, userID) customerID, err := accounts.service.customers.GetCustomerID(ctx, accounts.userID)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -15,13 +15,14 @@ import (
// creditCards is an implementation of payments.CreditCards. // creditCards is an implementation of payments.CreditCards.
type creditCards struct { type creditCards struct {
service *Service service *Service
userID uuid.UUID
} }
// List returns a list of PaymentMethods for a given Customer. // List returns a list of PaymentMethods for a given Customer.
func (creditCards *creditCards) List(ctx context.Context, userID uuid.UUID) (cards []payments.CreditCard, err error) { func (creditCards *creditCards) List(ctx context.Context) (cards []payments.CreditCard, err error) {
defer mon.Task()(&ctx, userID)(&err) defer mon.Task()(&ctx, creditCards.userID)(&err)
customerID, err := creditCards.service.customers.GetCustomerID(ctx, userID) customerID, err := creditCards.service.customers.GetCustomerID(ctx, creditCards.userID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -52,10 +53,10 @@ func (creditCards *creditCards) List(ctx context.Context, userID uuid.UUID) (car
} }
// Add is used to save new credit card and attach it to payment account. // Add is used to save new credit card and attach it to payment account.
func (creditCards *creditCards) Add(ctx context.Context, userID uuid.UUID, cardToken string) (err error) { func (creditCards *creditCards) Add(ctx context.Context, cardToken string) (err error) {
defer mon.Task()(&ctx, userID, cardToken)(&err) defer mon.Task()(&ctx, creditCards.userID, cardToken)(&err)
customerID, err := creditCards.service.customers.GetCustomerID(ctx, userID) customerID, err := creditCards.service.customers.GetCustomerID(ctx, creditCards.userID)
if err != nil { if err != nil {
return err return err
} }

View File

@ -4,6 +4,7 @@
package stripecoinpayments package stripecoinpayments
import ( import (
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/stripe/stripe-go/client" "github.com/stripe/stripe-go/client"
"github.com/zeebo/errs" "github.com/zeebo/errs"
"gopkg.in/spacemonkeygo/monkit.v2" "gopkg.in/spacemonkeygo/monkit.v2"
@ -38,6 +39,6 @@ func NewService(config Config, customers CustomersDB) *Service {
} }
// Accounts exposes all needed functionality to manage payment accounts. // Accounts exposes all needed functionality to manage payment accounts.
func (service *Service) Accounts() payments.Accounts { func (service *Service) Accounts(userID uuid.UUID) payments.Accounts {
return &accounts{service: service} return &accounts{service: service}
} }

View File

@ -20,10 +20,10 @@ type customers struct {
func (customers *customers) Insert(ctx context.Context, userID uuid.UUID, customerID string) (err error) { func (customers *customers) Insert(ctx context.Context, userID uuid.UUID, customerID string) (err error) {
defer mon.Task()(&ctx, userID, customerID)(&err) defer mon.Task()(&ctx, userID, customerID)(&err)
_, err = customers.db.Create_StripeCustomers( _, err = customers.db.Create_StripeCustomer(
ctx, ctx,
dbx.StripeCustomers_UserId(userID[:]), dbx.StripeCustomer_UserId(userID[:]),
dbx.StripeCustomers_CustomerId(customerID), dbx.StripeCustomer_CustomerId(customerID),
) )
return err return err
@ -33,7 +33,7 @@ func (customers *customers) Insert(ctx context.Context, userID uuid.UUID, custom
func (customers *customers) GetCustomerID(ctx context.Context, userID uuid.UUID) (customerID string, err error) { func (customers *customers) GetCustomerID(ctx context.Context, userID uuid.UUID) (customerID string, err error) {
defer mon.Task()(&ctx, userID)(&err) defer mon.Task()(&ctx, userID)(&err)
idRow, err := customers.db.Get_StripeCustomers_CustomerId_By_UserId(ctx, dbx.StripeCustomers_UserId(userID[:])) idRow, err := customers.db.Get_StripeCustomer_CustomerId_By_UserId(ctx, dbx.StripeCustomer_UserId(userID[:]))
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -878,7 +878,7 @@ read one (
//--- satellite payments ---// //--- satellite payments ---//
model stripe_customers ( model stripe_customer (
key user_id key user_id
unique customer_id unique customer_id
@ -887,8 +887,8 @@ model stripe_customers (
field created_at timestamp ( autoinsert ) field created_at timestamp ( autoinsert )
) )
create stripe_customers ( ) create stripe_customer ( )
read one ( read one (
select stripe_customers.customer_id select stripe_customer.customer_id
where stripe_customers.user_id = ? where stripe_customer.user_id = ?
) )

View File

@ -4438,73 +4438,73 @@ func (f StoragenodeStorageTally_DataTotal_Field) value() interface{} {
func (StoragenodeStorageTally_DataTotal_Field) _Column() string { return "data_total" } func (StoragenodeStorageTally_DataTotal_Field) _Column() string { return "data_total" }
type StripeCustomers struct { type StripeCustomer struct {
UserId []byte UserId []byte
CustomerId string CustomerId string
CreatedAt time.Time CreatedAt time.Time
} }
func (StripeCustomers) _Table() string { return "stripe_customers" } func (StripeCustomer) _Table() string { return "stripe_customers" }
type StripeCustomers_Update_Fields struct { type StripeCustomer_Update_Fields struct {
} }
type StripeCustomers_UserId_Field struct { type StripeCustomer_UserId_Field struct {
_set bool _set bool
_null bool _null bool
_value []byte _value []byte
} }
func StripeCustomers_UserId(v []byte) StripeCustomers_UserId_Field { func StripeCustomer_UserId(v []byte) StripeCustomer_UserId_Field {
return StripeCustomers_UserId_Field{_set: true, _value: v} return StripeCustomer_UserId_Field{_set: true, _value: v}
} }
func (f StripeCustomers_UserId_Field) value() interface{} { func (f StripeCustomer_UserId_Field) value() interface{} {
if !f._set || f._null { if !f._set || f._null {
return nil return nil
} }
return f._value return f._value
} }
func (StripeCustomers_UserId_Field) _Column() string { return "user_id" } func (StripeCustomer_UserId_Field) _Column() string { return "user_id" }
type StripeCustomers_CustomerId_Field struct { type StripeCustomer_CustomerId_Field struct {
_set bool _set bool
_null bool _null bool
_value string _value string
} }
func StripeCustomers_CustomerId(v string) StripeCustomers_CustomerId_Field { func StripeCustomer_CustomerId(v string) StripeCustomer_CustomerId_Field {
return StripeCustomers_CustomerId_Field{_set: true, _value: v} return StripeCustomer_CustomerId_Field{_set: true, _value: v}
} }
func (f StripeCustomers_CustomerId_Field) value() interface{} { func (f StripeCustomer_CustomerId_Field) value() interface{} {
if !f._set || f._null { if !f._set || f._null {
return nil return nil
} }
return f._value return f._value
} }
func (StripeCustomers_CustomerId_Field) _Column() string { return "customer_id" } func (StripeCustomer_CustomerId_Field) _Column() string { return "customer_id" }
type StripeCustomers_CreatedAt_Field struct { type StripeCustomer_CreatedAt_Field struct {
_set bool _set bool
_null bool _null bool
_value time.Time _value time.Time
} }
func StripeCustomers_CreatedAt(v time.Time) StripeCustomers_CreatedAt_Field { func StripeCustomer_CreatedAt(v time.Time) StripeCustomer_CreatedAt_Field {
return StripeCustomers_CreatedAt_Field{_set: true, _value: v} return StripeCustomer_CreatedAt_Field{_set: true, _value: v}
} }
func (f StripeCustomers_CreatedAt_Field) value() interface{} { func (f StripeCustomer_CreatedAt_Field) value() interface{} {
if !f._set || f._null { if !f._set || f._null {
return nil return nil
} }
return f._value return f._value
} }
func (StripeCustomers_CreatedAt_Field) _Column() string { return "created_at" } func (StripeCustomer_CreatedAt_Field) _Column() string { return "created_at" }
type User struct { type User struct {
Id []byte Id []byte
@ -6765,14 +6765,14 @@ func (obj *postgresImpl) CreateNoReturn_GracefulExitTransferQueue(ctx context.Co
} }
func (obj *postgresImpl) Create_StripeCustomers(ctx context.Context, func (obj *postgresImpl) Create_StripeCustomer(ctx context.Context,
stripe_customers_user_id StripeCustomers_UserId_Field, stripe_customer_user_id StripeCustomer_UserId_Field,
stripe_customers_customer_id StripeCustomers_CustomerId_Field) ( stripe_customer_customer_id StripeCustomer_CustomerId_Field) (
stripe_customers *StripeCustomers, err error) { stripe_customer *StripeCustomer, err error) {
__now := obj.db.Hooks.Now().UTC() __now := obj.db.Hooks.Now().UTC()
__user_id_val := stripe_customers_user_id.value() __user_id_val := stripe_customer_user_id.value()
__customer_id_val := stripe_customers_customer_id.value() __customer_id_val := stripe_customer_customer_id.value()
__created_at_val := __now __created_at_val := __now
var __embed_stmt = __sqlbundle_Literal("INSERT INTO stripe_customers ( user_id, customer_id, created_at ) VALUES ( ?, ?, ? ) RETURNING stripe_customers.user_id, stripe_customers.customer_id, stripe_customers.created_at") var __embed_stmt = __sqlbundle_Literal("INSERT INTO stripe_customers ( user_id, customer_id, created_at ) VALUES ( ?, ?, ? ) RETURNING stripe_customers.user_id, stripe_customers.customer_id, stripe_customers.created_at")
@ -6780,12 +6780,12 @@ func (obj *postgresImpl) Create_StripeCustomers(ctx context.Context,
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt) var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __user_id_val, __customer_id_val, __created_at_val) obj.logStmt(__stmt, __user_id_val, __customer_id_val, __created_at_val)
stripe_customers = &StripeCustomers{} stripe_customer = &StripeCustomer{}
err = obj.driver.QueryRow(__stmt, __user_id_val, __customer_id_val, __created_at_val).Scan(&stripe_customers.UserId, &stripe_customers.CustomerId, &stripe_customers.CreatedAt) err = obj.driver.QueryRow(__stmt, __user_id_val, __customer_id_val, __created_at_val).Scan(&stripe_customer.UserId, &stripe_customer.CustomerId, &stripe_customer.CreatedAt)
if err != nil { if err != nil {
return nil, obj.makeErr(err) return nil, obj.makeErr(err)
} }
return stripe_customers, nil return stripe_customer, nil
} }
@ -8352,14 +8352,14 @@ func (obj *postgresImpl) Get_GracefulExitTransferQueue_By_NodeId_And_Path(ctx co
} }
func (obj *postgresImpl) Get_StripeCustomers_CustomerId_By_UserId(ctx context.Context, func (obj *postgresImpl) Get_StripeCustomer_CustomerId_By_UserId(ctx context.Context,
stripe_customers_user_id StripeCustomers_UserId_Field) ( stripe_customer_user_id StripeCustomer_UserId_Field) (
row *CustomerId_Row, err error) { row *CustomerId_Row, err error) {
var __embed_stmt = __sqlbundle_Literal("SELECT stripe_customers.customer_id FROM stripe_customers WHERE stripe_customers.user_id = ?") var __embed_stmt = __sqlbundle_Literal("SELECT stripe_customers.customer_id FROM stripe_customers WHERE stripe_customers.user_id = ?")
var __values []interface{} var __values []interface{}
__values = append(__values, stripe_customers_user_id.value()) __values = append(__values, stripe_customer_user_id.value())
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt) var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...) obj.logStmt(__stmt, __values...)
@ -10946,14 +10946,14 @@ func (obj *sqlite3Impl) CreateNoReturn_GracefulExitTransferQueue(ctx context.Con
} }
func (obj *sqlite3Impl) Create_StripeCustomers(ctx context.Context, func (obj *sqlite3Impl) Create_StripeCustomer(ctx context.Context,
stripe_customers_user_id StripeCustomers_UserId_Field, stripe_customer_user_id StripeCustomer_UserId_Field,
stripe_customers_customer_id StripeCustomers_CustomerId_Field) ( stripe_customer_customer_id StripeCustomer_CustomerId_Field) (
stripe_customers *StripeCustomers, err error) { stripe_customer *StripeCustomer, err error) {
__now := obj.db.Hooks.Now().UTC() __now := obj.db.Hooks.Now().UTC()
__user_id_val := stripe_customers_user_id.value() __user_id_val := stripe_customer_user_id.value()
__customer_id_val := stripe_customers_customer_id.value() __customer_id_val := stripe_customer_customer_id.value()
__created_at_val := __now __created_at_val := __now
var __embed_stmt = __sqlbundle_Literal("INSERT INTO stripe_customers ( user_id, customer_id, created_at ) VALUES ( ?, ?, ? )") var __embed_stmt = __sqlbundle_Literal("INSERT INTO stripe_customers ( user_id, customer_id, created_at ) VALUES ( ?, ?, ? )")
@ -10969,7 +10969,7 @@ func (obj *sqlite3Impl) Create_StripeCustomers(ctx context.Context,
if err != nil { if err != nil {
return nil, obj.makeErr(err) return nil, obj.makeErr(err)
} }
return obj.getLastStripeCustomers(ctx, __pk) return obj.getLastStripeCustomer(ctx, __pk)
} }
@ -12536,14 +12536,14 @@ func (obj *sqlite3Impl) Get_GracefulExitTransferQueue_By_NodeId_And_Path(ctx con
} }
func (obj *sqlite3Impl) Get_StripeCustomers_CustomerId_By_UserId(ctx context.Context, func (obj *sqlite3Impl) Get_StripeCustomer_CustomerId_By_UserId(ctx context.Context,
stripe_customers_user_id StripeCustomers_UserId_Field) ( stripe_customer_user_id StripeCustomer_UserId_Field) (
row *CustomerId_Row, err error) { row *CustomerId_Row, err error) {
var __embed_stmt = __sqlbundle_Literal("SELECT stripe_customers.customer_id FROM stripe_customers WHERE stripe_customers.user_id = ?") var __embed_stmt = __sqlbundle_Literal("SELECT stripe_customers.customer_id FROM stripe_customers WHERE stripe_customers.user_id = ?")
var __values []interface{} var __values []interface{}
__values = append(__values, stripe_customers_user_id.value()) __values = append(__values, stripe_customer_user_id.value())
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt) var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...) obj.logStmt(__stmt, __values...)
@ -14530,21 +14530,21 @@ func (obj *sqlite3Impl) getLastGracefulExitTransferQueue(ctx context.Context,
} }
func (obj *sqlite3Impl) getLastStripeCustomers(ctx context.Context, func (obj *sqlite3Impl) getLastStripeCustomer(ctx context.Context,
pk int64) ( pk int64) (
stripe_customers *StripeCustomers, err error) { stripe_customer *StripeCustomer, err error) {
var __embed_stmt = __sqlbundle_Literal("SELECT stripe_customers.user_id, stripe_customers.customer_id, stripe_customers.created_at FROM stripe_customers WHERE _rowid_ = ?") var __embed_stmt = __sqlbundle_Literal("SELECT stripe_customers.user_id, stripe_customers.customer_id, stripe_customers.created_at FROM stripe_customers WHERE _rowid_ = ?")
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt) var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, pk) obj.logStmt(__stmt, pk)
stripe_customers = &StripeCustomers{} stripe_customer = &StripeCustomer{}
err = obj.driver.QueryRow(__stmt, pk).Scan(&stripe_customers.UserId, &stripe_customers.CustomerId, &stripe_customers.CreatedAt) err = obj.driver.QueryRow(__stmt, pk).Scan(&stripe_customer.UserId, &stripe_customer.CustomerId, &stripe_customer.CreatedAt)
if err != nil { if err != nil {
return nil, obj.makeErr(err) return nil, obj.makeErr(err)
} }
return stripe_customers, nil return stripe_customer, nil
} }
@ -15406,15 +15406,15 @@ func (rx *Rx) Create_ResetPasswordToken(ctx context.Context,
} }
func (rx *Rx) Create_StripeCustomers(ctx context.Context, func (rx *Rx) Create_StripeCustomer(ctx context.Context,
stripe_customers_user_id StripeCustomers_UserId_Field, stripe_customer_user_id StripeCustomer_UserId_Field,
stripe_customers_customer_id StripeCustomers_CustomerId_Field) ( stripe_customer_customer_id StripeCustomer_CustomerId_Field) (
stripe_customers *StripeCustomers, err error) { stripe_customer *StripeCustomer, err error) {
var tx *Tx var tx *Tx
if tx, err = rx.getTx(ctx); err != nil { if tx, err = rx.getTx(ctx); err != nil {
return return
} }
return tx.Create_StripeCustomers(ctx, stripe_customers_user_id, stripe_customers_customer_id) return tx.Create_StripeCustomer(ctx, stripe_customer_user_id, stripe_customer_customer_id)
} }
@ -15919,14 +15919,14 @@ func (rx *Rx) Get_StoragenodeStorageTally_By_Id(ctx context.Context,
return tx.Get_StoragenodeStorageTally_By_Id(ctx, storagenode_storage_tally_id) return tx.Get_StoragenodeStorageTally_By_Id(ctx, storagenode_storage_tally_id)
} }
func (rx *Rx) Get_StripeCustomers_CustomerId_By_UserId(ctx context.Context, func (rx *Rx) Get_StripeCustomer_CustomerId_By_UserId(ctx context.Context,
stripe_customers_user_id StripeCustomers_UserId_Field) ( stripe_customer_user_id StripeCustomer_UserId_Field) (
row *CustomerId_Row, err error) { row *CustomerId_Row, err error) {
var tx *Tx var tx *Tx
if tx, err = rx.getTx(ctx); err != nil { if tx, err = rx.getTx(ctx); err != nil {
return return
} }
return tx.Get_StripeCustomers_CustomerId_By_UserId(ctx, stripe_customers_user_id) return tx.Get_StripeCustomer_CustomerId_By_UserId(ctx, stripe_customer_user_id)
} }
func (rx *Rx) Get_User_By_Id(ctx context.Context, func (rx *Rx) Get_User_By_Id(ctx context.Context,
@ -16475,10 +16475,10 @@ type Methods interface {
reset_password_token_owner_id ResetPasswordToken_OwnerId_Field) ( reset_password_token_owner_id ResetPasswordToken_OwnerId_Field) (
reset_password_token *ResetPasswordToken, err error) reset_password_token *ResetPasswordToken, err error)
Create_StripeCustomers(ctx context.Context, Create_StripeCustomer(ctx context.Context,
stripe_customers_user_id StripeCustomers_UserId_Field, stripe_customer_user_id StripeCustomer_UserId_Field,
stripe_customers_customer_id StripeCustomers_CustomerId_Field) ( stripe_customer_customer_id StripeCustomer_CustomerId_Field) (
stripe_customers *StripeCustomers, err error) stripe_customer *StripeCustomer, err error)
Create_User(ctx context.Context, Create_User(ctx context.Context,
user_id User_Id_Field, user_id User_Id_Field,
@ -16693,8 +16693,8 @@ type Methods interface {
storagenode_storage_tally_id StoragenodeStorageTally_Id_Field) ( storagenode_storage_tally_id StoragenodeStorageTally_Id_Field) (
storagenode_storage_tally *StoragenodeStorageTally, err error) storagenode_storage_tally *StoragenodeStorageTally, err error)
Get_StripeCustomers_CustomerId_By_UserId(ctx context.Context, Get_StripeCustomer_CustomerId_By_UserId(ctx context.Context,
stripe_customers_user_id StripeCustomers_UserId_Field) ( stripe_customer_user_id StripeCustomer_UserId_Field) (
row *CustomerId_Row, err error) row *CustomerId_Row, err error)
Get_User_By_Id(ctx context.Context, Get_User_By_Id(ctx context.Context,