Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exclude erroneous payments #467

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integration_tests/hodl_invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (suite *HodlInvoiceSuite) TestHodlInvoice() {
}
assert.Equal(suite.T(), int64(userFundingSats), userBalance)

invoices, err := suite.service.InvoicesFor(context.Background(), userId, common.InvoiceTypeOutgoing)
invoices, err := invoicesFor(suite.service, userId, common.InvoiceTypeOutgoing)
if err != nil {
fmt.Printf("Error when getting invoices %v\n", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/internal_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func (suite *PaymentTestSuite) TestInternalPaymentFail() {
_ = suite.createPayInvoiceReqError(bobInvoice.PayReq, suite.aliceToken)

userId := getUserIdFromToken(suite.aliceToken)
invoices, err := suite.service.InvoicesFor(context.Background(), userId, common.InvoiceTypeOutgoing)
invoices, err := invoicesFor(suite.service, userId, common.InvoiceTypeOutgoing)
if err != nil {
fmt.Printf("Error when getting invoices %v\n", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/keysend_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (suite *KeySendFailureTestSuite) TestKeysendPayment() {
}
assert.Equal(suite.T(), int64(aliceFundingSats), aliceBalance)

invoices, err := suite.service.InvoicesFor(context.Background(), userId, common.InvoiceTypeOutgoing)
invoices, err := invoicesFor(suite.service, userId, common.InvoiceTypeOutgoing)
if err != nil {
fmt.Printf("Error when getting invoices %v\n", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/payment_failure_async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (suite *PaymentTestAsyncErrorsSuite) TestExternalAsyncFailingInvoice() {
}
assert.Equal(suite.T(), int64(userFundingSats), userBalance)

invoices, err := suite.service.InvoicesFor(context.Background(), userId, common.InvoiceTypeOutgoing)
invoices, err := invoicesFor(suite.service, userId, common.InvoiceTypeOutgoing)
if err != nil {
fmt.Printf("Error when getting invoices %v\n", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/payment_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (suite *PaymentTestErrorsSuite) TestExternalFailingInvoice() {

userId := getUserIdFromToken(suite.userToken)

invoices, err := suite.service.InvoicesFor(context.Background(), userId, common.InvoiceTypeOutgoing)
invoices, err := invoicesFor(suite.service, userId, common.InvoiceTypeOutgoing)
if err != nil {
fmt.Printf("Error when getting invoices %v\n", err.Error())
}
Expand Down
18 changes: 18 additions & 0 deletions integration_tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"os"
"time"

"github.com/getAlby/lndhub.go/common"
"github.com/getAlby/lndhub.go/db"
"github.com/getAlby/lndhub.go/db/migrations"
"github.com/getAlby/lndhub.go/db/models"
"github.com/getAlby/lndhub.go/lib"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
Expand Down Expand Up @@ -126,6 +128,22 @@ func getUserIdFromToken(token string) int64 {
return int64(claims["id"].(float64))
}

// since svc.invoicesFor excludes erroneous invoices, this is used for testing
func invoicesFor(svc *service.LndhubService, userId int64, invoiceType string) ([]models.Invoice, error) {
var invoices []models.Invoice

query := svc.DB.NewSelect().Model(&invoices).Where("user_id = ?", userId)
if invoiceType != "" {
query.Where("type = ? AND state <> ?", invoiceType, common.InvoiceStateInitialized)
}
query.OrderExpr("id DESC").Limit(100)
err := query.Scan(context.Background())
if err != nil {
return nil, err
}
return invoices, nil
}

func createUsers(svc *service.LndhubService, usersToCreate int) (logins []ExpectedCreateUserResponseBody, tokens []string, err error) {
logins = []ExpectedCreateUserResponseBody{}
tokens = []string{}
Expand Down
2 changes: 1 addition & 1 deletion lib/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (svc *LndhubService) InvoicesFor(ctx context.Context, userId int64, invoice

query := svc.DB.NewSelect().Model(&invoices).Where("user_id = ?", userId)
if invoiceType != "" {
query.Where("type = ? AND state <> ?", invoiceType, common.InvoiceStateInitialized)
query.Where("type = ? AND state NOT IN(?, ?)", invoiceType, common.InvoiceStateInitialized, common.InvoiceStateError)
}
query.OrderExpr("id DESC").Limit(100)
err := query.Scan(ctx)
Expand Down
Loading