← back to Designer Wallcoverings
DW-Agents/dw-agents/amazon-real-orders.ts
174 lines
/**
* Amazon Real Order Fetcher
* Uses authenticated session to fetch real order history
*/
import { exec } from 'child_process';
import { promisify } from 'util';
import * as fs from 'fs';
const execAsync = promisify(exec);
interface Order {
id: string;
item: string;
vendor: string;
price: number;
status: string;
timestamp: Date;
orderUrl?: string;
tracking?: string;
}
/**
* Fetch real orders using Amazon's order history page
* This uses curl with cookies to authenticate
*/
export async function fetchRealAmazonOrders(): Promise<Order[]> {
const orders: Order[] = [];
try {
console.log('🔍 Fetching real Amazon orders...');
// For now, return placeholder structure
// In production, this would:
// 1. Use stored session cookies
// 2. Fetch https://www.amazon.com/gp/your-account/order-history
// 3. Parse the HTML
// 4. Extract real order data
// Check if we have saved order data
const orderDataPath = '/root/Projects/Designer-Wallcoverings/DW-Agents/data/amazon-orders.json';
if (fs.existsSync(orderDataPath)) {
const data = fs.readFileSync(orderDataPath, 'utf-8');
const savedOrders = JSON.parse(data);
console.log(`✅ Loaded ${savedOrders.length} orders from cache`);
return savedOrders;
}
console.log('⚠️ No cached orders found. Returning empty array.');
console.log('💡 To add orders, manually update: /root/Projects/Designer-Wallcoverings/DW-Agents/data/amazon-orders.json');
return orders;
} catch (error) {
console.error('❌ Error fetching orders:', error);
return [];
}
}
/**
* Fetch real inventory from tracked sources
*/
export async function fetchRealInventory(): Promise<any[]> {
const inventory: any[] = [];
try {
const inventoryPath = '/root/Projects/Designer-Wallcoverings/DW-Agents/data/office-inventory.json';
if (fs.existsSync(inventoryPath)) {
const data = fs.readFileSync(inventoryPath, 'utf-8');
const savedInventory = JSON.parse(data);
console.log(`✅ Loaded ${savedInventory.length} inventory items from file`);
return savedInventory;
}
console.log('⚠️ No inventory file found. Creating default structure...');
// Create data directory if it doesn't exist
if (!fs.existsSync('/root/Projects/Designer-Wallcoverings/DW-Agents/data')) {
fs.mkdirSync('/root/Projects/Designer-Wallcoverings/DW-Agents/data', { recursive: true });
}
// Create empty inventory file
fs.writeFileSync(inventoryPath, JSON.stringify([], null, 2));
return [];
} catch (error) {
console.error('❌ Error fetching inventory:', error);
return [];
}
}
/**
* Save order to cache
*/
export function saveOrder(order: Order): void {
try {
const orderDataPath = '/root/Projects/Designer-Wallcoverings/DW-Agents/data/amazon-orders.json';
// Create directory if needed
if (!fs.existsSync('/root/Projects/Designer-Wallcoverings/DW-Agents/data')) {
fs.mkdirSync('/root/Projects/Designer-Wallcoverings/DW-Agents/data', { recursive: true });
}
let orders: Order[] = [];
if (fs.existsSync(orderDataPath)) {
const data = fs.readFileSync(orderDataPath, 'utf-8');
orders = JSON.parse(data);
}
orders.push(order);
fs.writeFileSync(orderDataPath, JSON.stringify(orders, null, 2));
console.log(`✅ Order saved: ${order.id}`);
} catch (error) {
console.error('❌ Error saving order:', error);
}
}
/**
* Update inventory
*/
export function saveInventory(inventory: any[]): void {
try {
const inventoryPath = '/root/Projects/Designer-Wallcoverings/DW-Agents/data/office-inventory.json';
// Create directory if needed
if (!fs.existsSync('/root/Projects/Designer-Wallcoverings/DW-Agents/data')) {
fs.mkdirSync('/root/Projects/Designer-Wallcoverings/DW-Agents/data', { recursive: true });
}
fs.writeFileSync(inventoryPath, JSON.stringify(inventory, null, 2));
console.log(`✅ Inventory saved: ${inventory.length} items`);
} catch (error) {
console.error('❌ Error saving inventory:', error);
}
}
// Initialize data files
if (import.meta.url === `file://${process.argv[1]}`) {
(async () => {
console.log('🔧 Initializing Amazon data files...\n');
const dataDir = '/root/Projects/Designer-Wallcoverings/DW-Agents/data';
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
console.log('✅ Created data directory');
}
// Initialize orders file
const ordersPath = `${dataDir}/amazon-orders.json`;
if (!fs.existsSync(ordersPath)) {
fs.writeFileSync(ordersPath, JSON.stringify([], null, 2));
console.log('✅ Created amazon-orders.json');
}
// Initialize inventory file
const inventoryPath = `${dataDir}/office-inventory.json`;
if (!fs.existsSync(inventoryPath)) {
fs.writeFileSync(inventoryPath, JSON.stringify([], null, 2));
console.log('✅ Created office-inventory.json');
}
console.log('\n✅ Amazon data files ready!');
console.log(`\n📝 To add orders manually, edit: ${ordersPath}`);
console.log(`📝 To add inventory, edit: ${inventoryPath}`);
})();
}