Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull user roles from infra config #181

Merged
merged 3 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 0 additions & 6 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,3 @@ app.listen(port, async () => {
console.log(`Server is running on port: ${port}`)
await updateGroup()
})

const cron = require('node-cron')
cron.schedule('0 */15 * * * *', async () => {
//read from the member spreadsheet and update the database every 15 mins
await updateGroup()
})
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
55 changes: 22 additions & 33 deletions backend/utils/getSheetData.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
const Gsheet = require('google-spreadsheet')
const fs = require('fs')
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 () => {
// check if ./data exists
if (!fs.existsSync('./data')) {
console.log('No user role data to be found.')
return []
}
const yamlFiles = await fs.promises.readdir('./data')
const userGroups = []
const promises = yamlFiles.map(async (yamlFile) => {
const doc = yaml.load(
await fs.promises.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 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
Loading