feat(org): add organization and membership contract and storage
This commit is contained in:
1 parent
a0551a34b8
commit
bdc5855001
90 files changed
+17545
-480
No files matched your search
@@ -0,0 +1,224 @@
|
||||
-- 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.
|
||||
-- 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 (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(name),
|
||||
sqlc.narg(description),
|
||||
sqlc.narg(sphere),
|
||||
sqlc.narg(contact_website),
|
||||
sqlc.narg(contact_email),
|
||||
sqlc.narg(contact_phone),
|
||||
sqlc.narg(contact_telegram),
|
||||
sqlc.arg(verification_level),
|
||||
sqlc.arg(verification_basis),
|
||||
sqlc.arg(verification_changed_at),
|
||||
sqlc.arg(version),
|
||||
sqlc.arg(created_at),
|
||||
sqlc.arg(updated_at)
|
||||
);
|
||||
|
||||
-- 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.
|
||||
-- name: InsertOrganizationRepresentative :exec
|
||||
INSERT INTO organization_representatives (
|
||||
organization_id,
|
||||
ordinal,
|
||||
name,
|
||||
job_title
|
||||
) VALUES (
|
||||
sqlc.arg(organization_id),
|
||||
sqlc.arg(ordinal),
|
||||
sqlc.arg(name),
|
||||
sqlc.arg(job_title)
|
||||
);
|
||||
|
||||
-- name: DeleteOrganizationRepresentatives :exec
|
||||
DELETE FROM organization_representatives
|
||||
WHERE organization_id = sqlc.arg(organization_id);
|
||||
|
||||
-- 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.
|
||||
-- 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 = sqlc.arg(organization_id)
|
||||
AND membership.user_id = sqlc.arg(user_id)
|
||||
AND membership.state = 'active';
|
||||
|
||||
-- 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.
|
||||
-- name: UpdateOrganizationProfile :one
|
||||
WITH target AS (
|
||||
SELECT id FROM organizations
|
||||
WHERE
|
||||
id = sqlc.arg(id)
|
||||
AND version = sqlc.arg(expected_version)
|
||||
FOR UPDATE
|
||||
),
|
||||
|
||||
actor AS (
|
||||
SELECT memberships.id FROM memberships
|
||||
INNER JOIN target ON memberships.organization_id = target.id
|
||||
WHERE
|
||||
memberships.user_id = sqlc.arg(actor_id)
|
||||
AND memberships.state = 'active'
|
||||
AND memberships.role = any(sqlc.arg(manager_roles)::text [])
|
||||
FOR SHARE OF memberships
|
||||
)
|
||||
|
||||
UPDATE organizations
|
||||
SET
|
||||
name = sqlc.arg(name),
|
||||
description = sqlc.narg(description),
|
||||
sphere = sqlc.narg(sphere),
|
||||
contact_website = sqlc.narg(contact_website),
|
||||
contact_email = sqlc.narg(contact_email),
|
||||
contact_phone = sqlc.narg(contact_phone),
|
||||
contact_telegram = sqlc.narg(contact_telegram),
|
||||
updated_at = sqlc.arg(updated_at),
|
||||
version = organizations.version + 1
|
||||
FROM actor
|
||||
WHERE
|
||||
organizations.id = sqlc.arg(id)
|
||||
AND organizations.version = sqlc.arg(expected_version)
|
||||
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;
|
||||
|
||||
-- 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.
|
||||
-- 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 = sqlc.arg(user_id)
|
||||
AND membership.state = 'active'
|
||||
AND (
|
||||
sqlc.narg(after_joined_at)::timestamptz IS NULL
|
||||
OR (membership.created_at, membership.organization_id) < (
|
||||
sqlc.narg(after_joined_at)::timestamptz,
|
||||
sqlc.narg(after_organization_id)::uuid
|
||||
)
|
||||
)
|
||||
ORDER BY membership.created_at DESC, membership.organization_id DESC
|
||||
LIMIT sqlc.arg(row_limit);
|
||||
|
||||
-- name: InsertMembership :exec
|
||||
INSERT INTO memberships (
|
||||
id,
|
||||
organization_id,
|
||||
user_id,
|
||||
role,
|
||||
state,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(organization_id),
|
||||
sqlc.arg(user_id),
|
||||
sqlc.arg(role),
|
||||
sqlc.arg(state),
|
||||
sqlc.arg(created_at),
|
||||
sqlc.arg(updated_at)
|
||||
);
|
||||
|
||||
-- Reads the membership of one account in one organization, in any state.
|
||||
-- name: GetMembership :one
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
user_id,
|
||||
role,
|
||||
state,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM memberships
|
||||
WHERE
|
||||
organization_id = sqlc.arg(organization_id)
|
||||
AND user_id = sqlc.arg(user_id);
|
||||
Reference in new issue
Block a user