EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
create_precise_pcs_tenant.js
Go to the documentation of this file.
1#!/usr/bin/env node
2
3/**
4 * Create "Precise PCs" Tenant in PostgreSQL
5 *
6 * This script creates a new tenant for Precise PCs customers
7 * being migrated from WHMCS.
8 */
9
10require('dotenv').config();
11const { Pool } = require('pg');
12const { v4: uuidv4 } = require('uuid');
13
14// PostgreSQL connection
15const pool = new Pool({
16 host: process.env.DB_HOST,
17 port: process.env.DB_PORT,
18 user: process.env.DB_USER,
19 password: process.env.DB_PASSWORD,
20 database: process.env.DB_NAME,
21 ssl: process.env.DB_HOST.includes('digitalocean') ? { rejectUnauthorized: false } : false
22});
23
24/**
25 *
26 */
27async function createPrecisePCsTenant() {
28 console.log('========================================');
29 console.log('Creating Precise PCs Tenant');
30 console.log('========================================');
31 console.log('');
32
33 try {
34 // Check if tenant already exists
35 const existingCheck = await pool.query(
36 "SELECT tenant_id, name, subdomain FROM tenants WHERE name ILIKE '%Precise%' OR subdomain ILIKE '%precise%'"
37 );
38
39 if (existingCheck.rows.length > 0) {
40 const existing = existingCheck.rows[0];
41 console.log('⚠️ Tenant already exists:');
42 console.log(` ID: ${existing.tenant_id}`);
43 console.log(` Name: ${existing.name}`);
44 console.log(` Subdomain: ${existing.subdomain || 'N/A'}`);
45 console.log('');
46 console.log('Using existing tenant for import...');
47
48 // Save to file
49 const fs = require('fs');
50 const path = require('path');
51 const exportDir = path.join(__dirname, '..', 'exports');
52
53 if (!fs.existsSync(exportDir)) {
54 fs.mkdirSync(exportDir, { recursive: true });
55 }
56
57 fs.writeFileSync(
58 path.join(exportDir, 'precise_pcs_tenant.json'),
59 JSON.stringify({
60 tenant_id: existing.tenant_id,
61 name: existing.name,
62 subdomain: existing.subdomain
63 }, null, 2)
64 );
65
66 return existing.tenant_id;
67 }
68
69 // Create new tenant
70 const tenantId = uuidv4();
71 const tenantData = {
72 tenant_id: tenantId,
73 name: 'Precise PCs',
74 subdomain: 'precisepcs',
75 status: 'active',
76 is_msp: false,
77 email_automation: JSON.stringify({
78 migrated_from: 'WHMCS',
79 migration_date: new Date().toISOString(),
80 original_whmcs_url: 'https://clientarea.precisewebhosting.com.au',
81 original_domain: 'precisewebhosting.com.au',
82 features: {
83 billing: true,
84 support_tickets: true,
85 domain_management: true,
86 wordpress_hosting: true
87 }
88 })
89 };
90
91 const insertQuery = `
92 INSERT INTO tenants (
93 tenant_id, name, subdomain, status, is_msp,
94 email_automation, created_at, updated_at
95 ) VALUES (
96 $1, $2, $3, $4, $5, $6, NOW(), NOW()
97 )
98 RETURNING tenant_id, name, subdomain, status, is_msp, created_at
99 `;
100
101 const result = await pool.query(insertQuery, [
102 tenantData.tenant_id,
103 tenantData.name,
104 tenantData.subdomain,
105 tenantData.status,
106 tenantData.is_msp,
107 tenantData.email_automation
108 ]);
109
110 const tenant = result.rows[0];
111
112 console.log('✅ Tenant created successfully!');
113 console.log('');
114 console.log('Tenant Details:');
115 console.log('─────────────────────────────────────');
116 console.log(`ID: ${tenant.tenant_id}`);
117 console.log(`Name: ${tenant.name}`);
118 console.log(`Subdomain: ${tenant.subdomain}`);
119 console.log(`Status: ${tenant.status}`);
120 console.log(`Is MSP: ${tenant.is_msp}`);
121 console.log(`Created: ${tenant.created_at}`);
122 console.log('─────────────────────────────────────');
123 console.log('');
124
125 // Save tenant ID to file for import script
126 const fs = require('fs');
127 const path = require('path');
128 const exportDir = path.join(__dirname, '..', 'exports');
129
130 if (!fs.existsSync(exportDir)) {
131 fs.mkdirSync(exportDir, { recursive: true });
132 }
133
134 fs.writeFileSync(
135 path.join(exportDir, 'precise_pcs_tenant.json'),
136 JSON.stringify({
137 tenant_id: tenant.tenant_id,
138 name: tenant.name,
139 subdomain: tenant.subdomain,
140 created_at: tenant.created_at
141 }, null, 2)
142 );
143
144 console.log('💾 Tenant ID saved to: exports/precise_pcs_tenant.json');
145 console.log('');
146
147 return tenant.tenant_id;
148
149 } catch (error) {
150 console.error('❌ Error creating tenant:', error.message);
151 throw error;
152 } finally {
153 await pool.end();
154 }
155}
156
157// Run if executed directly
158if (require.main === module) {
159 createPrecisePCsTenant()
160 .then(() => {
161 console.log('✅ Tenant creation complete');
162 process.exit(0);
163 })
164 .catch((error) => {
165 console.error('❌ Tenant creation failed:', error);
166 process.exit(1);
167 });
168}
169
170module.exports = { createPrecisePCsTenant };