diff --git a/satellite/payments/stripe/service.go b/satellite/payments/stripe/service.go index 5bcbc8bc2ea0..c37b6068f6ee 100644 --- a/satellite/payments/stripe/service.go +++ b/satellite/payments/stripe/service.go @@ -5,6 +5,7 @@ package stripe import ( "context" + "database/sql" "encoding/json" "errors" "fmt" @@ -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, @@ -209,7 +210,7 @@ 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) @@ -217,7 +218,7 @@ func (service *Service) processCustomers(ctx context.Context, customers []Custom 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) @@ -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 } diff --git a/satellite/satellitedb/invoiceprojectrecords.go b/satellite/satellitedb/invoiceprojectrecords.go index 292440bffaa5..7ef5746055f3 100644 --- a/satellite/satellitedb/invoiceprojectrecords.go +++ b/satellite/satellitedb/invoiceprojectrecords.go @@ -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 { @@ -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. @@ -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 ?