entities.SubscriptionName.Limit   B
last analyzed

Complexity

Conditions 8

Size

Total Lines 16
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 16
dl 0
loc 16
rs 7.3333
c 0
b 0
f 0
nop 0
1
package entities
2
3
import (
4
	"time"
5
6
	"github.com/google/uuid"
7
)
8
9
// UserID is the ID of a user
10
type UserID string
11
12
// String returns the string representation of a UserID
13
func (id UserID) String() string {
14
	return string(id)
15
}
16
17
// SubscriptionName is the name of the subscription
18
type SubscriptionName string
19
20
// Limit returns the limit of a subscription
21
func (subscription SubscriptionName) Limit() uint {
22
	switch subscription {
23
	case SubscriptionNameProMonthly, SubscriptionNameProYearly, SubscriptionNameProLifetime:
24
		return 5000
25
	case SubscriptionNameUltraMonthly, SubscriptionNameUltraYearly:
26
		return 10_000
27
	case SubscriptionName20KMonthly, SubscriptionName20KYearly:
28
		return 20_000
29
	case SubscriptionName50KMonthly:
30
		return 50_000
31
	case SubscriptionName100KMonthly:
32
		return 100_000
33
	case SubscriptionName200KMonthly:
34
		return 200_000
35
	default:
36
		return 200
37
	}
38
}
39
40
// SubscriptionNameFree represents a free subscription
41
const SubscriptionNameFree = SubscriptionName("free")
42
43
// SubscriptionNameProMonthly represents a monthly pro subscription
44
const SubscriptionNameProMonthly = SubscriptionName("pro-monthly")
45
46
// SubscriptionNameProYearly represents a yearly pro subscription
47
const SubscriptionNameProYearly = SubscriptionName("pro-yearly")
48
49
// SubscriptionNameUltraMonthly represents a monthly ultra subscription
50
const SubscriptionNameUltraMonthly = SubscriptionName("ultra-monthly")
51
52
// SubscriptionNameUltraYearly represents a yearly ultra subscription
53
const SubscriptionNameUltraYearly = SubscriptionName("ultra-yearly")
54
55
// SubscriptionNameProLifetime represents a pro lifetime subscription
56
const SubscriptionNameProLifetime = SubscriptionName("pro-lifetime")
57
58
// SubscriptionName20KMonthly represents a monthly 20k subscription
59
const SubscriptionName20KMonthly = SubscriptionName("20k-monthly")
60
61
// SubscriptionName100KMonthly represents a monthly 100k subscription
62
const SubscriptionName100KMonthly = SubscriptionName("100k-monthly")
63
64
// SubscriptionName50KMonthly represents a monthly 50k subscription
65
const SubscriptionName50KMonthly = SubscriptionName("50k-monthly")
66
67
// SubscriptionName200KMonthly represents a monthly 200k subscription
68
const SubscriptionName200KMonthly = SubscriptionName("200k-monthly")
69
70
// SubscriptionName20KYearly represents a yearly 20k subscription
71
const SubscriptionName20KYearly = SubscriptionName("20k-yearly")
72
73
// User stores information about a user
74
type User struct {
75
	ID                               UserID           `json:"id" gorm:"primaryKey;type:string;" example:"WB7DRDWrJZRGbYrv2CKGkqbzvqdC"`
76
	Email                            string           `json:"email" example:"[email protected]"`
77
	APIKey                           string           `json:"api_key" gorm:"uniqueIndex:idx_users_api_key;NOT NULL" example:"x-api-key"`
78
	Timezone                         string           `json:"timezone" example:"Europe/Helsinki" gorm:"default:Africa/Accra"`
79
	ActivePhoneID                    *uuid.UUID       `json:"active_phone_id" gorm:"type:uuid;" example:"32343a19-da5e-4b1b-a767-3298a73703cb"`
80
	SubscriptionName                 SubscriptionName `json:"subscription_name" example:"free"`
81
	SubscriptionID                   *string          `json:"subscription_id" example:"8f9c71b8-b84e-4417-8408-a62274f65a08"`
82
	SubscriptionStatus               *string          `json:"subscription_status" example:"on_trial"`
83
	SubscriptionRenewsAt             *time.Time       `json:"subscription_renews_at" example:"2022-06-05T14:26:02.302718+03:00"`
84
	SubscriptionEndsAt               *time.Time       `json:"subscription_ends_at" example:"2022-06-05T14:26:02.302718+03:00"`
85
	NotificationMessageStatusEnabled bool             `json:"notification_message_status_enabled" gorm:"default:true" example:"true"`
86
	NotificationWebhookEnabled       bool             `json:"notification_webhook_enabled" gorm:"default:true" example:"true"`
87
	NotificationHeartbeatEnabled     bool             `json:"notification_heartbeat_enabled" gorm:"default:true" example:"true"`
88
	NotificationNewsletterEnabled    bool             `json:"notification_newsletter_enabled" gorm:"default:true" example:"true"`
89
	CreatedAt                        time.Time        `json:"created_at" example:"2022-06-05T14:26:02.302718+03:00"`
90
	UpdatedAt                        time.Time        `json:"updated_at" example:"2022-06-05T14:26:10.303278+03:00"`
91
}
92
93
// IsOnProPlan checks if a user is on the pro plan
94
func (user User) IsOnProPlan() bool {
95
	return user.SubscriptionName == SubscriptionNameProLifetime || user.SubscriptionName == SubscriptionNameProMonthly || user.SubscriptionName == SubscriptionNameProYearly
96
}
97
98
// IsOnFreePlan checks if a user is on the free plan
99
func (user User) IsOnFreePlan() bool {
100
	return user.SubscriptionName == SubscriptionNameFree || user.SubscriptionName == ""
101
}
102
103
// IsOnUltraPlan checks if a user is on the ultra plan
104
func (user User) IsOnUltraPlan() bool {
105
	return user.SubscriptionName == SubscriptionNameUltraMonthly || user.SubscriptionName == SubscriptionNameUltraYearly
106
}
107
108
// IsOn20kPlan checks if a user is on the 20k plan
109
func (user User) IsOn20kPlan() bool {
110
	return user.SubscriptionName == SubscriptionName20KMonthly || user.SubscriptionName == SubscriptionName20KYearly
111
}
112
113
// UserTimeString converts the time to the user's timezone
114
func (user User) UserTimeString(timestamp time.Time) string {
115
	location, err := time.LoadLocation(user.Timezone)
116
	if err != nil {
117
		location = time.UTC
118
	}
119
	return timestamp.In(location).Format(time.RFC1123)
120
}
121
122
// Location gets the timezone of a user
123
func (user User) Location() *time.Location {
124
	location, err := time.LoadLocation(user.Timezone)
125
	if err != nil {
126
		location = time.UTC
127
	}
128
	return location
129
}
130