EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
test-create-payment-data.js
Go to the documentation of this file.
1/**
2 * Create test invoices and contracts for Stripe payment testing
3 */
4require('dotenv').config();
5const pool = require('./services/db');
6
7const TENANT_ID = '00000000-0000-0000-0000-000000000001';
8
9async function createTestData() {
10 try {
11 console.log('Creating test payment data...\n');
12
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',
16 [TENANT_ID]
17 );
18
19 let customerId;
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]
28 );
29 customerId = newCustomer.rows[0].customer_id;
30 console.log('โœ… Created customer:', newCustomer.rows[0].name, `(ID: ${customerId})\n`);
31 } else {
32 customerId = customerResult.rows[0].customer_id;
33 console.log('Using existing customer:', customerResult.rows[0].name, `(ID: ${customerId})\n`);
34 }
35
36 // Create some test invoices
37 console.log('Creating test invoices...');
38 const invoices = [
39 {
40 customer_id: customerId,
41 description: 'Monthly IT Support - March 2026',
42 subtotal: 500.00,
43 tax_total: 50.00,
44 total: 550.00,
45 due_date: '2026-03-31',
46 payment_status: 'unpaid',
47 currency: 'usd'
48 },
49 {
50 customer_id: customerId,
51 description: 'Website Hosting & Maintenance',
52 subtotal: 150.00,
53 tax_total: 15.00,
54 total: 165.00,
55 due_date: '2026-03-25',
56 payment_status: 'unpaid',
57 currency: 'usd'
58 },
59 {
60 customer_id: customerId,
61 description: 'Cloud Storage Subscription',
62 subtotal: 75.00,
63 tax_total: 7.50,
64 total: 82.50,
65 due_date: '2026-03-20',
66 payment_status: 'unpaid',
67 currency: 'usd'
68 }
69 ];
70
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,
77 issued_date
78 ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, 1, CURRENT_DATE)
79 RETURNING invoice_id, description, total, payment_status`,
80 [
81 inv.customer_id, inv.description, inv.subtotal, inv.tax_total, inv.total,
82 inv.due_date, inv.payment_status, inv.currency, TENANT_ID
83 ]
84 );
85 createdInvoices.push(result.rows[0]);
86 console.log(` โœ… Invoice #${result.rows[0].invoice_id}: $${result.rows[0].total} - ${result.rows[0].description}`);
87 }
88
89 // Check contracts table schema
90 const contractColumns = await pool.query(`
91 SELECT column_name
92 FROM information_schema.columns
93 WHERE table_name = 'contracts'
94 ORDER BY ordinal_position
95 `);
96
97 console.log('\n๐Ÿ“‹ Contracts table columns:', contractColumns.rows.map(r => r.column_name).join(', '));
98
99 // Create test contract (if table exists and has expected structure)
100 const hasContract = contractColumns.rows.some(r => r.column_name === 'customer_id');
101 if (hasContract) {
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`,
109 [
110 customerId,
111 'Managed IT Services Contract',
112 'Full IT support and management services including helpdesk, monitoring, and maintenance',
113 '2026-01-01',
114 '2026-12-31',
115 'monthly',
116 'active',
117 TENANT_ID,
118 500.00 // labor_rate as monthly value
119 ]
120 );
121 console.log(` โœ… Contract #${contractResult.rows[0].contract_id}: ${contractResult.rows[0].title} - $${contractResult.rows[0].labor_rate}/month`);
122 }
123
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)}`);
129
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');
136
137 await pool.end();
138 process.exit(0);
139
140 } catch (error) {
141 console.error('โŒ Error:', error.message);
142 console.error(error);
143 await pool.end();
144 process.exit(1);
145 }
146}
147
148createTestData();