-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-teams-fall-2019.js
108 lines (101 loc) · 3.89 KB
/
update-teams-fall-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
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
if (!process.env.DATABASE_PASSWORD) {
console.log("Need to set DATABASE_PASSWORD environment variable");
process.exit(1);
}
const management = ['jakob-plaschke', 'paula-estrada','kaashif-hajee', 'andrea-arletti', 'sobha-gadi', 'dylan-palladino', 'laura-assanmal'];
const editorial = ['vatsa-singh', 'andrijana-pejchinovska', 'ari-hawkins', 'luis-rodriguez', 'aayusha-shrestha', 'aasna-sijapati', 'ming-ee-tham', 'caroline-sullivan', 'sarah-afaneh', 'mari-velasquez-soler', 'abhyudaya-tyagi', 'emily-broad', 'matthew-gubbins', 'michelle-shin', 'chris-shim'];
const multimedia = ['liene-pekuse', 'mahgul-farooqui', 'darya-sukhova', 'vivi-zhu', 'olivia-bray', 'emily-broad', 'kyle-adams', 'quim-paredes', 'isabel-rios', 'scarlet-ng', 'yuree-chang', 'grace-shieh', 'katie-ferreol', 'chloe-venn', 'ilya-amikov', 'injoo-kang', 'jack-baek', 'armaan-agrawal'];
const writers = ['nicholas-patas', 'mohammad-khan-durrani', 'jude-al-qubaisi', 'olivia-bray', 'salama-al-ghafli', 'joanna-orphanide', 'vid-milakovic', 'liam-jansen', 'toby-le', 'aravind-kumar', 'reema-el-kaiali', 'youssef-azzam', 'angad-johar', 'cindy-li', 'michael-leo', 'maryam-almansoori', 'haewon-yoon', 'huma-umar', 'abdulla-almarzooqi', 'ananya-krishnakumar', 'safeeya-alawadi', 'grace-bechdol', 'beniamin-strzelecki', 'priyanshu-mishra', 'srinika-rajanikanth'];
const web = ['navya-suri', 'jacinta-hu'];
const copy = ['aaron-marcus-willers', 'eyza-irene-hamdani-hussain', 'malik-muhammad-shehyar-hanif'];
const social = ['raeed-riaz', 'sameera-singh', 'mueez-hasan', 'songyue-xu'];
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('teams').insert([
{
'slug': 'copy-editing',
'name': 'copy editing',
},
{
'slug': 'social-media',
'name': 'social media',
},
])
.then(() => {
console.log("Teams inserted successfully");
});
knex('semesters').insert([
{
'name': 'Fall 2019',
'date': '2019-09-01',
},
])
.then(() => {
console.log("Semesters inserted successfully");
populateTeam(management, 'management', 'Fall 2019', 0);
populateTeam(editorial, 'editorial', 'Fall 2019', 1);
populateTeam(multimedia, 'multimedia', 'Fall 2019', 2);
populateTeam(writers, 'writers', 'Fall 2019', 3);
populateTeam(web, 'web', 'Fall 2019', 4);
populateTeam(copy, 'copy-editing', 'Fall 2019', 5);
populateTeam(social, 'social-media', 'Fall 2019', 6);
});
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);
});
}