-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathseed.js
207 lines (177 loc) · 6.26 KB
/
seed.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
import { faker } from "@faker-js/faker";
import https from "https";
import userData from "./server/data/user.js";
import { journalData } from "./server/data/index.js";
import userInfo from "./users.js";
import feedbackInfo from "./feedbackFake.js";
import { addMessagesToChat, getAllChatId } from "./server/data/chat.js";
import { feedData } from "./server/data/index.js";
import { feedBackData } from "./server/data/index.js";
import feeds from "./feedsFake.js";
const createFeeds = async () => {
let feedIds = []
feedIds = await Promise.all(feeds.map(async (feed) => feedData.createFeed(
feed.title,
feed.description,
feed.type,
feed.images
)))
return feedIds
}
const imageUrlToBase64 = async (url) => {
return new Promise((resolve, reject) => {
https
.get(url, (response) => {
const chunks = [];
response.on("data", (chunk) => {
chunks.push(chunk);
});
response.on("end", () => {
const buffer = Buffer.concat(chunks);
const base64 = buffer.toString("base64");
resolve("data:image/jpeg;base64," + base64);
});
})
.on("error", (error) => {
reject(error);
});
});
};
const createFeedback = async (chatArray, feedbacks) => {
try {
for (const chat of chatArray) {
const userIds = chat.users;
const chatId = chat._id;
for (const userId of userIds) {
const feedback = feedbacks[Math.floor(Math.random() * feedbacks.length)];
const createdFeedback = await feedBackData.createFeedBack(
userId,
chatId,
feedback.isPublic,
feedback.rating.reconnect_probability,
feedback.rating.satisfied_with_chat,
feedback.rating.listener_rating,
feedback.description
);
console.log(`Created feedback ${createdFeedback._id} for user ${userId} in chat ${chatId}`);
}
}
} catch (e) {
console.log(e);
}
};
const createFeedLikeSaveComment = async (users, feedIds) => {
const likePromises = users.map((user) => feedData.updateLike(user._id, feedIds[0], true))
const likePromises2 = users.map((user) => feedData.updateLike(user._id, feedIds[1], true))
await Promise.all(likePromises)
await Promise.all(likePromises2)
console.log('Create likes for users')
const savePromises = users.map((user) => feedData.savePost(user._id, feedIds[0], true))
const savePromises2 = users.map((user) => feedData.savePost(user._id, feedIds[1], true))
await Promise.all(savePromises)
await Promise.all(savePromises2)
console.log('Create saves for users')
const commentPromises = users.map((user) => feedData.updateComment({ userId: user._id, feedId: feedIds[0], message: 'my comment', userName: `${user.firstName} ${user.lastName}` }))
const commentPromises2 = users.map((user) => feedData.updateComment({ userId: user._id, feedId: feedIds[1], message: 'my comment', userName: `${user.firstName} ${user.lastName}` }))
await Promise.all(commentPromises)
await Promise.all(commentPromises2)
console.log('Create comments for users')
}
const createConversations = async (seekerIds, listenerIds) => {
try {
const messages = [
"Hi! How are you?",
"What brings you here today?",
"How can I help you?",
"Tell me about yourself.",
"What's on your mind?",
"Is there anything you want to talk about?",
"How has your day been?",
"What do you like to do for fun?",
"Do you have any hobbies?",
"What's your favorite book/movie/band?",
];
for (const seekerId of seekerIds) {
for (const listenerId of listenerIds) {
await addMessagesToChat(
seekerId,
listenerId,
messages[Math.floor(Math.random() * messages.length)]
);
console.log(`Created chat between seeker ${seekerId} and listener ${listenerId}`);
}
}
const chatIdArray = await getAllChatId();
const feedbacks = feedbackInfo.feedback;
await createFeedback(chatIdArray, feedbacks);
} catch (e) {
console.log(e);
}
};
const createUsersAndJournals = async (users, messages) => {
try {
for (const user of users) {
const profilePicBase64 = await imageUrlToBase64(faker.image.avatar());
const createdUser = await userData.create(
user.firstName,
user.lastName,
user.username,
user.email,
user.password,
user.gender,
user.age,
user.city,
user.state,
user.isAnonymous,
user.role,
profilePicBase64
);
const randomIndex = Math.floor(Math.random() * messages.length);
const message = messages[randomIndex];
const startDate = "2023-05-07";
const endDate = "2023-12-31";
const start = new Date(startDate).getTime();
const end = new Date(endDate).getTime();
const randomTime = Math.random() * (end - start) + start;
const randomDate = new Date(randomTime);
const date = randomDate.toISOString();
const journalEntry = await journalData.CreateJournal(
createdUser._id,
message,
date
);
console.log(`Created journal entry for user ${createdUser.username}`);
}
const idArray = await userData.allUsers();
const seekerIds = [];
const listenerIds = [];
idArray.forEach((obj) => {
if (obj.role === "seeker") {
seekerIds.push(obj._id.toString());
} else if (obj.role === "listener") {
listenerIds.push(obj._id.toString());
}
});
await createConversations(seekerIds, listenerIds);
const feedIds = await createFeeds()
await createFeedLikeSaveComment(idArray, feedIds)
console.log('\n**** Successfully ran seed file! ****\n')
process.exit()
} catch (e) {
console.log(e);
}
};
const users = userInfo.users;
const messages = [
"YAY I am in the Journal",
"I am inside a Journal of a random user",
"This is a journal for web programming 1",
"Hello Prof Patrick Loved your dedication and hardwork for this course",
"I am generated by Sachin",
"I dont know where am I",
"I am trying to get an internship",
"My teammates worked so hard with me in this project",
"Spent 6 hours a day for this project at library with group memebers in last week of semester",
"Good Bye ",
];
createUsersAndJournals(users, messages);