Skip to content

Commit

Permalink
removed invoice from invoice list if th invoice is expired and not se…
Browse files Browse the repository at this point in the history
…ttled
  • Loading branch information
elraphty committed Jun 4, 2024
1 parent cf028ce commit a3b3517
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 12 deletions.
6 changes: 6 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,12 @@ func (db database) AddInvoice(invoice NewInvoiceList) NewInvoiceList {
return invoice
}

func (db database) DeleteInvoice(payment_request string) NewInvoiceList {
ms := NewInvoiceList{}
db.db.Model(&NewInvoiceList{}).Where("payment_request = ?", payment_request).Delete(&ms)
return ms
}

func (db database) AddUserInvoiceData(userData UserInvoiceData) UserInvoiceData {
db.db.Create(&userData)
return userData
Expand Down
1 change: 1 addition & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type Database interface {
GetWorkspaceInvoicesCount(workspace_uuid string) int64
UpdateInvoice(payment_request string) NewInvoiceList
AddInvoice(invoice NewInvoiceList) NewInvoiceList
DeleteInvoice(payment_request string) NewInvoiceList
AddUserInvoiceData(userData UserInvoiceData) UserInvoiceData
GetUserInvoiceData(payment_request string) UserInvoiceData
DeleteUserInvoiceData(payment_request string) UserInvoiceData
Expand Down
1 change: 1 addition & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ type Organization struct {
Mission string `json:"mission"`
Tactics string `json:"tactics"`
SchematicUrl string `json:"schematic_url"`
SchematicImg string `json:"schematic_img"`
}

type Workspace struct {
Expand Down
23 changes: 14 additions & 9 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,11 +895,6 @@ func (h *bountyHandler) PollInvoice(w http.ResponseWriter, r *http.Request) {
req.Header.Set("Content-Type", "application/json")
res, _ := h.httpClient.Do(req)

if err != nil {
log.Printf("[bounty] Request Failed: %s", err)
return
}

defer res.Body.Close()

body, _ := io.ReadAll(res.Body)
Expand All @@ -909,11 +904,15 @@ func (h *bountyHandler) PollInvoice(w http.ResponseWriter, r *http.Request) {
keysendRes := db.KeysendSuccess{}
err = json.Unmarshal(body, &keysendRes)

bounty, err := h.db.GetBountyByCreated(uint(invData.Created))
if err != nil {
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode("Could not decode keysend response")
return
}

bounty, err := h.db.GetBountyByCreated(uint(invData.Created))
if err == nil {
now := time.Now()

bounty.Paid = true
bounty.PaidDate = &now
bounty.Completed = true
Expand All @@ -925,13 +924,19 @@ func (h *bountyHandler) PollInvoice(w http.ResponseWriter, r *http.Request) {
// Unmarshal result
keysendError := db.KeysendError{}
err = json.Unmarshal(body, &keysendError)
log.Printf("[bounty] Keysend Payment to %s Failed, with Error: %s", invData.UserPubkey, keysendError.Error)
log.Printf("[bounty] Keysend Payment to %s Failed, with Error: %s", invData.UserPubkey, err)
}
}
// Update the invoice status
h.db.UpdateInvoice(paymentRequest)
}

} else {
// Cheeck if time has expired
isInvoiceExpired := utils.GetInvoiceExpired(paymentRequest)
// If the invoice has expired and it is not paid delete from the DB
if isInvoiceExpired {
h.db.DeleteInvoice(paymentRequest)
}
}

w.WriteHeader(http.StatusOK)
Expand Down
12 changes: 9 additions & 3 deletions handlers/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,8 @@ func (oh *workspaceHandler) PollBudgetInvoices(w http.ResponseWriter, r *http.Re
return
}

orgInvoices := oh.db.GetWorkspaceInvoices(uuid)

for _, inv := range orgInvoices {
workInvoices := oh.db.GetWorkspaceInvoices(uuid)
for _, inv := range workInvoices {
invoiceRes, invoiceErr := oh.getLightningInvoice(inv.PaymentRequest)

if invoiceErr.Error != "" {
Expand All @@ -669,6 +668,13 @@ func (oh *workspaceHandler) PollBudgetInvoices(w http.ResponseWriter, r *http.Re
// Update the invoice status
oh.db.UpdateInvoice(inv.PaymentRequest)
}
} else {
// Cheeck if time has expired
isInvoiceExpired := utils.GetInvoiceExpired(inv.PaymentRequest)
// If the invoice has expired and it is not paid delete from the DB
if isInvoiceExpired {
oh.db.DeleteInvoice(inv.PaymentRequest)
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions mocks/Database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ func GetInvoiceAmount(paymentRequest string) uint {
return amount
}

func GetInvoiceExpired(paymentRequest string) bool {
decodedInvoice, err := decodepay.Decodepay(paymentRequest)
if err != nil {
fmt.Println("Could not Decode Invoice", err)
return false
}

timeInUnix := time.Now().Unix()
expiryDate := decodedInvoice.CreatedAt + decodedInvoice.Expiry

if timeInUnix > int64(expiryDate) {
return true
} else {
return false
}
}

func GetDateDaysDifference(createdDate int64, paidDate *time.Time) int64 {
firstDate := time.Unix(createdDate, 0)
difference := paidDate.Sub(*&firstDate)
Expand Down
6 changes: 6 additions & 0 deletions utils/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ func TestGetInvoiceAmount(t *testing.T) {
amount2 := GetInvoiceAmount(invalidInvoice)
assert.Equal(t, uint(0), amount2)
}

func TestGetInvoiceExpired(t *testing.T) {
expiredInvoice := "lnbcrt100u1pnr5gtzpp5r7ew6nzqd9y9w5ktsspftnckxdn3te0y04n9mw7c6hkkrznh4pgsdqhgf6kgem9wssyjmnkda5kxegcqzpgxqyz5vqsp5mc09mpl4l3rllnfl3y902yxa29flke8r4ertqswdcrk766z5nq4q9qyyssq7wteenxtwlxatsd8dqdncqnn6u23jmcpe0d7ne6dcpafwlx9ckr3dp6y4p7sl4j3pq6l93g6vc4w8z04ry9yzwjv6cggm06eecad9psp9dh6u5"
isInvoiceExpired := GetInvoiceExpired(expiredInvoice)
assert.Equal(t, true, isInvoiceExpired)
}

0 comments on commit a3b3517

Please sign in to comment.