[MAI-009] feat: Add DDL for quality module missing tables

- Add no_conformidades table for non-conformity tracking
- Add acciones_correctivas table for corrective actions
- Add ticket_asignaciones table for ticket assignments

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Adrian Flores Cortes 2026-01-25 08:54:24 -06:00
parent 80dd204db2
commit 9d0457622f

View File

@ -552,6 +552,89 @@ CREATE TABLE IF NOT EXISTS construction.tickets_postventa (
CONSTRAINT uq_tickets_number_tenant UNIQUE (tenant_id, ticket_number)
);
-- Tabla: no_conformidades (no conformidades detectadas)
CREATE TABLE IF NOT EXISTS construction.no_conformidades (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES auth.tenants(id) ON DELETE CASCADE,
inspeccion_id UUID REFERENCES construction.inspecciones(id),
lote_id UUID REFERENCES construction.lotes(id),
nc_number VARCHAR(50) NOT NULL,
detection_date DATE NOT NULL,
category VARCHAR(100),
severity VARCHAR(20) NOT NULL DEFAULT 'minor',
description TEXT NOT NULL,
root_cause TEXT,
photo_url VARCHAR(500),
contractor_id UUID REFERENCES construction.subcontratistas(id),
status VARCHAR(20) NOT NULL DEFAULT 'open',
due_date DATE,
closed_at TIMESTAMPTZ,
closed_by UUID REFERENCES auth.users(id),
verified_at TIMESTAMPTZ,
verified_by UUID REFERENCES auth.users(id),
closure_photo_url VARCHAR(500),
closure_notes TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
created_by UUID REFERENCES auth.users(id),
updated_at TIMESTAMPTZ,
updated_by UUID REFERENCES auth.users(id),
CONSTRAINT uq_nc_number_tenant UNIQUE (tenant_id, nc_number),
CONSTRAINT chk_nc_severity CHECK (severity IN ('minor', 'major', 'critical')),
CONSTRAINT chk_nc_status CHECK (status IN ('open', 'in_progress', 'closed', 'verified'))
);
-- Tabla: acciones_correctivas (CAPA)
CREATE TABLE IF NOT EXISTS construction.acciones_correctivas (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES auth.tenants(id) ON DELETE CASCADE,
no_conformidad_id UUID NOT NULL REFERENCES construction.no_conformidades(id) ON DELETE CASCADE,
action_type VARCHAR(20) NOT NULL DEFAULT 'corrective',
description TEXT NOT NULL,
responsible_id UUID NOT NULL REFERENCES auth.users(id),
due_date DATE NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'pending',
completed_at TIMESTAMPTZ,
completion_notes TEXT,
verified_at TIMESTAMPTZ,
verified_by UUID REFERENCES auth.users(id),
effectiveness_verified BOOLEAN DEFAULT FALSE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
created_by UUID REFERENCES auth.users(id),
updated_at TIMESTAMPTZ,
updated_by UUID REFERENCES auth.users(id),
CONSTRAINT chk_action_type CHECK (action_type IN ('corrective', 'preventive', 'improvement')),
CONSTRAINT chk_action_status CHECK (status IN ('pending', 'in_progress', 'completed', 'verified'))
);
-- Tabla: ticket_asignaciones (asignaciones de tickets a técnicos)
CREATE TABLE IF NOT EXISTS construction.ticket_asignaciones (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES auth.tenants(id) ON DELETE CASCADE,
ticket_id UUID NOT NULL REFERENCES construction.tickets_postventa(id) ON DELETE CASCADE,
technician_id UUID NOT NULL REFERENCES auth.users(id),
assigned_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
assigned_by UUID NOT NULL REFERENCES auth.users(id),
status VARCHAR(20) NOT NULL DEFAULT 'assigned',
accepted_at TIMESTAMPTZ,
scheduled_date DATE,
scheduled_time TIME,
completed_at TIMESTAMPTZ,
work_notes TEXT,
reassignment_reason TEXT,
is_current BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ,
CONSTRAINT chk_assignment_status CHECK (status IN ('assigned', 'accepted', 'in_progress', 'completed', 'reassigned'))
);
-- Indices adicionales para calidad
CREATE INDEX IF NOT EXISTS idx_no_conformidades_tenant ON construction.no_conformidades(tenant_id);
CREATE INDEX IF NOT EXISTS idx_no_conformidades_status ON construction.no_conformidades(status);
CREATE INDEX IF NOT EXISTS idx_no_conformidades_inspeccion ON construction.no_conformidades(inspeccion_id);
CREATE INDEX IF NOT EXISTS idx_acciones_correctivas_nc ON construction.acciones_correctivas(no_conformidad_id);
CREATE INDEX IF NOT EXISTS idx_ticket_asignaciones_ticket ON construction.ticket_asignaciones(ticket_id);
CREATE INDEX IF NOT EXISTS idx_ticket_asignaciones_technician ON construction.ticket_asignaciones(technician_id);
-- ============================================================================
-- TABLES - CONTRATOS Y SUBCONTRATOS (MAI-012)
-- ============================================================================