Actualizar server.js
This commit is contained in:
152
server.js
152
server.js
@@ -311,18 +311,25 @@ async function autoUpdateDB() {
|
||||
updated_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS protection_plans (
|
||||
id SERIAL PRIMARY KEY,
|
||||
company_id INT REFERENCES users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
price DECIMAL(10,2) DEFAULT 0.00,
|
||||
renewal_price DECIMAL(10,2) DEFAULT 0.00,
|
||||
urgencies_limit INT DEFAULT 0,
|
||||
bricos_limit INT DEFAULT 0,
|
||||
coverages TEXT,
|
||||
created_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS protection_plans (
|
||||
id SERIAL PRIMARY KEY,
|
||||
company_id INT REFERENCES users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
price DECIMAL(10,2) DEFAULT 0.00,
|
||||
renewal_price DECIMAL(10,2) DEFAULT 0.00,
|
||||
urgencies_limit INT DEFAULT 0,
|
||||
bricos_limit INT DEFAULT 0,
|
||||
coverages TEXT,
|
||||
|
||||
billing_interval TEXT DEFAULT 'month',
|
||||
billing_interval_count INT DEFAULT 1,
|
||||
stripe_price_id TEXT,
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
updated_at TIMESTAMP DEFAULT NOW(),
|
||||
|
||||
created_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS protection_subscriptions (
|
||||
id SERIAL PRIMARY KEY,
|
||||
@@ -359,13 +366,21 @@ async function autoUpdateDB() {
|
||||
created_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS protection_activity (
|
||||
id SERIAL PRIMARY KEY,
|
||||
company_id INT REFERENCES users(id) ON DELETE CASCADE,
|
||||
type TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS protection_payment_events (
|
||||
id SERIAL PRIMARY KEY,
|
||||
subscription_id INT REFERENCES protection_subscriptions(id) ON DELETE CASCADE,
|
||||
company_id INT REFERENCES users(id) ON DELETE CASCADE,
|
||||
stripe_invoice_id TEXT,
|
||||
stripe_payment_intent_id TEXT,
|
||||
stripe_checkout_session_id TEXT,
|
||||
stripe_event_id TEXT,
|
||||
amount DECIMAL(10,2) DEFAULT 0.00,
|
||||
currency TEXT DEFAULT 'eur',
|
||||
status TEXT NOT NULL,
|
||||
event_type TEXT,
|
||||
paid_at TIMESTAMP,
|
||||
created_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
|
||||
-- 💬 CHAT Y NOTAS INTERNAS
|
||||
@@ -386,6 +401,105 @@ async function autoUpdateDB() {
|
||||
await client.query(`
|
||||
DO $$ BEGIN
|
||||
|
||||
-- ==========================================
|
||||
-- 🛡️ PARCHES MÓDULO PROTECCIÓN
|
||||
-- ==========================================
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_plans' AND column_name='billing_interval') THEN
|
||||
ALTER TABLE protection_plans ADD COLUMN billing_interval TEXT DEFAULT 'month';
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_plans' AND column_name='billing_interval_count') THEN
|
||||
ALTER TABLE protection_plans ADD COLUMN billing_interval_count INT DEFAULT 1;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_plans' AND column_name='stripe_price_id') THEN
|
||||
ALTER TABLE protection_plans ADD COLUMN stripe_price_id TEXT;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_plans' AND column_name='is_active') THEN
|
||||
ALTER TABLE protection_plans ADD COLUMN is_active BOOLEAN DEFAULT TRUE;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_plans' AND column_name='updated_at') THEN
|
||||
ALTER TABLE protection_plans ADD COLUMN updated_at TIMESTAMP DEFAULT NOW();
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='stripe_session_id') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN stripe_session_id TEXT;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='stripe_subscription_id') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN stripe_subscription_id TEXT;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='stripe_customer_id') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN stripe_customer_id TEXT;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='stripe_price_id') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN stripe_price_id TEXT;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='started_at') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN started_at TIMESTAMP;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='current_period_start') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN current_period_start TIMESTAMP;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='current_period_end') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN current_period_end TIMESTAMP;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='last_payment_at') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN last_payment_at TIMESTAMP;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='cancel_at_period_end') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN cancel_at_period_end BOOLEAN DEFAULT FALSE;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='cancelled_at') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN cancelled_at TIMESTAMP;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='ended_at') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN ended_at TIMESTAMP;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='protection_subscriptions' AND column_name='updated_at') THEN
|
||||
ALTER TABLE protection_subscriptions ADD COLUMN updated_at TIMESTAMP DEFAULT NOW();
|
||||
END IF;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS protection_payment_events (
|
||||
id SERIAL PRIMARY KEY,
|
||||
subscription_id INT REFERENCES protection_subscriptions(id) ON DELETE CASCADE,
|
||||
company_id INT REFERENCES users(id) ON DELETE CASCADE,
|
||||
stripe_invoice_id TEXT,
|
||||
stripe_payment_intent_id TEXT,
|
||||
stripe_checkout_session_id TEXT,
|
||||
stripe_event_id TEXT,
|
||||
amount DECIMAL(10,2) DEFAULT 0.00,
|
||||
currency TEXT DEFAULT 'eur',
|
||||
status TEXT NOT NULL,
|
||||
event_type TEXT,
|
||||
paid_at TIMESTAMP,
|
||||
created_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
UPDATE protection_subscriptions
|
||||
SET payment_status = 'impagado'
|
||||
WHERE payment_status IS NULL OR payment_status = '';
|
||||
|
||||
UPDATE protection_subscriptions
|
||||
SET status = 'pendiente_pago'
|
||||
WHERE status IS NULL OR status = '';
|
||||
|
||||
ALTER TABLE protection_subscriptions ALTER COLUMN payment_status SET DEFAULT 'impagado';
|
||||
ALTER TABLE protection_subscriptions ALTER COLUMN status SET DEFAULT 'pendiente_pago';
|
||||
|
||||
-- 🟢 AÑADIDO: Fecha de última lectura del chat por el operario
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='scraped_services' AND column_name='last_chat_read_worker') THEN
|
||||
ALTER TABLE scraped_services ADD COLUMN last_chat_read_worker TIMESTAMP DEFAULT '2000-01-01';
|
||||
|
||||
Reference in New Issue
Block a user