add field for customer whether they are pro or not

This commit is contained in:
talksik
2025-10-28 17:05:39 -07:00
parent 99c0dc5a41
commit b53f418607
+81
View File
@@ -34,6 +34,18 @@ type MetricResponse struct {
} `json:"metrics"`
}
type SubscriptionResponse struct {
Items []struct {
Status string `json:"status"`
GivesAccess bool `json:"gives_access"`
Entitlements struct {
Items []struct {
LookupKey string `json:"lookup_key"`
} `json:"items"`
} `json:"entitlements"`
} `json:"items"`
}
func getRevenue() string {
client := &http.Client{}
@@ -120,6 +132,58 @@ func getRevenue() string {
// return result.String()
}
func hasProSubscription(customerEmail string) bool {
client := &http.Client{}
url := fmt.Sprintf("https://api.revenuecat.com/v2/projects/proj5b2f7356/customers/%s/subscriptions", customerEmail)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
logrus.Errorf("Error creating subscription request for %s: %v", customerEmail, err)
return false
}
// Add authorization header
req.Header.Add("Authorization", "Bearer sk_wxmjNCLtnDSUPmSXlbEWyuqkpTVTi")
response, err := client.Do(req)
if err != nil {
logrus.Errorf("Error fetching subscriptions for %s: %v", customerEmail, err)
return false
}
defer response.Body.Close()
// If customer not found or no subscriptions, return false
if response.StatusCode != 200 {
return false
}
body, err := io.ReadAll(response.Body)
if err != nil {
logrus.Errorf("Error reading subscription response for %s: %v", customerEmail, err)
return false
}
var subResp SubscriptionResponse
err = json.Unmarshal(body, &subResp)
if err != nil {
logrus.Errorf("Error parsing subscription JSON for %s: %v", customerEmail, err)
return false
}
// Check if any subscription is active and has Pro entitlement
for _, sub := range subResp.Items {
if sub.Status == "active" && sub.GivesAccess {
for _, ent := range sub.Entitlements.Items {
if ent.LookupKey == "Pro" {
return true
}
}
}
}
return false
}
var (
titleStyle = lipgloss.NewStyle().
Bold(true).
@@ -193,6 +257,7 @@ type HumanData struct {
DisplayName string
JoinedAt time.Time
KeypadConfiguration string
HasProSubscription bool
}
type model struct {
@@ -358,6 +423,17 @@ func (m model) renderDetails() string {
joinedFormatted := human.JoinedAt.Format("Jan 2, 2006 at 3:04 PM")
b.WriteString(detailValueStyle.Render(fmt.Sprintf("%s (%s)", joinedFormatted, timeAgo)) + "\n\n")
// Pro Subscription Status
b.WriteString(detailLabelStyle.Render("Pro Subscription: "))
if human.HasProSubscription {
proStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("#00FF00")).
Bold(true)
b.WriteString(proStyle.Render("✓ Active") + "\n\n")
} else {
b.WriteString(dimItemStyle.Render("✗ None") + "\n\n")
}
// Keypad Configuration
if human.KeypadConfiguration != "" {
b.WriteString(detailLabelStyle.Render("Keypad Configuration:") + "\n")
@@ -456,11 +532,16 @@ func getHumansData() []*HumanData {
keypadConfiguration = keypad.GetYamlConfig()
}
}
// Check if customer has pro subscription
hasPro := hasProSubscription(human.GetEmail())
humansData = append(humansData, &HumanData{
Email: human.GetEmail(),
DisplayName: human.GetDisplayName(),
JoinedAt: human.GetJoinedAt().AsTime(),
KeypadConfiguration: keypadConfiguration,
HasProSubscription: hasPro,
})
}