Skip to content

Commit

Permalink
satellite/payments: treat deleted users as inactive
Browse files Browse the repository at this point in the history
Change-Id: Ic82a97ac5ea0e8bbc0b4ae52191fbdd9126dfbc2
  • Loading branch information
jtolio authored and Storj Robot committed Dec 7, 2023
1 parent af1c2cb commit 35d8455
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
16 changes: 10 additions & 6 deletions satellite/payments/stripe/service.go
Expand Up @@ -5,6 +5,7 @@ package stripe

import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -183,19 +184,19 @@ func (service *Service) processCustomers(ctx context.Context, customers []Custom
var recordsToAggregate []CreateProjectRecord
for _, customer := range customers {
if inactive, err := service.isUserInactive(ctx, customer.UserID); err != nil {
return 0, err
return 0, Error.New("unable to determine if user is inactive: %w", err)
} else if inactive {
continue
}

projects, err := service.projectsDB.GetOwn(ctx, customer.UserID)
if err != nil {
return 0, err
return 0, Error.New("unable to get own projects: %w", err)
}

records, err := service.createProjectRecords(ctx, customer.ID, projects, start, end)
if err != nil {
return 0, err
return 0, Error.New("unable to create project records: %w", err)
}

// We generate 3 invoice items for each user project which means,
Expand All @@ -209,15 +210,15 @@ func (service *Service) processCustomers(ctx context.Context, customers []Custom

err := service.db.ProjectRecords().Create(ctx, regularRecords, start, end)
if err != nil {
return 0, err
return 0, Error.New("failed to create regular project records: %w", err)
}

recordsCount := len(regularRecords)

if shouldAggregate {
err = service.db.ProjectRecords().CreateToBeAggregated(ctx, recordsToAggregate, start, end)
if err != nil {
return 0, err
return 0, Error.New("failed to create aggregated project records: %w", err)
}

recordsCount += len(recordsToAggregate)
Expand Down Expand Up @@ -1611,7 +1612,10 @@ func (service *Service) payInvoicesWithTokenBalance(ctx context.Context, cusID s
func (service *Service) isUserInactive(ctx context.Context, userID uuid.UUID) (bool, error) {
user, err := service.usersDB.Get(ctx, userID)
if err != nil {
return false, err
if errors.Is(err, sql.ErrNoRows) {
return true, nil
}
return false, Error.New("unable to look up user %s: %w", userID, err)
}
return user.Status != console.Active, nil
}
Expand Down
14 changes: 7 additions & 7 deletions satellite/satellitedb/invoiceprojectrecords.go
Expand Up @@ -59,7 +59,7 @@ func (db *invoiceProjectRecords) CreateToBeAggregated(ctx context.Context, recor
}

func (db *invoiceProjectRecords) createWithState(ctx context.Context, records []stripe.CreateProjectRecord, state invoiceProjectRecordState, start, end time.Time) error {
return db.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
return Error.Wrap(db.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
for _, record := range records {
id, err := uuid.New()
if err != nil {
Expand All @@ -79,12 +79,12 @@ func (db *invoiceProjectRecords) createWithState(ctx context.Context, records []
},
)
if err != nil {
return err
return Error.Wrap(err)
}
}

return nil
})
}))
}

// Check checks if invoice project record for specified project and billing period exists.
Expand Down Expand Up @@ -160,11 +160,11 @@ func (db *invoiceProjectRecords) ListUnapplied(ctx context.Context, cursor uuid.

func (db *invoiceProjectRecords) list(ctx context.Context, cursor uuid.UUID, limit, state int, start, end time.Time) (page stripe.ProjectRecordsPage, err error) {
err = withRows(db.db.QueryContext(ctx, db.db.Rebind(`
SELECT
SELECT
id, project_id, storage, egress, segments, period_start, period_end, state
FROM
stripecoinpayments_invoice_project_records
WHERE
FROM
stripecoinpayments_invoice_project_records
WHERE
id > ? AND period_start = ? AND period_end = ? AND state = ?
ORDER BY id
LIMIT ?
Expand Down

0 comments on commit 35d8455

Please sign in to comment.