#!/usr/bin/env node
/**
* Set the active farm for the user session
* This bypasses the farm_selection screen
*/
import pg from 'pg';
const { Client } = pg;
const client = new Client({
host: 'localhost',
port: 5433,
database: 'pg-litefarm',
user: 'postgres',
password: 'postgres'
});
const USER_EMAIL = 'your-email@example.com';
async function setActiveFarm() {
try {
await client.connect();
console.log(' Mit Datenbank verbunden\n');
// Hole User und seine Farms
const userResult = await client.query(
'SELECT user_id FROM users WHERE email = $1',
[USER_EMAIL]
);
if (userResult.rows.length === 0) {
console.error('L User nicht gefunden!');
process.exit(1);
}
const userId = userResult.rows[0].user_id;
// Hole alle Farms f�r diesen User
const farmsResult = await client.query(
`SELECT uf.farm_id, f.farm_name, uf.status
FROM "userFarm" uf
JOIN farm f ON uf.farm_id = f.farm_id
WHERE uf.user_id = $1 AND f.deleted = false`,
[userId]
);
console.log(`=� Verf�gbare Farms f�r ${USER_EMAIL}:\n`);
farmsResult.rows.forEach((farm, index) => {
console.log(`${index + 1}. ${farm.farm_name} (${farm.farm_id})`);
console.log(` Status: ${farm.status}\n`);
});
if (farmsResult.rows.length === 0) {
console.error('L Keine Farms gefunden!');
process.exit(1);
}
// W�hle die erste Farm
const activeFarm = farmsResult.rows[0];
console.log(` Setze aktive Farm: ${activeFarm.farm_name}`);
console.log(` Farm ID: ${activeFarm.farm_id}\n`);
// WICHTIG: In LiteFarm wird die aktive Farm im JWT Token gespeichert
// Die farm_selection Seite erscheint, wenn kein farm_id im Token ist
//
// Die L�sung: Beim n�chsten Login wird automatisch die erste Farm gew�hlt
// ODER: Du klickst einfach auf die Farm in der farm_selection Seite!
console.log('=� L�SUNG:');
console.log('');
console.log('Option 1 (EINFACHSTE):');
console.log(' 1. Gehe zu http://localhost:3001/farm_selection');
console.log(' 2. Klicke auf "${activeFarm.farm_name}"');
console.log(' 3. Du wirst direkt zum Dashboard weitergeleitet');
console.log(' 4. Diese Auswahl wird gespeichert!');
console.log('');
console.log('Option 2 (Falls Option 1 nicht funktioniert):');
console.log(' Die farm_selection Seite zeigt alle deine Farms.');
console.log(' W�hle einfach eine aus - das ist normal bei LiteFarm,');
console.log(' wenn ein User mehrere Farms hat.');
console.log('');
console.log('=� Nach der Auswahl:');
console.log(' - Die Farm wird in deinem Browser gespeichert');
console.log(' - Du landest beim n�chsten Login direkt im Dashboard');
console.log(' - MCP Server funktioniert normal weiter');
} catch (error) {
console.error('L Fehler:', error.message);
process.exit(1);
} finally {
await client.end();
}
}
setActiveFarm();