feat: QuickBooks Online integration

- Add QBO OAuth2 client with token refresh (lib/services/qbo-client.ts)
- Add QBO sync service for invoices, payments, deposits, purchases, journal entries, reports (lib/services/qbo-sync-service.ts)
- Add QBO types (lib/types/qbo.ts)
- Add API routes: /api/qbo/auth, /api/qbo/sync, /api/qbo/disconnect
- Add /admin/qbo status and sync management page
- Add legal pages: /legal/eula, /legal/privacy (Intuit app assessment)
- Add QBO nav link under Admin
- Fix reports: remove invalid summarize_column_by, add accounting_method from Preferences API, add showrows=all&showcols=all
- Add CashFlow report type alongside P&L and BalanceSheet
- Add NoReportData check to skip empty report months
- Add intuit_tid capture in error messages
- Add redirect: follow for cluster routing
- Migration 051: qbo_tokens, qbo_invoices, qbo_payments, qbo_deposits, qbo_transactions, qbo_reports tables

Also includes earlier work:
- Ping flap suppression pipeline step
- Ticket digest reports with LLM analysis
- Zabbix WAN monitor and gap analysis
- Kiosk is_deleted filter fixes
- Datto RMM ping target enrichment
- Entity sync soft-delete detection
This commit is contained in:
lorentz 2026-03-17 07:39:55 -04:00
parent c518eefdb2
commit b98c67482a
40 changed files with 6223 additions and 15 deletions

View file

