/**
* Debug Import Script
* Testet den Import Schritt für Schritt mit detailliertem Output
*/
import dotenv from "dotenv";
import { LiteFarmClient } from "./litefarm-client.js";
dotenv.config();
async function main() {
console.log("\n🔍 DEBUG: Starting Import Test...\n");
if (!process.env.LITEFARM_EMAIL || !process.env.LITEFARM_PASSWORD) {
console.error("❌ Error: LITEFARM_EMAIL and LITEFARM_PASSWORD must be set");
process.exit(1);
}
const client = new LiteFarmClient(
process.env.LITEFARM_EMAIL,
process.env.LITEFARM_PASSWORD
);
try {
// Step 1: Login
console.log("1️⃣ Testing login...");
await client.login();
console.log("✅ Login successful\n");
// Step 2: Get farms
console.log("2️⃣ Getting farms...");
const farms = await client.getFarms();
console.log(`✅ Found ${farms.length} farm(s):`);
farms.forEach(f => {
console.log(` - ${f.farm_name} (ID: ${f.farm_id})`);
});
if (farms.length === 0) {
console.error("\n❌ No farms found! Cannot proceed with import.");
return;
}
const farmId = farms[0].farm_id;
console.log(`\n Using farm: ${farms[0].farm_name}\n`);
// Step 3: Try to create a simple location
console.log("3️⃣ Testing location creation...");
// Try Method 1: farm_id as query parameter
console.log("\n Method 1: farm_id as query parameter");
try {
const testLocation1 = {
name: "DEBUG TEST Location 1",
figure: {
type: "field",
total_area: 100
}
};
const location1 = await client.post<any>(`/location/field?farm_id=${farmId}`, testLocation1);
console.log(" ✅ Method 1 SUCCESS!");
console.log(" Location ID:", location1.location_id, "\n");
} catch (error: any) {
console.log(" ❌ Method 1 failed:", error.response?.data?.error || error.message, "\n");
}
// Try Method 2: farm_id in body (this failed before, but let's confirm)
console.log(" Method 2: farm_id in body");
try {
const testLocation2 = {
farm_id: farmId,
name: "DEBUG TEST Location 2",
figure: {
type: "field",
total_area: 100
}
};
const location2 = await client.post<any>('/location/field', testLocation2);
console.log(" ✅ Method 2 SUCCESS!");
console.log(" Location ID:", location2.location_id, "\n");
} catch (error: any) {
console.log(" ❌ Method 2 failed:", error.response?.data?.error || error.message, "\n");
}
// Try Method 3: Use the client's interceptor with farm header
console.log(" Method 3: Testing with farm_id header");
try {
const response = await client.get<any>(`/farm/${farmId}`);
console.log(" ✅ Farm details retrieved, trying location creation...");
const testLocation3 = {
name: "DEBUG TEST Location 3",
figure: {
type: "field",
total_area: 100
}
};
// Use direct axios with custom headers
const location3 = await client.post<any>('/location/field', testLocation3);
console.log(" ✅ Method 3 SUCCESS!");
console.log(" Location ID:", location3.location_id, "\n");
} catch (error: any) {
console.log(" ❌ Method 3 failed:", error.response?.data?.error || error.message, "\n");
}
// Step 4: Get all locations to verify
console.log("4️⃣ Verifying locations...");
const locations = await client.get<any[]>(`/location/farm/${farmId}`);
console.log(`✅ Total locations in farm: ${locations.length}`);
locations.slice(-3).forEach(loc => {
console.log(` - ${loc.name} (${loc.total_area || 0}m²)`);
});
console.log("\n" + "=".repeat(60));
console.log("✅ DEBUG TEST COMPLETED SUCCESSFULLY!");
console.log("=".repeat(60) + "\n");
console.log("The import scripts should work. Check your WebApp!");
} catch (error: any) {
console.error("\n❌ DEBUG TEST FAILED!");
console.error("Error:", error.message);
if (error.response) {
console.error("Status:", error.response.status);
console.error("Response:", JSON.stringify(error.response.data, null, 2));
}
console.error("\nFull error:", error);
}
}
main();