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,227 @@
|
||||
-- +goose Up
|
||||
-- Organizations, their public representatives, and the memberships that give
|
||||
-- an account a role in an organization (PRD 2.2, FR07, and 6.4). Enum-like
|
||||
-- columns are text with CHECK constraints, as in the identity tables.
|
||||
--
|
||||
-- The organization row holds the profile that the owner edits and the
|
||||
-- verification level that only the server sets. The columns of the two are
|
||||
-- separate on purpose: the profile update statement never names the
|
||||
-- verification columns, so editing a profile cannot move the level. Every
|
||||
-- time comes from the application clock.
|
||||
--
|
||||
-- The rows hold personal data of employees (representative names, the
|
||||
-- membership of an account), so the application never copies them into
|
||||
-- logs, metrics, or events beyond identifiers.
|
||||
CREATE TABLE organizations (
|
||||
-- UUIDv7 generated in Go. It is the org_id of every organization path.
|
||||
id uuid NOT NULL,
|
||||
-- The name consent records snapshot, so the bound equals the bound of
|
||||
-- consent_records.organization_name.
|
||||
name text NOT NULL,
|
||||
description text,
|
||||
-- Field of business as free text; the product has no sphere taxonomy.
|
||||
sphere text,
|
||||
-- Public company contacts only, never personal ones. Each is NULL or a
|
||||
-- value in the format of the contract; the domain checks them in full.
|
||||
contact_website text,
|
||||
contact_email text,
|
||||
contact_phone text,
|
||||
contact_telegram text,
|
||||
-- Trust level (PRD FR07). It starts at e0 and moves only by a
|
||||
-- verification process of the server, which will record the change in
|
||||
-- its own history table. No profile statement writes these columns.
|
||||
verification_level text NOT NULL,
|
||||
verification_basis text NOT NULL,
|
||||
verification_changed_at timestamptz NOT NULL,
|
||||
-- Optimistic concurrency version of the profile, advanced by every
|
||||
-- profile update.
|
||||
version bigint NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
CONSTRAINT organizations_pkey PRIMARY KEY (id),
|
||||
CONSTRAINT organizations_name_format CHECK (
|
||||
char_length(name) BETWEEN 1 AND 200
|
||||
AND name !~ '^\s|\s$'
|
||||
AND name !~ '[[:cntrl:]]'
|
||||
),
|
||||
-- Line breaks are allowed inside the text; every other control
|
||||
-- character, and whitespace at the ends, are not.
|
||||
CONSTRAINT organizations_description_format CHECK (
|
||||
description IS NULL OR (
|
||||
char_length(description) BETWEEN 1 AND 2000
|
||||
AND description !~ '^\s|\s$'
|
||||
AND translate(description, E'\n', '') !~ '[[:cntrl:]]'
|
||||
)
|
||||
),
|
||||
CONSTRAINT organizations_sphere_format CHECK (
|
||||
sphere IS NULL OR (
|
||||
char_length(sphere) BETWEEN 1 AND 100
|
||||
AND sphere !~ '^\s|\s$'
|
||||
AND sphere !~ '[[:cntrl:]]'
|
||||
)
|
||||
),
|
||||
CONSTRAINT organizations_contact_website_format CHECK (
|
||||
contact_website IS NULL OR (
|
||||
char_length(contact_website) BETWEEN 9 AND 300
|
||||
AND contact_website ~ '^https://[^\s/?#@]+([/?#]\S*)?$'
|
||||
)
|
||||
),
|
||||
CONSTRAINT organizations_contact_email_format CHECK (
|
||||
contact_email IS NULL OR (
|
||||
char_length(contact_email) BETWEEN 3 AND 254
|
||||
AND contact_email ~ '^[^@\s]+@[^@\s]+$'
|
||||
)
|
||||
),
|
||||
CONSTRAINT organizations_contact_phone_format CHECK (
|
||||
contact_phone IS NULL OR contact_phone ~ '^\+[1-9][0-9]{6,14}$'
|
||||
),
|
||||
CONSTRAINT organizations_contact_telegram_format CHECK (
|
||||
contact_telegram IS NULL OR contact_telegram ~ '^@[A-Za-z0-9_]{5,32}$'
|
||||
),
|
||||
-- Each level has its own bases (PRD FR07), so a level never carries the
|
||||
-- ground of another one.
|
||||
CONSTRAINT organizations_verification_known CHECK (
|
||||
(verification_level = 'e0' AND verification_basis = 'confirmed_email')
|
||||
OR (
|
||||
verification_level = 'e1'
|
||||
AND verification_basis = 'corporate_domain'
|
||||
)
|
||||
OR (
|
||||
verification_level = 'e2'
|
||||
AND verification_basis = 'requisites_and_authority'
|
||||
)
|
||||
OR (
|
||||
verification_level = 'e3'
|
||||
AND verification_basis IN ('manual_review', 'partner_status')
|
||||
)
|
||||
),
|
||||
CONSTRAINT organizations_version_positive CHECK (version > 0),
|
||||
CONSTRAINT organizations_timestamps_after_creation CHECK (
|
||||
updated_at >= created_at
|
||||
AND verification_changed_at >= created_at
|
||||
)
|
||||
);
|
||||
|
||||
-- The representatives that the owner presents on the card. They are
|
||||
-- presentation only: no account link, no right. The list is replaced as a
|
||||
-- whole with the organization update, in the order of ordinal.
|
||||
CREATE TABLE organization_representatives (
|
||||
organization_id uuid NOT NULL,
|
||||
-- Position in the list, from 0; at most ten representatives.
|
||||
ordinal smallint NOT NULL,
|
||||
name text NOT NULL,
|
||||
job_title text NOT NULL,
|
||||
CONSTRAINT organization_representatives_pkey PRIMARY KEY (
|
||||
organization_id, ordinal
|
||||
),
|
||||
-- The list is part of the organization row and goes with it.
|
||||
CONSTRAINT organization_representatives_organization_id_fkey FOREIGN KEY (
|
||||
organization_id
|
||||
) REFERENCES organizations (id) ON DELETE CASCADE,
|
||||
CONSTRAINT organization_representatives_ordinal_range CHECK (
|
||||
ordinal BETWEEN 0 AND 9
|
||||
),
|
||||
CONSTRAINT organization_representatives_name_format CHECK (
|
||||
char_length(name) BETWEEN 1 AND 100
|
||||
AND name !~ '^\s|\s$'
|
||||
AND name !~ '[[:cntrl:]]'
|
||||
),
|
||||
CONSTRAINT organization_representatives_job_title_format CHECK (
|
||||
char_length(job_title) BETWEEN 1 AND 100
|
||||
AND job_title !~ '^\s|\s$'
|
||||
AND job_title !~ '[[:cntrl:]]'
|
||||
)
|
||||
);
|
||||
|
||||
-- One row per account and organization, whatever its role and state: a
|
||||
-- former member who returns gets the same row back in the active state, and
|
||||
-- the history of the pair belongs to the audit events. The role lives only
|
||||
-- here, never on the organization. Only an active membership gives access;
|
||||
-- the full role matrix (PRD FR07) is applied by the application, and a row
|
||||
-- grants nothing by itself.
|
||||
CREATE TABLE memberships (
|
||||
-- UUIDv7 generated in Go; the planned assignments of needs refer to it.
|
||||
id uuid NOT NULL,
|
||||
organization_id uuid NOT NULL,
|
||||
user_id uuid NOT NULL,
|
||||
-- owner, recruiter, hiring_manager, or technical_expert.
|
||||
role text NOT NULL,
|
||||
-- active, suspended, removed (by the owner or the platform), or left (by
|
||||
-- the member).
|
||||
state text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
CONSTRAINT memberships_pkey PRIMARY KEY (id),
|
||||
-- A repeated membership of the same pair fails here. The leading
|
||||
-- user_id also serves the list of an account's organizations.
|
||||
CONSTRAINT memberships_user_id_organization_id_key UNIQUE (
|
||||
user_id, organization_id
|
||||
),
|
||||
-- Target of the composite foreign keys that later tables use to bind a
|
||||
-- membership to its organization, such as the assignments of needs.
|
||||
CONSTRAINT memberships_organization_id_id_key UNIQUE (
|
||||
organization_id, id
|
||||
),
|
||||
-- Both references restrict: an organization with members and an account
|
||||
-- with memberships cannot be deleted until an explicit procedure ends
|
||||
-- the memberships first. The application never deletes an organization.
|
||||
CONSTRAINT memberships_organization_id_fkey FOREIGN KEY (
|
||||
organization_id
|
||||
) REFERENCES organizations (id) ON DELETE RESTRICT,
|
||||
CONSTRAINT memberships_user_id_fkey FOREIGN KEY (
|
||||
user_id
|
||||
) REFERENCES users (id) ON DELETE RESTRICT,
|
||||
CONSTRAINT memberships_role_known CHECK (
|
||||
role IN ('owner', 'recruiter', 'hiring_manager', 'technical_expert')
|
||||
),
|
||||
CONSTRAINT memberships_state_known CHECK (
|
||||
state IN ('active', 'suspended', 'removed', 'left')
|
||||
),
|
||||
CONSTRAINT memberships_updated_after_creation CHECK (
|
||||
updated_at >= created_at
|
||||
)
|
||||
);
|
||||
|
||||
-- Serves the members of an organization and the foreign key.
|
||||
CREATE INDEX memberships_organization_id_idx
|
||||
ON memberships (organization_id);
|
||||
|
||||
-- Refuses an organization that has no active owner when the transaction that
|
||||
-- inserted it commits (SQLSTATE 23514, constraint
|
||||
-- organizations_owner_required). The check is deferred, so the organization
|
||||
-- and its owner membership are inserted in either order inside one
|
||||
-- transaction, and a transaction that fails between the two statements
|
||||
-- leaves neither. Removing or demoting the last owner later is a rule of
|
||||
-- the member management, not of this constraint.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION organizations_require_owner() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM memberships
|
||||
WHERE
|
||||
memberships.organization_id = NEW.id
|
||||
AND memberships.role = 'owner'
|
||||
AND memberships.state = 'active'
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'organizations_owner_required',
|
||||
TABLE = 'organizations',
|
||||
MESSAGE = 'an organization needs an active owner';
|
||||
END IF;
|
||||
RETURN NULL;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE CONSTRAINT TRIGGER organizations_owner_required
|
||||
AFTER INSERT ON organizations
|
||||
DEFERRABLE INITIALLY DEFERRED
|
||||
FOR EACH ROW EXECUTE FUNCTION organizations_require_owner();
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE memberships;
|
||||
DROP TABLE organization_representatives;
|
||||
DROP TABLE organizations;
|
||||
DROP FUNCTION organizations_require_owner();
|
||||
@@ -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