@ -0,0 +1,17 @@
-- Ping Flap Suppression Table
-- Tracks ping targets that have been auto-suppressed due to flapping behaviour.
-- Used to prevent repeated Zabbix suppression calls and pipeline noise.
CREATE TABLE IF NOT EXISTS ping_flap_suppressions (
ping_target TEXT PRIMARY KEY,
trigger_count INTEGER NOT NULL,
suppressed_until TIMESTAMP NOT NULL,
zabbix_suppressed BOOLEAN NOT NULL DEFAULT false,
notes TEXT,
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_ping_flap_suppressions_until ON ping_flap_suppressions(suppressed_until);
COMMENT ON TABLE ping_flap_suppressions IS 'Tracks auto-suppressed flapping ping targets. Suppression expires at suppressed_until.';

View file

@ -0,0 +1,53 @@
-- Ticket Digest Report tables
-- Stores config, delivery webhooks, and report history for LLM-analyzed ticket digest reports
CREATE TABLE IF NOT EXISTS ticket_digest_config (
id INTEGER PRIMARY KEY DEFAULT 1,
daily_enabled BOOLEAN NOT NULL DEFAULT false,
weekly_enabled BOOLEAN NOT NULL DEFAULT false,
monthly_enabled BOOLEAN NOT NULL DEFAULT false,
daily_cron VARCHAR(50) NOT NULL DEFAULT '0 7 * * 1-5',
weekly_cron VARCHAR(50) NOT NULL DEFAULT '0 7 * * 1',
monthly_cron VARCHAR(50) NOT NULL DEFAULT '0 7 1 * *',
llm_provider VARCHAR(20) NOT NULL DEFAULT 'anthropic',
llm_model VARCHAR(100) NOT NULL DEFAULT 'claude-sonnet-4-20250514',
include_noise_analysis BOOLEAN NOT NULL DEFAULT true,
include_sla_analysis BOOLEAN NOT NULL DEFAULT true,
include_resource_analysis BOOLEAN NOT NULL DEFAULT true,
include_client_analysis BOOLEAN NOT NULL DEFAULT true,
include_recommendations BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
CONSTRAINT single_config CHECK (id = 1)
);
CREATE TABLE IF NOT EXISTS ticket_digest_webhooks (
id SERIAL PRIMARY KEY,
label VARCHAR(200) NOT NULL,
webhook_url TEXT NOT NULL,
digest_types TEXT[] NOT NULL DEFAULT '{daily,weekly,monthly}',
enabled BOOLEAN NOT NULL DEFAULT true,
last_delivered_at TIMESTAMP,
last_status VARCHAR(20),
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS ticket_digest_reports (
id SERIAL PRIMARY KEY,
period_type VARCHAR(10) NOT NULL, -- 'daily', 'weekly', 'monthly'
period_start TIMESTAMP NOT NULL,
period_end TIMESTAMP NOT NULL,
generated_at TIMESTAMP NOT NULL DEFAULT NOW(),
stats JSONB NOT NULL DEFAULT '{}',
llm_analysis TEXT,
card_payload JSONB,
delivery_status JSONB DEFAULT '{}',
tokens_used INTEGER,
processing_time_ms INTEGER,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_ticket_digest_reports_period ON ticket_digest_reports(period_type, period_start DESC);
-- Insert default config
INSERT INTO ticket_digest_config (id) VALUES (1) ON CONFLICT (id) DO NOTHING;

View file

@ -0,0 +1,60 @@
-- Migration 050: Zabbix WAN host cache + IT Glue WAN circuit cache
-- Enables local gap analysis and event correlation without live API calls every time.
-- ────────────────────────────────────────────────────────────────
-- Zabbix WAN host cache
-- Populated by /api/zabbix/sync-hosts (syncs from live Zabbix API)
-- ────────────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS zabbix_wan_hosts (
hostid VARCHAR(50) PRIMARY KEY,
host_name VARCHAR(255), -- technical/sanitized name (host field)
display_name VARCHAR(255), -- human-readable display name
wan_ip VARCHAR(45), -- ICMP interface IP
status INTEGER DEFAULT 0, -- 0=enabled, 1=disabled
rmm_site_uid VARCHAR(100), -- from {$RMM_SITE_UID} macro
autotask_company_id INTEGER, -- from {$AUTOTASK_COMPANY_ID} macro
autotask_company_name VARCHAR(255), -- from {$AUTOTASK_COMPANY_NAME} macro
isp_name VARCHAR(255), -- from {$ISP_NAME} macro
asn VARCHAR(50), -- from {$ASN} macro
is_multi_wan BOOLEAN DEFAULT FALSE,
source VARCHAR(50), -- 'datto-rmm', 'manual', etc.
tags JSONB,
last_problem_at TIMESTAMPTZ, -- most recent Zabbix problem clock
last_problem_name TEXT, -- most recent Zabbix problem name
last_synced_at TIMESTAMPTZ DEFAULT NOW(),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_zabbix_wan_hosts_company ON zabbix_wan_hosts (autotask_company_id);
CREATE INDEX IF NOT EXISTS idx_zabbix_wan_hosts_rmm_site ON zabbix_wan_hosts (rmm_site_uid);
CREATE INDEX IF NOT EXISTS idx_zabbix_wan_hosts_ip ON zabbix_wan_hosts (wan_ip);
-- ────────────────────────────────────────────────────────────────
-- IT Glue WAN circuit cache
-- Populated by /api/itglue/sync-wan (parses flexible_asset_type_id=3794)
-- ────────────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS itg_wan_circuits (
id BIGINT PRIMARY KEY, -- IT Glue flexible asset ID
organization_id BIGINT, -- IT Glue org ID
org_name VARCHAR(255),
autotask_company_id BIGINT, -- via itg_organizations.psa_id::bigint
provider VARCHAR(255),
link_type VARCHAR(100),
static_ips TEXT[], -- parsed public IPs from traits
raw_ip_text TEXT, -- original IT Glue text (for reference)
upload_mbps NUMERIC,
download_mbps NUMERIC,
location_name VARCHAR(255),
location_city VARCHAR(255),
notes TEXT,
is_decommissioned BOOLEAN DEFAULT FALSE,
zabbix_hostid VARCHAR(50), -- matched Zabbix host (NULL = gap)
last_synced_at TIMESTAMPTZ DEFAULT NOW(),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_itg_wan_circuits_org ON itg_wan_circuits (organization_id);
CREATE INDEX IF NOT EXISTS idx_itg_wan_circuits_company ON itg_wan_circuits (autotask_company_id);
CREATE INDEX IF NOT EXISTS idx_itg_wan_circuits_zabbix ON itg_wan_circuits (zabbix_hostid);

View file

@ -0,0 +1,125 @@
-- QBO OAuth2 tokens (single row for Wulf Consulting's QBO)
CREATE TABLE IF NOT EXISTS qbo_tokens (
id SERIAL PRIMARY KEY,
realm_id TEXT NOT NULL UNIQUE,
access_token TEXT NOT NULL,
refresh_token TEXT NOT NULL,
access_token_expires_at TIMESTAMPTZ NOT NULL,
refresh_token_expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Invoices
CREATE TABLE IF NOT EXISTS qbo_invoices (
id TEXT PRIMARY KEY, -- QBO Id
realm_id TEXT NOT NULL,
doc_number TEXT,
txn_date DATE,
due_date DATE,
customer_ref_id TEXT,
customer_ref_name TEXT,
email_address TEXT,
total_amt NUMERIC(12,2),
balance NUMERIC(12,2),
status TEXT, -- Open, Paid, Voided, etc.
currency_code TEXT DEFAULT 'USD',
line_items JSONB,
linked_txns JSONB,
sync_token TEXT,
qbo_created_at TIMESTAMPTZ,
qbo_updated_at TIMESTAMPTZ,
synced_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_qbo_invoices_txn_date ON qbo_invoices(txn_date);
CREATE INDEX IF NOT EXISTS idx_qbo_invoices_customer ON qbo_invoices(customer_ref_id);
CREATE INDEX IF NOT EXISTS idx_qbo_invoices_status ON qbo_invoices(status);
-- Payments
CREATE TABLE IF NOT EXISTS qbo_payments (
id TEXT PRIMARY KEY, -- QBO Id
realm_id TEXT NOT NULL,
txn_date DATE,
customer_ref_id TEXT,
customer_ref_name TEXT,
total_amt NUMERIC(12,2),
unapplied_amt NUMERIC(12,2),
currency_code TEXT DEFAULT 'USD',
payment_method_ref TEXT,
deposit_account_ref TEXT,
linked_txns JSONB,
sync_token TEXT,
qbo_created_at TIMESTAMPTZ,
qbo_updated_at TIMESTAMPTZ,
synced_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_qbo_payments_txn_date ON qbo_payments(txn_date);
CREATE INDEX IF NOT EXISTS idx_qbo_payments_customer ON qbo_payments(customer_ref_id);
-- Deposits
CREATE TABLE IF NOT EXISTS qbo_deposits (
id TEXT PRIMARY KEY, -- QBO Id
realm_id TEXT NOT NULL,
txn_date DATE,
deposit_to_account_ref_id TEXT,
deposit_to_account_ref_name TEXT,
total_amt NUMERIC(12,2),
line_items JSONB,
sync_token TEXT,
qbo_created_at TIMESTAMPTZ,
qbo_updated_at TIMESTAMPTZ,
synced_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_qbo_deposits_txn_date ON qbo_deposits(txn_date);
-- General ledger transactions (purchases, expenses, journal entries, etc.)
CREATE TABLE IF NOT EXISTS qbo_transactions (
id TEXT NOT NULL,
txn_type TEXT NOT NULL, -- Purchase, Expense, JournalEntry, Transfer, etc.
realm_id TEXT NOT NULL,
txn_date DATE,
doc_number TEXT,
entity_ref_id TEXT, -- Vendor/Customer ref
entity_ref_name TEXT,
entity_type TEXT, -- Vendor, Customer
account_ref_id TEXT,
account_ref_name TEXT,
total_amt NUMERIC(12,2),
currency_code TEXT DEFAULT 'USD',
private_note TEXT,
line_items JSONB,
sync_token TEXT,
qbo_created_at TIMESTAMPTZ,
qbo_updated_at TIMESTAMPTZ,
synced_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (id, txn_type)
);
CREATE INDEX IF NOT EXISTS idx_qbo_transactions_txn_date ON qbo_transactions(txn_date);
CREATE INDEX IF NOT EXISTS idx_qbo_transactions_type ON qbo_transactions(txn_type);
CREATE INDEX IF NOT EXISTS idx_qbo_transactions_entity ON qbo_transactions(entity_ref_id);
-- Financial reports: P&L and Balance Sheet stored as JSONB per period
CREATE TABLE IF NOT EXISTS qbo_reports (
id SERIAL PRIMARY KEY,
realm_id TEXT NOT NULL,
report_type TEXT NOT NULL, -- ProfitAndLoss, BalanceSheet
period_start DATE NOT NULL,
period_end DATE NOT NULL,
summarize_by TEXT DEFAULT 'Month',
report_data JSONB NOT NULL, -- Full report response
synced_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE (realm_id, report_type, period_start, period_end)
);
CREATE INDEX IF NOT EXISTS idx_qbo_reports_type_period ON qbo_reports(report_type, period_start);
COMMENT ON TABLE qbo_tokens IS 'QuickBooks Online OAuth2 tokens for Wulf Consulting';
COMMENT ON TABLE qbo_invoices IS 'QBO invoices synced from QuickBooks Online';
COMMENT ON TABLE qbo_payments IS 'QBO customer payments synced from QuickBooks Online';
COMMENT ON TABLE qbo_deposits IS 'QBO bank deposits synced from QuickBooks Online';
COMMENT ON TABLE qbo_transactions IS 'QBO general transactions (purchases, expenses, journals) synced from QuickBooks Online';
COMMENT ON TABLE qbo_reports IS 'QBO financial reports (P&L, Balance Sheet) stored per period';