diff --git a/.gitignore b/.gitignore index fcfc81a..119bbf7 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/backend/package-lock.json b/backend/package-lock.json index bec14e3..8640efa 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -20,6 +20,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", @@ -3287,8 +3288,7 @@ "node_modules/argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", - "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", - "optional": true + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" }, "node_modules/array-flatten": { "version": "1.1.1", @@ -4785,6 +4785,17 @@ "url": "https://github.com/sponsors/panva" } }, + "node_modules/js-yaml": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", + "dependencies": { + "argparse": "^2.0.1" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, "node_modules/js2xmlparser": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/js2xmlparser/-/js2xmlparser-4.0.2.tgz", @@ -9264,8 +9275,7 @@ "argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", - "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", - "optional": true + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" }, "array-flatten": { "version": "1.1.1", @@ -10387,6 +10397,14 @@ "@panva/asn1.js": "^1.0.0" } }, + "js-yaml": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", + "requires": { + "argparse": "^2.0.1" + } + }, "js2xmlparser": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/js2xmlparser/-/js2xmlparser-4.0.2.tgz", diff --git a/backend/package.json b/backend/package.json index 4040ade..ce33fe7 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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", diff --git a/backend/server.js b/backend/server.js index 1949725..fe56817 100644 --- a/backend/server.js +++ b/backend/server.js @@ -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() -}) diff --git a/backend/service/googlegroup.service.js b/backend/service/googlegroup.service.js index 2a4ce93..b386d9e 100644 --- a/backend/service/googlegroup.service.js +++ b/backend/service/googlegroup.service.js @@ -33,7 +33,7 @@ const upsertUsers = async (newUsers) => { update: { $set: { watiam: user.watiam, - title: user.title, + title: user.membership, }, }, upsert: true, @@ -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) diff --git a/backend/utils/getSheetData.js b/backend/utils/getSheetData.js index dabe3a6..c18d50a 100644 --- a/backend/utils/getSheetData.js +++ b/backend/utils/getSheetData.js @@ -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)