File size: 3,526 Bytes
057576a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
require('dotenv').config();

// Import models
const User = require('../src/models/User');
const Conversation = require('../src/models/Conversation');
const Message = require('../src/models/Message');

const initDB = async () => {
  try {
    // Connect to database
    await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/ysnrfd-messenger');
    console.log('βœ… Connected to database for initialization');

    // Clear existing data (optional - for development)
    if (process.argv.includes('--clean')) {
      await User.deleteMany({});
      await Conversation.deleteMany({});
      await Message.deleteMany({});
      console.log('πŸ—‘οΈ  Cleared existing data');
    }

    // Create sample users
    const sampleUsers = [
      {
        username: 'alice',
        email: 'alice@example.com',
        password: 'password123',
        displayName: 'Alice Johnson',
        bio: 'Hello! I love coding and chatting.',
        status: 'online'
      },
      {
        username: 'bob',
        email: 'bob@example.com',
        password: 'password123',
        displayName: 'Bob Smith',
        bio: 'Developer and tech enthusiast',
        status: 'online'
      },
      {
        username: 'charlie',
        email: 'charlie@example.com',
        password: 'password123',
        displayName: 'Charlie Brown',
        bio: 'Just here to chat with friends',
        status: 'away'
      }
    ];

    const createdUsers = await User.insertMany(sampleUsers);
    console.log(`πŸ‘€ Created ${createdUsers.length} sample users`);

    // Create a sample conversation
    const sampleConversation = await Conversation.create({
      type: 'group',
      name: 'General Chat',
      description: 'A place for general discussion',
      createdBy: createdUsers[0]._id,
      participants: [
        { user: createdUsers[0]._id, role: 'owner' },
        { user: createdUsers[1]._id, role: 'member' },
        { user: createdUsers[2]._id, role: 'member' }
      ]
    });

    console.log('πŸ’¬ Created sample conversation');

    // Create sample messages
    const sampleMessages = [
      {
        conversation: sampleConversation._id,
        sender: createdUsers[0]._id,
        content: 'Hello everyone! Welcome to the chat! πŸ‘‹',
        type: 'text'
      },
      {
        conversation: sampleConversation._id,
        sender: createdUsers[1]._id,
        content: 'Thanks for setting this up, Alice! 😊',
        type: 'text'
      },
      {
        conversation: sampleConversation._id,
        sender: createdUsers[2]._id,
        content: 'Hey guys! Looking forward to chatting with you all!',
        type: 'text'
      }
    ];

    await Message.insertMany(sampleMessages);
    console.log(`πŸ’Œ Created ${sampleMessages.length} sample messages`);

    console.log('\nπŸŽ‰ Database initialization completed successfully!');
    console.log('\nπŸ“‹ Sample login credentials:');
    console.log('Alice: alice@example.com / password123');
    console.log('Bob: bob@example.com / password123');
    console.log('Charlie: charlie@example.com / password123');

  } catch (error) {
    console.error('❌ Initialization error:', error);
    process.exit(1);
  } finally {
    await mongoose.connection.close();
    console.log('\nπŸ”Œ Database connection closed');
  }
};

// Run initialization
initDB();