Skip to content

Commit 89365f7

Browse files
committed
fix: resolve comments
1 parent 5f2bdd3 commit 89365f7

1 file changed

Lines changed: 24 additions & 26 deletions

File tree

util/billing.go

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,24 @@ import (
1313
"github.com/olivere/elastic/v7"
1414
)
1515

16-
var TierValidity int64
16+
var TimeValidity int64
17+
18+
type ArcUsage struct {
19+
ArcID string `json:"arc_id"`
20+
Timestamp int64 `json:"timestamp"`
21+
SubscriptionID string `json:"subscription_id"`
22+
Quantity int `json:"quantity"`
23+
Email string `json:"email"`
24+
}
25+
26+
type ArcUsageResponse struct {
27+
Accepted bool `json:"accepted"`
28+
FailureReason string `json:"failure_reason"`
29+
ErrorMsg string `json:"error_msg"`
30+
WarningMsg string `json:"warning_msg"`
31+
StatusCode int `json:"status_code"`
32+
TimeValidity int64 `json:"time_validity"`
33+
}
1734

1835
const (
1936
envEsURL = "ES_CLUSTER_URL"
@@ -25,14 +42,12 @@ const (
2542
// Middleware function, which will be called for each request
2643
func BillingMiddleware(next http.Handler) http.Handler {
2744
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
28-
currentTime := time.Now().Unix()
2945
// Valid plan
30-
if TierValidity > currentTime {
31-
remainingTime := TierValidity - currentTime
46+
if TimeValidity > 0 {
3247
// Check if remaining time is less than 24 hrs
33-
if remainingTime <= 3600*24 {
34-
// Print waring message if remaining time is less than 24 hrs
35-
log.Println("warning: payment required. arc will start sending out error messages in next", remainingTime/3600, "hours")
48+
if TimeValidity <= 3600*24 {
49+
// Print warning message if remaining time is less than 24 hrs
50+
log.Println("warning: payment required. arc will start sending out error messages in next", TimeValidity/3600, "hours")
3651
}
3752
next.ServeHTTP(w, r)
3853
} else {
@@ -42,23 +57,6 @@ func BillingMiddleware(next http.Handler) http.Handler {
4257
})
4358
}
4459

45-
type ArcUsage struct {
46-
ArcID string `json:"arc_id"`
47-
Timestamp int64 `json:"timestamp"`
48-
SubscriptionID string `json:"subscription_id"`
49-
Quantity int `json:"quantity"`
50-
Email string `json:"email"`
51-
}
52-
53-
type ArcUsageResponse struct {
54-
Accepted bool `json:"accepted"`
55-
FailureReason string `json:"failure_reason"`
56-
ErrorMsg string `json:"error_msg"`
57-
WarningMsg string `json:"warning_msg"`
58-
StatusCode int `json:"status_code"`
59-
TierValidity int64 `json:"tier_validity"`
60-
}
61-
6260
func ReportUsageRequest(arcUsage ArcUsage) (ArcUsageResponse, error) {
6361
response := ArcUsageResponse{}
6462
url := "https://accapi.appbase.io/arc/" + arcUsage.ArcID + "/report_usage"
@@ -73,7 +71,7 @@ func ReportUsageRequest(arcUsage ArcUsage) (ArcUsageResponse, error) {
7371

7472
res, err := http.DefaultClient.Do(req)
7573
if err != nil {
76-
log.Println("error while sending reequest: ", err)
74+
log.Println("error while sending request: ", err)
7775
return response, err
7876
}
7977
defer res.Body.Close()
@@ -123,7 +121,7 @@ func ReportUsage() {
123121
log.Println("please contact support. Usage not getting reported: ", err)
124122
}
125123

126-
TierValidity = response.TierValidity
124+
TimeValidity = response.TimeValidity
127125
if response.WarningMsg != "" {
128126
log.Println("warning:", response.WarningMsg)
129127
}

0 commit comments

Comments
 (0)