2023-08-15 17:58:50 +01:00
|
|
|
// Copyright (C) 2023 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
"storj.io/storj/satellite/payments/billing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ billing.Observer = (*InvoiceTokenPaymentObserver)(nil)
|
|
|
|
|
|
|
|
// InvoiceTokenPaymentObserver used to pay pending payments with STORJ tokens.
|
|
|
|
type InvoiceTokenPaymentObserver struct {
|
2023-09-07 17:38:24 +01:00
|
|
|
consoleDB DB
|
|
|
|
invoices payments.Invoices
|
|
|
|
freezeService *AccountFreezeService
|
2023-08-15 17:58:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInvoiceTokenPaymentObserver creates new observer instance.
|
2023-09-07 17:38:24 +01:00
|
|
|
func NewInvoiceTokenPaymentObserver(consoleDB DB, invoices payments.Invoices, freezeService *AccountFreezeService) *InvoiceTokenPaymentObserver {
|
2023-08-15 17:58:50 +01:00
|
|
|
return &InvoiceTokenPaymentObserver{
|
2023-09-07 17:38:24 +01:00
|
|
|
consoleDB: consoleDB,
|
|
|
|
invoices: invoices,
|
|
|
|
freezeService: freezeService,
|
2023-08-15 17:58:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process attempts to pay user's pending payments with tokens.
|
|
|
|
func (o *InvoiceTokenPaymentObserver) Process(ctx context.Context, transaction billing.Transaction) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
user, err := o.consoleDB.Users().Get(ctx, transaction.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.PaidTier {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-07 17:38:24 +01:00
|
|
|
err = o.invoices.AttemptPayOverdueInvoicesWithTokens(ctx, user.ID)
|
2023-08-15 17:58:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-07 17:38:24 +01:00
|
|
|
freeze, warning, err := o.freezeService.GetAll(ctx, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if freeze == nil && warning == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
invoices, err := o.invoices.List(ctx, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, inv := range invoices {
|
|
|
|
if inv.Status != payments.InvoiceStatusPaid {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if freeze != nil {
|
|
|
|
err = o.freezeService.UnfreezeUser(ctx, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if warning != nil {
|
|
|
|
err = o.freezeService.UnWarnUser(ctx, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 17:58:50 +01:00
|
|
|
return nil
|
|
|
|
}
|