-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathseed.ts
227 lines (207 loc) · 7.25 KB
/
seed.ts
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
/**
* This script is used to seed the database with example data.
* WARNING: This script will reset the database before seeding.
* To use it, run `npx prisma db seed`
*
* You can then log in to some test accounts with the following emails
* (password is empty):
*
* [email protected] (sample hacker account)
* [email protected] (sample admin account)
*/
import { lucia } from 'lucia';
import 'lucia/polyfill/node';
import { MY_TIMEZONE, events, questions, faq } from './data.ts';
import { PrismaClient, Status, Prisma } from '@prisma/client';
import { prisma as prismaAdapter } from '@lucia-auth/adapter-prisma';
const prisma = new PrismaClient();
const auth = lucia({
adapter: prismaAdapter(prisma, {
user: 'authUser',
session: 'authSession',
key: 'authKey',
}),
env: 'DEV',
});
async function register(email: string, password: string): Promise<string> {
const user = await auth.createUser({
attributes: {
email: email,
roles: ['HACKER'],
status: 'CREATED',
verifiedEmail: true,
},
key: {
providerId: 'email',
providerUserId: email,
password: password,
},
});
return user.userId;
}
async function main() {
// Reset database
await prisma.announcement.deleteMany();
await prisma.decision.deleteMany();
await prisma.event.deleteMany();
await prisma.question.deleteMany();
await prisma.settings.deleteMany();
await prisma.user.deleteMany();
await prisma.authUser.deleteMany();
await prisma.authSession.deleteMany();
await prisma.authKey.deleteMany();
await prisma.infoBox.deleteMany();
// Create example announcement
await prisma.announcement.create({
data: {
body: 'We are now accepting applications for HackTX! The deadline is Friday, September 17th at 11:59 PM.',
published: new Date('2021-09-01T00:00:00'),
},
});
// Create example questions
// Remove the generate function (used when creating dummy users) before creating questions
// eslint-disable-next-line @typescript-eslint/no-unused-vars
await prisma.question.createMany({ data: questions.map(({ generate, ...keep }) => keep) });
const createdQuestions = await prisma.question.findMany();
// Create example events
await prisma.event.createMany({ data: events });
// Create default settings
await prisma.settings.create({ data: { timezone: MY_TIMEZONE } });
// Create example FAQ and Challenges
await prisma.infoBox.createMany({ data: faq });
// Generate fake users and status changes
// NOTE: By "fake", I mean the fact that there is no way to sign in
// as these users, since they don't have passwords, and hashing
// passwords is slow by design
const authUsers: Prisma.AuthUserCreateManyInput[] = [];
const users: Prisma.UserCreateManyInput[] = [];
const statusChanges: Prisma.StatusChangeCreateManyInput[] = [];
const maxSecondsBetweenStatusChanges = 60 * 60 * 24 * 7; // 1 week
const startingTime = new Date();
// We must allow enough time for 4 status changes
// (CREATED -> PPLIED -> ACCEPTED/REJECTED/WAITLISTED -> CONFIRMED/DECLINED)
startingTime.setSeconds(-maxSecondsBetweenStatusChanges * 5);
for (let i = 0; i < 1000; i++) {
const id = `hacker${String(i).padStart(3, '0')}@yopmail.com`;
const statusFlow = generateStatusFlow(id, startingTime, maxSecondsBetweenStatusChanges);
// Generate a completed application for each hacker
const application = {};
for (const question of createdQuestions) {
// IMPORTANT: This assumes that the questions variable is ordered by the order field!!
application[question.id] = questions[question.order].generate();
}
authUsers.push({
id: id,
email: id,
roles: ['HACKER'],
status: statusFlow[statusFlow.length - 1].newStatus,
});
users.push({ authUserId: id, application });
statusChanges.push(...statusFlow);
}
await prisma.authUser.createMany({ data: authUsers });
// HACK: This is to avoid unique key constraint errors due to the
// trigger that automatically creates a User when an AuthUser is
// created and the fact that Prisma doesn't support bulk updates yet
await prisma.user.deleteMany();
await prisma.user.createMany({ data: users });
// HACK: Delete status changes autogenerated by trigger since we are
// generating them manually (must do this AFTER creating fake users
// but BEFORE creating test hacker and admin)
await prisma.statusChange.deleteMany();
await prisma.statusChange.createMany({ data: statusChanges });
// Create test hacker and admin
// (must do this AFTER calling prisma.user.deleteMany() and prisma.statusChange.deleteMany())
await register('[email protected]', '');
const adminId = await register('[email protected]', '');
await prisma.authUser.update({ where: { id: adminId }, data: { roles: ['ADMIN'] } });
// Generate decisions for fake users
const decisions: Prisma.DecisionCreateManyInput[] = [];
const hackers = await prisma.authUser.findMany({
where: {
roles: { has: 'HACKER' },
status: { in: ['APPLIED', 'WAITLISTED'] },
},
orderBy: { id: 'asc' },
});
for (const hacker of hackers) {
// Don't decide on every user probability
if (random() >= 0.5) {
decisions.push({
userId: hacker.id,
status: randomElement(['ACCEPTED', 'REJECTED', 'WAITLISTED']),
});
}
}
await prisma.decision.createMany({ data: decisions });
}
/**
* Generates a random status flow for a hacker.
*
* @param id The hacker's id
* @param startingTime The earliest time the first status change can
* occur (ideally somewhat close to the current date so the graph
* looks realistic)
* @param maxSecondsBetweenStatusChanges The maximum number of seconds
* that can pass between each status change
*/
function generateStatusFlow(
id: string,
startingTime: Date,
maxSecondsBetweenStatusChanges: number
): Prisma.StatusChangeCreateManyInput[] {
const statusChanges: Prisma.StatusChangeCreateManyInput[] = [];
const attritionRate = 0.1; // 10% of hackers drop out at each stage
let lastTimestamp = startingTime;
for (const status of ['CREATED', 'APPLIED'] as Status[]) {
lastTimestamp = new Date(
lastTimestamp.getTime() + 1000 * maxSecondsBetweenStatusChanges * random()
);
statusChanges.push({ newStatus: status, timestamp: lastTimestamp, userId: id });
if (random() < attritionRate) {
return statusChanges;
}
}
const afterStatusAppliedRandom = randomElement([
'ACCEPTED',
'REJECTED',
'WAITLISTED',
] as Status[]);
lastTimestamp = new Date(
lastTimestamp.getTime() + 1000 * maxSecondsBetweenStatusChanges * random()
);
statusChanges.push({
newStatus: afterStatusAppliedRandom,
timestamp: lastTimestamp,
userId: id,
});
if (random() >= attritionRate && afterStatusAppliedRandom === 'ACCEPTED') {
lastTimestamp = new Date(
lastTimestamp.getTime() + 1000 * maxSecondsBetweenStatusChanges * random()
);
statusChanges.push({
newStatus: randomElement(['CONFIRMED', 'DECLINED'] as Status[]),
timestamp: lastTimestamp,
userId: id,
});
}
return statusChanges;
}
// Quick and dirty seedable random number generator taken from https://stackoverflow.com/a/19303725/16458492
let seed = 0;
export function random() {
const x = Math.sin(seed++) * 10000;
return x - Math.floor(x);
}
export function randomElement<T>(array: T[]): T {
return array[Math.floor(random() * array.length)];
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});