Skip to content

Commit

Permalink
Merge branch 'master' into feature-internal-server-error-middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 authored Dec 16, 2024
2 parents 5d7170f + feaaa94 commit 35d9d99
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
33 changes: 19 additions & 14 deletions handlers/v2_payments_cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
)

func InitV2PaymentsCron() {
log.Println("Pending Invoice Cron Job Started")
paymentHistories := db.DB.GetPendingPaymentHistory()
for _, payment := range paymentHistories {
tag := payment.Tag
tagResult := GetInvoiceStatusByTag(tag)

bounty := db.DB.GetBounty(payment.BountyId)

if bounty.ID > 0 {
tag := payment.Tag
tagResult := GetInvoiceStatusByTag(tag)

if tagResult.Status == db.PaymentComplete {
db.DB.SetPaymentAsComplete(tag)
Expand All @@ -37,32 +37,37 @@ func InitV2PaymentsCron() {
bounty.CompletionDate = &now

db.DB.UpdateBountyPaymentStatuses(bounty)

} 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 {
log.Println("Payment Status From V2 BOT IS Pending", payment)
if payment.PaymentStatus == db.PaymentPending {
log.Println("Payment Status From DB IS Pending", payment)
created := utils.ConvertTimeToTimestamp(payment.Created.String())

now := time.Now()
daysDiff := utils.GetDateDaysDifference(int64(created), &now)

log.Println("Payment Date Difference Is", daysDiff)

if daysDiff >= 7 {

log.Println("Payment Date Difference Is Greater Or Equals 7 Days", payment)

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)
}
}
}
}
} 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 {
log.Println("Payment Status From V2 BOT IS Unknown", payment, tagResult)
}
}
}
}
Expand Down Expand Up @@ -99,7 +104,7 @@ func GetInvoiceStatusByTag(tag string) db.V2TagRes {
err = json.Unmarshal(body, &tagRes)

if err != nil {
log.Printf("Could not unmarshall get tag result: %s", err)
log.Printf("Could not unmarshal get tag result: %s", err)
}

resultLength := len(tagRes)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func main() {

func runCron() {
c := cron.New()
c.AddFunc("@every 00h30m00s", handlers.InitV2PaymentsCron)
c.AddFunc("@every 0h30m0s", handlers.InitV2PaymentsCron)
c.Start()
}

Expand Down
2 changes: 1 addition & 1 deletion routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func NewRouter() *http.Server {
r.Get("/poll/{challenge}", db.Poll)
r.Post("/save", db.PostSave)
r.Get("/save/{key}", db.PollSave)
r.Get("/websocket", handlers.HandleWebSocket)
r.Get("/migrate_bounties", handlers.MigrateBounties)
r.Get("/test/internal-server-error", testInternalServerError)
r.Get("/websocket", handlers.HandleWebSocket)
})

r.Group(func(r chi.Router) {
Expand Down
2 changes: 0 additions & 2 deletions websocket/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ func (pool *Pool) Start() {
} else {
fmt.Println("Websocket pool client save error")
}
break
case client := <-pool.Unregister:
pool.Clients[client.Host].Client.Conn.WriteJSON(Message{Type: 1, Body: "User Disconnected..."})
delete(pool.Clients, client.Host)
fmt.Println("Size of Connection Pool: ", len(pool.Clients))
break
case message := <-pool.Broadcast:
fmt.Println("Sending message to all clients in Pool")
for client, _ := range pool.Clients {
Expand Down
1 change: 1 addition & 0 deletions websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func ServeWs(pool *Pool, w http.ResponseWriter, r *http.Request) {

conn, err := Upgrade(w, r)
if err != nil {
fmt.Println("Error in ServeWs", err)
fmt.Fprintf(w, "%+v\n", err)
}

Expand Down

0 comments on commit 35d9d99

Please sign in to comment.