Skip to content

Commit

Permalink
Merge pull request #1955 from slntopp/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
coddmeistr authored Dec 26, 2024
2 parents 1f54ba4 + e9e2b4a commit deb6bf3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
github.com/rabbitmq/amqp091-go v1.9.0
github.com/rs/cors v1.10.1
github.com/slntopp/nocloud-proto v0.0.0-20241224160412-f75ea6359750
github.com/slntopp/nocloud-proto v0.0.0-20241226123624-36f115ce6bf0
github.com/spf13/viper v1.18.2
github.com/stoewer/go-strcase v1.3.0
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/slntopp/nocloud-proto v0.0.0-20241224160412-f75ea6359750 h1:QZszSPZ+JujuS5QBLGVETnRepEaFfNaqkuGbMrZjPwU=
github.com/slntopp/nocloud-proto v0.0.0-20241224160412-f75ea6359750/go.mod h1:qPbslPB2J9Q7qm6H9Jaqf/Ysf61YlPL0DUFhIdAEikI=
github.com/slntopp/nocloud-proto v0.0.0-20241226123624-36f115ce6bf0 h1:G0cFfUU8f2gR3Tdeq0TIdf+R/ZW5Woup/P7kOAlOXss=
github.com/slntopp/nocloud-proto v0.0.0-20241226123624-36f115ce6bf0/go.mod h1:qPbslPB2J9Q7qm6H9Jaqf/Ysf61YlPL0DUFhIdAEikI=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
Expand Down
19 changes: 10 additions & 9 deletions pkg/billing/cron_whmcs_invoices_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,24 @@ func (s *BillingServiceServer) WhmcsInvoicesSyncerCronJob(ctx context.Context, l
if _, ok := whmcsIdToInvoice[int(whmcsInvoice.Id)]; ok {
continue
}
// Do not create invoice if it is younger than half a day (preventing accidental duplicate)
dateCreated, err := time.Parse(time.DateTime, whmcsInvoice.CreatedAt)
if err != nil {
logI.Error("Failed to parse invoice created time", zap.Error(err))
continue
}
created := dateCreated.Unix()
// Do not create invoice if it is younger than half a day (preventing accidental duplicate) and if too old
const secondsInDay = 86400
if created > 0 && (now-created < secondsInDay/2) {
logI.Info("Invoice is not presented in Nocloud, but it is too young. Skip")
const tooOldDate = 1577836800 // GMT: Wednesday, 1 January 2020 г., 0:00:00
dateCreated, parseCreatedErr := time.Parse(time.DateTime, whmcsInvoice.CreatedAt)
if parseCreatedErr == nil && dateCreated.Unix() > 0 && (now-dateCreated.Unix() < secondsInDay/2 || dateCreated.Unix() < tooOldDate) {
continue
}
inv, err := s.whmcsGateway.GetInvoice(ctx, int(whmcsInvoice.Id))
if err != nil {
logI.Error("Failed to get body of whmcs invoice", zap.Error(err))
continue
}
if parseCreatedErr != nil || dateCreated.Unix() <= 0 {
dateCreated, err = time.Parse(time.DateOnly, inv.Date)
if err != nil || dateCreated.Unix() <= 0 || (now-dateCreated.Unix() < secondsInDay/2 || dateCreated.Unix() < tooOldDate) {
continue
}
}
if err = s.whmcsGateway.CreateFromWhmcsInvoice(ctx, log, inv); err != nil {
logI.Error("Failed to create whmcs invoice", zap.Error(err))
continue
Expand Down
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
4 changes: 2 additions & 2 deletions pkg/nocloud/payments/whmcs_gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ func (g *WhmcsGateway) UpdateInvoice(ctx context.Context, inv *pb.Invoice) error
}

id, ok := inv.GetMeta()[invoiceIdField]
if !ok {
return g.CreateInvoice(ctx, inv)
if !ok || int(id.GetNumberValue()) <= 0 {
return fmt.Errorf("no whmcs_invoice_id or zero value")
}

body := g.buildUpdateInvoiceQueryBase(int(id.GetNumberValue()))
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 deb6bf3

Please sign in to comment.