-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
739 lines (637 loc) Β· 27.4 KB
/
index.js
File metadata and controls
739 lines (637 loc) Β· 27.4 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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
import express from 'express';
import dotenv from 'dotenv';
import { initializeApp } from 'firebase/app';
import mongoose from 'mongoose';
import cors from 'cors';
import authRoutes from './routes/auth.js';
import codesRoutes from './routes/codes.js';
import { requireAuth, requireAuthSSR, requireAuthFlexible } from './utils/jwt.js';
import cookieParser from 'cookie-parser';
import session from 'express-session';
import MongoStore from 'connect-mongo';
import flash from 'connect-flash';
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import http from 'http';
import typeDefs from './src/schema/schema.js';
import resolvers from './src/resolvers/resolver.js';
import { verifyJwt } from './utils/jwt.js';
import BotCommand from './src/models/BotCommand.js';
// Load environment variables from .env file
dotenv.config();
console.log('π Environment check:');
console.log('NODE_ENV:', process.env.NODE_ENV);
console.log('PORT:', process.env.PORT);
console.log('MONGO_URI exists:', !!process.env.MONGO_URI);
console.log('MONGO_URI prefix:', process.env.MONGO_URI ? process.env.MONGO_URI.substring(0, 20) + '...' : 'undefined');
// MongoDB connection function
async function connectDB() {
try {
await mongoose.connect( process.env.MONGO_URI, {
serverSelectionTimeoutMS: 30000, // 30 seconds
socketTimeoutMS: 45000, // 45 seconds
connectTimeoutMS: 30000, // 30 seconds
maxPoolSize: 10, // Maintain up to 10 socket connections
});
console.log('β
Connected to MongoDB');
} catch (error) {
console.error('β MongoDB connection error:', error);
process.exit(1); // Exit if can't connect to database
}
}
// Initialize Express application
const app = express();
const httpServer = http.createServer(app);
// GraphQL context function for authentication
const getGraphQLContext = async ({ req }) => {
let user = null;
// Try to get user from JWT token - check multiple sources
const authHeader = req.headers.authorization?.replace('Bearer ', '');
const httpOnlyToken = req.cookies?.token;
const clientToken = req.cookies?.clientToken;
const token = authHeader || clientToken || httpOnlyToken;
if (token) {
try {
user = verifyJwt(token);
} catch (error) {
console.log('β GraphQL Context - Invalid token:', error.message);
}
}
return { user, req };
};
// Main server startup function
async function startServer() {
// MongoDB connection
await connectDB();
// Ensure connection is ready before proceeding
console.log('π Verifying MongoDB connection...');
await mongoose.connection.db.admin().ping();
console.log('β
MongoDB connection verified');
// Small delay to ensure everything is ready
await new Promise(resolve => setTimeout(resolve, 1000));
// Initialize Telegram bot
try {
const { startTelegramBot } = await import('./src/bot/index.js');
startTelegramBot();
} catch (error) {
console.error('β Failed to start Telegram bot:', error);
}
// Create Apollo Server
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
// Start Apollo Server
await apolloServer.start();
// Configure EJS as template engine
app.set('view engine', 'ejs');
app.set('views', './views');
// Middleware setup for static files, parsing, and session management
app.use(express.static('dist')); // Serve built assets
app.use(express.static('public')); // Serve public assets
app.use(cors()); // Enable CORS for API requests
app.use(express.json()); // Parse JSON request bodies
app.use(express.urlencoded({ extended: true })); // Parse URL-encoded bodies
app.use(cookieParser()); // Parse cookies from requests
// Session middleware for flash messages and user state
// Generate a unique server instance ID to invalidate old sessions on restart
const serverInstanceId = Date.now().toString(36) + Math.random().toString(36).substr(2);
// Create session store with error handling
let sessionStore;
try {
sessionStore = MongoStore.create({
client: mongoose.connection.getClient(),
dbName: 'taskquest',
collectionName: 'sessions',
ttl: 24 * 60 * 60 // 1 day in seconds
});
console.log('β
Session store created successfully');
} catch (error) {
console.error('β Error creating session store:', error);
console.log('βΉοΈ Using memory store as fallback');
sessionStore = undefined; // Use default memory store
}
app.use(session({
secret: process.env.SESSION_SECRET || 'taskquest_secret',
resave: false,
saveUninitialized: true, // Changed to true to ensure session exists
store: sessionStore,
cookie: {
maxAge: 1000 * 60 * 60 * 24, // 1 day session
httpOnly: true, // Prevent XSS attacks
secure: process.env.NODE_ENV === 'production' // Use secure cookies in production
},
name: 'taskquest.sid' // Custom session name
}));
// Middleware to ensure session exists and handle server instance changes
app.use((req, res, next) => {
// Ensure session exists before proceeding
if (!req.session) {
console.error('β Session not available, regenerating...');
return req.session.regenerate((err) => {
if (err) {
console.error('Failed to regenerate session:', err);
return next(err);
}
req.session.serverInstanceId = serverInstanceId;
next();
});
}
// Check if this is a session from a previous server instance
if (req.session.serverInstanceId && req.session.serverInstanceId !== serverInstanceId) {
console.log('π Destroying session from previous server instance');
return req.session.destroy((err) => {
if (err) console.error('Error destroying old session:', err);
// Clear cookies and redirect to home to get a fresh session
res.clearCookie('taskquest.sid');
res.clearCookie('token');
return res.redirect('/');
});
} else {
// Mark session with current server instance
req.session.serverInstanceId = serverInstanceId;
next();
}
});
// Flash message middleware for user notifications
app.use(flash());
// Make flash messages available in all EJS templates with robust error handling
app.use((req, res, next) => {
try {
// Ensure session and flash are available before calling
if (req.session && typeof req.flash === 'function') {
res.locals.success = req.flash('success') || [];
res.locals.error = req.flash('error') || [];
res.locals.info = req.flash('info') || [];
} else {
console.warn('β οΈ Session or flash not available, using defaults');
res.locals.success = [];
res.locals.error = [];
res.locals.info = [];
}
} catch (error) {
console.error('β Flash middleware error:', error);
// Provide default empty arrays if flash fails
res.locals.success = [];
res.locals.error = [];
res.locals.info = [];
}
next();
});
// Mount authentication routes
app.use('/api/auth', authRoutes);
// Mount codes routes
app.use('/api/codes', codesRoutes);
// Simple API endpoint to get quest members (like quests.ejs does)
app.get('/api/quest/:questId/members', requireAuthFlexible, async (req, res) => {
try {
const { questId } = req.params;
console.log('π Fetching quest members for questId:', questId);
const Quest = (await import('./src/models/Quest.js')).default;
const User = (await import('./src/models/User.js')).default;
// Get quest with populated members
const quest = await Quest.findById(questId).populate('members', '_id username email').lean();
if (!quest) {
return res.status(404).json({ error: 'Quest not found' });
}
// Check for duplicates in the raw database data
const rawQuest = await Quest.findById(questId).lean();
const originalMembersCount = rawQuest.members.length;
const uniqueMemberIds = [...new Set(rawQuest.members.map(id => id.toString()))];
// If we found duplicates, clean them up
if (uniqueMemberIds.length !== originalMembersCount) {
console.log(`π§Ή Found ${originalMembersCount - uniqueMemberIds.length} duplicate members in quest, cleaning up...`);
const mongoose = (await import('mongoose')).default;
await Quest.findByIdAndUpdate(questId, {
members: uniqueMemberIds.map(id => new mongoose.Types.ObjectId(id))
});
console.log(`β
Cleaned up ${originalMembersCount - uniqueMemberIds.length} duplicate members`);
// Re-fetch the quest with cleaned data
const cleanedQuest = await Quest.findById(questId).populate('members', '_id username email').lean();
quest.members = cleanedQuest.members;
}
// Also get all users in the system for comparison
const allUsers = await User.find({}, '_id username email').lean();
console.log('π Quest members after cleanup:', quest.members.length);
console.log('π All users in system:', allUsers.length);
console.log('β
Final quest members:');
quest.members.forEach((member, index) => {
console.log(` ${index + 1}. ${member.username} (${member.email}) - ID: ${member._id}`);
});
console.log('π All users for reference:');
allUsers.forEach((user, index) => {
const inQuest = quest.members.some(m => m._id.toString() === user._id.toString());
console.log(` ${index + 1}. ${user.username} (${user.email}) - ID: ${user._id} ${inQuest ? 'β
IN QUEST' : 'β NOT IN QUEST'}`);
});
res.json({
success: true,
members: quest.members,
questTitle: quest.title,
totalUsersInSystem: allUsers.length,
questMembersCount: quest.members.length,
duplicatesRemoved: originalMembersCount - quest.members.length
});
} catch (error) {
console.error('β Error fetching quest members:', error);
res.status(500).json({ error: 'Failed to fetch quest members' });
}
});
// Temporary cleanup endpoint to remove duplicate members from quests
app.post('/api/quest/:questId/cleanup-members', requireAuthFlexible, async (req, res) => {
try {
const { questId } = req.params;
console.log('π§Ή Cleaning up duplicate members for questId:', questId);
const Quest = (await import('./src/models/Quest.js')).default;
const quest = await Quest.findById(questId);
if (!quest) {
return res.status(404).json({ error: 'Quest not found' });
}
console.log('π Original members array length:', quest.members.length);
console.log('π Original members:', quest.members);
// Remove duplicates by converting ObjectIds to strings and using Set
const uniqueMemberIds = [...new Set(quest.members.map(id => id.toString()))];
const mongoose = (await import('mongoose')).default;
quest.members = uniqueMemberIds.map(id => new mongoose.Types.ObjectId(id));
console.log('β
Cleaned members array length:', quest.members.length);
console.log('β
Cleaned members:', quest.members);
await quest.save();
res.json({
success: true,
message: 'Duplicate members removed',
originalCount: quest.members.length + (uniqueMemberIds.length - quest.members.length),
cleanedCount: quest.members.length,
removedDuplicates: (quest.members.length + (uniqueMemberIds.length - quest.members.length)) - quest.members.length
});
} catch (error) {
console.error('β Error cleaning up quest members:', error);
res.status(500).json({ error: 'Failed to cleanup quest members' });
}
});
// Mount GraphQL endpoint
app.use('/graphql', cors(), express.json(), expressMiddleware(apolloServer, {
context: getGraphQLContext
}));
// Health check endpoint for monitoring and keeping Render warm
app.get('/api/health', (req, res) => {
res.json({
status: 'OK',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
memory: process.memoryUsage(),
version: process.version,
environment: process.env.NODE_ENV || 'development',
serverInstanceId: serverInstanceId,
graphql: '/graphql endpoint available'
});
});
// Comprehensive health check that tests database connectivity
app.get('/api/health/detailed', async (req, res) => {
const healthCheck = {
status: 'OK',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
environment: process.env.NODE_ENV || 'development',
serverInstanceId: serverInstanceId,
checks: {
database: 'unknown',
memory: 'ok',
uptime: 'ok'
}
};
try {
// Test database connection
await mongoose.connection.db.admin().ping();
healthCheck.checks.database = 'connected';
} catch (error) {
healthCheck.checks.database = 'disconnected';
healthCheck.status = 'ERROR';
console.error('Database health check failed:', error);
}
// Check memory usage
const memUsage = process.memoryUsage();
const memUsedMB = Math.round(memUsage.heapUsed / 1024 / 1024);
healthCheck.checks.memory = memUsedMB > 500 ? 'high' : 'ok';
healthCheck.memory = {
used: `${memUsedMB}MB`,
total: `${Math.round(memUsage.heapTotal / 1024 / 1024)}MB`
};
// Check uptime
const uptimeHours = process.uptime() / 3600;
healthCheck.checks.uptime = uptimeHours > 24 ? 'long' : 'ok';
const statusCode = healthCheck.status === 'OK' ? 200 : 503;
res.status(statusCode).json(healthCheck);
});
// Simple ping endpoint for external monitoring services
app.get('/ping', (req, res) => {
res.status(200).send('pong');
});
// Warmup endpoint that can be called to prevent cold starts
app.get('/warmup', (req, res) => {
console.log('π₯ Warmup request received at', new Date().toISOString());
res.json({
status: 'warm',
timestamp: new Date().toISOString(),
message: 'Server is warm and ready'
});
});
// Bot Command Management API endpoints
app.get('/api/bot-commands', requireAuthFlexible, async (req, res) => {
try {
const commands = await BotCommand.find({}).sort({ category: 1, command: 1 });
res.json(commands);
} catch (error) {
console.error('Error fetching bot commands:', error);
res.status(500).json({ error: 'Failed to fetch bot commands' });
}
});
app.put('/api/bot-commands/:id', requireAuthFlexible, async (req, res) => {
try {
const { id } = req.params;
const { description, responseMessage, isActive } = req.body;
const updatedCommand = await BotCommand.findByIdAndUpdate(
id,
{ description, responseMessage, isActive },
{ new: true, runValidators: true }
);
if (!updatedCommand) {
return res.status(404).json({ error: 'Bot command not found' });
}
res.json(updatedCommand);
} catch (error) {
console.error('Error updating bot command:', error);
res.status(500).json({ error: 'Failed to update bot command' });
}
});
app.post('/api/bot-commands/bulk-update', requireAuthFlexible, async (req, res) => {
try {
const { commands } = req.body;
if (!Array.isArray(commands)) {
return res.status(400).json({ error: 'Commands must be an array' });
}
const updatePromises = commands.map(cmd =>
BotCommand.findOneAndUpdate(
{ command: cmd.command },
{
description: cmd.description,
responseMessage: cmd.responseMessage,
isActive: cmd.isActive !== undefined ? cmd.isActive : true
},
{ new: true, runValidators: true }
)
);
const updatedCommands = await Promise.all(updatePromises);
res.json(updatedCommands);
} catch (error) {
console.error('Error bulk updating bot commands:', error);
res.status(500).json({ error: 'Failed to update bot commands' });
}
});
// Firebase configuration for client-side authentication
const firebaseConfig = {
apiKey: "AIzaSyCmS_rBZ9PWYlp_OCH-eUqboeYAIq_YsN0",
authDomain: "oauth-practise-d450b.firebaseapp.com",
projectId: "oauth-practise-d450b",
storageBucket: "oauth-practise-d450b.firebasestorage.app",
messagingSenderId: "337604840275",
appId: "1:337604840275:web:0dbde6ee75e4b27d66afa6"
};
// Initialize Firebase app
const firebaseApp = initializeApp(firebaseConfig);
// Server port configuration
const port = process.env.PORT || 4000;
// Homepage route - Clear any existing auth state and show landing page
app.get('/', (req, res) => {
res.clearCookie('token'); // Clear authentication cookie
res.clearCookie('taskquest.sid'); // Clear custom session cookie
if (req.session) {
// Destroy any existing session
req.session.destroy((err) => {
if (err) {
console.log('Session destroy error:', err);
}
// Always render the homepage, even if session destroy fails
res.render('index', { title: 'TaskQuest' });
});
} else {
res.render('index', { title: 'TaskQuest' });
}
});
// Dashboard route - Main application dashboard with real data from MongoDB
app.get('/dashboard', requireAuthSSR, async (req, res) => {
try {
// Import models dynamically to avoid circular dependency issues
const User = (await import('./src/models/User.js')).default;
const Quest = (await import('./src/models/Quest.js')).default;
const mongoose = (await import('mongoose')).default;
// Find the authenticated user by their JWT userId
let user = await User.findById(req.user.userId || req.user._id).lean();
if (!user) {
req.flash('error', 'User not found. Please log in again.');
return res.redirect('/logout');
}
// Check if there are any quests in the system
const allQuests = await Quest.find({}).lean();
console.log(`π Total quests in system: ${allQuests.length}`);
// Fetch quests assigned to this user ONLY - no auto-linking
let quests = [];
if (user.questsIn && user.questsIn.length > 0) {
quests = await Quest.find({ _id: { $in: user.questsIn } })
.populate('creator', 'username email role')
.populate('members', 'username email role createdAt')
.lean();
}
console.log('π« REMOVED AUTO-LINKING: Users are now only associated with quests they are explicitly added to');
console.log('π User quest membership:', {
userId: user._id,
username: user.username,
questsInArray: user.questsIn || [],
actualQuestsFound: quests.length,
totalQuestsInSystem: allQuests.length
});
// Debug: Log quests data
console.log('π Dashboard Debug Info:');
console.log('π€ User:', user.username);
console.log('π’ User questsIn array:', user.questsIn);
console.log('π― Fetched quests:', quests.length, 'quests found');
if (quests.length > 0) {
console.log('π Quest details:', quests.map(q => ({ title: q.title, progress: q.progress, completed: q.completed })));
}
// Check if user has no quests - if so, show create quest modal on dashboard
// Allow skipping the modal with ?skipModal=true query parameter for testing
const skipModal = req.query.skipModal === 'true';
// Show create quest prompt if user has no quests assigned to them
const showCreateQuestPrompt = !skipModal && quests.length === 0;
// Fetch tasks related to the user
const Task = (await import('./src/models/Task.js')).default;
let tasks = [];
try {
if (user.role === 'ADMIN') {
// Admins can see all tasks in the system for assignment purposes
tasks = await Task.find({})
.populate('assignedTo', 'username email')
.populate('quest', 'title description')
.populate('createdBy', 'username email')
.sort({ createdAt: -1 })
.lean();
console.log('π Admin: Fetched all tasks:', tasks.length, 'tasks found');
} else {
// Regular users only see tasks assigned to them
const userId = user._id;
tasks = await Task.find({ assignedTo: userId })
.populate('quest', 'title description')
.populate('createdBy', 'username email')
.sort({ createdAt: -1 })
.lean();
console.log('π User: Fetched assigned tasks:', tasks.length, 'tasks found');
}
} catch (taskError) {
console.warn('β οΈ Error fetching tasks:', taskError.message);
// Continue with empty tasks array
}
// Fetch all users for team member management
let allUsers = [];
try {
console.log('π USER FILTERING DEBUG - Start');
console.log('π Current user ID:', user._id);
console.log('π Current user role:', user.role);
console.log('π Current user questsIn:', user.questsIn);
if (user.role === 'ADMIN') {
console.log('π ADMIN PATH: Fetching users from current admin\'s quests only');
// Get quests where current admin is the creator or member
const adminQuests = await Quest.find({
$or: [
{ creator: user._id },
{ members: user._id }
]
}).select('_id title members').lean();
console.log('π Admin\'s quests:', adminQuests.map(q => ({ id: q._id, title: q.title, memberCount: q.members.length })));
if (adminQuests.length > 0) {
// Get all unique member IDs from admin's quests
const memberIds = new Set();
adminQuests.forEach(quest => {
quest.members.forEach(memberId => {
memberIds.add(memberId.toString());
});
});
console.log('π Unique member IDs from admin\'s quests:', Array.from(memberIds));
// Only show users who are members of admin's quests
allUsers = await User.find({
_id: { $in: Array.from(memberIds) }
})
.select('username email role questsIn createdAt phone')
.populate('questsIn', 'title')
.sort({ createdAt: -1 })
.lean();
console.log('οΏ½ FINAL ADMIN USERS - Count:', allUsers.length);
allUsers.forEach((u, index) => {
console.log(`π ${index + 1}. ${u.username} (${u.email}) - ID: ${u._id}`);
console.log(`π Quests: ${u.questsIn.map(q => q.title).join(', ')}`);
});
} else {
console.log('π Admin has no quests, showing empty list');
allUsers = [];
}
} else {
console.log('π NON-ADMIN PATH: Regular user filtering');
// Regular users can see users who share quests with them, plus themselves
const userQuestIds = user.questsIn || [];
console.log('π User quest IDs:', userQuestIds);
if (userQuestIds.length > 0) {
// Find users who are part of the same quests as the current user
allUsers = await User.find({
$or: [
{ questsIn: { $in: userQuestIds } }, // Users who share quests
{ _id: user._id } // Always include current user
]
})
.select('username email role questsIn createdAt phone')
.populate('questsIn', 'title')
.sort({ createdAt: -1 })
.lean();
console.log('π FINAL NON-ADMIN USERS - Count:', allUsers.length);
allUsers.forEach((u, index) => {
console.log(`π ${index + 1}. ${u.username} (${u.email}) - ID: ${u._id}`);
console.log(`π Quests: ${u.questsIn.map(q => q.title).join(', ')}`);
});
console.log('π₯ User: Fetched team members:', allUsers.length, 'users found');
} else {
console.log('π₯ Current user has no quests, showing only themselves');
// If user has no quests, only show themselves
allUsers = [user];
}
}
console.log('π USER FILTERING DEBUG - End');
} catch (userError) {
console.warn('β οΈ Error fetching users:', userError.message);
// Continue with empty users array
}
// Set welcome message only once per session with proper session checks
if (req.session && typeof req.flash === 'function' && !req.session.welcomeShown) {
try {
req.flash('success', `Welcome back, ${user.username || 'User'}! π`);
req.session.welcomeShown = true;
} catch (flashError) {
console.warn('β οΈ Could not set welcome flash message:', flashError.message);
}
}
// Render dashboard with real user data and quest creation prompt if needed
// Process quests to include their specific members for proper quest isolation
const questsWithMembers = quests.map(quest => {
console.log('π Processing quest for member isolation:', {
questId: quest._id,
questTitle: quest.title,
membersCount: quest.members ? quest.members.length : 0,
memberDetails: quest.members ? quest.members.map(m => ({ id: m._id, username: m.username })) : []
});
return {
...quest,
questMembers: quest.members || [] // These are already populated from the query
};
});
res.render('dashboard', {
user,
tasks,
quests: questsWithMembers,
allUsers, // Keep for backwards compatibility, but template should use quest.questMembers
activeSection: req.query.section || null,
showCreateQuestPrompt // This will trigger the modal popup if no quests exist
});
} catch (err) {
console.error('Error loading dashboard:', err);
try {
req.flash('error', 'Failed to load dashboard.');
} catch (flashError) {
console.warn('β οΈ Could not set error flash message:', flashError.message);
}
res.redirect('/');
}
});
// OTP route - Firebase phone authentication page
app.get('/otp', (req, res) => {
res.render('otp');
});
// Logout route - Clear session and cookies, redirect to home
app.get('/logout', (req, res) => {
res.clearCookie('token');
res.clearCookie('clientToken');
req.session.destroy(() => {
res.redirect('/');
});
});
// Start the HTTP server (not just Express app)
httpServer.listen(port, () => {
console.log('π TaskQuest Server Started');
console.log(`π Server running on http://localhost:${port}`);
console.log(`π§ GraphQL endpoint: http://localhost:${port}/graphql`);
console.log(`π Server Instance ID: ${serverInstanceId}`);
console.log('π All previous sessions have been invalidated');
console.log('ββββββββββββββββββββββββββββββββββββββββββββββββ');
});
}
// Start the server
startServer().catch(error => {
console.error('β Failed to start server:', error);
process.exit(1);
});