-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathseeder.rs
More file actions
403 lines (363 loc) · 13.1 KB
/
seeder.rs
File metadata and controls
403 lines (363 loc) · 13.1 KB
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
use server::models::app::init_app_state;
use server::models::app::AppState;
use server::models::offer::Offer;
use server::models::rating::{Rating, NewRating};
use server::models::user::{User, UserRole};
use server::models::organisation::{Organisation};
use server::models::role::{Role, RoleUpdate};
use server::models::question::*;
use server::models::answer::*;
use server::models::application::{Application, NewApplication, ApplicationRole};
use chrono::{DateTime, Utc};
/// Struct which hold AppState that contains database connection
pub struct Seeder {
pub app_state: AppState
}
pub async fn init() -> Seeder {
let seeder = Seeder {
app_state: init_app_state().await
};
seeder
}
pub async fn seed_database(dev_email: String, mut seeder: Seeder) {
let mut tx = seeder.app_state.db.begin().await.expect("Error beginning DB transaction");
// Super User
let users = vec![
User {
id: 1,
email: dev_email,
zid: Some("z5555558".to_string()),
name: "Chaos Developer".to_string(),
pronouns: None,
gender: None,
degree_name: Some("Bachelor of Chaos Development (Honours)".to_string()),
degree_starting_year: Some(2024),
role: UserRole::SuperUser,
},
User {
id: 2,
email: "example.admin@chaos.devsoc.app".to_string(),
zid: Some("z5555556".to_string()),
name: "Edmund Blackadder".to_string(),
pronouns: Some("De/Da".to_string()),
gender: Some("Kuma".to_string()),
degree_name: Some("Bachelor of Engineering (Honours) (Mining)".to_string()),
degree_starting_year: Some(1914),
role: UserRole::User,
},
User {
id: 3,
email: "example.user@chaos.devsoc.app".to_string(),
zid: Some("z5555557".to_string()),
name: "John Bull".to_string(),
pronouns: None,
gender: None,
degree_name: Some("Bachelor of Social Work (Honours)".to_string()),
degree_starting_year: Some(2024),
role: UserRole::User,
},
User {
id: 4,
email: "example.superuser@chaos.devsoc.app".to_string(),
zid: Some("z5555555".to_string()),
name: "Francis Urquhart".to_string(),
pronouns: Some("Ze/Za".to_string()),
gender: Some("Otter".to_string()),
degree_name: Some("Bachelor of Arts".to_string()),
degree_starting_year: Some(1900),
role: UserRole::SuperUser,
},
];
for user in users {
User::create_user(user, &mut tx).await.expect("Failed seeding Root User");
}
let org_id = Organisation::create(
1, // User number 1, i.e. Developer
"devsoc".to_string(),
"UNSW DevSoc".to_string(),
"contact@devsoc.app".to_string(),
Some("https://devsoc.app".to_string()),
&mut seeder.app_state.snowflake_generator,
&mut tx).await.expect("Failed seeding Organisation");
Organisation::update_admins(org_id,
vec![2],
&mut tx).await.expect("Failed updating Organisation Admin");
let campaign_id = Organisation::create_campaign(
org_id,
"subcommittee-recruitment-2025".to_string(),
"Subcommittee Recruitment 2025".to_string(),
Some("This Campaign will MAKE EVERYONE EMPLOYEED".to_string()),
DateTime::<Utc>::from_naive_utc_and_offset(
chrono::NaiveDate::from_ymd_opt(2024, 1, 1).unwrap().and_hms_milli_opt(0, 0, 0, 0).unwrap(),
Utc,
)
,
DateTime::<Utc>::from_naive_utc_and_offset(
chrono::NaiveDate::from_ymd_opt(2040, 1, 1).unwrap().and_hms_milli_opt(0, 0, 0, 0).unwrap(),
Utc,
),
Some(DateTime::<Utc>::from_naive_utc_and_offset(
chrono::NaiveDate::from_ymd_opt(2041, 1, 1).unwrap().and_hms_milli_opt(0, 0, 0, 0).unwrap(),
Utc,
)),
Some(DateTime::<Utc>::from_naive_utc_and_offset(
chrono::NaiveDate::from_ymd_opt(2042, 1, 1).unwrap().and_hms_milli_opt(0, 0, 0, 0).unwrap(),
Utc,
)),
Some("in-person".to_string()),
Some(DateTime::<Utc>::from_naive_utc_and_offset(
chrono::NaiveDate::from_ymd_opt(2043, 1, 1).unwrap().and_hms_milli_opt(0, 0, 0, 0).unwrap(),
Utc,
)),
Some("Resume required".to_string()),
&mut tx,
&mut seeder.app_state.snowflake_generator,
)
.await.expect("Failed seeding Campaign");
let role_id_1 = Role::create(
campaign_id,
RoleUpdate {
name: "Chaos".to_string(),
description: Some("The DevSoc Rust project".to_string()),
min_available: 1,
max_available: 10,
finalised: false,
},
&mut tx,
&mut seeder.app_state.snowflake_generator,
)
.await.expect("Failed seeding Role 1");
let role_id_2 = Role::create(
campaign_id,
RoleUpdate {
name: "Notangles".to_string(),
description: Some("We hate tangles.".to_string()),
min_available: 1,
max_available: 20,
finalised: true,
},
&mut tx,
&mut seeder.app_state.snowflake_generator,
)
.await.expect("Failed seeding Role 2");
let question_id_1 = Question::create(
campaign_id,
"What year are you in?".to_string(),
None,
// Some("How many years of industry experience do you have?".to_string()),
true,
None,
true,
QuestionData::DropDown(
MultiOptionData {
options: vec![
MultiOptionQuestionOption {
id: 0,
text: "1".to_string(),
display_order: 1,
},
MultiOptionQuestionOption {
id: 0,
text: "2".to_string(),
display_order: 2,
},
MultiOptionQuestionOption {
id: 0,
text: "3".to_string(),
display_order: 3,
},
MultiOptionQuestionOption {
id: 0,
text: "4+".to_string(),
display_order: 4,
},
]
},
),
&mut seeder.app_state.snowflake_generator,
&mut tx,
)
.await.expect("Failed seeding Question 1");
let question_id_2 = Question::create(
campaign_id,
"Why do you love Rust?".to_string(),
Some("This is a special question just for Chaos applicants".to_string()),
false,
Some(vec![role_id_1]),
true,
QuestionData::ShortAnswer,
&mut seeder.app_state.snowflake_generator,
&mut tx,
)
.await.expect("Failed seeding Question 1");
let question_id_3 = Question::create(
campaign_id,
"What languages are you familiar with?".to_string(),
Some("This is a general question for all technical roles".to_string()),
false,
Some(vec![role_id_1, role_id_2]),
true,
QuestionData::MultiSelect(
MultiOptionData {
options: vec![
MultiOptionQuestionOption {
id: 0,
text: "Rust".to_string(),
display_order: 1,
},
MultiOptionQuestionOption {
id: 0,
text: "Python".to_string(),
display_order: 2,
},
MultiOptionQuestionOption {
id: 0,
text: "JavaScript".to_string(),
display_order: 3,
},
MultiOptionQuestionOption {
id: 0,
text: "Java".to_string(),
display_order: 4,
},
MultiOptionQuestionOption {
id: 0,
text: "C++".to_string(),
display_order: 5,
},
]
}
),
&mut seeder.app_state.snowflake_generator,
&mut tx,
)
.await.expect("Failed seeding Question 3");
let application_id_1 = Application::create(
campaign_id,
2,
NewApplication {
applied_roles: vec![
ApplicationRole {
id: 0,
application_id: 0,
campaign_role_id: role_id_1,
preference: 1,
}
]
},
&mut seeder.app_state.snowflake_generator,
&mut tx,
)
.await.expect("Failed seeding Application 1");
let application_id_2 = Application::create(
campaign_id,
3,
NewApplication {
applied_roles: vec![
ApplicationRole {
id: 0,
application_id: 0,
campaign_role_id: role_id_2,
preference: 1,
}
]
},
&mut seeder.app_state.snowflake_generator,
&mut tx,
)
.await.expect("Failed seeding Application 1");
let qtn_1_data = Question::get(question_id_1, &mut tx).await.expect("Failed getting Question 1");
let qtn_1_options =
match qtn_1_data.question_data {
QuestionData::DropDown(options) => options.options,
_ => panic!("Question 1 is not a DropDown question"),
};
Answer::create(
application_id_1,
question_id_1,
AnswerData::DropDown(qtn_1_options[0].id),
&mut seeder.app_state.snowflake_generator,
&mut tx,
)
.await.expect("Failed seeding Answer 1");
Answer::create(
application_id_2,
question_id_1,
AnswerData::DropDown(qtn_1_options[0].id),
&mut seeder.app_state.snowflake_generator,
&mut tx,
)
.await.expect("Failed seeding Answer 2");
Answer::create(
application_id_1,
question_id_2,
AnswerData::ShortAnswer("A Moand is a Monoid in the Category of Endofunctors, what else do you want?".to_string()),
&mut seeder.app_state.snowflake_generator,
&mut tx,
)
.await.expect("Failed seeding Answer 3");
let qtn_3_data = Question::get(question_id_3, &mut tx).await.expect("Failed getting Question 3");
let qtn_3_options =
match qtn_3_data.question_data {
QuestionData::MultiSelect(options) => options.options,
_ => panic!("Question 3 is not a MultiSelect question"),
};
Answer::create(
application_id_1,
question_id_3,
AnswerData::MultiSelect(vec![qtn_3_options[0].id, qtn_3_options[1].id]),
&mut seeder.app_state.snowflake_generator,
&mut tx,
)
.await.expect("Failed seeding Answer 3");
Rating::create(
NewRating { rating: 69, comment: Some("This guy does not know what they are talking about!".to_string()) },
application_id_1,
2,
&mut seeder.app_state.snowflake_generator,
&mut tx)
.await.expect("Failed seeding Rating 1");
Rating::create(
NewRating { rating: 100, comment: None },
application_id_2,
2,
&mut seeder.app_state.snowflake_generator,
&mut tx)
.await.expect("Failed seeding Rating 2");
Rating::create(
NewRating { rating: 100, comment: Some("My cousin's restaurant could use a janitor".to_string()) },
application_id_2,
1,
&mut seeder.app_state.snowflake_generator,
&mut tx)
.await.expect("Failed seeding Rating 3");
let template =
"
Hello {{name}},
Congratulations! You have been selected for the role of {{role}} at {{organisation_name}} for our {{campaign_name}}.
Please confirm your acceptance by {{expiry_date}}.
Best regards,
The {{organisation_name}} Team
".to_string();
let email_template_id = Organisation::create_email_template(
org_id,
"Offer".to_string(),
"[DevSoc] Position Offer".to_string(),
template,
&mut tx,
&mut seeder.app_state.snowflake_generator)
.await.expect("Failed seeding Email Template");
Offer::create(
campaign_id,
application_id_1,
email_template_id,
role_id_1,
DateTime::<Utc>::from_naive_utc_and_offset(
chrono::NaiveDate::from_ymd_opt(2024, 2, 1).unwrap().and_hms_milli_opt(0, 0, 0, 0).unwrap(),
Utc,
),
&mut tx,
&mut seeder.app_state.snowflake_generator)
.await.expect("Failed seeding Offer");
tx.commit().await.expect("Failed to commit DB transaction");
}