Skip to content

Commit

Permalink
do not create young invoices
Browse files Browse the repository at this point in the history
  • Loading branch information
coddmeistr committed Dec 25, 2024
1 parent 2b3399e commit 3dbd0fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
19 changes: 17 additions & 2 deletions pkg/billing/cron_whmcs_invoices_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/structpb"
"reflect"
"time"
)

func (s *BillingServiceServer) WhmcsInvoicesSyncerCronJob(ctx context.Context, log *zap.Logger) {
log = log.Named("WhmcsInvoicesSyncerCronJob")
log.Info("Starting WHMCS Invoices syncer cron job")
now := time.Now().Unix()

ncInvoices, err := s.invoices.List(ctx, "")
if err != nil {
Expand Down Expand Up @@ -75,16 +77,29 @@ func (s *BillingServiceServer) WhmcsInvoicesSyncerCronJob(ctx context.Context, l
log.Warn("Whmcs invoice id is zero value")
continue
}
logI := log.With(zap.Int("whmcs_id", int(whmcsInvoice.Id)))
if _, ok := whmcsIdToInvoice[int(whmcsInvoice.Id)]; ok {
continue
}
// Do not create invoice if it is younger than 1 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()
const secondsInDay = 86400
if created > 0 && (now-created < secondsInDay) {
logI.Info("Invoice is not presented in Nocloud, but it is too young. Skip")
continue
}
inv, err := s.whmcsGateway.GetInvoice(ctx, int(whmcsInvoice.Id))
if err != nil {
log.Error("Failed to get body of whmcs invoice", zap.Error(err))
logI.Error("Failed to get body of whmcs invoice", zap.Error(err))
continue
}
if err = s.whmcsGateway.CreateFromWhmcsInvoice(ctx, log, inv); err != nil {
log.Error("Failed to create whmcs invoice", zap.Error(err))
logI.Error("Failed to create whmcs invoice", zap.Error(err))
continue
}
createdCount++
Expand Down
3 changes: 2 additions & 1 deletion pkg/nocloud/payments/whmcs_gateway/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ type GetInvoicesResponse struct {
}

type InvoiceInList struct {
Id IntOrString `json:"id"`
Id IntOrString `json:"id"`
CreatedAt string `json:"created_at"`
}

type InvoicesHolder struct {
Expand Down

0 comments on commit 3dbd0fe

Please sign in to comment.