-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-teams-spring-2019.js
96 lines (88 loc) · 3.41 KB
/
update-teams-spring-2019.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
'use strict';
if (!process.env.DATABASE_PASSWORD) {
console.log("Need to set DATABASE_PASSWORD environment variable");
process.exit(1);
}
const management = ['maya-morsli', 'jocilyn-estes', 'aaron-marcus-willers', 'haneen-fathy', 'mueez-hasan', 'eyza-irene-hamdani-hussain', 'navya-suri', 'emil-goldsmith-olesen', 'auguste-nomeikaite', 'sameera-singh'];
const editorial = ['taj-chapman', 'mari-velasquez-soler', 'davit-jintcharadze', 'anna-pustovoit', 'andrea-arletti', 'abhyudaya-tyagi', 'aayusha-shrestha', 'ari-hawkins', 'khaled-alhosani', 'kyle-adams', 'ming-ee-tham', 'katharina-klaunig', 'gayoung-lee'];
const multimedia = ['tom-abi-samra', 'mahgul-farooqui', 'emily-broad', 'katarina-holtzapple', 'ta-hyun-lee', 'liene-pekuse', 'darya-sukhova', 'vivi-zhu'];
const writers = ['ian-hoyt', 'elyazyeh-al-falacy', 'liam-meier', 'laila-hashem', 'andrijana-pejchinovska', 'chhete-sherpa', 'sarah-afaneh', 'chisom-ezeifemeelu', 'rashtra-bandari', 'malak-abdel-ghaffar', 'steffen-holter', 'sara-monsalve'];
const web = ['navya-suri', 'abdullah-zameek', 'jacinta-hu', 'nurpeiis-baimukan', 'junior-garcia', 'simran-parwani', 'jung-soo-ha', 'rick-kim', 'mariam-el-sahhar', 'manesha-ramesh'];
const knex = require('knex')({
client: 'mysql',
connection: {
"host": "127.0.0.1",
"user": "root",
"password": process.env.DATABASE_PASSWORD,
"database": "the_gazelle",
"charset": "latin1"
},
});
knex('semesters').insert([
{
'name': 'Spring 2019',
'date': '2019-02-01',
},
])
.then(() => {
console.log("Semesters inserted successfully");
populateTeam(management, 'management', 'Spring 2019', 0);
populateTeam(editorial, 'editorial', 'Spring 2019', 1);
populateTeam(multimedia, 'multimedia', 'Spring 2019', 2);
populateTeam(writers, 'writers', 'Spring 2019', 3);
populateTeam(web, 'web', 'Spring 2019', 4);
addMarcelo();
});
function addMarcelo() {
knex('teams_staff').insert([{team_id: 6, staff_id: 647, team_order: 4, staff_order: 9, semester_id: 5}]).then(() => console.log(`Inserted Marcelo`));
}
function populateTeam(slugs, teamSlug, semesterName, teamOrder) {
let semesterId = null;
let teamId = null;
knex.select('id').from('semesters').where('name', semesterName)
.then(semesterRows => {
if (semesterRows.length != 1) {
throw new Error("Can't find the semester");
}
return semesterRows[0].id;
})
.then(fetchedSemesterId => {
semesterId = fetchedSemesterId;
return knex.select('id').from('teams').where('slug', teamSlug);
})
.then(teamRows => {
if (teamRows.length != 1) {
throw new Error("Can't find the team");
}
return teamRows[0].id;
})
.then(fetchedTeamId => {
teamId = fetchedTeamId;
// Time to actually insert
const insert = [];
const num = slugs.length;
let cur = 0;
slugs.forEach((slug, index) => {
knex.select('id').from('staff').where('slug', slug).then((rows) => {
if (rows.length != 1) {
console.log(`row length equals ${rows.length} at ${teamSlug} slug: ${slug}`);
}
insert.push({
team_id: teamId,
staff_id: rows[0].id,
team_order: teamOrder,
staff_order: index,
semester_id: semesterId,
});
cur++;
if (cur >= num) {
knex('teams_staff').insert(insert).then(() => console.log(`Inserted ${teamSlug}`));
}
});
});
})
.catch(e => {
console.log("ERROR:", e);
process.exit(1);
});
}