mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-18 21:25:06 +01:00
fix: exclude erroneous payments (#467)
* fix: exclude errorneous payments * fix: add invoicesfor in tests --------- Co-authored-by: im-adithya <imadithyavardhan@gmail.com>
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
@@ -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{}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user