Skip to content

Commit c8c4545

Browse files
committed
Updated seed file to also add all freshers (incl. resits)
1 parent 2f44b9a commit c8c4545

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

hono/seed.ts

+79
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,86 @@
11
import { db } from './db';
2+
import { students } from './family/schema';
23
import { meta } from './admin/schema';
34

5+
const now = new Date();
6+
const academicYear =
7+
now.getFullYear() - Math.floor(now.getFullYear() / 100) * 100;
8+
const baseUrl = `https://abc-api.doc.ic.ac.uk/${academicYear}${academicYear + 1}`;
9+
10+
// This is purely to ensure that you have the env file set up properly,
11+
// otherwise we'll make unnecessary calls to the ABC api.
12+
await db.select().from(meta);
13+
14+
// Add all first years, so that we don't make a resit student into a parent.
15+
// (any student redoing a first year module is a fresher)
16+
console.log(
17+
`The following information is required to authorize you for the ABC API. You have the source code, so you can see it is only ever stored in memory.`
18+
);
19+
const shortcode = prompt('Shortcode: ');
20+
const password = prompt('Password: ');
21+
22+
const authToken = btoa(`${shortcode}:${password}`);
23+
const authHeader = `Basic ${authToken}`;
24+
25+
console.log('--- Getting & filtering modules... ---');
26+
// Get modules that C1 & J1 can take
27+
const modulesReq = await fetch(
28+
`${baseUrl}/modules?term=1&cohort=c1&cohort=j1`,
29+
{
30+
headers: {
31+
Authorization: authHeader
32+
}
33+
}
34+
);
35+
const modulesRes = (await modulesReq.json()) as any[];
36+
37+
// Filter to be the modules that ONLY first years take.
38+
const modules = modulesRes.filter(
39+
m =>
40+
(m.applicable_cohorts.length == 1 &&
41+
(m.applicable_cohorts[0] == 'j1' || m.applicable_cohorts[0] == 'c1')) ||
42+
(m.applicable_cohorts.length == 2 &&
43+
(m.applicable_cohorts.includes('j1') ||
44+
m.applicable_cohorts.includes('c1')))
45+
);
46+
console.log('--- Modules got! ---');
47+
48+
console.log('--- Getting all freshers... ---');
49+
const studentsSet = new Set() as Set<string>;
50+
for (const module of modules) {
51+
const studentsReq = await fetch(
52+
`${baseUrl}/modules/${module.code}/enrolled`,
53+
{
54+
headers: {
55+
Authorization: authHeader
56+
}
57+
}
58+
);
59+
const moduleStudents = await studentsReq.json();
60+
for (const student of moduleStudents) studentsSet.add(student.login);
61+
}
62+
console.log('--- Freshers got! ---');
63+
64+
console.log('--- Adding freshers to the db... ---');
65+
for (const student of studentsSet) {
66+
try {
67+
await db.insert(students).values({
68+
shortcode: student,
69+
role: 'fresher',
70+
completedSurvey: false
71+
});
72+
} catch {
73+
console.log(
74+
`${student} has already signed in & thus been created an account.`
75+
);
76+
}
77+
}
78+
console.log('--- Freshers added! ---');
79+
80+
console.log('--- Adding state to the db...');
81+
// Add the required meta values for state
482
await db.insert(meta).values({
583
id: 1,
684
state: 'parents_open'
785
});
86+
console.log('--- State added! ---');

0 commit comments

Comments
 (0)