generated from uclaacm/committee-website-template-cms
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetOfficers.ts
87 lines (85 loc) · 2.51 KB
/
getOfficers.ts
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
import * as dotenv from 'dotenv';
import { google } from 'googleapis';
// .env config
dotenv.config({ path: '.env.local' });
const SPREADSHEET_ID = process.env.DIRECTORY_SPREADSHEET_ID;
const SERVICE_ACCOUNT = process.env.SERVICE_ACCOUNT ?? '{}';
export default async function getOfficerData(
committeeName: string,
): Promise<object[]> {
const sheets = google.sheets({ version: 'v4' });
// Get JWT Token to access sheet
const service_account = JSON.parse(SERVICE_ACCOUNT);
const jwtClient = new google.auth.JWT(
service_account.client_email,
'',
service_account.private_key,
['https://www.googleapis.com/auth/spreadsheets'],
);
jwtClient.authorize(function (err) {
if (err) {
throw err;
}
});
// Get officer data from google spreadsheets
const res = await sheets.spreadsheets.values.get({
auth: jwtClient,
spreadsheetId: SPREADSHEET_ID,
range: 'Officers',
});
const rows = res?.data.values;
if (!rows || rows.length == 0) {
throw new Error('Error: no data found');
}
// Map committee names in the spreadsheet to more concise names
// Ignore board as it's not a committee
const committees = new Map<string, string>([
['AI', 'ai'],
['Cyber', 'cyber'],
['Design', 'design'],
['Studio', 'studio'],
['Hack', 'hack'],
['ICPC', 'icpc'],
['Teach LA', 'teachla'],
['W', 'w'],
]);
// // Store officer data
const officers: object[] = []; // list of officers in desired committee
let currCommittee = '';
let officerID = 1;
rows.forEach((row) => {
if (row.length == 0)
// empty row
return;
if (committees.has(row[0])) {
// row with only committee name
const committee = row[0];
currCommittee = committees.get(committee) ?? ''; // empty string means ACM Board
return;
}
if (currCommittee != committeeName)
// skip all rows other than desired committee
return;
// push row data into officers list
let image = row[10];
if (!image) {
image = '/profile.png';
} else if (image.includes('drive.google.com')) {
const fileID = image.match(/\/file\/d\/(.+?)\//)[1];
image = `https://drive.google.com/uc?export=download&id=${fileID}`;
}
// create officer
const officer = {
id: officerID,
position: row[0] ?? null,
name: row[1] ?? null,
pronouns: row[2] ?? null,
email: row[3] ?? null,
github: row[9] ?? null,
imageURL: image ?? null,
};
officers.push(officer);
officerID++;
});
return officers;
}