From bea39547ff20a07bf6e04d8a35815ae6fa0a2357 Mon Sep 17 00:00:00 2001 From: elraphty Date: Wed, 20 Nov 2024 10:39:50 +0100 Subject: [PATCH] feat: reverse pending bounties after 7 days --- handlers/bounty.go | 15 +++++++++++++++ handlers/v2_payments_cron.go | 35 ++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/handlers/bounty.go b/handlers/bounty.go index 4d3b6d028..8e1d4eb2e 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -1021,6 +1021,21 @@ func (h *bountyHandler) UpdateBountyPaymentStatus(w http.ResponseWriter, r *http w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(msg) return + } else if tagResult.Status == db.PaymentPending { + if payment.PaymentStatus == db.PaymentPending { + created := utils.ConvertTimeToTimestamp(payment.Created.String()) + + now := time.Now() + daysDiff := utils.GetDateDaysDifference(int64(created), &now) + + if daysDiff >= 7 { + + err = h.db.ProcessReversePayments(payment.ID) + if err != nil { + log.Printf("Could not reverse bounty payment after 7 days : Bounty ID - %d, Payment ID - %d, Error - %s", bounty.ID, payment.ID, err) + } + } + } } } diff --git a/handlers/v2_payments_cron.go b/handlers/v2_payments_cron.go index 66f830a70..ae2d6be81 100644 --- a/handlers/v2_payments_cron.go +++ b/handlers/v2_payments_cron.go @@ -10,6 +10,7 @@ import ( "github.com/stakwork/sphinx-tribes/config" "github.com/stakwork/sphinx-tribes/db" + "github.com/stakwork/sphinx-tribes/utils" ) func InitV2PaymentsCron() { @@ -18,12 +19,13 @@ func InitV2PaymentsCron() { tag := payment.Tag tagResult := GetInvoiceStatusByTag(tag) - if tagResult.Status == db.PaymentComplete { - db.DB.SetPaymentAsComplete(tag) + bounty := db.DB.GetBounty(payment.BountyId) - bounty := db.DB.GetBounty(payment.BountyId) + if bounty.ID > 0 { + + if tagResult.Status == db.PaymentComplete { + db.DB.SetPaymentAsComplete(tag) - if bounty.ID > 0 { now := time.Now() bounty.PaymentPending = false @@ -35,17 +37,32 @@ func InitV2PaymentsCron() { bounty.CompletionDate = &now db.DB.UpdateBountyPaymentStatuses(bounty) - } - } else if tagResult.Status == db.PaymentFailed { - // Handle failed payments - bounty := db.DB.GetBounty(payment.BountyId) - if bounty.ID > 0 { + } else if tagResult.Status == db.PaymentFailed { + // Handle failed payments + err := db.DB.ProcessReversePayments(payment.ID) if err != nil { log.Printf("Could not reverse bounty payment : Bounty ID - %d, Payment ID - %d, Error - %s", bounty.ID, payment.ID, err) } + + } else if tagResult.Status == db.PaymentPending { + if payment.PaymentStatus == db.PaymentPending { + created := utils.ConvertTimeToTimestamp(payment.Created.String()) + + now := time.Now() + daysDiff := utils.GetDateDaysDifference(int64(created), &now) + + if daysDiff >= 7 { + + err := db.DB.ProcessReversePayments(payment.ID) + if err != nil { + log.Printf("Could not reverse bounty payment after 7 days : Bounty ID - %d, Payment ID - %d, Error - %s", bounty.ID, payment.ID, err) + } + } + } } + } } }