-
Notifications
You must be signed in to change notification settings - Fork 2
/
account.go
118 lines (98 loc) · 3.27 KB
/
account.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package types
import (
"context"
"encoding/json"
"errors"
sourcetypes "github.com/vlab-research/vlab/inference/sources/types"
)
type AccountType string
const (
TypeformAccount AccountType = "typeform"
FlyAccount AccountType = "fly"
AlchemerAccount AccountType = "alchemer"
FacebookAccount AccountType = "facebook"
)
type AccountRepository interface {
Create(ctx context.Context, a Account) error
Delete(ctx context.Context, a Account) error
List(ctx context.Context, offset, limit int, userID, accType string) ([]Account, error)
}
var ErrAccountAlreadyExists = errors.New("Account already exists")
var ErrAccountDoesNotExist = errors.New("Account not found")
type Account struct {
UserID string `json:"userId"`
Name string `json:"name" validate:"required"`
AuthType AccountType `json:"authType" validate:"required"`
// Interim structure to hold credential values until account type can be determined
Account json.RawMessage `json:"connectedAccount"`
ConnectedAccount ConnectedAccount `json:"-"`
}
// Takes the account and sets it to a connected account
func (a *Account) SetConnectedAccount() error {
b, err := json.Marshal(a.Account)
if err != nil {
return err
}
a.Account = b
return nil
}
// Used as a concept to avoid complex logic on the frontend
// If null then the account is considered not yet connected
type ConnectedAccount interface {
MarshalCredentials() (string, error)
}
type FlyConnectedAccount struct {
CreatedAt int `json:"createdAt"`
Credentials sourcetypes.FlyCredentials `json:"credentials" validate:"required"`
}
// Used to input data into the database as a JSONB field
func (f FlyConnectedAccount) MarshalCredentials() (string, error) {
b, err := json.Marshal(f.Credentials)
if err != nil {
return "", err
}
return string(b), nil
}
type TypeformConnectedAccount struct {
CreatedAt int `json:"createdAt"`
Credentials sourcetypes.TypeformCredentials `json:"credentials" validate:"required"`
}
// Used to input data into the database as a JSONB field
func (t TypeformConnectedAccount) MarshalCredentials() (string, error) {
b, err := json.Marshal(t.Credentials)
if err != nil {
return "", err
}
return string(b), nil
}
type FacebookCredentials struct {
ExpiresIn int `json:"expires_in"`
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
}
// Used to connect Virtual Lab to a Facebook ad account
type FacebookConnectedAccount struct {
CreatedAt int `json:"createdAt"`
Credentials FacebookCredentials `json:"credentials" validate:"required"`
}
// Used to input data into the database as a JSONB field
func (f FacebookConnectedAccount) MarshalCredentials() (string, error) {
b, err := json.Marshal(f.Credentials)
if err != nil {
return "", err
}
return string(b), nil
}
// Used to connect Virtual Lab to a Facebook ad account
type AlchemerConnectedAccount struct {
CreatedAt int `json:"createdAt"`
Credentials sourcetypes.AlchemerCreds `json:"credentials" validate:"required"`
}
// Used to input data into the database as a JSONB field
func (a AlchemerConnectedAccount) MarshalCredentials() (string, error) {
b, err := json.Marshal(a.Credentials)
if err != nil {
return "", err
}
return string(b), nil
}