EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
move-to-test-customer.js
Go to the documentation of this file.
1/**
2 * Create a test customer and move test invoices/contracts to it
3 */
4require('dotenv').config();
5const pool = require('./services/db');
6
7const TENANT_ID = '00000000-0000-0000-0000-000000000001';
8const TEST_INVOICE_IDS = [503, 504, 505];
9const TEST_CONTRACT_ID = 29;
10
11async function moveToTestCustomer() {
12 try {
13 console.log('Creating test customer...\n');
14
15 // Create test customer
16 const customerResult = await pool.query(
17 `INSERT INTO customers (name, email, phone, address, city, state, postal_code, country, tenant_id)
18 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
19 RETURNING customer_id, name, email`,
20 [
21 'Testing Inc',
22 'test@example.com',
23 '555-TEST-123',
24 '123 Test Street',
25 'Test City',
26 'TX',
27 '12345',
28 'US',
29 TENANT_ID
30 ]
31 );
32
33 const testCustomerId = customerResult.rows[0].customer_id;
34 console.log(`✅ Created test customer: ${customerResult.rows[0].name} (ID: ${testCustomerId})`);
35 console.log(` Email: ${customerResult.rows[0].email}\n`);
36
37 // Move invoices to test customer
38 console.log('Moving test invoices...');
39 for (const invoiceId of TEST_INVOICE_IDS) {
40 const result = await pool.query(
41 `UPDATE invoices
42 SET customer_id = $1
43 WHERE invoice_id = $2 AND tenant_id = $3
44 RETURNING invoice_id, description, total`,
45 [testCustomerId, invoiceId, TENANT_ID]
46 );
47
48 if (result.rows.length > 0) {
49 console.log(` ✅ Invoice #${result.rows[0].invoice_id}: $${result.rows[0].total} - ${result.rows[0].description}`);
50 } else {
51 console.log(` ⚠️ Invoice #${invoiceId} not found`);
52 }
53 }
54
55 // Move contract to test customer
56 console.log('\nMoving test contract...');
57 const contractResult = await pool.query(
58 `UPDATE contracts
59 SET customer_id = $1
60 WHERE contract_id = $2 AND tenant_id = $3
61 RETURNING contract_id, title, labor_rate`,
62 [testCustomerId, TEST_CONTRACT_ID, TENANT_ID]
63 );
64
65 if (contractResult.rows.length > 0) {
66 console.log(` ✅ Contract #${contractResult.rows[0].contract_id}: ${contractResult.rows[0].title} - $${contractResult.rows[0].labor_rate}/month`);
67 } else {
68 console.log(` ⚠️ Contract #${TEST_CONTRACT_ID} not found`);
69 }
70
71 console.log('\n✅ Test data organized successfully!');
72 console.log(`\n📋 Test Customer ID: ${testCustomerId}`);
73 console.log(' Name: Testing Inc');
74 console.log(' Email: test@example.com');
75
76 await pool.end();
77 process.exit(0);
78
79 } catch (error) {
80 console.error('❌ Error:', error.message);
81 console.error(error);
82 await pool.end();
83 process.exit(1);
84 }
85}
86
87moveToTestCustomer();