mirror of
https://github.com/PrivateCaptcha/PrivateCaptcha.git
synced 2026-02-09 07:19:08 -06:00
247 lines
7.1 KiB
Go
247 lines
7.1 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: org_users.sql
|
|
|
|
package generated
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const getOrganizationUsers = `-- name: GetOrganizationUsers :many
|
|
SELECT u.id, u.name, u.email, u.subscription_id, u.created_at, u.updated_at, u.deleted_at, ou.level
|
|
FROM backend.organization_users ou
|
|
JOIN backend.users u ON ou.user_id = u.id
|
|
WHERE ou.org_id = $1 AND u.deleted_at IS NULL
|
|
`
|
|
|
|
type GetOrganizationUsersRow struct {
|
|
User User `db:"user" json:"user"`
|
|
Level AccessLevel `db:"level" json:"level"`
|
|
}
|
|
|
|
func (q *Queries) GetOrganizationUsers(ctx context.Context, orgID int32) ([]*GetOrganizationUsersRow, error) {
|
|
rows, err := q.db.Query(ctx, getOrganizationUsers, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*GetOrganizationUsersRow
|
|
for rows.Next() {
|
|
var i GetOrganizationUsersRow
|
|
if err := rows.Scan(
|
|
&i.User.ID,
|
|
&i.User.Name,
|
|
&i.User.Email,
|
|
&i.User.SubscriptionID,
|
|
&i.User.CreatedAt,
|
|
&i.User.UpdatedAt,
|
|
&i.User.DeletedAt,
|
|
&i.Level,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getOrganizationUsersWithEmailInvites = `-- name: GetOrganizationUsersWithEmailInvites :many
|
|
SELECT ou.org_id, ou.user_id, ou.level, ou.created_at, ou.updated_at, ou.id, ou.email, u.id AS linked_user_id, u.name AS user_name, u.email AS user_email
|
|
FROM backend.organization_users ou
|
|
LEFT JOIN backend.users u ON ou.user_id = u.id AND u.deleted_at IS NULL
|
|
WHERE ou.org_id = $1
|
|
`
|
|
|
|
type GetOrganizationUsersWithEmailInvitesRow struct {
|
|
OrganizationUser OrganizationUser `db:"organization_user" json:"organization_user"`
|
|
LinkedUserID pgtype.Int4 `db:"linked_user_id" json:"linked_user_id"`
|
|
UserName pgtype.Text `db:"user_name" json:"user_name"`
|
|
UserEmail pgtype.Text `db:"user_email" json:"user_email"`
|
|
}
|
|
|
|
func (q *Queries) GetOrganizationUsersWithEmailInvites(ctx context.Context, orgID int32) ([]*GetOrganizationUsersWithEmailInvitesRow, error) {
|
|
rows, err := q.db.Query(ctx, getOrganizationUsersWithEmailInvites, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*GetOrganizationUsersWithEmailInvitesRow
|
|
for rows.Next() {
|
|
var i GetOrganizationUsersWithEmailInvitesRow
|
|
if err := rows.Scan(
|
|
&i.OrganizationUser.OrgID,
|
|
&i.OrganizationUser.UserID,
|
|
&i.OrganizationUser.Level,
|
|
&i.OrganizationUser.CreatedAt,
|
|
&i.OrganizationUser.UpdatedAt,
|
|
&i.OrganizationUser.ID,
|
|
&i.OrganizationUser.Email,
|
|
&i.LinkedUserID,
|
|
&i.UserName,
|
|
&i.UserEmail,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const inviteEmailToOrg = `-- name: InviteEmailToOrg :one
|
|
INSERT INTO backend.organization_users (org_id, email, level) VALUES ($1, $2, 'invited') RETURNING org_id, user_id, level, created_at, updated_at, id, email
|
|
`
|
|
|
|
type InviteEmailToOrgParams struct {
|
|
OrgID int32 `db:"org_id" json:"org_id"`
|
|
Email pgtype.Text `db:"email" json:"email"`
|
|
}
|
|
|
|
func (q *Queries) InviteEmailToOrg(ctx context.Context, arg *InviteEmailToOrgParams) (*OrganizationUser, error) {
|
|
row := q.db.QueryRow(ctx, inviteEmailToOrg, arg.OrgID, arg.Email)
|
|
var i OrganizationUser
|
|
err := row.Scan(
|
|
&i.OrgID,
|
|
&i.UserID,
|
|
&i.Level,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.ID,
|
|
&i.Email,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const inviteUserToOrg = `-- name: InviteUserToOrg :one
|
|
INSERT INTO backend.organization_users (org_id, user_id, level) VALUES ($1, $2, 'invited') RETURNING org_id, user_id, level, created_at, updated_at, id, email
|
|
`
|
|
|
|
type InviteUserToOrgParams struct {
|
|
OrgID int32 `db:"org_id" json:"org_id"`
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) InviteUserToOrg(ctx context.Context, arg *InviteUserToOrgParams) (*OrganizationUser, error) {
|
|
row := q.db.QueryRow(ctx, inviteUserToOrg, arg.OrgID, arg.UserID)
|
|
var i OrganizationUser
|
|
err := row.Scan(
|
|
&i.OrgID,
|
|
&i.UserID,
|
|
&i.Level,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.ID,
|
|
&i.Email,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const linkOrgInviteToUser = `-- name: LinkOrgInviteToUser :one
|
|
UPDATE backend.organization_users
|
|
SET user_id = $1, email = NULL, updated_at = NOW()
|
|
WHERE id = $2 AND user_id IS NULL
|
|
RETURNING org_id, user_id, level, created_at, updated_at, id, email
|
|
`
|
|
|
|
type LinkOrgInviteToUserParams struct {
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
ID int32 `db:"id" json:"id"`
|
|
}
|
|
|
|
func (q *Queries) LinkOrgInviteToUser(ctx context.Context, arg *LinkOrgInviteToUserParams) (*OrganizationUser, error) {
|
|
row := q.db.QueryRow(ctx, linkOrgInviteToUser, arg.UserID, arg.ID)
|
|
var i OrganizationUser
|
|
err := row.Scan(
|
|
&i.OrgID,
|
|
&i.UserID,
|
|
&i.Level,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.ID,
|
|
&i.Email,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const removeOrgInviteByID = `-- name: RemoveOrgInviteByID :exec
|
|
DELETE FROM backend.organization_users WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) RemoveOrgInviteByID(ctx context.Context, id int32) error {
|
|
_, err := q.db.Exec(ctx, removeOrgInviteByID, id)
|
|
return err
|
|
}
|
|
|
|
const removeUserFromOrg = `-- name: RemoveUserFromOrg :exec
|
|
DELETE FROM backend.organization_users WHERE org_id = $1 AND user_id = $2
|
|
`
|
|
|
|
type RemoveUserFromOrgParams struct {
|
|
OrgID int32 `db:"org_id" json:"org_id"`
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) RemoveUserFromOrg(ctx context.Context, arg *RemoveUserFromOrgParams) error {
|
|
_, err := q.db.Exec(ctx, removeUserFromOrg, arg.OrgID, arg.UserID)
|
|
return err
|
|
}
|
|
|
|
const swapOrgOwnership = `-- name: SwapOrgOwnership :exec
|
|
WITH delete_new_owner AS (
|
|
DELETE FROM backend.organization_users ou WHERE ou.org_id = $1 AND ou.user_id = $2
|
|
),
|
|
upsert_old_owner AS (
|
|
UPDATE backend.organization_users ou2
|
|
SET level = 'member', updated_at = NOW()
|
|
WHERE ou2.org_id = $1 AND ou2.user_id = $3
|
|
RETURNING ou2.id
|
|
),
|
|
insert_old_owner AS (
|
|
INSERT INTO backend.organization_users (org_id, user_id, level)
|
|
SELECT $1, $3, 'member'
|
|
WHERE NOT EXISTS (SELECT 1 FROM upsert_old_owner)
|
|
)
|
|
SELECT 1
|
|
`
|
|
|
|
type SwapOrgOwnershipParams struct {
|
|
OrgID int32 `db:"org_id" json:"org_id"`
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
UserID_2 pgtype.Int4 `db:"user_id_2" json:"user_id_2"`
|
|
}
|
|
|
|
func (q *Queries) SwapOrgOwnership(ctx context.Context, arg *SwapOrgOwnershipParams) error {
|
|
_, err := q.db.Exec(ctx, swapOrgOwnership, arg.OrgID, arg.UserID, arg.UserID_2)
|
|
return err
|
|
}
|
|
|
|
const updateOrgMembershipLevel = `-- name: UpdateOrgMembershipLevel :exec
|
|
UPDATE backend.organization_users SET level = $1, updated_at = NOW() WHERE org_id = $2 AND user_id = $3 AND level = $4
|
|
`
|
|
|
|
type UpdateOrgMembershipLevelParams struct {
|
|
Level AccessLevel `db:"level" json:"level"`
|
|
OrgID int32 `db:"org_id" json:"org_id"`
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
Level_2 AccessLevel `db:"level_2" json:"level_2"`
|
|
}
|
|
|
|
func (q *Queries) UpdateOrgMembershipLevel(ctx context.Context, arg *UpdateOrgMembershipLevelParams) error {
|
|
_, err := q.db.Exec(ctx, updateOrgMembershipLevel,
|
|
arg.Level,
|
|
arg.OrgID,
|
|
arg.UserID,
|
|
arg.Level_2,
|
|
)
|
|
return err
|
|
}
|