fixed setting env managment

This commit is contained in:
2ManyProjects 2025-05-03 22:28:55 -05:00
parent de4e8486c4
commit 6bb9bd40dd
6 changed files with 100 additions and 54 deletions

View file

@ -61,10 +61,10 @@ const config = {
// Site configuration (domain and protocol based on environment)
site: {
domain: process.env.ENVIRONMENT === 'prod' ? 'rocks.2many.ca' : 'localhost:3000',
domain: process.env.ENVIRONMENT === 'prod' ? (process.env.APP_PROD_URL || 'rocks.2many.ca') : 'localhost:3000',
protocol: process.env.ENVIRONMENT === 'prod' ? 'https' : 'http',
apiDomain: process.env.ENVIRONMENT === 'prod' ? 'api.rocks.2many.ca' : 'localhost:4000',
analyticApiKey: '',
apiDomain: process.env.ENVIRONMENT === 'prod' ? (process.env.API_PROD_URL || 'api.rocks.2many.ca') : 'localhost:4000',
analyticApiKey: process.env.SITE_ANALYTIC_API || '',
}
};

View file

@ -35,6 +35,7 @@ module.exports = (pool, query) => {
[email, firstName, lastName]
);
console.log("REGISTERED NEW USER ", email)
// If First User, promot to Admin
if(isFirstUser){
console.log("First User, promoting ", email, " to admin")
await query(

View file

@ -217,21 +217,50 @@ module.exports = (pool, query, authMiddleware) => {
const { key, value, category } = setting;
// Map database settings to config structure
if (category === 'email') {
if (category === 'server') {
if (key === 'port') config.port = parseInt(value, 10);
if (key === 'node_env') config.nodeEnv = value;
if (key === 'environment') config.environment = value;
} else if (category === 'database') {
if (key === 'db_host') config.db.host = value;
if (key === 'db_user') config.db.user = value;
if (key === 'db_password') config.db.password = value;
if (key === 'db_name') config.db.database = value;
if (key === 'db_port') config.db.port = parseInt(value, 10);
} else if (category === 'email') {
if (key === 'smtp_host') config.email.host = value;
if (key === 'smtp_port') config.email.port = parseInt(value, 10);
if (key === 'smtp_user') config.email.user = value;
if (key === 'smtp_password') config.email.pass = value;
if (key === 'smtp_from_email') config.email.reply = value;
} else if (category === 'payment') {
if (key === 'stripe_enabled') config.payment.stripeEnabled = value === 'true';
if (key === 'stripe_public_key') config.payment.stripePublicKey = value;
if (key === 'stripe_secret_key') config.payment.stripeSecretKey = value;
if (key === 'stripe_webhook_secret') config.payment.stripeWebhookSecret = value;
} else if (category === 'shipping') {
if (key === 'shipping_enabled') config.shipping.enabled = value === 'true';
if (key === 'easypost_enabled') config.shipping.easypostEnabled = value === 'true';
if (key === 'easypost_api_key') config.shipping.easypostApiKey = value;
if (key === 'shipping_flat_rate') config.shipping.flatRate = parseFloat(value);
if (key === 'shipping_free_threshold') config.shipping.freeThreshold = parseFloat(value);
if (key === 'shipping_origin_street') config.shipping.originAddress.street = value;
if (key === 'shipping_origin_city') config.shipping.originAddress.city = value;
if (key === 'shipping_origin_state') config.shipping.originAddress.state = value;
if (key === 'shipping_origin_zip') config.shipping.originAddress.zip = value;
if (key === 'shipping_origin_country') config.shipping.originAddress.country = value;
if (key === 'shipping_default_package_length') config.shipping.defaultPackage.length = parseFloat(value);
if (key === 'shipping_default_package_width') config.shipping.defaultPackage.width = parseFloat(value);
if (key === 'shipping_default_package_height') config.shipping.defaultPackage.height = parseFloat(value);
if (key === 'shipping_default_package_unit') config.shipping.defaultPackage.unit = value;
if (key === 'shipping_default_weight_unit') config.shipping.defaultPackage.weightUnit = value;
if (key === 'shipping_carriers_allowed') config.shipping.carriersAllowed = value.split(',');
} else if (category === 'site') {
if (key === 'site_name') config.site.name = value;
if (key === 'site_domain') config.site.domain = value;
if (key === 'site_api_domain') config.site.apiDomain = value;
if (key === 'site_protocol') config.site.protocol = value;
if (key === 'site_analytics_api_key') config.site.analyticApiKey = value;
if (key === 'site_environment') config.environment = value;
}
// You can add more mappings for other categories here
}
/**
@ -241,16 +270,21 @@ module.exports = (pool, query, authMiddleware) => {
try {
// Get all settings from database
const allSettings = await SystemSettings.getAllSettings(pool, query);
config.updateFromDatabase(allSettings)
// First update the config with the database settings
config.updateFromDatabase(allSettings);
// Build environment variables string
let envContent = '';
// Add standard environment variables
envContent += `PORT=${process.env.PORT || config.port || 4000}\n`;
envContent += `NODE_ENV=${process.env.NODE_ENV || 'development'}\n\n`;
// Server configuration
envContent += '# Server configuration\n';
envContent += `PORT=${config.port}\n`;
envContent += `NODE_ENV=${config.nodeEnv}\n`;
envContent += `ENVIRONMENT=${config.environment}\n\n`;
// Add database configuration - use existing config values as fallbacks
envContent += `# Database connection\n`;
// Database configuration
envContent += '# Database configuration\n';
envContent += `DB_HOST=${config.db.host}\n`;
envContent += `DB_USER=${config.db.user}\n`;
envContent += `DB_PASSWORD=${config.db.password}\n`;
@ -258,45 +292,54 @@ module.exports = (pool, query, authMiddleware) => {
envContent += `DB_PORT=${config.db.port}\n`;
envContent += `POSTGRES_USER=${config.db.user}\n`;
envContent += `POSTGRES_PASSWORD=${config.db.password}\n`;
envContent += `POSTGRES_DB=${config.db.database}\n`;
envContent += `POSTGRES_DB=${config.db.database}\n\n`;
// Get site environment from settings or use existing config
const siteEnvSetting = allSettings.find(s => s.key === 'site_environment');
const currentEnvironment = siteEnvSetting?.value || config.environment || process.env.ENVIRONMENT || 'beta';
envContent += `ENVIRONMENT=${currentEnvironment}\n\n`;
// Email configuration
envContent += '# Email configuration\n';
envContent += `EMAIL_HOST=${config.email.host}\n`;
envContent += `EMAIL_PORT=${config.email.port}\n`;
envContent += `EMAIL_USER=${config.email.user}\n`;
envContent += `EMAIL_PASS=${config.email.pass}\n`;
envContent += `EMAIL_REPLY=${config.email.reply}\n\n`;
// Add email configuration with fallbacks to existing config
envContent += `# Email configuration\n`;
const emailSettings = allSettings.filter(s => s.category === 'email');
// Payment configuration
envContent += '# Payment configuration\n';
envContent += `STRIPE_ENABLED=${config.payment.stripeEnabled}\n`;
envContent += `STRIPE_PUBLIC_KEY=${config.payment.stripePublicKey}\n`;
envContent += `STRIPE_SECRET_KEY=${config.payment.stripeSecretKey}\n`;
envContent += `STRIPE_WEBHOOK_SECRET=${config.payment.stripeWebhookSecret}\n\n`;
const smtpHost = emailSettings.find(s => s.key === 'smtp_host');
const smtpPort = emailSettings.find(s => s.key === 'smtp_port');
const smtpUser = emailSettings.find(s => s.key === 'smtp_user');
const smtpPass = emailSettings.find(s => s.key === 'smtp_password');
const smtpReply = emailSettings.find(s => s.key === 'smtp_from_email');
// Shipping configuration
envContent += '# Shipping configuration\n';
envContent += `SHIPPING_ENABLED=${config.shipping.enabled}\n`;
envContent += `EASYPOST_ENABLED=${config.shipping.easypostEnabled}\n`;
envContent += `EASYPOST_API_KEY=${config.shipping.easypostApiKey}\n`;
envContent += `SHIPPING_FLAT_RATE=${config.shipping.flatRate}\n`;
envContent += `SHIPPING_FREE_THRESHOLD=${config.shipping.freeThreshold}\n`;
envContent += `SHIPPING_ORIGIN_STREET=${config.shipping.originAddress.street}\n`;
envContent += `SHIPPING_ORIGIN_CITY=${config.shipping.originAddress.city}\n`;
envContent += `SHIPPING_ORIGIN_STATE=${config.shipping.originAddress.state}\n`;
envContent += `SHIPPING_ORIGIN_ZIP=${config.shipping.originAddress.zip}\n`;
envContent += `SHIPPING_ORIGIN_COUNTRY=${config.shipping.originAddress.country}\n`;
envContent += `SHIPPING_DEFAULT_PACKAGE_LENGTH=${config.shipping.defaultPackage.length}\n`;
envContent += `SHIPPING_DEFAULT_PACKAGE_WIDTH=${config.shipping.defaultPackage.width}\n`;
envContent += `SHIPPING_DEFAULT_PACKAGE_HEIGHT=${config.shipping.defaultPackage.height}\n`;
envContent += `SHIPPING_DEFAULT_PACKAGE_UNIT=${config.shipping.defaultPackage.unit}\n`;
envContent += `SHIPPING_DEFAULT_WEIGHT_UNIT=${config.shipping.defaultPackage.weightUnit}\n`;
envContent += `SHIPPING_CARRIERS_ALLOWED=${config.shipping.carriersAllowed.join(',')}\n\n`;
// Use existing config values as fallbacks in this order: database setting → config value → env value → default
envContent += `EMAIL_HOST=${smtpHost?.value || config.email.host || process.env.EMAIL_HOST || 'smtp.postmarkapp.com'}\n`;
envContent += `EMAIL_PORT=${smtpPort?.value || config.email.port || process.env.EMAIL_PORT || '587'}\n`;
envContent += `EMAIL_USER=${smtpUser?.value || config.email.user || process.env.EMAIL_USER || ''}\n`;
envContent += `EMAIL_PASS=${smtpPass?.value || config.email.pass || process.env.EMAIL_PASS || ''}\n`;
envContent += `EMAIL_REPLY=${smtpReply?.value || config.email.reply || process.env.EMAIL_REPLY || 'noreply@2many.ca'}\n\n`;
// Add payment configuration with fallbacks to existing values
const paymentSettings = allSettings.filter(s => s.category === 'payment');
if (paymentSettings.length > 0 || process.env.STRIPE_PUBLIC_KEY || process.env.STRIPE_SECRET_KEY) {
envContent += `# Payment configuration\n`;
const stripePublic = paymentSettings.find(s => s.key === 'stripe_public_key');
const stripeSecret = paymentSettings.find(s => s.key === 'stripe_secret_key');
// Include payment settings if they exist in either DB or environment
if (stripePublic?.value || process.env.STRIPE_PUBLIC_KEY) {
envContent += `STRIPE_PUBLIC_KEY=${stripePublic?.value || process.env.STRIPE_PUBLIC_KEY}\n`;
}
if (stripeSecret?.value || process.env.STRIPE_SECRET_KEY) {
envContent += `STRIPE_SECRET_KEY=${stripeSecret?.value || process.env.STRIPE_SECRET_KEY}\n`;
}
// Site configuration
envContent += '# Site configuration\n';
if (config.environment === 'prod') {
// For production, use the actual domain values
envContent += `APP_PROD_URL=${config.site.domain}\n`;
envContent += `API_PROD_URL=${config.site.apiDomain}\n`;
} else {
// For beta/development, still include these but they won't be used
envContent += `APP_PROD_URL=${config.site.domain === 'localhost:3000' ? '' : config.site.domain}\n`;
envContent += `API_PROD_URL=${config.site.apiDomain === 'localhost:4000' ? '' : config.site.apiDomain}\n`;
}
envContent += `SITE_ANALYTIC_API=${config.site.analyticApiKey}\n`;
// Write to .env file
const envPath = path.join(__dirname, '../../.env');

View file

@ -12,9 +12,9 @@ const config = {
// Site configuration (domain and protocol based on environment)
site: {
domain: import.meta.env.VITE_ENVIRONMENT === 'prod' ? 'rocks.2many.ca' : 'localhost:3000',
domain: import.meta.env.VITE_ENVIRONMENT === 'prod' ? import.meta.env.VITE_APP_PROD_URL : 'localhost:3000',
protocol: import.meta.env.VITE_ENVIRONMENT === 'prod' ? 'https' : 'http',
apiDomain: import.meta.env.VITE_ENVIRONMENT === 'prod' ? 'api.rocks.2many.ca' : 'localhost:4000'
apiDomain: import.meta.env.VITE_ENVIRONMENT === 'prod' ? import.meta.env.VITE_API_PROD_URL : 'localhost:4000'
}
};

View file

@ -400,7 +400,6 @@ const EmailTemplatesPage = () => {
};
const onEditorReady = () => {
// You can perform any setup actions here when the editor is loaded
console.log('Email editor is ready');
// If there's a template being edited, load its design

View file

@ -12,13 +12,15 @@ export default defineConfig(({ mode }) => {
// 1. Check env loaded by Vite
// 2. Check process.env (for Node.js environment)
// 3. Try to read from .env file directly as fallback
let environment = env.ENVIRONMENT;
let environment = env.VITE_ENVIRONMENT;
if (!environment && process.env.ENVIRONMENT) {
console.log(`No environment found at VITE_ENVIRONMENT checking ENVIRONMENT${environment}`);
environment = process.env.ENVIRONMENT;
}
if (!environment) {
console.log(`No environment trying to manually read env ${environment}`);
try {
const envContent = fs.readFileSync('.env', 'utf8');
const envMatch = envContent.match(/ENVIRONMENT\s*=\s*(\w+)/);
@ -33,6 +35,7 @@ export default defineConfig(({ mode }) => {
// Default to 'beta' if still not found
if (!environment) {
console.log(`No environment set to beta manually: ${environment}`);
environment = 'beta';
}
@ -46,7 +49,7 @@ export default defineConfig(({ mode }) => {
if (isProduction) {
// Production: use the domain name without clientPort
hmrConfig = {
host: 'rocks.2many.ca',
host: env.VITE_APP_PROD_URL,
protocol: 'https'
};
} else {
@ -75,7 +78,7 @@ export default defineConfig(({ mode }) => {
}
},
server: {
allowedHosts: ['localhost', 'rocks.2many.ca'],
allowedHosts: ['localhost', env.VITE_APP_PROD_URL],
host: '0.0.0.0',
port: 3000,
watch: {