Files
tenant-registry/migrations/0011_trial_offers.up.sql
sharangandClaude Opus 5.5 60f20e6419
ci / shared (push) Successful in 13s
ci / test (push) Successful in 23m58s
ci / image (push) Successful in 36s
feat(trial): legal trial signup: offer, confirmation, activation (#58)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-09-30 16:00:52 +00:00

65 lines
3.5 KiB
PL/PgSQL

-- Trial offers (portal#64): the customer's binding offer to conclude the free
-- trial contract (§ 2 Abs. 1 Testbedingungen), recorded when they click
-- "Kostenlosen 14-Tage-Testzugang anfordern". The snapshot columns prove who
-- offered what for which company under which exact contract text; they are
-- written once and never change (trigger below). Breakpilot accepts the offer
-- by activating the trial after the e-mail is confirmed; that moment is the
-- conclusion of the contract and the start of the 14 days.
CREATE TABLE trial_offers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
-- snapshot (immutable)
company_name TEXT NOT NULL CHECK (char_length(company_name) BETWEEN 1 AND 255),
street TEXT NOT NULL CHECK (char_length(street) BETWEEN 1 AND 200),
postcode TEXT NOT NULL CHECK (char_length(postcode) BETWEEN 1 AND 20),
city TEXT NOT NULL CHECK (char_length(city) BETWEEN 1 AND 100),
country TEXT NOT NULL CHECK (country ~ '^[A-Z]{2}$'),
first_name TEXT NOT NULL CHECK (char_length(first_name) BETWEEN 1 AND 100),
last_name TEXT NOT NULL CHECK (char_length(last_name) BETWEEN 1 AND 100),
email TEXT NOT NULL CHECK (char_length(email) BETWEEN 3 AND 254),
terms_document_id TEXT NOT NULL,
terms_version TEXT NOT NULL,
terms_sha256 TEXT NOT NULL CHECK (terms_sha256 ~ '^[0-9a-f]{64}$'),
notice_version TEXT NOT NULL,
flow_version TEXT NOT NULL,
contract_language TEXT NOT NULL DEFAULT 'de',
terms_accepted BOOLEAN NOT NULL CHECK (terms_accepted),
offered_at TIMESTAMPTZ NOT NULL,
-- state
state TEXT NOT NULL DEFAULT 'email_verification_pending'
CHECK (state IN ('email_verification_pending', 'verified', 'activated')),
verify_token_hash TEXT NOT NULL,
verify_expires_at TIMESTAMPTZ NOT NULL,
receipt_mail_sent_at TIMESTAMPTZ,
verified_at TIMESTAMPTZ,
tenant_id UUID REFERENCES tenants(id) ON DELETE SET NULL,
contract_accepted_at TIMESTAMPTZ,
trial_started_at TIMESTAMPTZ,
trial_expires_at TIMESTAMPTZ,
activation_mail_sent_at TIMESTAMPTZ
);
CREATE UNIQUE INDEX trial_offers_token ON trial_offers (verify_token_hash);
CREATE INDEX trial_offers_email ON trial_offers (lower(email), offered_at DESC);
CREATE INDEX trial_offers_tenant ON trial_offers (tenant_id);
CREATE FUNCTION trial_offers_snapshot_immutable() RETURNS trigger AS $$
BEGIN
IF (NEW.company_name, NEW.street, NEW.postcode, NEW.city, NEW.country,
NEW.first_name, NEW.last_name, NEW.email,
NEW.terms_document_id, NEW.terms_version, NEW.terms_sha256,
NEW.notice_version, NEW.flow_version, NEW.contract_language,
NEW.terms_accepted, NEW.offered_at)
IS DISTINCT FROM
(OLD.company_name, OLD.street, OLD.postcode, OLD.city, OLD.country,
OLD.first_name, OLD.last_name, OLD.email,
OLD.terms_document_id, OLD.terms_version, OLD.terms_sha256,
OLD.notice_version, OLD.flow_version, OLD.contract_language,
OLD.terms_accepted, OLD.offered_at) THEN
RAISE EXCEPTION 'trial_offers: the offer snapshot is immutable';
END IF;
RETURN NEW;
END $$ LANGUAGE plpgsql;
CREATE TRIGGER trial_offers_snapshot_immutable
BEFORE UPDATE ON trial_offers
FOR EACH ROW EXECUTE FUNCTION trial_offers_snapshot_immutable();