2020-03-16 19:34:15 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-03-31 17:49:16 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-03-16 19:34:15 +00:00
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UserData contains the uuid and email of a satellite user.
|
|
|
|
type UserData struct {
|
2020-03-31 17:49:16 +01:00
|
|
|
ID uuid.UUID
|
2020-03-16 19:34:15 +00:00
|
|
|
Email string
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateStripeCustomers creates missing stripe-customers for users in our database.
|
|
|
|
func generateStripeCustomers(ctx context.Context) (err error) {
|
2020-05-18 13:21:35 +01:00
|
|
|
return runBillingCmd(func(payments *stripecoinpayments.Service, dbxDB *dbx.DB) error {
|
|
|
|
accounts := payments.Accounts()
|
2020-03-16 19:34:15 +00:00
|
|
|
|
2020-05-18 13:21:35 +01:00
|
|
|
rows, err := dbxDB.Query(ctx, "SELECT id, email FROM users WHERE id NOT IN (SELECT user_id from stripe_customers) AND users.status=1")
|
2020-03-16 19:34:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-18 13:21:35 +01:00
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, rows.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
var n int64
|
|
|
|
for rows.Next() {
|
|
|
|
n++
|
|
|
|
var user UserData
|
|
|
|
err := rows.Scan(&user.ID, &user.Email)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = accounts.Setup(ctx, user.ID, user.Email)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-31 17:49:16 +01:00
|
|
|
|
2020-03-16 19:34:15 +00:00
|
|
|
}
|
2020-05-15 09:46:41 +01:00
|
|
|
|
2020-05-18 13:21:35 +01:00
|
|
|
zap.L().Info("Ensured Stripe-Customer", zap.Int64("created", n))
|
2020-03-16 19:34:15 +00:00
|
|
|
|
2020-05-18 13:21:35 +01:00
|
|
|
return err
|
|
|
|
})
|
2020-03-16 19:34:15 +00:00
|
|
|
}
|