-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattributeRandomFollowsLikes.js
106 lines (87 loc) · 3.16 KB
/
attributeRandomFollowsLikes.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
require("dotenv").config();
const mongoose = require("mongoose");
process.chdir(__dirname); // Set the current working directory to the script's directory
const shuffleArray = (array) => {
// Fisher-Yates shuffle algorithm
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
};
// Helper function to create an array of random users IDs
const getRandomIdsUser = async (model, count, isOrganizer) => {
const documents = await model.find({ isOrganizer });
// console.log("documents: ", documents);
// Shuffle the array of documents
shuffleArray(documents);
const ids = [];
for (let i = 0; i < count && i < documents.length; i++) {
const randomDocument = documents[i];
ids.push(randomDocument._id);
}
return ids;
};
// Helper function to create an array of random activities IDs
const getRandomIdsActivity = async (model, count) => {
const documents = await model.find();
const ids = [];
for (let i = 0; i < count; i++) {
const randomDocument =
documents.length > 0
? documents[Math.floor(Math.random() * documents.length)]
: null;
if (randomDocument) {
ids.push(randomDocument._id);
}
}
return ids;
};
// Asynchronous function to generate sample data
async function UpdateActivityUserDB() {
// Replace with your MongoDB connection string
await mongoose
.connect(process.env.CONNECTION_STRING, { connectTimeoutMS: 2000 })
.then(() => {
console.log("Database connected");
});
// Models
const User = require("./models/users"); // Adjust the path accordingly
const Activity = require("./models/activities"); // Adjust the path accordingly
const activities = await Activity.find();
for (const activity of activities) {
const idAuthor = (await getRandomIdsUser(User, 1, false))[0];
const idOrganizer = (await getRandomIdsUser(User, 1, true))[0];
const idUsersLikes = await getRandomIdsUser(User, 3, false);
// console.log(idAuthor, idOrganizer, idUsersLikes);
activity.author = idAuthor;
activity.organizer = idOrganizer;
activity.likes = idUsersLikes;
await activity
.save()
.then(console.log("Successful update of the activity"));
let organizer = await User.findById(idOrganizer);
organizer.organizerDetails.activities.push(activity._id);
await organizer
.save()
.then(
console.log("Successful update of the activities of the organizer")
);
organizer = await User.findById(idOrganizer);
if (organizer.organizerDetails.followedBy.length === 0) {
const idUsersFollows = await getRandomIdsUser(User, 4, false);
organizer.organizerDetails.followedBy = idUsersFollows;
await organizer
.save()
.then(
console.log("Successful update of the followers of the organizer")
);
}
}
const users = await User.find({ isOrganizer: false });
for (const user of users) {
const idUsersFollows = await getRandomIdsUser(User, 5, false);
user.followedBy = idUsersFollows;
await user.save().then(console.log("Successful update of the parent"));
}
}
UpdateActivityUserDB();