Skip to content

Commit

Permalink
use filters in invoices List
Browse files Browse the repository at this point in the history
  • Loading branch information
coddmeistr committed Dec 25, 2024
1 parent 22cee0d commit a84e74c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
15 changes: 12 additions & 3 deletions pkg/graph/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type InvoicesController interface {
Update(ctx context.Context, tx *Invoice) (*Invoice, error)
Replace(ctx context.Context, tx *Invoice) (*Invoice, error)
Patch(ctx context.Context, id string, patch map[string]interface{}) error
List(ctx context.Context, account string) ([]*Invoice, error)
List(ctx context.Context, account string, filter ...map[string]interface{}) ([]*Invoice, error)
Transfer(ctx context.Context, uuid string, account string, resCurr *pb.Currency) (err error)
}

Expand Down Expand Up @@ -322,7 +322,7 @@ const listInvoices = `
RETURN MERGE(doc, {uuid: doc._key})
`

func (ctrl *invoicesController) List(ctx context.Context, account string) ([]*Invoice, error) {
func (ctrl *invoicesController) List(ctx context.Context, account string, filter ...map[string]interface{}) ([]*Invoice, error) {
log := ctrl.log.Named("List")

list := make([]*Invoice, 0)
Expand All @@ -331,9 +331,18 @@ func (ctrl *invoicesController) List(ctx context.Context, account string) ([]*In
}
var filters = ""
if account != "" {
filters = ` FILTER doc.account == @account`
filters += ` FILTER doc.account == @account`
bindVars["account"] = account
}
if len(filter) > 0 {
i := 1
for key, val := range filter[0] {
keyVal := fmt.Sprintf("filterField%d", i)
filters += fmt.Sprintf(" FILTER doc.%s == @%s", key, keyVal)
bindVars[keyVal] = val
i++
}
}
cur, err := ctrl.col.Database().Query(ctx, fmt.Sprintf(listInvoices, filters), bindVars)
if err != nil {
log.Error("Failed to list invoices", zap.Error(err))
Expand Down
10 changes: 4 additions & 6 deletions pkg/nocloud/payments/whmcs_gateway/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ const invoiceIdField = "whmcs_invoice_id"
const userIdField = "whmcs_id"

func (g *WhmcsGateway) getInvoiceByWhmcsId(whmcsInvoiceId int) (*pb.Invoice, error) {
invoices, err := g.invMan.InvoicesController().List(context.Background(), "")
invoices, err := g.invMan.InvoicesController().List(context.Background(), "", map[string]interface{}{
fmt.Sprintf("meta.%s", invoiceIdField): whmcsInvoiceId,
})
if err != nil {
return nil, err
}
for _, inv := range invoices {
id, ok := inv.GetMeta()[invoiceIdField]
if !ok {
continue
}
if int(id.GetNumberValue()) == whmcsInvoiceId {
if inv != nil && int(inv.GetMeta()[invoiceIdField].GetNumberValue()) == whmcsInvoiceId {
return inv.Invoice, nil
}
}
Expand Down

0 comments on commit a84e74c

Please sign in to comment.