2022-04-26 21:23:27 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-16 17:48:07 +01:00
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"time"
|
2022-04-26 21:23:27 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2022-09-06 13:43:09 +01:00
|
|
|
"storj.io/common/currency"
|
2022-04-26 21:23:27 +01:00
|
|
|
"storj.io/common/uuid"
|
2022-08-04 18:29:55 +01:00
|
|
|
"storj.io/private/dbutil/pgutil/pgerrcode"
|
2022-04-26 21:23:27 +01:00
|
|
|
"storj.io/storj/satellite/payments/billing"
|
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ensures that *billingDB implements billing.TransactionsDB.
|
|
|
|
var _ billing.TransactionsDB = (*billingDB)(nil)
|
|
|
|
|
|
|
|
// billingDB is billing DB.
|
|
|
|
//
|
|
|
|
// architecture: Database
|
|
|
|
type billingDB struct {
|
|
|
|
db *satelliteDB
|
|
|
|
}
|
|
|
|
|
2022-06-16 17:48:07 +01:00
|
|
|
func (db billingDB) Insert(ctx context.Context, billingTX billing.Transaction) (txID int64, err error) {
|
2022-04-26 21:23:27 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2022-06-16 17:48:07 +01:00
|
|
|
var dbxTX *dbx.BillingTransaction
|
2022-08-04 18:29:55 +01:00
|
|
|
var retryCount int
|
|
|
|
for {
|
2022-08-27 00:08:03 +01:00
|
|
|
oldBalance, err := db.GetBalance(ctx, billingTX.UserID)
|
2022-06-16 17:48:07 +01:00
|
|
|
if err != nil {
|
2022-08-04 18:29:55 +01:00
|
|
|
return 0, Error.Wrap(err)
|
|
|
|
}
|
2022-09-06 13:43:09 +01:00
|
|
|
billingAmount := currency.AmountFromDecimal(billingTX.Amount.AsDecimal().Truncate(currency.USDollarsMicro.DecimalPlaces()), currency.USDollarsMicro)
|
|
|
|
newBalance, err := currency.Add(oldBalance, billingAmount)
|
2022-08-27 00:08:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
if newBalance.IsNegative() {
|
2022-08-04 18:29:55 +01:00
|
|
|
return 0, billing.ErrInsufficientFunds
|
2022-06-16 17:48:07 +01:00
|
|
|
}
|
2022-08-04 18:29:55 +01:00
|
|
|
|
|
|
|
err = db.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
|
|
|
|
updatedRow, err := tx.Update_BillingBalance_By_UserId_And_Balance(ctx,
|
2022-06-16 17:48:07 +01:00
|
|
|
dbx.BillingBalance_UserId(billingTX.UserID[:]),
|
2022-08-27 00:08:03 +01:00
|
|
|
dbx.BillingBalance_Balance(oldBalance.BaseUnits()),
|
2022-08-04 18:29:55 +01:00
|
|
|
dbx.BillingBalance_Update_Fields{
|
2022-08-27 00:08:03 +01:00
|
|
|
Balance: dbx.BillingBalance_Balance(newBalance.BaseUnits()),
|
2022-08-04 18:29:55 +01:00
|
|
|
})
|
2022-06-16 17:48:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2022-08-04 18:29:55 +01:00
|
|
|
if updatedRow == nil {
|
|
|
|
// Try an insert here, in case the user never had a record in the table.
|
2022-08-27 00:08:03 +01:00
|
|
|
// If the user already had a record, and the oldBalance was not as expected,
|
2022-08-04 18:29:55 +01:00
|
|
|
// the insert will fail anyways.
|
|
|
|
err = tx.CreateNoReturn_BillingBalance(ctx,
|
|
|
|
dbx.BillingBalance_UserId(billingTX.UserID[:]),
|
2022-08-27 00:08:03 +01:00
|
|
|
dbx.BillingBalance_Balance(newBalance.BaseUnits()))
|
2022-08-04 18:29:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
2022-06-16 17:48:07 +01:00
|
|
|
|
2022-08-04 18:29:55 +01:00
|
|
|
dbxTX, err = tx.Create_BillingTransaction(ctx,
|
|
|
|
dbx.BillingTransaction_UserId(billingTX.UserID[:]),
|
2022-08-27 00:08:03 +01:00
|
|
|
dbx.BillingTransaction_Amount(billingAmount.BaseUnits()),
|
|
|
|
dbx.BillingTransaction_Currency(billingAmount.Currency().Symbol()),
|
2022-08-04 18:29:55 +01:00
|
|
|
dbx.BillingTransaction_Description(billingTX.Description),
|
|
|
|
dbx.BillingTransaction_Source(billingTX.Source),
|
|
|
|
dbx.BillingTransaction_Status(string(billingTX.Status)),
|
|
|
|
dbx.BillingTransaction_Type(string(billingTX.Type)),
|
|
|
|
dbx.BillingTransaction_Metadata(handleMetaDataZeroValue(billingTX.Metadata)),
|
|
|
|
dbx.BillingTransaction_Timestamp(billingTX.Timestamp))
|
|
|
|
return err
|
|
|
|
})
|
2022-08-22 19:31:48 +01:00
|
|
|
if pgerrcode.IsConstraintViolation(err) {
|
2022-08-04 18:29:55 +01:00
|
|
|
retryCount++
|
|
|
|
if retryCount > 5 {
|
|
|
|
return 0, Error.New("Unable to insert new billing transaction after several retries: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if dbxTX == nil {
|
|
|
|
return 0, Error.New("Unable to insert new billing transaction")
|
|
|
|
}
|
|
|
|
break
|
2022-05-10 20:19:53 +01:00
|
|
|
}
|
2022-06-16 17:48:07 +01:00
|
|
|
return dbxTX.Id, err
|
2022-04-26 21:23:27 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 20:19:53 +01:00
|
|
|
func (db billingDB) UpdateStatus(ctx context.Context, txID int64, status billing.TransactionStatus) (err error) {
|
2022-04-26 21:23:27 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2022-05-10 20:19:53 +01:00
|
|
|
return db.db.UpdateNoReturn_BillingTransaction_By_Id(ctx, dbx.BillingTransaction_Id(txID), dbx.BillingTransaction_Update_Fields{
|
|
|
|
Status: dbx.BillingTransaction_Status(string(status)),
|
|
|
|
})
|
|
|
|
}
|
2022-06-16 17:48:07 +01:00
|
|
|
|
2022-05-10 20:19:53 +01:00
|
|
|
func (db billingDB) UpdateMetadata(ctx context.Context, txID int64, newMetadata []byte) (err error) {
|
2022-06-16 17:48:07 +01:00
|
|
|
|
2022-05-10 20:19:53 +01:00
|
|
|
dbxTX, err := db.db.Get_BillingTransaction_Metadata_By_Id(ctx, dbx.BillingTransaction_Id(txID))
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
2022-04-26 21:23:27 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 20:19:53 +01:00
|
|
|
updatedMetadata, err := updateMetadata(dbxTX.Metadata, newMetadata)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
2022-04-26 21:23:27 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 20:19:53 +01:00
|
|
|
return db.db.UpdateNoReturn_BillingTransaction_By_Id(ctx, dbx.BillingTransaction_Id(txID), dbx.BillingTransaction_Update_Fields{
|
|
|
|
Metadata: dbx.BillingTransaction_Metadata(updatedMetadata),
|
|
|
|
})
|
2022-04-26 21:23:27 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 00:29:31 +01:00
|
|
|
func (db billingDB) LastTransaction(ctx context.Context, txSource string, txType billing.TransactionType) (_ time.Time, metadata []byte, err error) {
|
2022-04-26 21:23:27 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2022-06-16 17:48:07 +01:00
|
|
|
|
2022-08-23 22:59:15 +01:00
|
|
|
lastTransaction, err := db.db.First_BillingTransaction_By_Source_And_Type_OrderBy_Desc_CreatedAt(
|
2022-06-16 17:48:07 +01:00
|
|
|
ctx,
|
|
|
|
dbx.BillingTransaction_Source(txSource),
|
|
|
|
dbx.BillingTransaction_Type(string(txType)))
|
2022-06-17 00:29:31 +01:00
|
|
|
|
2022-06-16 17:48:07 +01:00
|
|
|
if err != nil {
|
2022-06-17 00:29:31 +01:00
|
|
|
return time.Time{}, nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastTransaction == nil {
|
2022-08-15 15:41:19 +01:00
|
|
|
return time.Time{}, nil, billing.ErrNoTransactions
|
2022-06-16 17:48:07 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 00:29:31 +01:00
|
|
|
return lastTransaction.Timestamp, lastTransaction.Metadata, nil
|
2022-06-16 17:48:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db billingDB) List(ctx context.Context, userID uuid.UUID) (txs []billing.Transaction, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
dbxTXs, err := db.db.All_BillingTransaction_By_UserId_OrderBy_Desc_Timestamp(ctx,
|
|
|
|
dbx.BillingTransaction_UserId(userID[:]))
|
2022-04-26 21:23:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dbxTX := range dbxTXs {
|
|
|
|
tx, err := fromDBXBillingTransaction(dbxTX)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
txs = append(txs, *tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return txs, nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 13:43:09 +01:00
|
|
|
func (db billingDB) GetBalance(ctx context.Context, userID uuid.UUID) (_ currency.Amount, err error) {
|
2022-04-26 21:23:27 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2022-06-16 17:48:07 +01:00
|
|
|
dbxBilling, err := db.db.Get_BillingBalance_Balance_By_UserId(ctx,
|
|
|
|
dbx.BillingBalance_UserId(userID[:]))
|
2022-04-26 21:23:27 +01:00
|
|
|
if err != nil {
|
2022-06-16 17:48:07 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-09-06 13:43:09 +01:00
|
|
|
return currency.USDollarsMicro.Zero(), nil
|
2022-04-26 21:23:27 +01:00
|
|
|
}
|
2022-09-06 13:43:09 +01:00
|
|
|
return currency.USDollarsMicro.Zero(), Error.Wrap(err)
|
2022-04-26 21:23:27 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 13:43:09 +01:00
|
|
|
return currency.AmountFromBaseUnits(dbxBilling.Balance, currency.USDollarsMicro), nil
|
2022-04-26 21:23:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// fromDBXBillingTransaction converts *dbx.BillingTransaction to *billing.Transaction.
|
|
|
|
func fromDBXBillingTransaction(dbxTX *dbx.BillingTransaction) (*billing.Transaction, error) {
|
|
|
|
userID, err := uuid.FromBytes(dbxTX.UserId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
return &billing.Transaction{
|
2022-06-16 17:48:07 +01:00
|
|
|
ID: dbxTX.Id,
|
|
|
|
UserID: userID,
|
2022-09-06 13:43:09 +01:00
|
|
|
Amount: currency.AmountFromBaseUnits(dbxTX.Amount, currency.USDollarsMicro),
|
2022-04-26 21:23:27 +01:00
|
|
|
Description: dbxTX.Description,
|
2022-06-16 17:48:07 +01:00
|
|
|
Source: dbxTX.Source,
|
|
|
|
Status: billing.TransactionStatus(dbxTX.Status),
|
|
|
|
Type: billing.TransactionType(dbxTX.Type),
|
|
|
|
Metadata: dbxTX.Metadata,
|
2022-04-26 21:23:27 +01:00
|
|
|
Timestamp: dbxTX.Timestamp,
|
|
|
|
CreatedAt: dbxTX.CreatedAt,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-06-16 17:48:07 +01:00
|
|
|
|
|
|
|
func updateMetadata(oldMetaData []byte, newMetaData []byte) ([]byte, error) {
|
|
|
|
var updatedMetadata map[string]interface{}
|
|
|
|
|
|
|
|
err := json.Unmarshal(oldMetaData, &updatedMetadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-10 20:19:53 +01:00
|
|
|
err = json.Unmarshal(handleMetaDataZeroValue(newMetaData), &updatedMetadata)
|
2022-06-16 17:48:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(updatedMetadata)
|
|
|
|
}
|
2022-05-10 20:19:53 +01:00
|
|
|
|
|
|
|
func handleMetaDataZeroValue(metaData []byte) []byte {
|
|
|
|
if metaData != nil {
|
|
|
|
return metaData
|
|
|
|
}
|
|
|
|
return []byte(`{}`)
|
|
|
|
}
|