/
organizations.go
239 lines (205 loc) · 4.95 KB
/
organizations.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package database
import (
"errors"
"log"
)
// OrganizationGet gets an organization
func (d *Database) OrganizationGet(OrgID string) (*Organization, error) {
var org = &Organization{
OrganizationID: "",
Name: "",
CreatedDate: "",
UpdatedDate: "",
}
e := d.db.QueryRow(
`SELECT id, name, created_date, updated_date FROM organization_get_by_id($1)`,
OrgID,
).Scan(
&org.OrganizationID,
&org.Name,
&org.CreatedDate,
&org.UpdatedDate,
)
if e != nil {
log.Println(e)
return nil, errors.New("error getting organization")
}
return org, nil
}
// OrganizationUserRole gets a users role in organization
func (d *Database) OrganizationUserRole(UserID string, OrgID string) (string, error) {
var role string
e := d.db.QueryRow(
`SELECT role FROM organization_get_user_role($1, $2)`,
UserID,
OrgID,
).Scan(
&role,
)
if e != nil {
log.Println(e)
return "", errors.New("error getting organization users role")
}
return role, nil
}
// OrganizationList gets a list of organizations the user is apart of
func (d *Database) OrganizationListByUser(UserID string, Limit int, Offset int) []*Organization {
var organizations = make([]*Organization, 0)
rows, err := d.db.Query(
`SELECT id, name, created_date, updated_date FROM organization_list_by_user($1, $2, $3);`,
UserID,
Limit,
Offset,
)
if err == nil {
defer rows.Close()
for rows.Next() {
var org Organization
if err := rows.Scan(
&org.OrganizationID,
&org.Name,
&org.CreatedDate,
&org.UpdatedDate,
); err != nil {
log.Println(err)
} else {
organizations = append(organizations, &org)
}
}
} else {
log.Println(err)
}
return organizations
}
// OrganizationCreate creates an organization
func (d *Database) OrganizationCreate(UserID string, OrgName string) (string, error) {
var OrgID string
err := d.db.QueryRow(`
SELECT organizationId FROM organization_create($1, $2);`,
UserID,
OrgName,
).Scan(&OrgID)
if err != nil {
log.Println("Unable to create organization: ", err)
return "", err
}
return OrgID, nil
}
// OrganizationUserList gets a list of organization users
func (d *Database) OrganizationUserList(OrgID string, Limit int, Offset int) []*OrganizationUser {
var users = make([]*OrganizationUser, 0)
rows, err := d.db.Query(
`SELECT id, name, email, role FROM organization_user_list($1, $2, $3);`,
OrgID,
Limit,
Offset,
)
if err == nil {
defer rows.Close()
for rows.Next() {
var usr OrganizationUser
if err := rows.Scan(
&usr.UserID,
&usr.Name,
&usr.Email,
&usr.Role,
); err != nil {
log.Println(err)
} else {
users = append(users, &usr)
}
}
} else {
log.Println(err)
}
return users
}
// OrganizationAddUser adds a user to an organization
func (d *Database) OrganizationAddUser(OrgID string, UserID string, Role string) (string, error) {
_, err := d.db.Exec(
`SELECT organization_user_add($1, $2, $3);`,
OrgID,
UserID,
Role,
)
if err != nil {
log.Println("Unable to add user to organization: ", err)
return "", err
}
return OrgID, nil
}
// OrganizationRemoveUser removes a user from a organization
func (d *Database) OrganizationRemoveUser(OrganizationID string, UserID string) error {
_, err := d.db.Exec(
`CALL organization_user_remove($1, $2);`,
OrganizationID,
UserID,
)
if err != nil {
log.Println("Unable to remove user from organization: ", err)
return err
}
return nil
}
// OrganizationTeamList gets a list of organization teams
func (d *Database) OrganizationTeamList(OrgID string, Limit int, Offset int) []*Team {
var teams = make([]*Team, 0)
rows, err := d.db.Query(
`SELECT id, name, created_date, updated_date FROM organization_team_list($1, $2, $3);`,
OrgID,
Limit,
Offset,
)
if err == nil {
defer rows.Close()
for rows.Next() {
var team Team
if err := rows.Scan(
&team.TeamID,
&team.Name,
&team.CreatedDate,
&team.UpdatedDate,
); err != nil {
log.Println(err)
} else {
teams = append(teams, &team)
}
}
} else {
log.Println(err)
}
return teams
}
// OrganizationTeamCreate creates an organization team
func (d *Database) OrganizationTeamCreate(OrgID string, TeamName string) (string, error) {
var TeamID string
err := d.db.QueryRow(`
SELECT teamId FROM organization_team_create($1, $2);`,
OrgID,
TeamName,
).Scan(&TeamID)
if err != nil {
log.Println("Unable to create organization team: ", err)
return "", err
}
return TeamID, nil
}
// OrganizationTeamUserRole gets a users role in organization team
func (d *Database) OrganizationTeamUserRole(UserID string, OrgID string, TeamID string) (string, string, error) {
var orgRole string
var teamRole string
e := d.db.QueryRow(
`SELECT orgRole, teamRole FROM organization_team_user_role($1, $2, $3)`,
UserID,
OrgID,
TeamID,
).Scan(
&orgRole,
&teamRole,
)
if e != nil {
log.Println(e)
return "", "", errors.New("error getting organization team users role")
}
return orgRole, teamRole, nil
}