Go · 15913 bytes Raw Blame History
1 // Code generated by sqlc. DO NOT EDIT.
2 // versions:
3 // sqlc v1.31.1
4 // source: teams.sql
5
6 package orgsdb
7
8 import (
9 "context"
10
11 "github.com/jackc/pgx/v5/pgtype"
12 )
13
14 const addTeamMember = `-- name: AddTeamMember :exec
15
16 INSERT INTO team_members (team_id, user_id, role, added_by_user_id)
17 VALUES ($1, $2, $3, $4::bigint)
18 ON CONFLICT (team_id, user_id) DO NOTHING
19 `
20
21 type AddTeamMemberParams struct {
22 TeamID int64
23 UserID int64
24 Role TeamRole
25 AddedByUserID pgtype.Int8
26 }
27
28 // ─── team_members ─────────────────────────────────────────────────
29 func (q *Queries) AddTeamMember(ctx context.Context, db DBTX, arg AddTeamMemberParams) error {
30 _, err := db.Exec(ctx, addTeamMember,
31 arg.TeamID,
32 arg.UserID,
33 arg.Role,
34 arg.AddedByUserID,
35 )
36 return err
37 }
38
39 const createTeam = `-- name: CreateTeam :one
40
41
42 INSERT INTO teams (org_id, slug, display_name, description, parent_team_id, privacy, created_by_user_id)
43 VALUES ($1, $2, $3, $4, $6::bigint, $5, $7::bigint)
44 RETURNING id, org_id, slug, display_name, description, parent_team_id, privacy, created_by_user_id, created_at, updated_at
45 `
46
47 type CreateTeamParams struct {
48 OrgID int64
49 Slug string
50 DisplayName string
51 Description string
52 Privacy TeamPrivacy
53 ParentTeamID pgtype.Int8
54 CreatedByUserID pgtype.Int8
55 }
56
57 // SPDX-License-Identifier: AGPL-3.0-or-later
58 // ─── teams ─────────────────────────────────────────────────────────
59 func (q *Queries) CreateTeam(ctx context.Context, db DBTX, arg CreateTeamParams) (Team, error) {
60 row := db.QueryRow(ctx, createTeam,
61 arg.OrgID,
62 arg.Slug,
63 arg.DisplayName,
64 arg.Description,
65 arg.Privacy,
66 arg.ParentTeamID,
67 arg.CreatedByUserID,
68 )
69 var i Team
70 err := row.Scan(
71 &i.ID,
72 &i.OrgID,
73 &i.Slug,
74 &i.DisplayName,
75 &i.Description,
76 &i.ParentTeamID,
77 &i.Privacy,
78 &i.CreatedByUserID,
79 &i.CreatedAt,
80 &i.UpdatedAt,
81 )
82 return i, err
83 }
84
85 const deleteTeam = `-- name: DeleteTeam :exec
86 DELETE FROM teams WHERE id = $1
87 `
88
89 // Children's parent_team_id flips to NULL via the FK ON DELETE SET NULL,
90 // promoting them to top-level — matches the "deleting parent doesn't
91 // delete children" intent in the spec's pitfalls.
92 func (q *Queries) DeleteTeam(ctx context.Context, db DBTX, id int64) error {
93 _, err := db.Exec(ctx, deleteTeam, id)
94 return err
95 }
96
97 const getTeamByID = `-- name: GetTeamByID :one
98 SELECT id, org_id, slug, display_name, description, parent_team_id, privacy, created_by_user_id, created_at, updated_at FROM teams WHERE id = $1
99 `
100
101 func (q *Queries) GetTeamByID(ctx context.Context, db DBTX, id int64) (Team, error) {
102 row := db.QueryRow(ctx, getTeamByID, id)
103 var i Team
104 err := row.Scan(
105 &i.ID,
106 &i.OrgID,
107 &i.Slug,
108 &i.DisplayName,
109 &i.Description,
110 &i.ParentTeamID,
111 &i.Privacy,
112 &i.CreatedByUserID,
113 &i.CreatedAt,
114 &i.UpdatedAt,
115 )
116 return i, err
117 }
118
119 const getTeamByOrgAndSlug = `-- name: GetTeamByOrgAndSlug :one
120 SELECT id, org_id, slug, display_name, description, parent_team_id, privacy, created_by_user_id, created_at, updated_at FROM teams WHERE org_id = $1 AND slug = $2
121 `
122
123 type GetTeamByOrgAndSlugParams struct {
124 OrgID int64
125 Slug string
126 }
127
128 func (q *Queries) GetTeamByOrgAndSlug(ctx context.Context, db DBTX, arg GetTeamByOrgAndSlugParams) (Team, error) {
129 row := db.QueryRow(ctx, getTeamByOrgAndSlug, arg.OrgID, arg.Slug)
130 var i Team
131 err := row.Scan(
132 &i.ID,
133 &i.OrgID,
134 &i.Slug,
135 &i.DisplayName,
136 &i.Description,
137 &i.ParentTeamID,
138 &i.Privacy,
139 &i.CreatedByUserID,
140 &i.CreatedAt,
141 &i.UpdatedAt,
142 )
143 return i, err
144 }
145
146 const getTeamRepoAccess = `-- name: GetTeamRepoAccess :one
147 SELECT team_id, repo_id, role, added_by_user_id, added_at FROM team_repo_access WHERE team_id = $1 AND repo_id = $2
148 `
149
150 type GetTeamRepoAccessParams struct {
151 TeamID int64
152 RepoID int64
153 }
154
155 func (q *Queries) GetTeamRepoAccess(ctx context.Context, db DBTX, arg GetTeamRepoAccessParams) (TeamRepoAccess, error) {
156 row := db.QueryRow(ctx, getTeamRepoAccess, arg.TeamID, arg.RepoID)
157 var i TeamRepoAccess
158 err := row.Scan(
159 &i.TeamID,
160 &i.RepoID,
161 &i.Role,
162 &i.AddedByUserID,
163 &i.AddedAt,
164 )
165 return i, err
166 }
167
168 const grantTeamRepoAccess = `-- name: GrantTeamRepoAccess :exec
169
170 INSERT INTO team_repo_access (team_id, repo_id, role, added_by_user_id)
171 VALUES ($1, $2, $3, $4::bigint)
172 ON CONFLICT (team_id, repo_id) DO UPDATE SET role = EXCLUDED.role
173 `
174
175 type GrantTeamRepoAccessParams struct {
176 TeamID int64
177 RepoID int64
178 Role TeamRepoRole
179 AddedByUserID pgtype.Int8
180 }
181
182 // ─── team_repo_access ─────────────────────────────────────────────
183 func (q *Queries) GrantTeamRepoAccess(ctx context.Context, db DBTX, arg GrantTeamRepoAccessParams) error {
184 _, err := db.Exec(ctx, grantTeamRepoAccess,
185 arg.TeamID,
186 arg.RepoID,
187 arg.Role,
188 arg.AddedByUserID,
189 )
190 return err
191 }
192
193 const isTeamMember = `-- name: IsTeamMember :one
194 SELECT EXISTS(SELECT 1 FROM team_members WHERE team_id = $1 AND user_id = $2)
195 `
196
197 type IsTeamMemberParams struct {
198 TeamID int64
199 UserID int64
200 }
201
202 // Replaces the inline EXISTS query in handlers/orgs/teams.go
203 // canSeeTeam + filterSecretTeams (SR2 M2). Used by the visibility
204 // gate for secret teams.
205 func (q *Queries) IsTeamMember(ctx context.Context, db DBTX, arg IsTeamMemberParams) (bool, error) {
206 row := db.QueryRow(ctx, isTeamMember, arg.TeamID, arg.UserID)
207 var exists bool
208 err := row.Scan(&exists)
209 return exists, err
210 }
211
212 const listChildTeams = `-- name: ListChildTeams :many
213 SELECT id, org_id, slug, display_name, description, parent_team_id, privacy, created_by_user_id, created_at, updated_at FROM teams WHERE parent_team_id = $1 ORDER BY slug ASC
214 `
215
216 func (q *Queries) ListChildTeams(ctx context.Context, db DBTX, parentTeamID pgtype.Int8) ([]Team, error) {
217 rows, err := db.Query(ctx, listChildTeams, parentTeamID)
218 if err != nil {
219 return nil, err
220 }
221 defer rows.Close()
222 items := []Team{}
223 for rows.Next() {
224 var i Team
225 if err := rows.Scan(
226 &i.ID,
227 &i.OrgID,
228 &i.Slug,
229 &i.DisplayName,
230 &i.Description,
231 &i.ParentTeamID,
232 &i.Privacy,
233 &i.CreatedByUserID,
234 &i.CreatedAt,
235 &i.UpdatedAt,
236 ); err != nil {
237 return nil, err
238 }
239 items = append(items, i)
240 }
241 if err := rows.Err(); err != nil {
242 return nil, err
243 }
244 return items, nil
245 }
246
247 const listRepoTeamGrants = `-- name: ListRepoTeamGrants :many
248 SELECT a.team_id, a.repo_id, a.role, a.added_by_user_id, a.added_at,
249 t.slug AS team_slug, t.display_name AS team_display_name,
250 t.privacy::text AS team_privacy
251 FROM team_repo_access a
252 JOIN teams t ON t.id = a.team_id
253 WHERE a.repo_id = $1
254 ORDER BY t.slug ASC
255 `
256
257 type ListRepoTeamGrantsRow struct {
258 TeamID int64
259 RepoID int64
260 Role TeamRepoRole
261 AddedByUserID pgtype.Int8
262 AddedAt pgtype.Timestamptz
263 TeamSlug string
264 TeamDisplayName string
265 TeamPrivacy string
266 }
267
268 // All teams with grants on a single repo; for repo settings/access page.
269 func (q *Queries) ListRepoTeamGrants(ctx context.Context, db DBTX, repoID int64) ([]ListRepoTeamGrantsRow, error) {
270 rows, err := db.Query(ctx, listRepoTeamGrants, repoID)
271 if err != nil {
272 return nil, err
273 }
274 defer rows.Close()
275 items := []ListRepoTeamGrantsRow{}
276 for rows.Next() {
277 var i ListRepoTeamGrantsRow
278 if err := rows.Scan(
279 &i.TeamID,
280 &i.RepoID,
281 &i.Role,
282 &i.AddedByUserID,
283 &i.AddedAt,
284 &i.TeamSlug,
285 &i.TeamDisplayName,
286 &i.TeamPrivacy,
287 ); err != nil {
288 return nil, err
289 }
290 items = append(items, i)
291 }
292 if err := rows.Err(); err != nil {
293 return nil, err
294 }
295 return items, nil
296 }
297
298 const listTeamAccessForRepoAndTeams = `-- name: ListTeamAccessForRepoAndTeams :many
299 SELECT team_id, repo_id, role
300 FROM team_repo_access
301 WHERE repo_id = $1 AND team_id = ANY($2::bigint[])
302 `
303
304 type ListTeamAccessForRepoAndTeamsParams struct {
305 RepoID int64
306 Column2 []int64
307 }
308
309 type ListTeamAccessForRepoAndTeamsRow struct {
310 TeamID int64
311 RepoID int64
312 Role TeamRepoRole
313 }
314
315 // Policy hot-path: given a repo and a set of team_ids the actor
316 // belongs to (incl. inherited), return the access rows. Caller picks
317 // the highest role.
318 func (q *Queries) ListTeamAccessForRepoAndTeams(ctx context.Context, db DBTX, arg ListTeamAccessForRepoAndTeamsParams) ([]ListTeamAccessForRepoAndTeamsRow, error) {
319 rows, err := db.Query(ctx, listTeamAccessForRepoAndTeams, arg.RepoID, arg.Column2)
320 if err != nil {
321 return nil, err
322 }
323 defer rows.Close()
324 items := []ListTeamAccessForRepoAndTeamsRow{}
325 for rows.Next() {
326 var i ListTeamAccessForRepoAndTeamsRow
327 if err := rows.Scan(&i.TeamID, &i.RepoID, &i.Role); err != nil {
328 return nil, err
329 }
330 items = append(items, i)
331 }
332 if err := rows.Err(); err != nil {
333 return nil, err
334 }
335 return items, nil
336 }
337
338 const listTeamMembers = `-- name: ListTeamMembers :many
339 SELECT m.team_id, m.user_id, m.role, m.added_by_user_id, m.added_at,
340 u.username, u.display_name
341 FROM team_members m
342 JOIN users u ON u.id = m.user_id
343 WHERE m.team_id = $1 AND u.deleted_at IS NULL
344 ORDER BY m.role ASC, u.username ASC
345 `
346
347 type ListTeamMembersRow struct {
348 TeamID int64
349 UserID int64
350 Role TeamRole
351 AddedByUserID pgtype.Int8
352 AddedAt pgtype.Timestamptz
353 Username string
354 DisplayName string
355 }
356
357 func (q *Queries) ListTeamMembers(ctx context.Context, db DBTX, teamID int64) ([]ListTeamMembersRow, error) {
358 rows, err := db.Query(ctx, listTeamMembers, teamID)
359 if err != nil {
360 return nil, err
361 }
362 defer rows.Close()
363 items := []ListTeamMembersRow{}
364 for rows.Next() {
365 var i ListTeamMembersRow
366 if err := rows.Scan(
367 &i.TeamID,
368 &i.UserID,
369 &i.Role,
370 &i.AddedByUserID,
371 &i.AddedAt,
372 &i.Username,
373 &i.DisplayName,
374 ); err != nil {
375 return nil, err
376 }
377 items = append(items, i)
378 }
379 if err := rows.Err(); err != nil {
380 return nil, err
381 }
382 return items, nil
383 }
384
385 const listTeamRepoAccess = `-- name: ListTeamRepoAccess :many
386 SELECT a.team_id, a.repo_id, a.role, a.added_by_user_id, a.added_at,
387 r.name AS repo_name, r.visibility::text AS visibility
388 FROM team_repo_access a
389 JOIN repos r ON r.id = a.repo_id
390 WHERE a.team_id = $1 AND r.deleted_at IS NULL
391 ORDER BY r.name ASC
392 `
393
394 type ListTeamRepoAccessRow struct {
395 TeamID int64
396 RepoID int64
397 Role TeamRepoRole
398 AddedByUserID pgtype.Int8
399 AddedAt pgtype.Timestamptz
400 RepoName string
401 Visibility string
402 }
403
404 // All repos the team has any grant on; for the team-view page.
405 func (q *Queries) ListTeamRepoAccess(ctx context.Context, db DBTX, teamID int64) ([]ListTeamRepoAccessRow, error) {
406 rows, err := db.Query(ctx, listTeamRepoAccess, teamID)
407 if err != nil {
408 return nil, err
409 }
410 defer rows.Close()
411 items := []ListTeamRepoAccessRow{}
412 for rows.Next() {
413 var i ListTeamRepoAccessRow
414 if err := rows.Scan(
415 &i.TeamID,
416 &i.RepoID,
417 &i.Role,
418 &i.AddedByUserID,
419 &i.AddedAt,
420 &i.RepoName,
421 &i.Visibility,
422 ); err != nil {
423 return nil, err
424 }
425 items = append(items, i)
426 }
427 if err := rows.Err(); err != nil {
428 return nil, err
429 }
430 return items, nil
431 }
432
433 const listTeamsForOrg = `-- name: ListTeamsForOrg :many
434 SELECT id, org_id, slug, display_name, description, parent_team_id, privacy, created_by_user_id, created_at, updated_at FROM teams WHERE org_id = $1 ORDER BY slug ASC
435 `
436
437 func (q *Queries) ListTeamsForOrg(ctx context.Context, db DBTX, orgID int64) ([]Team, error) {
438 rows, err := db.Query(ctx, listTeamsForOrg, orgID)
439 if err != nil {
440 return nil, err
441 }
442 defer rows.Close()
443 items := []Team{}
444 for rows.Next() {
445 var i Team
446 if err := rows.Scan(
447 &i.ID,
448 &i.OrgID,
449 &i.Slug,
450 &i.DisplayName,
451 &i.Description,
452 &i.ParentTeamID,
453 &i.Privacy,
454 &i.CreatedByUserID,
455 &i.CreatedAt,
456 &i.UpdatedAt,
457 ); err != nil {
458 return nil, err
459 }
460 items = append(items, i)
461 }
462 if err := rows.Err(); err != nil {
463 return nil, err
464 }
465 return items, nil
466 }
467
468 const listTeamsForUserInOrg = `-- name: ListTeamsForUserInOrg :many
469 SELECT t.id, t.org_id, t.slug, t.display_name, t.description,
470 t.parent_team_id, t.privacy, t.created_by_user_id,
471 t.created_at, t.updated_at,
472 m.role AS member_role
473 FROM team_members m
474 JOIN teams t ON t.id = m.team_id
475 WHERE t.org_id = $1 AND m.user_id = $2
476 `
477
478 type ListTeamsForUserInOrgParams struct {
479 OrgID int64
480 UserID int64
481 }
482
483 type ListTeamsForUserInOrgRow struct {
484 ID int64
485 OrgID int64
486 Slug string
487 DisplayName string
488 Description string
489 ParentTeamID pgtype.Int8
490 Privacy TeamPrivacy
491 CreatedByUserID pgtype.Int8
492 CreatedAt pgtype.Timestamptz
493 UpdatedAt pgtype.Timestamptz
494 MemberRole TeamRole
495 }
496
497 // Returns the teams a user directly belongs to within an org. The
498 // policy aggregator unions this with each row's parent_team_id to
499 // get the inherited set.
500 func (q *Queries) ListTeamsForUserInOrg(ctx context.Context, db DBTX, arg ListTeamsForUserInOrgParams) ([]ListTeamsForUserInOrgRow, error) {
501 rows, err := db.Query(ctx, listTeamsForUserInOrg, arg.OrgID, arg.UserID)
502 if err != nil {
503 return nil, err
504 }
505 defer rows.Close()
506 items := []ListTeamsForUserInOrgRow{}
507 for rows.Next() {
508 var i ListTeamsForUserInOrgRow
509 if err := rows.Scan(
510 &i.ID,
511 &i.OrgID,
512 &i.Slug,
513 &i.DisplayName,
514 &i.Description,
515 &i.ParentTeamID,
516 &i.Privacy,
517 &i.CreatedByUserID,
518 &i.CreatedAt,
519 &i.UpdatedAt,
520 &i.MemberRole,
521 ); err != nil {
522 return nil, err
523 }
524 items = append(items, i)
525 }
526 if err := rows.Err(); err != nil {
527 return nil, err
528 }
529 return items, nil
530 }
531
532 const removeTeamMember = `-- name: RemoveTeamMember :exec
533 DELETE FROM team_members WHERE team_id = $1 AND user_id = $2
534 `
535
536 type RemoveTeamMemberParams struct {
537 TeamID int64
538 UserID int64
539 }
540
541 func (q *Queries) RemoveTeamMember(ctx context.Context, db DBTX, arg RemoveTeamMemberParams) error {
542 _, err := db.Exec(ctx, removeTeamMember, arg.TeamID, arg.UserID)
543 return err
544 }
545
546 const revokeTeamRepoAccess = `-- name: RevokeTeamRepoAccess :exec
547 DELETE FROM team_repo_access WHERE team_id = $1 AND repo_id = $2
548 `
549
550 type RevokeTeamRepoAccessParams struct {
551 TeamID int64
552 RepoID int64
553 }
554
555 func (q *Queries) RevokeTeamRepoAccess(ctx context.Context, db DBTX, arg RevokeTeamRepoAccessParams) error {
556 _, err := db.Exec(ctx, revokeTeamRepoAccess, arg.TeamID, arg.RepoID)
557 return err
558 }
559
560 const setTeamMemberRole = `-- name: SetTeamMemberRole :exec
561 UPDATE team_members SET role = $3 WHERE team_id = $1 AND user_id = $2
562 `
563
564 type SetTeamMemberRoleParams struct {
565 TeamID int64
566 UserID int64
567 Role TeamRole
568 }
569
570 func (q *Queries) SetTeamMemberRole(ctx context.Context, db DBTX, arg SetTeamMemberRoleParams) error {
571 _, err := db.Exec(ctx, setTeamMemberRole, arg.TeamID, arg.UserID, arg.Role)
572 return err
573 }
574
575 const setTeamParent = `-- name: SetTeamParent :exec
576 UPDATE teams SET parent_team_id = $2::bigint, updated_at = now()
577 WHERE id = $1
578 `
579
580 type SetTeamParentParams struct {
581 ID int64
582 ParentTeamID pgtype.Int8
583 }
584
585 // The one-level-nesting BEFORE trigger blocks invalid moves; the
586 // caller surfaces the SQLSTATE 23514 as a friendly error.
587 func (q *Queries) SetTeamParent(ctx context.Context, db DBTX, arg SetTeamParentParams) error {
588 _, err := db.Exec(ctx, setTeamParent, arg.ID, arg.ParentTeamID)
589 return err
590 }
591
592 const updateTeamProfile = `-- name: UpdateTeamProfile :exec
593 UPDATE teams SET display_name = $2, description = $3, privacy = $4, updated_at = now()
594 WHERE id = $1
595 `
596
597 type UpdateTeamProfileParams struct {
598 ID int64
599 DisplayName string
600 Description string
601 Privacy TeamPrivacy
602 }
603
604 func (q *Queries) UpdateTeamProfile(ctx context.Context, db DBTX, arg UpdateTeamProfileParams) error {
605 _, err := db.Exec(ctx, updateTeamProfile,
606 arg.ID,
607 arg.DisplayName,
608 arg.Description,
609 arg.Privacy,
610 )
611 return err
612 }
613