-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.go
74 lines (58 loc) · 1.72 KB
/
plan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package malak
import (
"context"
"time"
"github.com/google/uuid"
)
var (
ErrPlanNotFound = MalakError("plan does not exists")
ErrCounterExhausted = MalakError("no more units left")
)
type Counter int64
func (c Counter) Add() { c++ }
func (c Counter) Take() error {
if c <= 0 {
return ErrCounterExhausted
}
c--
return nil
}
func (c Counter) TakeN(n int64) error {
if c <= 0 {
return c.Take()
}
c -= Counter(n)
return nil
}
type PlanMetadata struct {
Team struct {
Size Counter `json:"size,omitempty"`
Enabled bool `json:"enabled,omitempty"`
} `json:"team,omitempty"`
Deck struct {
Count Counter `json:"count,omitempty"`
} `json:"deck,omitempty"`
}
type Plan struct {
ID uuid.UUID `bun:"type:uuid,default:uuid_generate_v4()" json:"id,omitempty"`
PlanName string `json:"plan_name,omitempty"`
// Can use a fake id really
// As this only matters if you turn on Stripe
Reference string `json:"reference,omitempty"`
Metadata PlanMetadata `json:"metadata,omitempty" bson:"metadata"`
// Stripe default price id. Again not needed if not using Stripe
DefaultPriceID string `json:"default_price_id,omitempty"`
// Defaults to zero
Amount int64 `json:"amount,omitempty"`
CreatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"created_at,omitempty" bson:"created_at"`
UpdatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"updated_at,omitempty" bson:"updated_at"`
DeletedAt *time.Time `bun:",soft_delete,nullzero" json:"-,omitempty" bson:"deleted_at"`
}
type FetchPlanOptions struct {
Reference string
ID uuid.UUID
}
type PlanRepository interface {
Get(context.Context, *FetchPlanOptions) (*Plan, error)
List(context.Context) ([]*Plan, error)
}