const { Pool } = require('pg'); const fs = require('fs'); (async () => { const env = fs.readFileSync('/opt/ai-fresh-delivery/server/.env', 'utf8'); const m = env.match(/^DATABASE_URL=(.+)$/m); const pool = new Pool({ connectionString: m[1].trim().replace(/^["']|["']$/g, ''), }); await pool.query(`SELECT set_config('search_path', 'tenant_br002, public', false)`); const cust = 'CUS34985111820'; const c = await pool.query( `SELECT customer_code, company_name, branch_code FROM customers WHERE customer_code = $1`, [cust] ); console.log('CUSTOMER', c.rows); const bound = await pool.query( `SELECT qsc.quote_sheet_code, qs.approval_status, qs.status, qs.branch_code, qs.expires_at FROM customer_quote_sheet_customers qsc JOIN customer_quote_sheets qs ON qs.quote_sheet_code = qsc.quote_sheet_code WHERE qsc.customer_code = $1`, [cust] ); console.log('BOUND_SHEETS', bound.rows); const items = await pool.query( `SELECT i.quote_sheet_code, i.sku_code, i.cost_price, i.sale_price FROM customer_quote_sheet_items i JOIN customer_quote_sheet_customers qsc ON qsc.quote_sheet_code = i.quote_sheet_code WHERE qsc.customer_code = $1 AND i.sku_code = 'SKU78664119567'`, [cust] ); console.log('THIS_SKU_ON_CUST_SHEETS', items.rows); const oi = await pool.query( `SELECT order_code, sku_code, item_name, price, unit, quantity FROM order_items WHERE order_code = 'ORD_MP_178522708768369K6' AND sku_code = 'SKU78664119567'` ); console.log('ORDER_ITEM', oi.rows); const sheetCusts = await pool.query( `SELECT customer_code FROM customer_quote_sheet_customers WHERE quote_sheet_code = 'QS1783780349488EOU0' ORDER BY 1` ); console.log( 'SHEET_CUSTOMERS', sheetCusts.rows.map((r) => r.customer_code) ); await pool.end(); })().catch((e) => { console.error(e); process.exit(1); });