542 lines
14 KiB
Go
542 lines
14 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: organizations.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const deleteOrganizationRepresentatives = `-- name: DeleteOrganizationRepresentatives :exec
|
|
DELETE FROM organization_representatives
|
|
WHERE organization_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteOrganizationRepresentatives(ctx context.Context, organizationID pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteOrganizationRepresentatives, organizationID)
|
|
return err
|
|
}
|
|
|
|
const getMembership = `-- name: GetMembership :one
|
|
SELECT
|
|
id,
|
|
organization_id,
|
|
user_id,
|
|
role,
|
|
state,
|
|
created_at,
|
|
updated_at
|
|
FROM memberships
|
|
WHERE
|
|
organization_id = $1
|
|
AND user_id = $2
|
|
`
|
|
|
|
type GetMembershipParams struct {
|
|
OrganizationID pgtype.UUID
|
|
UserID pgtype.UUID
|
|
}
|
|
|
|
// Reads the membership of one account in one organization, in any state.
|
|
func (q *Queries) GetMembership(ctx context.Context, arg GetMembershipParams) (Membership, error) {
|
|
row := q.db.QueryRow(ctx, getMembership, arg.OrganizationID, arg.UserID)
|
|
var i Membership
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.OrganizationID,
|
|
&i.UserID,
|
|
&i.Role,
|
|
&i.State,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getOrganizationCreationResult = `-- name: GetOrganizationCreationResult :one
|
|
SELECT snapshot
|
|
FROM organization_creation_results
|
|
WHERE scope_hash = $1
|
|
`
|
|
|
|
// Reads only the historical result. It intentionally does not join the live
|
|
// organization or membership, whose profile and state may have since changed.
|
|
func (q *Queries) GetOrganizationCreationResult(ctx context.Context, scopeHash []byte) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, getOrganizationCreationResult, scopeHash)
|
|
var snapshot []byte
|
|
err := row.Scan(&snapshot)
|
|
return snapshot, err
|
|
}
|
|
|
|
const getOrganizationForMember = `-- name: GetOrganizationForMember :one
|
|
SELECT
|
|
organization.id,
|
|
organization.name,
|
|
organization.description,
|
|
organization.sphere,
|
|
organization.contact_website,
|
|
organization.contact_email,
|
|
organization.contact_phone,
|
|
organization.contact_telegram,
|
|
organization.verification_level,
|
|
organization.verification_basis,
|
|
organization.verification_changed_at,
|
|
organization.version,
|
|
organization.created_at,
|
|
organization.updated_at,
|
|
coalesce((
|
|
SELECT
|
|
jsonb_agg(
|
|
jsonb_build_object('name', representative.name, 'job_title', representative.job_title)
|
|
ORDER BY representative.ordinal
|
|
)
|
|
FROM organization_representatives AS representative
|
|
WHERE representative.organization_id = organization.id
|
|
), '[]'::jsonb)::text AS representatives,
|
|
membership.id AS membership_id,
|
|
membership.role AS membership_role,
|
|
membership.state AS membership_state,
|
|
membership.created_at AS membership_created_at,
|
|
membership.updated_at AS membership_updated_at
|
|
FROM organizations AS organization
|
|
INNER JOIN memberships AS membership ON organization.id = membership.organization_id
|
|
WHERE
|
|
organization.id = $1
|
|
AND membership.user_id = $2
|
|
AND membership.state = 'active'
|
|
`
|
|
|
|
type GetOrganizationForMemberParams struct {
|
|
OrganizationID pgtype.UUID
|
|
UserID pgtype.UUID
|
|
}
|
|
|
|
type GetOrganizationForMemberRow struct {
|
|
ID pgtype.UUID
|
|
Name string
|
|
Description pgtype.Text
|
|
Sphere pgtype.Text
|
|
ContactWebsite pgtype.Text
|
|
ContactEmail pgtype.Text
|
|
ContactPhone pgtype.Text
|
|
ContactTelegram pgtype.Text
|
|
VerificationLevel string
|
|
VerificationBasis string
|
|
VerificationChangedAt pgtype.Timestamptz
|
|
Version int64
|
|
CreatedAt pgtype.Timestamptz
|
|
UpdatedAt pgtype.Timestamptz
|
|
Representatives string
|
|
MembershipID pgtype.UUID
|
|
MembershipRole string
|
|
MembershipState string
|
|
MembershipCreatedAt pgtype.Timestamptz
|
|
MembershipUpdatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// Reads an organization together with the membership of one account in it
|
|
// and its representatives in list order, as one statement, so the profile and
|
|
// the list come from one snapshot. Only an active membership matches, so an
|
|
// unknown organization, an account that is no member, and a suspended,
|
|
// removed, or left member all return no row and cannot be told apart.
|
|
func (q *Queries) GetOrganizationForMember(ctx context.Context, arg GetOrganizationForMemberParams) (GetOrganizationForMemberRow, error) {
|
|
row := q.db.QueryRow(ctx, getOrganizationForMember, arg.OrganizationID, arg.UserID)
|
|
var i GetOrganizationForMemberRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Sphere,
|
|
&i.ContactWebsite,
|
|
&i.ContactEmail,
|
|
&i.ContactPhone,
|
|
&i.ContactTelegram,
|
|
&i.VerificationLevel,
|
|
&i.VerificationBasis,
|
|
&i.VerificationChangedAt,
|
|
&i.Version,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Representatives,
|
|
&i.MembershipID,
|
|
&i.MembershipRole,
|
|
&i.MembershipState,
|
|
&i.MembershipCreatedAt,
|
|
&i.MembershipUpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const insertMembership = `-- name: InsertMembership :exec
|
|
INSERT INTO memberships (
|
|
id,
|
|
organization_id,
|
|
user_id,
|
|
role,
|
|
state,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7
|
|
)
|
|
`
|
|
|
|
type InsertMembershipParams struct {
|
|
ID pgtype.UUID
|
|
OrganizationID pgtype.UUID
|
|
UserID pgtype.UUID
|
|
Role string
|
|
State string
|
|
CreatedAt pgtype.Timestamptz
|
|
UpdatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
func (q *Queries) InsertMembership(ctx context.Context, arg InsertMembershipParams) error {
|
|
_, err := q.db.Exec(ctx, insertMembership,
|
|
arg.ID,
|
|
arg.OrganizationID,
|
|
arg.UserID,
|
|
arg.Role,
|
|
arg.State,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertOrganization = `-- name: InsertOrganization :exec
|
|
INSERT INTO organizations (
|
|
id,
|
|
name,
|
|
description,
|
|
sphere,
|
|
contact_website,
|
|
contact_email,
|
|
contact_phone,
|
|
contact_telegram,
|
|
verification_level,
|
|
verification_basis,
|
|
verification_changed_at,
|
|
version,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9,
|
|
$10,
|
|
$11,
|
|
$12,
|
|
$13,
|
|
$14
|
|
)
|
|
`
|
|
|
|
type InsertOrganizationParams struct {
|
|
ID pgtype.UUID
|
|
Name string
|
|
Description pgtype.Text
|
|
Sphere pgtype.Text
|
|
ContactWebsite pgtype.Text
|
|
ContactEmail pgtype.Text
|
|
ContactPhone pgtype.Text
|
|
ContactTelegram pgtype.Text
|
|
VerificationLevel string
|
|
VerificationBasis string
|
|
VerificationChangedAt pgtype.Timestamptz
|
|
Version int64
|
|
CreatedAt pgtype.Timestamptz
|
|
UpdatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// Stores a new organization with the verification level that the domain
|
|
// gave it. The deferred constraint organizations_owner_required fails the
|
|
// transaction at commit unless an active owner membership was inserted in
|
|
// it too.
|
|
func (q *Queries) InsertOrganization(ctx context.Context, arg InsertOrganizationParams) error {
|
|
_, err := q.db.Exec(ctx, insertOrganization,
|
|
arg.ID,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.Sphere,
|
|
arg.ContactWebsite,
|
|
arg.ContactEmail,
|
|
arg.ContactPhone,
|
|
arg.ContactTelegram,
|
|
arg.VerificationLevel,
|
|
arg.VerificationBasis,
|
|
arg.VerificationChangedAt,
|
|
arg.Version,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertOrganizationRepresentative = `-- name: InsertOrganizationRepresentative :exec
|
|
INSERT INTO organization_representatives (
|
|
organization_id,
|
|
ordinal,
|
|
name,
|
|
job_title
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4
|
|
)
|
|
`
|
|
|
|
type InsertOrganizationRepresentativeParams struct {
|
|
OrganizationID pgtype.UUID
|
|
Ordinal int16
|
|
Name string
|
|
JobTitle string
|
|
}
|
|
|
|
// Stores one representative at its position in the list. The adapter calls it
|
|
// once per representative after it deleted the previous list, in the same
|
|
// transaction.
|
|
func (q *Queries) InsertOrganizationRepresentative(ctx context.Context, arg InsertOrganizationRepresentativeParams) error {
|
|
_, err := q.db.Exec(ctx, insertOrganizationRepresentative,
|
|
arg.OrganizationID,
|
|
arg.Ordinal,
|
|
arg.Name,
|
|
arg.JobTitle,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const listOrganizationsForUser = `-- name: ListOrganizationsForUser :many
|
|
SELECT
|
|
organization.id,
|
|
organization.name,
|
|
organization.verification_level,
|
|
membership.id AS membership_id,
|
|
membership.role AS membership_role,
|
|
membership.state AS membership_state,
|
|
membership.created_at AS membership_created_at,
|
|
membership.updated_at AS membership_updated_at
|
|
FROM memberships AS membership
|
|
INNER JOIN organizations AS organization ON membership.organization_id = organization.id
|
|
WHERE
|
|
membership.user_id = $1
|
|
AND membership.state = 'active'
|
|
AND (
|
|
$2::timestamptz IS NULL
|
|
OR (membership.created_at, membership.organization_id) < (
|
|
$2::timestamptz,
|
|
$3::uuid
|
|
)
|
|
)
|
|
ORDER BY membership.created_at DESC, membership.organization_id DESC
|
|
LIMIT $4
|
|
`
|
|
|
|
type ListOrganizationsForUserParams struct {
|
|
UserID pgtype.UUID
|
|
AfterJoinedAt pgtype.Timestamptz
|
|
AfterOrganizationID pgtype.UUID
|
|
RowLimit int32
|
|
}
|
|
|
|
type ListOrganizationsForUserRow struct {
|
|
ID pgtype.UUID
|
|
Name string
|
|
VerificationLevel string
|
|
MembershipID pgtype.UUID
|
|
MembershipRole string
|
|
MembershipState string
|
|
MembershipCreatedAt pgtype.Timestamptz
|
|
MembershipUpdatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// Lists the organizations where an account has an active membership,
|
|
// newest membership first. The page continues after the (membership
|
|
// creation time, organization ID) position of the last item of the
|
|
// previous page. Only the active memberships of the given account match.
|
|
func (q *Queries) ListOrganizationsForUser(ctx context.Context, arg ListOrganizationsForUserParams) ([]ListOrganizationsForUserRow, error) {
|
|
rows, err := q.db.Query(ctx, listOrganizationsForUser,
|
|
arg.UserID,
|
|
arg.AfterJoinedAt,
|
|
arg.AfterOrganizationID,
|
|
arg.RowLimit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListOrganizationsForUserRow
|
|
for rows.Next() {
|
|
var i ListOrganizationsForUserRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.VerificationLevel,
|
|
&i.MembershipID,
|
|
&i.MembershipRole,
|
|
&i.MembershipState,
|
|
&i.MembershipCreatedAt,
|
|
&i.MembershipUpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const saveOrganizationCreationResult = `-- name: SaveOrganizationCreationResult :exec
|
|
INSERT INTO organization_creation_results (scope_hash, snapshot)
|
|
VALUES ($1, $2)
|
|
ON CONFLICT (scope_hash) DO UPDATE
|
|
SET snapshot = excluded.snapshot
|
|
`
|
|
|
|
type SaveOrganizationCreationResultParams struct {
|
|
ScopeHash []byte
|
|
Snapshot []byte
|
|
}
|
|
|
|
// Stores the immutable domain snapshot of a successful create command. The
|
|
// generic idempotency row and this result are committed with the organization
|
|
// and owner membership. A reused expired scope replaces the snapshot in the
|
|
// same transaction as the new organization.
|
|
func (q *Queries) SaveOrganizationCreationResult(ctx context.Context, arg SaveOrganizationCreationResultParams) error {
|
|
_, err := q.db.Exec(ctx, saveOrganizationCreationResult, arg.ScopeHash, arg.Snapshot)
|
|
return err
|
|
}
|
|
|
|
const updateOrganizationProfile = `-- name: UpdateOrganizationProfile :one
|
|
WITH target AS (
|
|
SELECT id FROM organizations
|
|
WHERE
|
|
id = $9
|
|
AND version = $10
|
|
FOR UPDATE
|
|
),
|
|
|
|
actor AS (
|
|
SELECT memberships.id FROM memberships
|
|
INNER JOIN target ON memberships.organization_id = target.id
|
|
WHERE
|
|
memberships.user_id = $11
|
|
AND memberships.state = 'active'
|
|
AND memberships.role = any($12::text [])
|
|
FOR SHARE OF memberships
|
|
)
|
|
|
|
UPDATE organizations
|
|
SET
|
|
name = $1,
|
|
description = $2,
|
|
sphere = $3,
|
|
contact_website = $4,
|
|
contact_email = $5,
|
|
contact_phone = $6,
|
|
contact_telegram = $7,
|
|
updated_at = $8,
|
|
version = organizations.version + 1
|
|
FROM actor
|
|
WHERE
|
|
organizations.id = $9
|
|
AND organizations.version = $10
|
|
RETURNING
|
|
organizations.id,
|
|
organizations.name,
|
|
organizations.description,
|
|
organizations.sphere,
|
|
organizations.contact_website,
|
|
organizations.contact_email,
|
|
organizations.contact_phone,
|
|
organizations.contact_telegram,
|
|
organizations.verification_level,
|
|
organizations.verification_basis,
|
|
organizations.verification_changed_at,
|
|
organizations.version,
|
|
organizations.created_at,
|
|
organizations.updated_at
|
|
`
|
|
|
|
type UpdateOrganizationProfileParams struct {
|
|
Name string
|
|
Description pgtype.Text
|
|
Sphere pgtype.Text
|
|
ContactWebsite pgtype.Text
|
|
ContactEmail pgtype.Text
|
|
ContactPhone pgtype.Text
|
|
ContactTelegram pgtype.Text
|
|
UpdatedAt pgtype.Timestamptz
|
|
ID pgtype.UUID
|
|
ExpectedVersion int64
|
|
ActorID pgtype.UUID
|
|
ManagerRoles []string
|
|
}
|
|
|
|
// Changes the profile of an organization over the version the caller read
|
|
// and advances the version in the same statement. It names no verification
|
|
// column, so a profile update cannot move the trust level. The change
|
|
// applies only for an account whose active membership has one of the
|
|
// manager roles, checked in the statement itself so that a membership that
|
|
// ended after the caller's read still stops the write. The statement locks
|
|
// the organization row first and then the actor's membership row FOR SHARE,
|
|
// so a revocation that commits while the statement waits is seen again
|
|
// (EvalPlanQual) and stops the write; the order organizations, then
|
|
// memberships, is the lock order of every writer (member management must
|
|
// follow it). No row returns for a stale version, an account that is no
|
|
// active manager, or an unknown organization; the adapter reads the
|
|
// membership to tell them apart.
|
|
func (q *Queries) UpdateOrganizationProfile(ctx context.Context, arg UpdateOrganizationProfileParams) (Organization, error) {
|
|
row := q.db.QueryRow(ctx, updateOrganizationProfile,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.Sphere,
|
|
arg.ContactWebsite,
|
|
arg.ContactEmail,
|
|
arg.ContactPhone,
|
|
arg.ContactTelegram,
|
|
arg.UpdatedAt,
|
|
arg.ID,
|
|
arg.ExpectedVersion,
|
|
arg.ActorID,
|
|
arg.ManagerRoles,
|
|
)
|
|
var i Organization
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Sphere,
|
|
&i.ContactWebsite,
|
|
&i.ContactEmail,
|
|
&i.ContactPhone,
|
|
&i.ContactTelegram,
|
|
&i.VerificationLevel,
|
|
&i.VerificationBasis,
|
|
&i.VerificationChangedAt,
|
|
&i.Version,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|