-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #510 from Klimatbyran/staging
Update garbo to production
- Loading branch information
Showing
7 changed files
with
196 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ async function seedUsers() { | |
}, | ||
{ | ||
email: '[email protected]', | ||
name: 'Alexandra Palmquist', | ||
name: 'Alex (Klimatkollen)', | ||
}, | ||
], | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ const TOKENS = tokens.reduce<{ garbo: string; alex: string }>( | |
{} as any | ||
) | ||
|
||
const USERS = { | ||
export const USERS = { | ||
garbo: { | ||
email: '[email protected]', | ||
token: TOKENS.garbo, | ||
|
@@ -289,7 +289,7 @@ function getReportingPeriods( | |
/** | ||
* Get an array of numbers from start to end, with inclusive boundaries. | ||
*/ | ||
function range(start: number, end: number) { | ||
export function range(start: number, end: number) { | ||
return Array.from({ length: end - start + 1 }, (_, i) => start + i) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { isMainModule } from './utils' | ||
import 'dotenv/config' | ||
import ExcelJS from 'exceljs' | ||
import { resolve } from 'path' | ||
import apiConfig from '../src/config/api' | ||
import { postJSON, range, USERS } from './import-spreadsheet-companies' | ||
|
||
const workbook = new ExcelJS.Workbook() | ||
await workbook.xlsx.readFile(resolve('src/data/Company GHG data.xlsx')) | ||
|
||
function getSheetHeaders({ | ||
sheet, | ||
row, | ||
}: { | ||
sheet: ExcelJS.Worksheet | ||
row: number | ||
}) { | ||
return Object.values(sheet.getRow(row).values!) | ||
} | ||
|
||
const { baseURL } = apiConfig | ||
|
||
const HIDDEN_FROM_API = new Set([ | ||
'Q22629259', | ||
'Q37562781', | ||
'Q489097', | ||
'Q10432209', | ||
'Q5168854', | ||
'Q115167497', | ||
'Q549624', | ||
'Q34', | ||
'Q8301325', | ||
'Q112055015', | ||
'Q97858523', | ||
'Q2438127', | ||
'Q117352880', | ||
'Q115167497', | ||
]) | ||
|
||
async function updateReportURLs(years: number[]) { | ||
const sheet = workbook.getWorksheet('import')! | ||
const headers = getSheetHeaders({ sheet, row: 2 }) | ||
|
||
const baseCompanies = sheet | ||
.getSheetValues() | ||
.slice(3) | ||
.reduce<{ wikidataId: string; name: string }[]>((companies, row) => { | ||
if (!row) return companies | ||
const columns = headers.reduce((acc, header, i) => { | ||
acc[header!.toString()] = row[i + 1]?.result || row[i + 1] | ||
return acc | ||
}, {} as any) | ||
if (!HIDDEN_FROM_API.has(columns['Wiki ID'])) { | ||
companies.push({ | ||
wikidataId: columns['Wiki ID'], | ||
name: columns['Company'], | ||
}) | ||
} | ||
return companies | ||
}, []) | ||
|
||
for (const year of years) { | ||
const sheet = workbook.getWorksheet(year.toString()) | ||
if (!sheet) continue | ||
const headers = getSheetHeaders({ sheet, row: 2 }) | ||
|
||
sheet | ||
.getSheetValues() | ||
.slice(3) // Skip header | ||
.forEach((row) => { | ||
if (!row) return | ||
|
||
const columns = headers.reduce((acc, header, i) => { | ||
const index = i + 1 | ||
acc[header!.toString()] = | ||
row[index]?.result || row[index]?.hyperlink || row[index] | ||
return acc | ||
}, {}) | ||
|
||
const company = baseCompanies.find( | ||
(c) => | ||
c.name.trim().toLowerCase() === | ||
columns['Company']?.trim().toLowerCase() | ||
) | ||
|
||
if (company && columns[`URL ${year}`]) { | ||
const body = JSON.stringify({ | ||
year: year.toString(), | ||
reportURL: columns[`URL ${year}`], | ||
}) | ||
|
||
try { | ||
return fetch( | ||
`${baseURL}/companies/${company.wikidataId}/report-url`, | ||
{ | ||
body, | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${USERS['garbo'].token}`, | ||
}, | ||
} | ||
) | ||
} catch (error) { | ||
console.error('Failed to fetch:', error) | ||
throw error | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
async function main() { | ||
const years = range(2015, 2023).reverse() | ||
await updateReportURLs(years) | ||
console.log('✅ Report URLs updated.') | ||
} | ||
|
||
if (isMainModule(import.meta.url)) { | ||
await main() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters