-- ============================================= -- BANCO DE DADOS: hubglpi -- DESCRIÇÃO: Sistema de integração entre HubSoft e GLPI -- OBS: Chamados sempre são abertos pelo HubSoft -- ============================================= -- ============================================= -- TABELA: hubsoft_tickets -- DESCRIÇÃO: Armazena os chamados originados do HubSoft -- ============================================= CREATE TABLE hubsoft_tickets ( id_atendimento INT PRIMARY KEY, codigo_cliente INTEGER NOT NULL, status_atendimento VARCHAR(64) NOT NULL, servico_nome VARCHAR(255), protocolo_hub VARCHAR(50) UNIQUE NOT NULL, ticket_mundiale INT, cliente_nome VARCHAR(255) NOT NULL, descricao_fechamento TEXT, data_fechamento TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- ============================================= -- TABELA: sync_data -- DESCRIÇÃO: Armazena o estado da sincronização entre HubSoft e GLPI -- ============================================= CREATE TYPE source_last_enum AS ENUM ('hubsoft', 'glpi'); CREATE TABLE sync_data ( id SERIAL PRIMARY KEY, hubsoft_ticket_id INTEGER NOT NULL REFERENCES hubsoft_tickets(id_atendimento), glpi_ticket_id INTEGER, source_last source_last_enum DEFAULT 'hubsoft', status_sync status_sync_enum DEFAULT 'pending_create', sync_metadata JSONB, last_sync_attempt TIMESTAMP, sync_error_message TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UNIQUE (hubsoft_ticket_id, glpi_ticket_id) ); -- ============================================= -- TABELA: sync_comments -- DESCRIÇÃO: Armazena o estado de comentarios entre HubSoft e GLPI -- ============================================= CREATE TABLE sync_comments ( id SERIAL PRIMARY KEY, sync_data_id INTEGER NOT NULL REFERENCES sync_data(id), source_system VARCHAR(20) NOT NULL, -- 'hubsoft' ou 'glpi' source_comment_id VARCHAR(255) NOT NULL, -- ID do comentário no sistema de origem destination_comment_id VARCHAR(255), -- ID do comentário no sistema de destino content TEXT NOT NULL, author VARCHAR(255), -- Nome do autor do comentário (opcional, mas útil) sync_status VARCHAR(50) NOT NULL DEFAULT 'pending_sync', -- ex: 'pending_sync', 'synced', 'sync_error' sync_attempts INTEGER DEFAULT 0, error_message TEXT, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); -- Índice para evitar duplicatas e otimizar buscas CREATE UNIQUE INDEX idx_unique_source_comment ON sync_comments(source_system, source_comment_id); CREATE INDEX idx_sync_status ON sync_comments(sync_status, sync_attempts); -- ============================================= -- TABELA: sync_data -- DESCRIÇÃO: Armazena o estado de comentarios entre HubSoft e GLPI -- ============================================= CREATE TABLE sync_control ( job_name VARCHAR(100) PRIMARY KEY, last_run_timestamp TIMESTAMPTZ NOT NULL ); -- Inserir o registro inicial para o nosso novo cron de comentários INSERT INTO sync_control (job_name, last_run_timestamp) VALUES ('hubsoft_comments_sync', '2024-01-01 00:00:00'); -- ============================================= -- ALTERAÇÕES NA TABELA hubsoft_tickets -- ============================================= ALTER TABLE hubsoft_tickets ADD COLUMN ticket_type VARCHAR(50) NOT NULL DEFAULT 'MUNDIALE'; ALTER TABLE hubsoft_tickets ADD CONSTRAINT hubsoft_id_atendimento_unique UNIQUE (id_atendimento);