-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsupportmembers.go
172 lines (141 loc) · 4.12 KB
/
supportmembers.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
package database
import (
"context"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4/pgxpool"
)
type SupportTeamMembersTable struct {
*pgxpool.Pool
}
func newSupportTeamMembersTable(db *pgxpool.Pool) *SupportTeamMembersTable {
return &SupportTeamMembersTable{
db,
}
}
func (s SupportTeamMembersTable) Schema() string {
return `
CREATE TABLE IF NOT EXISTS support_team_members(
"team_id" int NOT NULL,
"user_id" int8 NOT NULL,
FOREIGN KEY("team_id") REFERENCES support_team("id") ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY("team_id", "user_id")
);`
}
func (s *SupportTeamMembersTable) Get(ctx context.Context, teamId int) (members []uint64, e error) {
rows, err := s.Query(ctx, `SELECT "user_id" from support_team_members WHERE "team_id" = $1;`, teamId)
if err != nil {
return nil, err
}
for rows.Next() {
var userId uint64
if err := rows.Scan(&userId); err != nil {
return nil, err
}
members = append(members, userId)
}
return
}
func (s *SupportTeamMembersTable) Add(ctx context.Context, teamId int, userId uint64) (err error) {
query := `INSERT INTO support_team_members("team_id", "user_id") VALUES($1, $2) ON CONFLICT (team_id, user_id) DO NOTHING;`
_, err = s.Exec(ctx, query, teamId, userId)
return
}
func (s *SupportTeamMembersTable) Delete(ctx context.Context, teamId int, userId uint64) (err error) {
_, err = s.Exec(ctx, `DELETE FROM support_team_members WHERE "team_id"=$1 AND "user_id"=$2;`, teamId, userId)
return
}
func (s *SupportTeamMembersTable) IsSupport(ctx context.Context, guildId, userId uint64) (isSupport bool, err error) {
query := `
SELECT EXISTS(
SELECT 1
FROM support_team_members
INNER JOIN support_team
ON support_team_members.team_id = support_team.id
WHERE support_team.guild_id = $1 AND support_team_members.user_id = $2
);
`
err = s.QueryRow(ctx, query, guildId, userId).Scan(&isSupport)
return
}
func (s *SupportTeamMembersTable) IsSupportSubset(ctx context.Context, guildId, userId uint64, teams []int) (isSupport bool, err error) {
query := `
SELECT EXISTS(
SELECT 1
FROM support_team_members
INNER JOIN support_team
ON support_team_members.team_id = support_team.id
WHERE support_team.guild_id = $1 AND support_team_members.user_id = $2 AND support_team.id = ANY($3)
);
`
teamIdArray := &pgtype.Int4Array{}
if err := teamIdArray.Set(teams); err != nil {
return false, err
}
err = s.QueryRow(ctx, query, guildId, userId, teamIdArray).Scan(&isSupport)
return
}
func (s *SupportTeamMembersTable) GetAllSupportMembers(ctx context.Context, guildId uint64) (users []uint64, err error) {
query := `
SELECT support_team_members.user_id
FROM support_team_members
INNER JOIN support_team
ON support_team_members.team_id = support_team.id
WHERE support_team.guild_id = $1;
`
rows, err := s.Query(ctx, query, guildId)
if err != nil {
return nil, err
}
for rows.Next() {
var userId uint64
if err := rows.Scan(&userId); err != nil {
return nil, err
}
users = append(users, userId)
}
return
}
func (s *SupportTeamMembersTable) GetAllSupportMembersForPanel(ctx context.Context, panelId int) (users []uint64, err error) {
query := `
SELECT DISTINCT support_team_members.user_id
FROM support_team_members
INNER JOIN panel_teams
ON support_team_members.team_id = panel_teams.team_id
WHERE panel_teams.panel_id = $1;
`
rows, err := s.Query(ctx, query, panelId)
if err != nil {
return nil, err
}
for rows.Next() {
var userId uint64
if err := rows.Scan(&userId); err != nil {
return nil, err
}
users = append(users, userId)
}
return
}
func (s *SupportTeamMembersTable) GetAllTeamsForUser(ctx context.Context, guildId, userId uint64) ([]int, error) {
query := `
SELECT support_team_members.team_id
FROM support_team_members
INNER JOIN support_team
ON support_team_members.team_id = support_team.id
WHERE support_team.guild_id = $1 AND support_team_members.user_id = $2;
`
rows, err := s.Query(ctx, query, guildId, userId)
if err != nil {
return nil, err
}
defer rows.Close()
var teamIds []int
for rows.Next() {
var teamId int
if err := rows.Scan(&teamId); err != nil {
return nil, err
}
teamIds = append(teamIds, teamId)
}
return teamIds, nil
}