-- Document Center module tables -- Safe to run multiple times (CREATE TABLE IF NOT EXISTS, CREATE INDEX IF NOT EXISTS). CREATE TABLE IF NOT EXISTS "document_template_categories" ( "id" serial PRIMARY KEY NOT NULL, "organization_id" integer NOT NULL, "name" varchar(100) NOT NULL, "icon" varchar(50) DEFAULT 'file-text', "color" varchar(7) DEFAULT '#1976d2', "sort_order" integer DEFAULT 0, "created_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "document_templates" ( "id" serial PRIMARY KEY NOT NULL, "organization_id" integer NOT NULL, "category_id" integer, "form_id" integer, "name" varchar(200) NOT NULL, "description" text, "content" text NOT NULL, "page_orientation" varchar(10) DEFAULT 'portrait', "page_size" varchar(10) DEFAULT 'A4', "margins" jsonb DEFAULT '{"top": 1440, "right": 1440, "bottom": 1440, "left": 1440}'::jsonb, "asset_placements" jsonb DEFAULT '[]'::jsonb, "status" varchar(20) DEFAULT 'draft' NOT NULL, "created_by" integer NOT NULL, "created_at" timestamp DEFAULT now(), "updated_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "template_variables" ( "id" serial PRIMARY KEY NOT NULL, "template_id" integer NOT NULL, "code" varchar(100) NOT NULL, "label" varchar(200), "description" text, "source" varchar(30) NOT NULL, "mapping" jsonb DEFAULT '{}'::jsonb, "default_value" text, "sort_order" integer DEFAULT 0, "created_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "organization_assets" ( "id" serial PRIMARY KEY NOT NULL, "organization_id" integer NOT NULL, "type" varchar(20) NOT NULL, "label" varchar(200) NOT NULL, "file_key" varchar(500) NOT NULL, "user_id" integer, "position" varchar(200), "settings" jsonb DEFAULT '{}'::jsonb, "is_active" boolean DEFAULT true, "created_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "document_generations" ( "id" serial PRIMARY KEY NOT NULL, "organization_id" integer NOT NULL, "template_id" integer NOT NULL, "task_id" integer, "generated_by" integer, "template_snapshot" text NOT NULL, "substituted_data" jsonb DEFAULT '{}'::jsonb NOT NULL, "unresolved_variables" text[], "pdf_file_key" varchar(500), "docx_file_key" varchar(500), "status" varchar(20) DEFAULT 'pending' NOT NULL, "error_message" text, "generation_duration_ms" integer, "signature_status" varchar(20) DEFAULT 'unsigned', "created_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "document_packages" ( "id" serial PRIMARY KEY NOT NULL, "organization_id" integer NOT NULL, "name" varchar(200) NOT NULL, "description" text, "category" varchar(50) DEFAULT 'general', "is_active" boolean DEFAULT true, "created_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "document_package_items" ( "id" serial PRIMARY KEY NOT NULL, "package_id" integer NOT NULL, "template_id" integer NOT NULL, "sort_order" integer DEFAULT 0, "custom_name" varchar(200), "condition" jsonb ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "document_signatures" ( "id" serial PRIMARY KEY NOT NULL, "document_generation_id" integer NOT NULL, "type" varchar(20) DEFAULT 'manual' NOT NULL, "status" varchar(20) DEFAULT 'pending' NOT NULL, "signed_by" integer, "signed_at" timestamp, "comment" text, "is_current" boolean DEFAULT true, "created_at" timestamp DEFAULT now() ); --> statement-breakpoint -- Foreign keys ALTER TABLE "document_template_categories" DROP CONSTRAINT IF EXISTS "document_template_categories_organization_id_organizations_id_fk"; ALTER TABLE "document_template_categories" ADD CONSTRAINT "document_template_categories_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE cascade; ALTER TABLE "document_templates" DROP CONSTRAINT IF EXISTS "document_templates_organization_id_organizations_id_fk"; ALTER TABLE "document_templates" ADD CONSTRAINT "document_templates_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE cascade; ALTER TABLE "document_templates" DROP CONSTRAINT IF EXISTS "document_templates_category_id_document_template_categories_id_fk"; ALTER TABLE "document_templates" ADD CONSTRAINT "document_templates_category_id_document_template_categories_id_fk" FOREIGN KEY ("category_id") REFERENCES "document_template_categories"("id") ON DELETE set null; ALTER TABLE "document_templates" DROP CONSTRAINT IF EXISTS "document_templates_form_id_forms_id_fk"; ALTER TABLE "document_templates" ADD CONSTRAINT "document_templates_form_id_forms_id_fk" FOREIGN KEY ("form_id") REFERENCES "forms"("id") ON DELETE set null; ALTER TABLE "document_templates" DROP CONSTRAINT IF EXISTS "document_templates_created_by_users_id_fk"; ALTER TABLE "document_templates" ADD CONSTRAINT "document_templates_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "users"("id"); ALTER TABLE "template_variables" DROP CONSTRAINT IF EXISTS "template_variables_template_id_document_templates_id_fk"; ALTER TABLE "template_variables" ADD CONSTRAINT "template_variables_template_id_document_templates_id_fk" FOREIGN KEY ("template_id") REFERENCES "document_templates"("id") ON DELETE cascade; ALTER TABLE "organization_assets" DROP CONSTRAINT IF EXISTS "organization_assets_organization_id_organizations_id_fk"; ALTER TABLE "organization_assets" ADD CONSTRAINT "organization_assets_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE cascade; ALTER TABLE "organization_assets" DROP CONSTRAINT IF EXISTS "organization_assets_user_id_users_id_fk"; ALTER TABLE "organization_assets" ADD CONSTRAINT "organization_assets_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE set null; ALTER TABLE "document_generations" DROP CONSTRAINT IF EXISTS "document_generations_organization_id_organizations_id_fk"; ALTER TABLE "document_generations" ADD CONSTRAINT "document_generations_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE cascade; ALTER TABLE "document_generations" DROP CONSTRAINT IF EXISTS "document_generations_template_id_document_templates_id_fk"; ALTER TABLE "document_generations" ADD CONSTRAINT "document_generations_template_id_document_templates_id_fk" FOREIGN KEY ("template_id") REFERENCES "document_templates"("id"); ALTER TABLE "document_generations" DROP CONSTRAINT IF EXISTS "document_generations_task_id_tasks_id_fk"; ALTER TABLE "document_generations" ADD CONSTRAINT "document_generations_task_id_tasks_id_fk" FOREIGN KEY ("task_id") REFERENCES "tasks"("id") ON DELETE set null; ALTER TABLE "document_generations" DROP CONSTRAINT IF EXISTS "document_generations_generated_by_users_id_fk"; ALTER TABLE "document_generations" ADD CONSTRAINT "document_generations_generated_by_users_id_fk" FOREIGN KEY ("generated_by") REFERENCES "users"("id") ON DELETE set null; ALTER TABLE "document_packages" DROP CONSTRAINT IF EXISTS "document_packages_organization_id_organizations_id_fk"; ALTER TABLE "document_packages" ADD CONSTRAINT "document_packages_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE cascade; ALTER TABLE "document_package_items" DROP CONSTRAINT IF EXISTS "document_package_items_package_id_document_packages_id_fk"; ALTER TABLE "document_package_items" ADD CONSTRAINT "document_package_items_package_id_document_packages_id_fk" FOREIGN KEY ("package_id") REFERENCES "document_packages"("id") ON DELETE cascade; ALTER TABLE "document_package_items" DROP CONSTRAINT IF EXISTS "document_package_items_template_id_document_templates_id_fk"; ALTER TABLE "document_package_items" ADD CONSTRAINT "document_package_items_template_id_document_templates_id_fk" FOREIGN KEY ("template_id") REFERENCES "document_templates"("id"); ALTER TABLE "document_signatures" DROP CONSTRAINT IF EXISTS "document_signatures_document_generation_id_document_generations_id_fk"; ALTER TABLE "document_signatures" ADD CONSTRAINT "document_signatures_document_generation_id_document_generations_id_fk" FOREIGN KEY ("document_generation_id") REFERENCES "document_generations"("id") ON DELETE cascade; ALTER TABLE "document_signatures" DROP CONSTRAINT IF EXISTS "document_signatures_signed_by_users_id_fk"; ALTER TABLE "document_signatures" ADD CONSTRAINT "document_signatures_signed_by_users_id_fk" FOREIGN KEY ("signed_by") REFERENCES "users"("id") ON DELETE set null; -- Indexes CREATE INDEX IF NOT EXISTS "document_templates_org_status_idx" ON "document_templates" ("organization_id", "status"); CREATE INDEX IF NOT EXISTS "document_templates_org_category_idx" ON "document_templates" ("organization_id", "category_id"); CREATE UNIQUE INDEX IF NOT EXISTS "template_variables_code_unique" ON "template_variables" ("template_id", "code"); CREATE INDEX IF NOT EXISTS "organization_assets_org_type_idx" ON "organization_assets" ("organization_id", "type"); CREATE INDEX IF NOT EXISTS "document_generations_org_status_idx" ON "document_generations" ("organization_id", "status"); CREATE INDEX IF NOT EXISTS "document_generations_template_idx" ON "document_generations" ("template_id"); CREATE INDEX IF NOT EXISTS "document_generations_task_idx" ON "document_generations" ("task_id");