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();
|
||||
Reference in new issue
Block a user