-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
204 lines (200 loc) · 4.84 KB
/
gatsby-node.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
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
require("dotenv").config();
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type ConferenceAwardsData__DelegateAwards {
type: String!
awards: [String!]
}
type ConferenceAwardsData implements Node {
name: String!
month: String!
year: Int!
time: Date! @dateformat
delegationAward: String
delegateAwards: [ConferenceAwardsData__DelegateAwards]
imageURL: String
}
`;
createTypes(typeDefs);
};
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest,
}) => {
const { createNode } = actions;
let awardsData = [];
try {
const admin = require("firebase-admin");
if (!process.env.FB_SERVICE_ACCOUNT) {
throw {
code: "NO_FIREBASE_KEY",
message:
"Please set the firebase service account key as an environment variable. The easiest way to do this is by creating a .env file and populating it with the required secrets, which can be found here: https://drive.google.com/file/d/1xMUy8PBSROb6Mc7mUoMDs_v6_4JDjS4p/view?usp=sharing (part of the MUN Officers folder).",
};
}
const FB_SERVICE_ACCOUNT = JSON.parse(process.env.FB_SERVICE_ACCOUNT);
admin.initializeApp({
credential: admin.credential.cert(FB_SERVICE_ACCOUNT),
databaseURL: "https://montavistamodelun.firebaseio.com",
});
const snapshot = await admin.firestore().collection("awards").get();
snapshot.forEach((doc) => {
const key = doc.id;
const { time, ...rest } = doc.data();
awardsData.push({
serverId: key,
...rest,
time: time.toDate(),
});
});
} catch (e) {
console.log(e);
console.warn(
"Unable to fetch firebase data for awards due to error code " +
e.code +
". Using dummy data instead."
);
const data = [
{
name: "BMUN 2020",
time: "March 2020",
delegationAward: null,
delegateAwards: [
{
type: "Honorable",
awards: [
"Arthur Ji (Financial Security Board)",
"Howard Peng (Joint Crisis Committee -- New)",
],
},
],
},
{
name: "SCVMUN 2020",
time: "January 2020",
delegationAward: null,
delegateAwards: [
{
type: "Best Delegate",
awards: [
"Eric Lee & Madeline Choi (UNESCO)",
"Arthur Ji & Pranav Kumar (World Bank)",
"Medhansh Kashyap & Howard Peng (UNSC)",
],
},
{
type: "Honorable",
awards: [
"Dylan Yang & Sylvia Li (SOCHUM)",
"Juhi Shyamsukha & Sophia Chen (WHO)",
"Christopher Lee & Anshul Dash (DISEC)",
"Larry Wu & Thomas Yu (IAEA)",
"Michael Ding & Adi Budaraju (World Bank)",
],
},
{
type: "Outstanding",
awards: [
"Soumil Gupta & Sriya Vennam (UNEP)",
"Jiani Tian & Alekhya Natarajan (WHO)",
"Nathan Wang & Elizabeth Lee (DISEC)",
],
},
],
},
{
name: "SMUNC 2019",
time: "November 2019",
delegationAward: null,
delegateAwards: [
{
type: "Best Delegate",
awards: ["Parth Asawa (JCC India)"],
},
{
type: "Outstanding",
awards: [
"Nathan Wang & Elizabeth Lee (Senate)",
"Eugene Yoon and Conner Yin (SOCHUM)",
],
},
{
type: "Honorable",
awards: [
"Matthew Philip & Medhansh Kashyap (ECOSOC)",
"Advait Buduraju & Michael Ding (SOCHUM)",
"Aman Desai (Cyber)",
],
},
{
type: "Verbal",
awards: [
"Chris Lee and Larry Wu (ECOSOC)",
"Howard Peng (UNEP)",
],
},
],
},
{
name: "GMUNC VI",
time: "October 2019",
delegationAward: null,
delegateAwards: [
{
type: "Best Delegate",
awards: [
"Parth Asawa (CIA)",
"Iman Malik & Aman Desai (WHO)",
"Matthew Philip & Medhansh Kashyap (Arctic)",
"Eric Lee (SOCHUM)",
],
},
{ type: "Outstanding", awards: ["Nelson Mu (BHOC)"] },
{
type: "Honorable",
awards: [
"Sean Chen & David Dang (UNSC)",
"Sivani Gangaram (SOCHUM)",
"Sophia Chen (SOCHUM)",
],
},
{
type: "Verbal",
awards: [
"Zubayir Kazi (UNHCR)",
"Maggie Yang (SOCHUM)",
],
},
{ type: "Research", awards: ["Dylan Yang (BHOC)"] },
],
},
];
data.forEach((doc, i) => {
const key = "dummy-data-" + i;
const { time, ...rest } = doc;
awardsData.push({
serverId: key,
...rest,
month: time.split(" ")[0],
year: parseInt(time.split(" ")[1]),
time: new Date(time),
});
});
}
awardsData.forEach((data) => {
const nodeMeta = {
id: createNodeId(`firestore-awards-${data.serverId}`),
parent: null,
children: [],
internal: {
type: `ConferenceAwardsData`,
mediaType: `application/json`,
contentDigest: createContentDigest(awardsData),
},
};
const node = Object.assign({}, data, nodeMeta);
createNode(node);
});
};