-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
661 lines (586 loc) · 28.9 KB
/
server.js
File metadata and controls
661 lines (586 loc) · 28.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import express from 'express';
import cors from 'cors';
import sqlite3 from 'sqlite3';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { readFileSync } from 'fs';
import { initDatabase } from './database/init.js';
import { migrateDatabase } from './database/migrate.js';
import appointmentsRoutes from './routes/appointments.js';
import servicesRoutes from './routes/services.js';
import locationsRoutes from './routes/locations.js';
import profileRoutes from './routes/profile.js';
import invoiceRoutes from './routes/invoice.js';
import authRoutes from './routes/auth.js';
import financialRoutes from './routes/financial.js';
import emailLogsRoutes from './routes/email-logs.js';
import adminUsersRoutes from './routes/admin-users.js';
import subscriptionsRoutes from './routes/subscriptions.js';
console.log('Profile routes imported:', profileRoutes ? 'YES' : 'NO');
if (profileRoutes) {
console.log('Profile router type:', typeof profileRoutes);
console.log('Profile router is function:', typeof profileRoutes === 'function' ? 'YES' : 'NO');
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const PORT = 3001;
// Middleware
app.use(cors());
app.use(express.json({ limit: '50mb' })); // Increase limit for PDF uploads
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
// Debug middleware to log ALL requests (even non-API)
console.log('[SERVER INIT] Registering request logging middleware...');
app.use((req, res, next) => {
console.log(`\n[REQUEST] ${req.method} ${req.originalUrl}`);
if (req.originalUrl.startsWith('/api')) {
console.log(`[API Request] ${req.method} ${req.originalUrl}`);
console.log(`[API Request] Path: ${req.path}, BaseURL: ${req.baseUrl}, URL: ${req.url}`);
if (req.originalUrl.includes('admin')) {
console.log(`[API Request] *** ADMIN REQUEST DETECTED ***`);
}
}
next();
});
console.log('[SERVER INIT] Request logging middleware registered');
// Profile routes are now handled by the profile router (routes/profile.js)
// which includes authentication middleware
// Test email endpoint removed - using SendGrid only now
/*
app.post('/api/profile/test-email', async (req, res) => {
console.log('\n[DIRECT PROFILE ROUTE] POST /api/profile/test-email - HANDLER CALLED');
console.log('[DIRECT PROFILE ROUTE] Request body:', {
email: req.body?.email ? '***' : 'missing',
email_password: req.body?.email_password ? '***' : 'missing',
smtp_host: req.body?.smtp_host ? req.body.smtp_host : 'missing',
smtp_port: req.body?.smtp_port,
smtp_secure: req.body?.smtp_secure,
smtp_username: req.body?.smtp_username ? '***' : 'missing'
});
try {
const { email, email_password, smtp_host, smtp_port, smtp_secure, smtp_username } = req.body;
if (!email || !email_password) {
return res.status(400).json({ error: 'Email and password are required' });
}
// Create a temporary profile object for testing
const testProfile = {
email,
email_password,
smtp_host,
smtp_port: smtp_port ? parseInt(smtp_port) : null,
smtp_secure: smtp_secure === true || smtp_secure === 1 || smtp_secure === '1',
smtp_username
};
// Helper function to test email connection
const testEmailConnection = async (testEmail) => {
const profile = { ...testProfile, email: testEmail };
let transporter;
let configUsed = {};
// If custom SMTP settings are provided, use them
if (profile.smtp_host) {
const isOutlookHost = profile.smtp_host.includes('outlook.com') || profile.smtp_host.includes('office365.com');
const config = {
host: profile.smtp_host,
port: profile.smtp_port || 587,
secure: profile.smtp_secure === true || profile.smtp_secure === 1,
auth: {
user: profile.smtp_username || profile.email,
pass: profile.email_password
}
};
// For Outlook/Office365, require TLS/STARTTLS
if (isOutlookHost) {
config.requireTLS = true;
}
if (!config.secure) {
config.tls = {
ciphers: 'SSLv3',
// Some Office365 setups may need this
rejectUnauthorized: isOutlookHost ? false : true
};
}
configUsed = {
host: config.host,
port: config.port,
secure: config.secure,
username: config.auth.user,
passwordSet: !!config.auth.pass
};
console.log('[EMAIL TEST] Using custom SMTP config:', { ...configUsed, passwordSet: '***' });
transporter = nodemailer.createTransport(config);
} else if (profile.smtp_username && (profile.smtp_username.includes('@outlook.com') || profile.smtp_username.includes('@office365.com') || profile.smtp_username.includes('@microsoft.com'))) {
// If SMTP username is provided and it's an Outlook/Office365 address, use Outlook SMTP
// This handles custom domains that use Outlook/Office365
// Try smtp.office365.com first (for Office365), then fall back to smtp-mail.outlook.com
const isOffice365 = profile.smtp_username.includes('@office365.com') || profile.smtp_username.includes('@microsoft.com');
const smtpHost = isOffice365 ? 'smtp.office365.com' : 'smtp-mail.outlook.com';
const config = {
host: smtpHost,
port: profile.smtp_port ? parseInt(profile.smtp_port) : 587,
secure: profile.smtp_secure === true || profile.smtp_secure === 1,
auth: {
user: profile.smtp_username,
pass: profile.email_password
},
requireTLS: true // Force TLS/STARTTLS
};
if (!config.secure) {
config.tls = {
ciphers: 'SSLv3',
rejectUnauthorized: false // Some Office365 setups require this
};
}
configUsed = {
host: config.host,
port: config.port,
secure: config.secure,
username: config.auth.user,
passwordSet: !!config.auth.pass,
autoDetected: `Outlook/Office365 (via SMTP username) - ${smtpHost}`
};
console.log('[EMAIL TEST] Using Outlook/Office365 SMTP (detected from SMTP username):', { ...configUsed, passwordSet: '***' });
transporter = nodemailer.createTransport(config);
} else {
// Auto-detect
const emailDomain = testEmail.split('@')[1]?.toLowerCase() || '';
const isOutlook = emailDomain.includes('outlook.com') ||
emailDomain.includes('hotmail.com') ||
emailDomain.includes('live.com') ||
emailDomain.includes('msn.com');
if (emailDomain.includes('gmail.com')) {
transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: testEmail,
pass: profile.email_password
}
});
} else if (isOutlook) {
transporter = nodemailer.createTransport({
host: 'smtp-mail.outlook.com',
port: 587,
secure: false, // false = use STARTTLS (upgrade connection to TLS)
requireTLS: true, // Require STARTTLS/TLS encryption
auth: {
user: testEmail,
pass: profile.email_password
},
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false // Some Outlook setups require this
}
});
} else {
transporter = nodemailer.createTransport({
host: 'smtp-mail.outlook.com',
port: 587,
secure: false, // false = use STARTTLS (upgrade connection to TLS)
requireTLS: true, // Require STARTTLS/TLS encryption
auth: {
user: testEmail,
pass: profile.email_password
},
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false // Some Outlook setups require this
}
});
}
}
console.log('[EMAIL TEST] Attempting connection with:', {
email: testEmail,
config: configUsed.host ? configUsed : 'auto-detect',
timestamp: new Date().toISOString()
});
await transporter.verify();
console.log('[EMAIL TEST] Connection successful!');
return { success: true, email: testEmail, config: configUsed };
};
// Try with the provided email first
try {
const result = await testEmailConnection(email);
res.json({ success: true, message: 'Email connection test successful' });
return;
} catch (error) {
// If using custom SMTP with Outlook and authentication fails, try with @outlook.com username
if (smtp_host && smtp_host.includes('outlook')) {
const emailDomain = email.split('@')[1]?.toLowerCase() || '';
const isOutlookDomain = emailDomain.includes('outlook.com') ||
emailDomain.includes('hotmail.com') ||
emailDomain.includes('live.com') ||
emailDomain.includes('msn.com');
// If not already using @outlook.com and we have a username specified or it's a different domain
if (isOutlookDomain && !emailDomain.includes('outlook.com') && !smtp_username) {
const username = email.split('@')[0];
const outlookEmail = `${username}@outlook.com`;
console.log(`[EMAIL TEST] Custom SMTP auth failed, trying with @outlook.com username: ${outlookEmail}`);
try {
// Try with @outlook.com as the username
const retryProfile = { ...testProfile, email: outlookEmail, smtp_username: outlookEmail };
const retryConfig = {
host: smtp_host,
port: smtp_port ? parseInt(smtp_port) : 587,
secure: smtp_secure === true || smtp_secure === 1,
auth: {
user: outlookEmail,
pass: email_password
}
};
if (!retryConfig.secure) {
retryConfig.tls = { ciphers: 'SSLv3' };
}
const retryTransporter = nodemailer.createTransport(retryConfig);
await retryTransporter.verify();
res.json({
success: true,
message: 'Email connection test successful! Note: Outlook required the @outlook.com address as the SMTP username.',
usedEmail: outlookEmail,
usedSmtpUsername: outlookEmail
});
return;
} catch (retryError) {
console.log('[EMAIL TEST] Retry with @outlook.com also failed');
// Continue to show the original error with improved suggestions
}
}
}
// Log detailed error information
console.error('[EMAIL TEST] Connection failed:', {
error: error.message,
code: error.code,
command: error.command,
response: error.response,
responseCode: error.responseCode,
stack: error.stack?.substring(0, 500)
});
// Check if it's an Outlook authentication error and we're using an alias
const isAuthError = error.message && (
error.message.includes('Invalid login') ||
error.message.includes('Authentication unsuccessful') ||
error.message.includes('535') ||
error.message.includes('534') ||
error.message.includes('530') ||
error.code === 'EAUTH'
);
const isConnectionError = error.code && (
error.code === 'ECONNREFUSED' ||
error.code === 'ETIMEDOUT' ||
error.code === 'ENOTFOUND' ||
error.code === 'ECONNRESET'
);
// If it's an Outlook domain and we're not already using @outlook.com, try with @outlook.com
// Only if custom SMTP is not configured
if (isAuthError && !smtp_host) {
const emailDomain = email.split('@')[1]?.toLowerCase() || '';
const isOutlook = emailDomain.includes('outlook.com') ||
emailDomain.includes('hotmail.com') ||
emailDomain.includes('live.com') ||
emailDomain.includes('msn.com');
if (isOutlook && !emailDomain.includes('outlook.com')) {
const username = email.split('@')[0];
const outlookEmail = `${username}@outlook.com`;
console.log(`[EMAIL TEST] First attempt failed, trying with @outlook.com domain: ${outlookEmail}`);
try {
await testEmailConnection(outlookEmail);
res.json({
success: true,
message: 'Email connection test successful! Note: Outlook required the @outlook.com domain instead of your alias.',
usedEmail: outlookEmail
});
return;
} catch (retryError) {
// Both attempts failed
const retryIsAuthError = retryError.message && (
retryError.message.includes('Invalid login') ||
retryError.message.includes('Authentication unsuccessful') ||
retryError.message.includes('535') ||
retryError.code === 'EAUTH'
);
if (retryIsAuthError) {
res.status(400).json({
error: 'Email authentication failed. This could be due to:\n' +
'• Incorrect password\n' +
'• Two-factor authentication (2FA) is enabled - you may need to use an App Password instead of your regular password\n' +
'• For Outlook aliases, try using your primary @outlook.com email address\n' +
'• Your account may require additional security settings to allow SMTP access',
details: 'Both the provided email and @outlook.com domain were tried. Please verify your credentials.'
});
return;
}
throw retryError;
}
}
}
// Handle connection errors
if (isConnectionError) {
let errorMessage = 'Could not connect to the email server.';
let suggestions = [];
if (smtp_host) {
errorMessage = `Could not connect to ${smtp_host}:${smtp_port || 587}`;
suggestions.push(`• Verify the SMTP host "${smtp_host}" is correct`);
suggestions.push(`• Verify the SMTP port "${smtp_port || 587}" is correct`);
suggestions.push('• Check if your firewall is blocking the connection');
suggestions.push('• Try a different port (587 for TLS, 465 for SSL)');
} else {
errorMessage = 'Could not connect to the email server.';
suggestions.push('• Check your internet connection');
suggestions.push('• Try configuring custom SMTP settings manually');
}
res.status(400).json({
error: errorMessage,
suggestions: suggestions.join('\n'),
details: error.message || error.code || 'Connection failed',
diagnostic: {
code: error.code,
host: smtp_host || 'auto-detect',
port: smtp_port || 'auto-detect'
}
});
return;
}
// Handle authentication errors
if (isAuthError) {
let errorMessage = 'Email authentication failed.';
let suggestions = [];
const authUser = smtp_username || email;
const isOutlook535 = error.message && error.message.includes('535 5.7.139');
const isBasicAuthDisabled = error.message && error.message.includes('basic authentication is disabled');
if (smtp_host) {
errorMessage = `Authentication failed for ${authUser}@${smtp_host}`;
if (isBasicAuthDisabled) {
errorMessage = 'Outlook/Office365 Basic Authentication is Disabled';
suggestions.push('• Your Microsoft account has basic authentication (username/password) disabled');
suggestions.push('• This means even App Passwords will not work for SMTP');
suggestions.push('• For Office 365 Family (personal accounts):');
suggestions.push(' - Go to https://account.microsoft.com/security');
suggestions.push(' - Check "Security defaults" settings - this may be blocking SMTP');
suggestions.push(' - Look for "App passwords" section and verify 2FA is properly configured');
suggestions.push(' - Microsoft may have permanently disabled basic auth for your account');
suggestions.push('• Unfortunately, Microsoft has been phasing out basic authentication for personal accounts');
suggestions.push('• You may need to use a different email provider (Gmail, etc.) that still supports basic auth');
suggestions.push('• Or consider using a business email account that allows SMTP AUTH to be enabled');
} else if (isOutlook535) {
errorMessage = 'Outlook authentication failed (Error 535 5.7.139)';
suggestions.push('• This error typically means Outlook rejected your credentials');
suggestions.push('• Verify you\'re using an App Password (not your regular password) if 2FA is enabled');
suggestions.push('• Make sure the App Password was generated for "Mail" or "Other" app type');
suggestions.push('• Try using your primary @outlook.com email address in the "SMTP Username" field (even if your email is an alias)');
suggestions.push('• Verify the App Password was copied correctly (no extra spaces)');
suggestions.push('• Check if your Microsoft account has "Security defaults" enabled - you may need to disable it or use Conditional Access');
suggestions.push('• Try port 587 with "SMTP Secure" unchecked (TLS), or port 465 with "SMTP Secure" checked (SSL)');
suggestions.push('• If using a custom domain with Outlook, ensure SMTP is enabled in your Microsoft 365 admin center');
} else {
suggestions.push(`• Verify the SMTP username "${authUser}" is correct`);
suggestions.push('• Verify the password/App Password is correct');
suggestions.push(`• Verify the SMTP host "${smtp_host}" is correct for your email provider`);
suggestions.push(`• Verify the SMTP port "${smtp_port || 587}" and security settings match your provider's requirements`);
suggestions.push('• For Outlook: Make sure you\'re using an App Password if 2FA is enabled');
suggestions.push('• For Outlook: Try using your primary @outlook.com address in the SMTP Username field');
}
} else {
const emailDomain = email.split('@')[1]?.toLowerCase() || '';
const isOutlook = emailDomain.includes('outlook.com') ||
emailDomain.includes('hotmail.com') ||
emailDomain.includes('live.com');
if (isOutlook) {
if (isBasicAuthDisabled) {
errorMessage = 'Outlook/Office365 Basic Authentication is Disabled';
suggestions.push('• Your Microsoft account has basic authentication (username/password) disabled');
suggestions.push('• This means even App Passwords will not work for SMTP');
suggestions.push('• For Office 365 Family (personal accounts):');
suggestions.push(' - Go to https://account.microsoft.com/security');
suggestions.push(' - Check "Security defaults" settings - this may be blocking SMTP');
suggestions.push(' - Look for "App passwords" section and verify 2FA is properly configured');
suggestions.push(' - Microsoft may have permanently disabled basic auth for your account');
suggestions.push('• Unfortunately, Microsoft has been phasing out basic authentication for personal accounts');
suggestions.push('• You may need to use a different email provider (Gmail, etc.) that still supports basic auth');
suggestions.push('• Or consider using a business email account that allows SMTP AUTH to be enabled');
} else if (isOutlook535) {
errorMessage = 'Outlook authentication failed (Error 535 5.7.139)';
suggestions.push('• This error means Outlook rejected your credentials');
suggestions.push('• You MUST use an App Password if 2FA is enabled (regular password will not work)');
suggestions.push('• Create a new App Password: https://account.microsoft.com/security');
suggestions.push('• Make sure you copy the App Password exactly (16 characters, no spaces)');
suggestions.push('• Try configuring custom SMTP settings with your primary @outlook.com address in the SMTP Username field');
suggestions.push('• Check Microsoft account security settings - some accounts require App Passwords for SMTP');
} else {
suggestions.push('• Verify your password is correct');
suggestions.push('• If you have 2FA enabled, use an App Password instead of your regular password');
suggestions.push('• Try using your primary @outlook.com email address if you\'re using an alias');
suggestions.push('• Check that SMTP access is enabled in your account settings');
}
} else if (emailDomain.includes('gmail.com')) {
suggestions.push('• Verify your password is correct');
suggestions.push('• If you have 2FA enabled, use an App Password instead of your regular password');
suggestions.push('• Enable "Less secure app access" or use App Passwords in your Google Account settings');
} else {
suggestions.push('• Verify your email address and password are correct');
suggestions.push('• Consider configuring custom SMTP settings in the advanced options below');
}
}
// Determine actual SMTP host used (might be auto-detected from username)
let actualSmtpHost = smtp_host;
let actualSmtpPort = smtp_port;
if (!actualSmtpHost && smtp_username && smtp_username.includes('@outlook.com')) {
actualSmtpHost = 'smtp-mail.outlook.com (auto-detected from SMTP username)';
actualSmtpPort = smtp_port || '587 (auto-detected)';
} else if (!actualSmtpHost) {
actualSmtpHost = 'auto-detect';
actualSmtpPort = smtp_port || 'auto-detect';
}
res.status(400).json({
error: errorMessage,
suggestions: suggestions.join('\n'),
details: error.message || error.code || 'Authentication failed',
diagnostic: {
code: error.code,
responseCode: error.responseCode,
command: error.command,
email: email,
smtpHost: actualSmtpHost,
smtpPort: actualSmtpPort,
smtpSecure: smtp_secure ? 'yes' : 'no',
smtpUsername: smtp_username || email,
note: smtp_username && smtp_username.includes('@outlook.com') && !smtp_host
? 'Using Outlook SMTP (detected from SMTP username)'
: undefined
}
});
return;
}
// For other errors, provide generic message with diagnostic info
res.status(400).json({
error: 'Email connection test failed',
details: error.message || error.code || 'Unknown error',
diagnostic: {
code: error.code,
responseCode: error.responseCode,
command: error.command,
email: email,
smtpHost: smtp_host || 'auto-detect',
smtpPort: smtp_port || 'auto-detect',
smtpSecure: smtp_secure ? 'yes' : 'no',
smtpUsername: smtp_username || email
}
});
}
} catch (error) {
console.error('[DIRECT PROFILE ROUTE] Email test error:', error);
let userMessage = 'Failed to connect to email server.';
if (error.message) {
if (error.message.includes('Invalid login') || error.message.includes('Authentication unsuccessful')) {
userMessage = 'Email authentication failed. Please check your email address and password.';
} else if (error.message.includes('ECONNREFUSED') || error.message.includes('ETIMEDOUT')) {
userMessage = 'Could not connect to the email server. Please check your SMTP host and port settings.';
} else {
userMessage = error.message;
}
}
res.status(400).json({
error: userMessage,
details: error.message || error.code || 'Unknown error occurred',
diagnostic: {
code: error.code,
email: req.body?.email || 'unknown',
smtpHost: req.body?.smtp_host || 'auto-detect',
smtpPort: req.body?.smtp_port || 'auto-detect'
}
});
}
});
*/
// Profile routes will be registered after database initialization
// Initialize database
// Use data directory in production (Docker), or current directory in development
const dataDir = process.env.NODE_ENV === 'production'
? join(__dirname, 'data')
: __dirname;
const dbPath = join(dataDir, 'hairmanager.db');
// Initialize database first, then migrate, then start server
initDatabase(dbPath)
.then(() => migrateDatabase(dbPath))
.then(() => {
// Create database connection after initialization
const db = new sqlite3.Database(dbPath, (err) => {
if (err) {
console.error('Error opening database:', err);
process.exit(1);
} else {
console.log('Connected to SQLite database');
}
});
// Make db available to routes
app.locals.db = db;
// Routes - register all routes
try {
console.log('Registering all routes...');
// Auth routes (no authentication required)
app.use('/api/auth', authRoutes);
console.log('✓ Auth routes registered');
// Protected routes (require authentication)
app.use('/api/appointments', appointmentsRoutes);
console.log('✓ Appointments routes registered');
app.use('/api/services', servicesRoutes);
console.log('✓ Services routes registered');
app.use('/api/locations', locationsRoutes);
console.log('✓ Locations routes registered');
app.use('/api/profile', profileRoutes);
console.log('✓ Profile routes registered');
app.use('/api/invoice', invoiceRoutes);
console.log('✓ Invoice routes registered');
app.use('/api/email-logs', emailLogsRoutes);
console.log('✓ Email logs routes registered');
app.use('/api/admin/users', adminUsersRoutes);
console.log('✓ Admin users routes registered');
app.use('/api/subscriptions', subscriptionsRoutes);
console.log('✓ Subscription routes registered');
app.use('/api/financial', financialRoutes);
console.log('✓ Financial routes registered');
} catch (err) {
console.error('ERROR registering routes:', err);
console.error(err.stack);
}
// Health check
app.get('/api/health', (req, res) => {
console.log('[Health Check] Route handler called');
res.json({ status: 'ok' });
});
// Serve static files from the dist directory (built frontend)
const distPath = join(__dirname, 'dist');
app.use(express.static(distPath));
// Serve index.html for all non-API routes (SPA routing)
// Express 5: Use app.use with a catch-all middleware instead of app.get('*')
app.use((req, res, next) => {
if (!req.path.startsWith('/api') && req.method === 'GET') {
res.sendFile(join(distPath, 'index.html'));
} else {
next();
}
});
// Read and log version
try {
const packageJson = JSON.parse(readFileSync(join(__dirname, 'package.json'), 'utf8'));
console.log(`\n╔════════════════════════════════════════╗`);
console.log(`║ HairManager v${packageJson.version} ║`);
console.log(`╚════════════════════════════════════════╝`);
} catch (err) {
console.log('\n[Warning] Could not read version from package.json');
}
app.listen(PORT, () => {
console.log(`\nServer running on http://localhost:${PORT}`);
console.log('Available API endpoints:');
console.log(' GET /api/health');
console.log(' POST /api/auth/register');
console.log(' POST /api/auth/login');
console.log(' GET /api/auth/me');
console.log(' GET /api/profile (protected)');
console.log(' PUT /api/profile (protected)');
console.log(' POST /api/profile/clear-postcode-resync (protected)');
console.log(' All /api/appointments, /api/services, /api/locations, /api/invoice routes are protected');
console.log('\nWaiting for requests...\n');
});
})
.catch((err) => {
console.error('Failed to initialize database:', err);
process.exit(1);
});