storj/satellite/console/observerpayinvoicewithtokens.go
Wilfred Asomani 6308da2cc0 satellite/{payment,console,analytics} extend freeze functionality for violation freeze
This change extends the account freeze functionality account for
violation freezes as well.
Also, debug level logs in the freeze chore have been changed to info.
It adds an analytics event for when an invoice is found that belongs to
a user frozen for violation.
And finally adds whether a user is frozen for violation to the
/account/freezestatus response.

Issue: https://github.com/storj/storj-private/issues/386

Change-Id: Id8e40282dc8fd8f242da52791ab8ddbbef3da2bc
2023-10-10 18:39:29 +00:00

83 lines
1.9 KiB
Go

// 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 {
consoleDB DB
invoices payments.Invoices
freezeService *AccountFreezeService
}
// NewInvoiceTokenPaymentObserver creates new observer instance.
func NewInvoiceTokenPaymentObserver(consoleDB DB, invoices payments.Invoices, freezeService *AccountFreezeService) *InvoiceTokenPaymentObserver {
return &InvoiceTokenPaymentObserver{
consoleDB: consoleDB,
invoices: invoices,
freezeService: freezeService,
}
}
// 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
}
err = o.invoices.AttemptPayOverdueInvoicesWithTokens(ctx, user.ID)
if err != nil {
return err
}
freezes, err := o.freezeService.GetAll(ctx, user.ID)
if err != nil {
return err
}
if freezes.BillingFreeze == nil && freezes.BillingWarning == 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 freezes.BillingFreeze != nil {
err = o.freezeService.BillingUnfreezeUser(ctx, user.ID)
if err != nil {
return err
}
} else if freezes.BillingWarning != nil {
err = o.freezeService.BillingUnWarnUser(ctx, user.ID)
if err != nil {
return err
}
}
return nil
}