import { MigrationInterface, QueryRunner } from 'typeorm'; export class PhaseF4DocumentFlowFoundation1790035200000 implements MigrationInterface { name = 'PhaseF4DocumentFlowFoundation1790035200000'; public async up(queryRunner: QueryRunner): Promise { await queryRunner.query(` ALTER TABLE inspection_acts ADD COLUMN urgency varchar(20), ADD COLUMN deadline_days integer, ADD COLUMN deadline_day_type varchar(20), ADD COLUMN deadline_basis varchar(24), ADD COLUMN deadline_base_on date, ADD COLUMN deadline_due_on date, ADD COLUMN deadline_policy_snapshot jsonb, ADD COLUMN locked_at timestamptz, ADD COLUMN locked_by uuid, ADD COLUMN locked_sha256 char(64), ADD COLUMN sealed_at timestamptz, ADD COLUMN sealed_by uuid `); await queryRunner.query(` ALTER TABLE inspection_acts ADD CONSTRAINT chk_inspection_acts_urgency CHECK (urgency IS NULL OR urgency IN ('URGENT','NON_URGENT')), ADD CONSTRAINT chk_inspection_acts_deadline_days CHECK (deadline_days IS NULL OR deadline_days > 0), ADD CONSTRAINT chk_inspection_acts_deadline_day_type CHECK (deadline_day_type IS NULL OR deadline_day_type IN ('BUSINESS','CALENDAR')), ADD CONSTRAINT chk_inspection_acts_deadline_basis CHECK (deadline_basis IS NULL OR deadline_basis IN ('ACT_DATE','GEDO_LOAD_DATE')), ADD CONSTRAINT fk_inspection_acts_locked_by FOREIGN KEY (locked_by) REFERENCES users(id) ON DELETE SET NULL, ADD CONSTRAINT fk_inspection_acts_sealed_by FOREIGN KEY (sealed_by) REFERENCES users(id) ON DELETE SET NULL `); await queryRunner.query(` CREATE INDEX idx_inspection_acts_deadline_due_on ON inspection_acts(deadline_due_on) WHERE deadline_due_on IS NOT NULL `); await queryRunner.query(` CREATE TABLE inspection_deadline_policies ( urgency varchar(20) PRIMARY KEY, days integer NOT NULL, day_type varchar(20) NOT NULL, basis varchar(24) NOT NULL, updated_by uuid, created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT chk_inspection_deadline_policies_urgency CHECK (urgency IN ('URGENT','NON_URGENT')), CONSTRAINT chk_inspection_deadline_policies_days CHECK (days > 0), CONSTRAINT chk_inspection_deadline_policies_day_type CHECK (day_type IN ('BUSINESS','CALENDAR')), CONSTRAINT chk_inspection_deadline_policies_basis CHECK (basis IN ('ACT_DATE','GEDO_LOAD_DATE')), CONSTRAINT fk_inspection_deadline_policies_updated_by FOREIGN KEY (updated_by) REFERENCES users(id) ON DELETE SET NULL ) `); await queryRunner.query(` INSERT INTO inspection_deadline_policies (urgency, days, day_type, basis) VALUES ('URGENT', 5, 'BUSINESS', 'ACT_DATE'), ('NON_URGENT', 10, 'BUSINESS', 'GEDO_LOAD_DATE') `); await queryRunner.query(` CREATE TABLE inspection_non_working_days ( day date PRIMARY KEY, label varchar(200) NOT NULL, enabled boolean NOT NULL DEFAULT true, created_by uuid, updated_by uuid, created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT chk_inspection_non_working_days_label CHECK (LENGTH(TRIM(label)) > 0), CONSTRAINT fk_inspection_non_working_days_created_by FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL, CONSTRAINT fk_inspection_non_working_days_updated_by FOREIGN KEY (updated_by) REFERENCES users(id) ON DELETE SET NULL ) `); await queryRunner.query(` ALTER TABLE inspection_findings ADD COLUMN is_recurrence boolean NOT NULL DEFAULT false, ADD COLUMN antecedent_finding_id uuid `); await queryRunner.query(` ALTER TABLE inspection_findings ADD CONSTRAINT fk_inspection_findings_antecedent FOREIGN KEY (antecedent_finding_id) REFERENCES inspection_findings(id) ON DELETE RESTRICT, ADD CONSTRAINT chk_inspection_findings_recurrence_link CHECK ( (is_recurrence = false AND antecedent_finding_id IS NULL) OR (is_recurrence = true AND antecedent_finding_id IS NOT NULL) ), ADD CONSTRAINT chk_inspection_findings_not_self_antecedent CHECK (antecedent_finding_id IS NULL OR antecedent_finding_id <> id) `); await queryRunner.query(` CREATE INDEX idx_inspection_findings_antecedent ON inspection_findings(antecedent_finding_id) WHERE antecedent_finding_id IS NOT NULL `); await queryRunner.query(` ALTER TABLE inspection_reports ADD COLUMN reference_text text, ADD COLUMN general_objective text, ADD COLUMN specific_objective text, ADD COLUMN background text, ADD COLUMN legal_framework text, ADD COLUMN executive_summary text, ADD COLUMN description text, ADD COLUMN conclusion text, ADD COLUMN gedo_if_identifier varchar(255), ADD COLUMN gedo_officialized_on date, ADD COLUMN gedo_pdf_original_name varchar(255), ADD COLUMN gedo_pdf_stored_name varchar(255), ADD COLUMN gedo_pdf_mime_type varchar(120), ADD COLUMN gedo_pdf_size_bytes integer, ADD COLUMN gedo_pdf_sha256 char(64), ADD COLUMN gedo_pdf_uploaded_at timestamptz, ADD COLUMN gedo_pdf_uploaded_by uuid `); await queryRunner.query(` ALTER TABLE inspection_reports ADD CONSTRAINT chk_inspection_reports_gedo_pdf_size CHECK (gedo_pdf_size_bytes IS NULL OR gedo_pdf_size_bytes >= 0), ADD CONSTRAINT fk_inspection_reports_gedo_pdf_uploaded_by FOREIGN KEY (gedo_pdf_uploaded_by) REFERENCES users(id) ON DELETE SET NULL `); await queryRunner.query(` CREATE INDEX idx_inspection_reports_gedo_if_identifier ON inspection_reports(gedo_if_identifier) WHERE gedo_if_identifier IS NOT NULL `); await queryRunner.query(` CREATE TABLE inspection_report_follow_ups ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), report_id uuid NOT NULL, event_type varchar(32) NOT NULL, reference_number varchar(255), occurred_on date NOT NULL, description text, created_by uuid, created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT chk_inspection_report_follow_ups_type CHECK (event_type IN ('COMPANY_NOTE','COMPANY_DOCUMENT','INTERNAL_NOTE','VERIFICATION','OTHER')), CONSTRAINT fk_inspection_report_follow_ups_report FOREIGN KEY (report_id) REFERENCES inspection_reports(id) ON DELETE RESTRICT, CONSTRAINT fk_inspection_report_follow_ups_created_by FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL ) `); await queryRunner.query(` CREATE INDEX idx_inspection_report_follow_ups_report_occurred ON inspection_report_follow_ups(report_id, occurred_on, created_at) `); await queryRunner.query(` CREATE INDEX idx_inspection_report_follow_ups_type ON inspection_report_follow_ups(event_type) `); await queryRunner.query(` CREATE TABLE inspection_report_follow_up_files ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), follow_up_id uuid NOT NULL, original_name varchar(255) NOT NULL, stored_name varchar(255) NOT NULL, mime_type varchar(120) NOT NULL, size_bytes integer NOT NULL, sha256 char(64) NOT NULL, created_by uuid, created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT chk_inspection_report_follow_up_files_size CHECK (size_bytes >= 0), CONSTRAINT fk_inspection_report_follow_up_files_follow_up FOREIGN KEY (follow_up_id) REFERENCES inspection_report_follow_ups(id) ON DELETE RESTRICT, CONSTRAINT fk_inspection_report_follow_up_files_created_by FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL ) `); await queryRunner.query(` CREATE INDEX idx_inspection_report_follow_up_files_follow_up ON inspection_report_follow_up_files(follow_up_id) `); } public async down(queryRunner: QueryRunner): Promise { await queryRunner.query(`DROP TABLE IF EXISTS inspection_report_follow_up_files`); await queryRunner.query(`DROP TABLE IF EXISTS inspection_report_follow_ups`); await queryRunner.query(`DROP INDEX IF EXISTS idx_inspection_reports_gedo_if_identifier`); await queryRunner.query(` ALTER TABLE inspection_reports DROP CONSTRAINT IF EXISTS fk_inspection_reports_gedo_pdf_uploaded_by, DROP CONSTRAINT IF EXISTS chk_inspection_reports_gedo_pdf_size, DROP COLUMN IF EXISTS gedo_pdf_uploaded_by, DROP COLUMN IF EXISTS gedo_pdf_uploaded_at, DROP COLUMN IF EXISTS gedo_pdf_sha256, DROP COLUMN IF EXISTS gedo_pdf_size_bytes, DROP COLUMN IF EXISTS gedo_pdf_mime_type, DROP COLUMN IF EXISTS gedo_pdf_stored_name, DROP COLUMN IF EXISTS gedo_pdf_original_name, DROP COLUMN IF EXISTS gedo_officialized_on, DROP COLUMN IF EXISTS gedo_if_identifier, DROP COLUMN IF EXISTS conclusion, DROP COLUMN IF EXISTS description, DROP COLUMN IF EXISTS executive_summary, DROP COLUMN IF EXISTS legal_framework, DROP COLUMN IF EXISTS background, DROP COLUMN IF EXISTS specific_objective, DROP COLUMN IF EXISTS general_objective, DROP COLUMN IF EXISTS reference_text `); await queryRunner.query(`DROP INDEX IF EXISTS idx_inspection_findings_antecedent`); await queryRunner.query(` ALTER TABLE inspection_findings DROP CONSTRAINT IF EXISTS chk_inspection_findings_not_self_antecedent, DROP CONSTRAINT IF EXISTS chk_inspection_findings_recurrence_link, DROP CONSTRAINT IF EXISTS fk_inspection_findings_antecedent, DROP COLUMN IF EXISTS antecedent_finding_id, DROP COLUMN IF EXISTS is_recurrence `); await queryRunner.query(`DROP TABLE IF EXISTS inspection_non_working_days`); await queryRunner.query(`DROP TABLE IF EXISTS inspection_deadline_policies`); await queryRunner.query(`DROP INDEX IF EXISTS idx_inspection_acts_deadline_due_on`); await queryRunner.query(` ALTER TABLE inspection_acts DROP CONSTRAINT IF EXISTS fk_inspection_acts_sealed_by, DROP CONSTRAINT IF EXISTS fk_inspection_acts_locked_by, DROP CONSTRAINT IF EXISTS chk_inspection_acts_deadline_basis, DROP CONSTRAINT IF EXISTS chk_inspection_acts_deadline_day_type, DROP CONSTRAINT IF EXISTS chk_inspection_acts_deadline_days, DROP CONSTRAINT IF EXISTS chk_inspection_acts_urgency, DROP COLUMN IF EXISTS sealed_by, DROP COLUMN IF EXISTS sealed_at, DROP COLUMN IF EXISTS locked_sha256, DROP COLUMN IF EXISTS locked_by, DROP COLUMN IF EXISTS locked_at, DROP COLUMN IF EXISTS deadline_policy_snapshot, DROP COLUMN IF EXISTS deadline_due_on, DROP COLUMN IF EXISTS deadline_base_on, DROP COLUMN IF EXISTS deadline_basis, DROP COLUMN IF EXISTS deadline_day_type, DROP COLUMN IF EXISTS deadline_days, DROP COLUMN IF EXISTS urgency `); } }