2019-10-10 18:12:23 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-05 13:16:02 +00:00
|
|
|
"database/sql"
|
|
|
|
"time"
|
2019-10-10 18:12:23 +01:00
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-11-04 12:30:07 +00:00
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
2020-01-15 02:29:51 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
2019-10-10 18:12:23 +01:00
|
|
|
)
|
|
|
|
|
2019-11-04 14:37:39 +00:00
|
|
|
// ensures that customers implements stripecoinpayments.CustomersDB.
|
2019-11-04 12:30:07 +00:00
|
|
|
var _ stripecoinpayments.CustomersDB = (*customers)(nil)
|
|
|
|
|
2019-10-11 16:00:35 +01:00
|
|
|
// customers is an implementation of stripecoinpayments.CustomersDB.
|
2019-11-05 13:16:02 +00:00
|
|
|
//
|
|
|
|
// architecture: Database
|
2019-10-10 18:12:23 +01:00
|
|
|
type customers struct {
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2019-10-10 18:12:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert inserts a stripe customer into the database.
|
|
|
|
func (customers *customers) Insert(ctx context.Context, userID uuid.UUID, customerID string) (err error) {
|
|
|
|
defer mon.Task()(&ctx, userID, customerID)(&err)
|
|
|
|
|
2019-10-15 19:05:45 +01:00
|
|
|
_, err = customers.db.Create_StripeCustomer(
|
2019-10-10 18:12:23 +01:00
|
|
|
ctx,
|
2019-10-15 19:05:45 +01:00
|
|
|
dbx.StripeCustomer_UserId(userID[:]),
|
|
|
|
dbx.StripeCustomer_CustomerId(customerID),
|
2019-10-10 18:12:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2019-10-11 16:00:35 +01:00
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
// GetCustomerID returns stripe customers id.
|
|
|
|
func (customers *customers) GetCustomerID(ctx context.Context, userID uuid.UUID) (_ string, err error) {
|
2019-10-11 16:00:35 +01:00
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
2019-10-15 19:05:45 +01:00
|
|
|
idRow, err := customers.db.Get_StripeCustomer_CustomerId_By_UserId(ctx, dbx.StripeCustomer_UserId(userID[:]))
|
2019-10-11 16:00:35 +01:00
|
|
|
if err != nil {
|
2019-11-05 13:16:02 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return "", stripecoinpayments.ErrNoCustomer
|
|
|
|
}
|
|
|
|
|
2019-10-11 16:00:35 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return idRow.CustomerId, nil
|
|
|
|
}
|
2019-11-05 13:16:02 +00:00
|
|
|
|
|
|
|
// List returns paginated customers id list, with customers created before specified date.
|
|
|
|
func (customers *customers) List(ctx context.Context, offset int64, limit int, before time.Time) (_ stripecoinpayments.CustomersPage, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var page stripecoinpayments.CustomersPage
|
|
|
|
|
|
|
|
dbxCustomers, err := customers.db.Limited_StripeCustomer_By_CreatedAt_LessOrEqual_OrderBy_Desc_CreatedAt(ctx,
|
|
|
|
dbx.StripeCustomer_CreatedAt(before),
|
|
|
|
limit+1,
|
|
|
|
offset,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return stripecoinpayments.CustomersPage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dbxCustomers) == limit+1 {
|
|
|
|
page.Next = true
|
2020-01-29 00:57:15 +00:00
|
|
|
page.NextOffset = offset + int64(limit)
|
2019-11-05 13:16:02 +00:00
|
|
|
|
|
|
|
dbxCustomers = dbxCustomers[:len(dbxCustomers)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dbxCustomer := range dbxCustomers {
|
|
|
|
cus, err := fromDBXCustomer(dbxCustomer)
|
|
|
|
if err != nil {
|
|
|
|
return stripecoinpayments.CustomersPage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
page.Customers = append(page.Customers, *cus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return page, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromDBXCustomer converts *dbx.StripeCustomer to *stripecoinpayments.Customer.
|
|
|
|
func fromDBXCustomer(dbxCustomer *dbx.StripeCustomer) (*stripecoinpayments.Customer, error) {
|
2020-03-31 17:49:16 +01:00
|
|
|
userID, err := uuid.FromBytes(dbxCustomer.UserId)
|
2019-11-05 13:16:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &stripecoinpayments.Customer{
|
|
|
|
ID: dbxCustomer.CustomerId,
|
|
|
|
UserID: userID,
|
|
|
|
}, nil
|
|
|
|
}
|