2020-01-24 13:38:53 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-01-24 13:38:53 +00:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
"storj.io/storj/satellite/payments/coinpayments"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreditsDB is an interface for managing credits table.
|
|
|
|
//
|
|
|
|
// architecture: Database
|
|
|
|
type CreditsDB interface {
|
|
|
|
// InsertCredit inserts credit to user's credit balance into the database.
|
|
|
|
InsertCredit(ctx context.Context, credit payments.Credit) error
|
|
|
|
// GetCredit returns credit by transactionID.
|
|
|
|
GetCredit(ctx context.Context, transactionID coinpayments.TransactionID) (_ payments.Credit, err error)
|
|
|
|
// ListCredits returns all credits of specific user.
|
|
|
|
ListCredits(ctx context.Context, userID uuid.UUID) ([]payments.Credit, error)
|
|
|
|
// ListCreditsPaged returns all credits of specific user.
|
|
|
|
ListCreditsPaged(ctx context.Context, offset int64, limit int, before time.Time, userID uuid.UUID) (payments.CreditsPage, error)
|
|
|
|
|
|
|
|
// InsertCreditsSpending inserts spending to user's spending list into the database.
|
|
|
|
InsertCreditsSpending(ctx context.Context, spending CreditsSpending) error
|
|
|
|
// ListCreditsSpendings returns spending received for concrete deposit.
|
|
|
|
ListCreditsSpendings(ctx context.Context, userID uuid.UUID) ([]CreditsSpending, error)
|
|
|
|
// ListCreditsSpendingsPaged returns all spending of specific user.
|
|
|
|
ListCreditsSpendingsPaged(ctx context.Context, status int, offset int64, limit int, before time.Time) (CreditsSpendingsPage, error)
|
|
|
|
// ApplyCreditsSpending updated spending's status.
|
2020-02-11 14:48:28 +00:00
|
|
|
ApplyCreditsSpending(ctx context.Context, spendingID uuid.UUID) (err error)
|
2020-01-24 13:38:53 +00:00
|
|
|
|
|
|
|
// Balance returns difference between all credits and creditsSpendings of specific user.
|
|
|
|
Balance(ctx context.Context, userID uuid.UUID) (int64, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensures that credits implements payments.Credits.
|
|
|
|
var _ payments.Credits = (*credits)(nil)
|
|
|
|
|
|
|
|
// credits is an implementation of payments.Credits.
|
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type credits struct {
|
|
|
|
service *Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreditsSpending is an entity that holds funds been used from Accounts bonus credit balance.
|
|
|
|
// Status shows if spending have been used to pay for invoice already or not.
|
|
|
|
type CreditsSpending struct {
|
2020-02-11 14:48:28 +00:00
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
ProjectID uuid.UUID `json:"projectId"`
|
|
|
|
UserID uuid.UUID `json:"userId"`
|
|
|
|
Amount int64 `json:"amount"`
|
|
|
|
Status CreditsSpendingStatus `json:"status"`
|
|
|
|
Created time.Time `json:"created"`
|
2020-01-24 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 14:48:28 +00:00
|
|
|
// CreditsSpendingsPage holds set of creditsSpendings and indicates if
|
|
|
|
// there are more creditsSpendings to fetch.
|
2020-01-24 13:38:53 +00:00
|
|
|
type CreditsSpendingsPage struct {
|
|
|
|
Spendings []CreditsSpending
|
|
|
|
Next bool
|
|
|
|
NextOffset int64
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:48:28 +00:00
|
|
|
// CreditsSpendingStatus indicates the state of the creditsSpending.
|
2020-01-24 13:38:53 +00:00
|
|
|
type CreditsSpendingStatus int
|
|
|
|
|
|
|
|
const (
|
2020-02-11 14:48:28 +00:00
|
|
|
// CreditsSpendingStatusUnapplied is a default creditsSpending state.
|
2020-01-24 13:38:53 +00:00
|
|
|
CreditsSpendingStatusUnapplied CreditsSpendingStatus = 0
|
|
|
|
// CreditsSpendingStatusApplied status indicates that spending was applied.
|
|
|
|
CreditsSpendingStatusApplied CreditsSpendingStatus = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create attaches a credit for payment account.
|
|
|
|
func (credits *credits) Create(ctx context.Context, credit payments.Credit) (err error) {
|
|
|
|
defer mon.Task()(&ctx, credit)(&err)
|
|
|
|
|
|
|
|
return Error.Wrap(credits.service.db.Credits().InsertCredit(ctx, credit))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListByUserID return list of all credits of specified payment account.
|
|
|
|
func (credits *credits) ListByUserID(ctx context.Context, userID uuid.UUID) (_ []payments.Credit, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
creditsList, err := credits.service.db.Credits().ListCredits(ctx, userID)
|
|
|
|
|
|
|
|
return creditsList, Error.Wrap(err)
|
|
|
|
}
|