Skip to content

Commit

Permalink
pull user roles from infra config
Browse files Browse the repository at this point in the history
  • Loading branch information
victorzheng02 committed Jan 2, 2024
1 parent aa02a6d commit b944a19
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ node_modules
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# will be added upon deployment as it is located on separate repo. Will store user roles.
# see https://github.com/WATonomous/infra-config/tree/master/directory/users/data
/backend/data
26 changes: 22 additions & 4 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"firebase-admin": "^11.4.1",
"firebase-functions": "^4.1.1",
"google-spreadsheet": "^3.3.0",
"js-yaml": "^4.1.0",
"lorem-ipsum": "^2.0.8",
"mongoose": "^6.5.5",
"mongoose-sequence": "^5.3.1",
Expand Down
4 changes: 2 additions & 2 deletions backend/service/googlegroup.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const upsertUsers = async (newUsers) => {
update: {
$set: {
watiam: user.watiam,
title: user.title,
title: user.membership,
},
},
upsert: true,
Expand All @@ -56,7 +56,7 @@ const updateGoogleGroups = async (body) => {
})

const currentGroups = await getAllGoogleGroups()
// currentEmails comes from our database, which needs to be synced from our sheet
// currentEmails comes from our mongo database, which needs to be synced with the users directory in infra-config
const currentEmails = currentGroups.map((group) => group.email)
// new emails comes directly from our sheet, which is our source of truth
const newEmails = newUserDetails.map((userDetail) => userDetail.email)
Expand Down
49 changes: 17 additions & 32 deletions backend/utils/getSheetData.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
const Gsheet = require('google-spreadsheet')
const fs = require('fs/promises')
const yaml = require('js-yaml')
require('dotenv').config()
const { updateGoogleGroups } = require('../service/googlegroup.service')

// Config variables
const GOOGLE_SHEET_ID = process.env.GOOGLE_SHEET_ID
const SHEET_TAB_ID = process.env.SHEET_TAB_ID
const SERVICE_ACCOUNT_EMAIL = process.env.SERVICE_ACCOUNT_EMAIL
const SERVICE_ACCOUNT_PRIVATE_KEY = process.env.SERVICE_ACCOUNT_PRIVATE_KEY
const doc = new Gsheet.GoogleSpreadsheet(GOOGLE_SHEET_ID)

const SCHOOL_EMAIL_HEADER = 'Email'
const TITLE_HEADER = 'Title'

const readSpreadsheet = async () => {
try {
await doc.useServiceAccountAuth({
client_email: SERVICE_ACCOUNT_EMAIL,
private_key: SERVICE_ACCOUNT_PRIVATE_KEY,
})
// loads document properties and worksheets
await doc.loadInfo()

const sheet = doc.sheetsById[SHEET_TAB_ID]
const rows = await sheet.getRows()
const { updateGoogleGroups } = require('../service/googlegroup.service')

return rows.map((row) => ({
email: row[SCHOOL_EMAIL_HEADER],
title: row[TITLE_HEADER],
}))
} catch (e) {
console.error('Error: ', e)
}
const readUserGroups = async () => {
const yamlFiles = await fs.readdir('./data')
const userGroups = []
const promises = yamlFiles.map(async (yamlFile) => {
const doc = yaml.load(await fs.readFile(`./data/${yamlFile}`, 'utf8'))
if (doc?.finance_system?.enabled) {
userGroups.push(doc.finance_system)
}
})
await Promise.all(promises)
return userGroups
}

const updateGroup = async () => {
const userRows = await readSpreadsheet()
// const userRows = await readSpreadsheet()
const users = await readUserGroups()
try {
// only write if there was an associated title with the user. there should be one for everyone.
const cleanedUserRows = userRows.filter((userRow) => userRow.title)
await updateGoogleGroups(cleanedUserRows)
await updateGoogleGroups(users)
console.log('Updated google groups')
} catch (err) {
console.error(err)
Expand Down

0 comments on commit b944a19

Please sign in to comment.