2 * Create test invoices and contracts for Stripe payment testing
4require('dotenv').config();
5const pool = require('./services/db');
7const TENANT_ID = '00000000-0000-0000-0000-000000000001';
9async function createTestData() {
11 console.log('Creating test payment data...\n');
13 // Get a customer to use
14 const customerResult = await pool.query(
15 'SELECT customer_id, name, email FROM customers WHERE tenant_id = $1 LIMIT 1',
20 if (customerResult.rows.length === 0) {
21 // Create a test customer
22 console.log('Creating test customer...');
23 const newCustomer = await pool.query(
24 `INSERT INTO customers (name, email, phone, tenant_id, created_by)
25 VALUES ($1, $2, $3, $4, 1)
26 RETURNING customer_id, name, email`,
27 ['Acme Corporation', 'billing@acme-test.com', '555-0123', TENANT_ID]
29 customerId = newCustomer.rows[0].customer_id;
30 console.log('โ
Created customer:', newCustomer.rows[0].name, `(ID: ${customerId})\n`);
32 customerId = customerResult.rows[0].customer_id;
33 console.log('Using existing customer:', customerResult.rows[0].name, `(ID: ${customerId})\n`);
36 // Create some test invoices
37 console.log('Creating test invoices...');
40 customer_id: customerId,
41 description: 'Monthly IT Support - March 2026',
45 due_date: '2026-03-31',
46 payment_status: 'unpaid',
50 customer_id: customerId,
51 description: 'Website Hosting & Maintenance',
55 due_date: '2026-03-25',
56 payment_status: 'unpaid',
60 customer_id: customerId,
61 description: 'Cloud Storage Subscription',
65 due_date: '2026-03-20',
66 payment_status: 'unpaid',
71 const createdInvoices = [];
72 for (const inv of invoices) {
73 const result = await pool.query(
74 `INSERT INTO invoices (
75 customer_id, description, subtotal, tax_total, total,
76 due_date, payment_status, currency, tenant_id, created_by,
78 ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, 1, CURRENT_DATE)
79 RETURNING invoice_id, description, total, payment_status`,
81 inv.customer_id, inv.description, inv.subtotal, inv.tax_total, inv.total,
82 inv.due_date, inv.payment_status, inv.currency, TENANT_ID
85 createdInvoices.push(result.rows[0]);
86 console.log(` โ
Invoice #${result.rows[0].invoice_id}: $${result.rows[0].total} - ${result.rows[0].description}`);
89 // Check contracts table schema
90 const contractColumns = await pool.query(`
92 FROM information_schema.columns
93 WHERE table_name = 'contracts'
94 ORDER BY ordinal_position
97 console.log('\n๐ Contracts table columns:', contractColumns.rows.map(r => r.column_name).join(', '));
99 // Create test contract (if table exists and has expected structure)
100 const hasContract = contractColumns.rows.some(r => r.column_name === 'customer_id');
102 console.log('\nCreating test contract...');
103 const contractResult = await pool.query(
104 `INSERT INTO contracts (
105 customer_id, title, description, start_date, end_date,
106 billing_interval, status, tenant_id, labor_rate
107 ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
108 RETURNING contract_id, title, labor_rate, billing_interval`,
111 'Managed IT Services Contract',
112 'Full IT support and management services including helpdesk, monitoring, and maintenance',
118 500.00 // labor_rate as monthly value
121 console.log(` โ
Contract #${contractResult.rows[0].contract_id}: ${contractResult.rows[0].title} - $${contractResult.rows[0].labor_rate}/month`);
124 console.log('\nโ
Test data created successfully!');
125 console.log('\n๐ Summary:');
126 console.log(` - Customer ID: ${customerId}`);
127 console.log(` - Invoices created: ${createdInvoices.length}`);
128 console.log(` - Total invoice value: $${createdInvoices.reduce((sum, inv) => sum + parseFloat(inv.total), 0).toFixed(2)}`);
130 console.log('\n๐งช Stripe Test Cards:');
131 console.log(' - 4242 4242 4242 4242 (Visa - always succeeds)');
132 console.log(' - 5555 5555 5555 4444 (Mastercard - always succeeds)');
133 console.log(' - 4000 0025 0000 3155 (Visa - requires authentication)');
134 console.log(' - 4000 0000 0000 9995 (Visa - declined - insufficient funds)');
135 console.log(' Exp: any future date, CVV: any 3 digits, ZIP: any 5 digits');
141 console.error('โ Error:', error.message);
142 console.error(error);