-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseederTest.js
523 lines (426 loc) · 16.1 KB
/
seederTest.js
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
const { MongoClient, ObjectId } = require("mongodb");
const bcrypt = require("bcrypt");
const mongo = new MongoClient("mongodb://localhost");
const db = mongo.db("gspp");
// Connect to the database
async function connectToDB() {
await mongo.connect();
}
// Helper function to generate random usernames and emails
function generateRandomUserData(role) {
const username = `${role.toLowerCase()}_${Math.floor(
Math.random() * 100000
)}`;
const email = `${username}@example.com`;
return { username, email };
}
// Function to generate a random date within a range
function generateRandomDate(start, end) {
return new Date(
start.getTime() + Math.random() * (end.getTime() - start.getTime())
);
}
// Function to generate a random number between a range
function generateRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
// Function to generate a random boolean
function generateRandomBoolean() {
return Math.random() < 0.5;
}
// Function to generate a random card number
function generateRandomCardNumber() {
return `${generateRandomNumber(1000, 9999)} ${generateRandomNumber(
1000,
9999
)} ${generateRandomNumber(1000, 9999)} ${generateRandomNumber(1000, 9999)}`;
}
// Seed users (admin, teachers, students)
async function seedUsers() {
await db.collection("users").deleteMany({});
const roles = ["Teacher", "Student"];
const numTeachers = 20;
const numStudents = 127;
const usersData = [];
// Seed admin users
for (let i = 0; i < numTeachers; i++) {
const userData = generateRandomUserData("Teacher");
usersData.push({
...userData,
password: await bcrypt.hash("password", 10),
profile: `@${userData.username.substring(0, 4)}`,
role: "Teacher",
avatar: "teacher_avatar.jpg",
bio: "Teacher bio",
student_id: [], // Initialize an empty array
courses: [],
chatRoom_id: [],
bluemark: generateRandomBoolean(),
type: ["visa", "mastercard"][Math.floor(Math.random() * 2)],
amount: 30000,
cardNumber: generateRandomCardNumber(),
CVV: `${generateRandomNumber(100, 999)}`,
expired_data: "12/12/2027",
});
}
// Seed student users
for (let i = 0; i < numStudents; i++) {
const userData = generateRandomUserData("Student");
const enrolledCourses = [];
const courses = await db.collection("courses").find().toArray();
const numEnrolledCourses = Math.min(
courses.length,
Math.floor(Math.random() * 4) + 2
);
for (let j = 0; j < numEnrolledCourses; j++) {
enrolledCourses.push(courses[j]._id);
}
usersData.push({
...userData,
password: await bcrypt.hash("password", 10),
profile: `@${userData.username.substring(0, 4)}`,
role: "Student",
avatar: "student_avatar.jpg",
enrolledCourses,
chatRoom_id: [],
type: ["visa", "mastercard"][Math.floor(Math.random() * 2)],
amount: 30000,
cardNumber: generateRandomCardNumber(),
CVV: `${generateRandomNumber(100, 999)}`,
expired_data: "12/12/2027",
});
}
// Insert the user data into the collection
await db.collection("users").insertMany(usersData);
// Insert student ObjectIds into teacher student_id
const teacherUsers = usersData.filter(user => user.role === "Teacher");
const studentUsers = usersData.filter(user => user.role === "Student");
for (const teacher of teacherUsers) {
const numStudentsToAssign = Math.floor(Math.random() * 11) + 10; // 10 to 20 students
const randomStudentIds = getRandomStudentIds(studentUsers, numStudentsToAssign);
teacher.student_id = randomStudentIds;
}
// Update the teacher users with student references
for (const teacher of teacherUsers) {
await db.collection("users").updateOne({ _id: teacher._id }, { $set: { student_id: teacher.student_id } });
}
}
function getRandomStudentIds(studentUsers, count) {
const shuffledStudents = [...studentUsers];
for (let i = shuffledStudents.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledStudents[i], shuffledStudents[j]] = [shuffledStudents[j], shuffledStudents[i]];
}
return shuffledStudents.slice(0, count).map(student => student._id);
}
//seed admin
async function seedAdmin() {
const roles = ["Admin"];
const numAdmins = 3;
const usersData = [];
for (let i = 0; i < numAdmins; i++) {
const userData = generateRandomUserData("Admin");
const teacherIds = [];
const studentIds = [];
const teacherUsers = await db
.collection("users")
.find({ role: "Teacher" })
.toArray();
for (const teacher of teacherUsers) {
teacherIds.push(teacher._id);
}
const studentUsers = await db
.collection("users")
.find({ role: "Student" })
.toArray();
for (const student of studentUsers) {
studentIds.push(student._id);
}
usersData.push({
...userData,
password: await bcrypt.hash("password", 10),
profile: `@${userData.username.substring(0, 4)}`,
role: "Admin",
avatar: "admin_avatar.jpg", // Replace with a random admin avatar
teacher_ids: teacherIds,
student_ids: studentIds,
type: ["visa", "mastercard"][Math.floor(Math.random() * 2)],
amount: 30000,
cardNumber: generateRandomCardNumber(),
CVV: `${generateRandomNumber(100, 999)}`,
expired_data: "12/12/2027",
});
}
await db.collection("users").insertMany(usersData);
}
// Seed courses
// Seed courses
async function seedCourses() {
await db.collection("courses").deleteMany({});
const courseIdsForStu = [];
const teachers = await db
.collection("users")
.find({ role: "Teacher" })
.toArray();
for (const teacher of teachers) {
const courseIds = [];
const numCoursesPerTeacher = 10;
for (let i = 0; i < numCoursesPerTeacher; i++) {
const category = ["Graphic Design", "Programming", "Accounting"][
Math.floor(Math.random() * 3)
];
let thumb;
if (category === "Accounting") {
thumb = "/src/assets/courses/thumbs/acc.jpeg";
} else if (category === "Graphic Design") {
thumb = "/src/assets/courses/thumbs/gd.jpg";
} else if (category === "Programming") {
thumb = "/src/assets/courses/thumbs/prog.jpeg";
}
const title = `Course Title ${i + 1}`;
const description = "Course description";
const price = Math.floor(Math.random() * 451) + 50; // Random price between 50 and 500
const modules = [];
const numModules = Math.floor(Math.random() * 5) + 6; // Random number of modules between 6 and 10
for (let j = 0; j < numModules; j++) {
modules.push({
title: `Module ${j + 1}`,
video: "https://example.com/video",
description: "Module description",
});
}
const rating = Math.floor(Math.random() * 5) + 1; // Random rating between 1 and 5
const likes = [];
const comments = [];
const courseData = {
title,
description,
category,
thumb, // Add the thumb field based on the category
price,
courseOwner: teacher._id,
modules,
rating,
likes,
comments,
};
const result = await db.collection("courses").insertOne(courseData);
courseIds.push(result.insertedId);
courseIdsForStu.push(result.insertedId);
}
await db
.collection("users")
.updateOne({ _id: teacher._id }, { $set: { courses: courseIds } });
const students = await db
.collection("users")
.find({ role: "Student" })
.toArray();
for (const student of students) {
// Generate random courseIds for each student
const studentCourseIds = [];
const numEnrolledCourses = Math.floor(Math.random() * courseIdsForStu.length) + 1;
for (let i = 0; i < 20; i++) {
const randomCourseId = courseIdsForStu[Math.floor(Math.random() * courseIdsForStu.length)];
studentCourseIds.push(randomCourseId);
}
await db
.collection("users")
.updateOne(
{ _id: student._id },
{ $set: { enrolledCourses: studentCourseIds } }
);
}
}
}
// Seed chat rooms
async function seedChatRooms() {
await db.collection("chatRooms").deleteMany({});
const students = await db
.collection("users")
.find({ role: "Student" })
.toArray();
const teachers = await db
.collection("users")
.find({ role: "Teacher" })
.toArray();
const chatRoomsData = [];
for (const student of students) {
const teacher = teachers[Math.floor(Math.random() * teachers.length)];
chatRoomsData.push({
participants: [student._id, teacher._id],
messages: [],
});
}
await db.collection("chatRooms").insertMany(chatRoomsData);
}
// Seed modules
async function seedModules() {
await db.collection("modules").deleteMany({});
const courses = await db.collection("courses").find().toArray();
for (const course of courses) {
const moduleIds = [];
const numModules = Math.floor(Math.random() * 5) + 6; // Random number of modules between 6 and 10
for (let i = 0; i < numModules; i++) {
const module = {
title: `Module ${i + 1}`,
video: "https://example.com/video",
description: "Module description",
};
const result = await db.collection("modules").insertOne(module);
moduleIds.push(result.insertedId);
}
await db
.collection("courses")
.updateOne({ _id: course._id }, { $set: { modules: moduleIds } });
}
}
// Seed transactions
async function seedTransactions() {
await db.collection("transactions").deleteMany({});
const users = await db.collection("users").find().toArray();
const teachers = users.filter(user => user.role === "Teacher");
const students = users.filter(user => user.role === "Student");
const transactionsData = [];
for (const user of users) {
if (user.role !== "Admin" && user.role !== "Teacher") {
continue;
}
const receiver =
user.role === "Admin"
? teachers[Math.floor(Math.random() * teachers.length)]
: teachers.filter(teacher => teacher._id !== user._id)[
Math.floor(Math.random() * (teachers.length - 1))
];
transactionsData.push({
amount: Math.floor(Math.random() * 401) + 100, // Random amount between 100 and 500
paymentMethod: ["visa", "mastercard"][
Math.floor(Math.random() * 2)
],
giver: user._id,
teacherReceiver: receiver._id,
adminReceiver: user.role === "Admin" ? user._id : null,
whyTran: "Transaction reason",
created_at: generateRandomDate(new Date(2022, 0, 1), new Date()), // Generate random date within a range
});
}
await db.collection("transactions").insertMany(transactionsData);
}
// Seed messages
async function seedMessages() {
await db.collection("messages").deleteMany({});
const chatRooms = await db.collection("chatRooms").find().toArray();
const messagesData = [];
for (const chatRoom of chatRooms) {
const sender = chatRoom.participants[Math.floor(Math.random() * 2)];
const receiver = chatRoom.participants.find(
participant => participant !== sender
);
messagesData.push({
sender,
textMessage: "Message content",
created_at: new Date(),
});
await db
.collection("chatRooms")
.updateOne(
{ _id: chatRoom._id },
{ $push: { messages: messagesData[messagesData.length - 1] } }
);
}
await db.collection("messages").insertMany(messagesData);
}
// Seed comments
// Seed comments
async function seedComments() {
await db.collection("comments").deleteMany({});
const users = await db
.collection("users")
.find({ role: "Student" })
.toArray();
const courses = await db.collection("courses").find().toArray();
const commentsData = [];
for (const user of users) {
for (const course of courses) {
const courseOwner = await db
.collection("users")
.findOne({ _id: course.courseOwner });
const commentedCourse = course._id;
const comment = {
commentOwner: user._id,
commentedCourse: commentedCourse,
text: "Comment text",
created_at: new Date(),
};
// commentsData.push({
// commentOwner: user._id,
// commentedCourse,
// text: "Comment text",
// created_at: new Date(),
// });
const result = await db.collection("comments").insertOne(comment);
await db.collection("courses").updateOne(
{ _id: commentedCourse },
{
$push: {
comments: result.insertedId,
},
}
);
}
}
// await db.collection("comments").insertMany(commentsData);
}
// // Call the modified seedComments function
// seedComments();
// // Call the modified seedComments function
// seedComments();
// // Call the modified seedComments function
// seedComments();
//seed review
async function seedReview() {
const numReviews = 200;
const reviewsData = [];
const students = await db
.collection("users")
.find({ role: "Student" })
.toArray();
const courses = await db.collection("courses").find().toArray();
for (let i = 0; i < numReviews; i++) {
const randomStudent = students[Math.floor(Math.random() * students.length)];
const randomCourse = courses[Math.floor(Math.random() * courses.length)];
const review = {
star: Math.floor(Math.random() * 5) + 1, // Random star rating between 1 and 5
reviewComment: "This course is great!", // You can customize the review comment
giver: randomStudent._id, // Random student as the giver
original: randomCourse._id, // Random course as the original
};
reviewsData.push(review);
}
await db.collection("review").insertMany(reviewsData);
}
// Seed function
async function seed() {
console.log("Started seeding...");
await connectToDB();
// Clear previous data from collections
await db.collection("users").deleteMany({});
await db.collection("courses").deleteMany({});
await db.collection("chatRooms").deleteMany({});
await db.collection("modules").deleteMany({});
await db.collection("transactions").deleteMany({});
await db.collection("messages").deleteMany({});
await db.collection("comments").deleteMany({});
await db.collection("review").deleteMany({});
await seedUsers();
await seedAdmin();
await seedCourses();
await seedChatRooms();
await seedModules();
await seedTransactions();
await seedMessages();
await seedComments();
await seedReview();
console.log("Seeding complete.");
process.exit(0);
}
seed();