-
Notifications
You must be signed in to change notification settings - Fork 15
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
Update sgyfetch function on firebase functions #115
Open
ImNotRog
wants to merge
5
commits into
main
Choose a base branch
from
updatefetch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb7bc99
Update sgyfetch function on firebase functions
ImNotRog 0725e3f
Use client types for better type safety
ky28059 a84ad50
Fix type error
ky28059 fc6b7a8
Remember to commit this file
ky28059 af50813
Fix a minor bug Kevin found
ImNotRog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import * as functions from 'firebase-functions'; | ||
import admin from './util/adminInit'; | ||
import {get} from './util/sgyOAuth'; | ||
import {SgyData, SgyPeriod, SgyPeriodData} from '@watt/client/src/contexts/UserDataContext'; | ||
|
||
const SEMESTER = 1; // We are in semester 1 | ||
const CURRENT_YEAR = 2022; // will work for 2022-2023 school year | ||
|
||
const YEAR_STR = `${CURRENT_YEAR}-${CURRENT_YEAR+1}`; | ||
|
||
const firestore = admin.firestore(); | ||
const periods = ['0', '1', '2', '3', '4', '5', '6', '7', '8', 'SELF']; | ||
const periods = ['0', '1', '2', '3', '4', '5', '6', '7', '8', 'S', 'P', 'H']; | ||
|
||
|
||
// Get a user's stored firestore sgy info | ||
|
@@ -17,21 +21,15 @@ async function getSgyInfo(uid: string) { | |
return {uid: creds.sgy.uid, key: creds.sgy.key, sec: creds.sgy.sec, classes: creds.classes}; | ||
} | ||
|
||
// Parses info about the given schoology info string. Ex. | ||
// `"Study Hall Kaneko (9813 43 FY)"` -> `{ pName: "Study Hall", pTeacher: "Kaneko", term: "FY" }` | ||
// `"2 Liberatore (9813 43 FY)"` -> `{ pName: "2", pTeacher: "Liberatore", term: "FY" }` | ||
function getClassInfo(info: string) { | ||
const words = info.split(' '); | ||
const pRegex = info.match(/\((.+)\)/); | ||
let term = null; | ||
if(pRegex) { | ||
const parenblock = pRegex[1]; // 2696 1 FY | ||
const items = parenblock.split(' '); // [2696, 1, FY] | ||
term = items[items.length - 1]; // FY | ||
} | ||
|
||
return { pName: words[0], pTeacher: words[1], term }; | ||
const match = info.match(/(.+?) (\w+) \(\d+ \d+ (\w+)\)/); | ||
if (!match) return { pName: '', pTeacher: '', term: null }; | ||
return { pName: match[1], pTeacher: match[2], term: match[3] }; | ||
} | ||
|
||
type SgyPeriodData = {n: string, c: string, l: string, o: string, s: string}; | ||
|
||
|
||
// sgyfetch-init | ||
// Populate the user's period customization with fetched class names from schoology, | ||
|
@@ -51,16 +49,19 @@ export const init = functions.https.onCall(async (data, context) => { | |
|
||
const classes: {[key: string]: SgyPeriodData} = {}; | ||
for (const p in periods) { | ||
classes[p[0]] = { n: '', c: '', l: '', o: '', s: '' }; | ||
classes[p] = { n: '', c: '', l: '', o: '', s: '' }; | ||
} | ||
|
||
const teachers: {[key: string]: [string, string]} = {}; | ||
for (const element of sgyClasses) { | ||
let {pName, pTeacher, term} = getClassInfo(element['section_title']); | ||
if (term === `S${3-SEMESTER}`) continue; // 3 - SEMESTER will rule out S1 courses if SEMESTER is 2, and S2 courses if SEMESTER is 1 | ||
|
||
if (pName === 'SELF') pName = 'S' | ||
if (pName === 'PRIME') pName = 'P' | ||
if (pName === 'Study Hall') pName = 'H' | ||
|
||
if (periods.includes(pName)) { | ||
if (pName === 'SELF') pName = 'S' | ||
if (pName === 'PRIME') pName = 'P' | ||
classes[pName] = { | ||
n: `${element.course_title} · ${pTeacher}`, | ||
c: sgyInfo.classes[pName].c, | ||
|
@@ -132,10 +133,22 @@ export const fetchMaterials = functions.https.onCall(async (data, context) => { | |
|
||
// Fetch courses, then kick out yucky ones | ||
// TODO: type this better | ||
let courses = (await get(`users/${sgyInfo.uid}/sections`, sgyInfo.key, sgyInfo.sec)).section | ||
const unfiltered = await get(`users/${sgyInfo.uid}/sections`, sgyInfo.key, sgyInfo.sec); | ||
const gunnStudentCourse = unfiltered.section.find((sec: {section_title: string}) => { | ||
const title = sec.section_title.toLowerCase(); | ||
return (title.endsWith(YEAR_STR) && title !== YEAR_STR); | ||
}) | ||
const grad_year = ['se', 'ju', 'so', 'fr'].indexOf(gunnStudentCourse.section_title.slice(0,2)) + CURRENT_YEAR + 1; | ||
|
||
let courses = unfiltered.section | ||
.filter((sec: {section_title: string}) => { | ||
const {pName, term} = getClassInfo(sec.section_title); | ||
let {pName, term} = getClassInfo(sec.section_title); | ||
if (term === `S${3 - SEMESTER}`) return false; // 3 - SEMESTER will rule out S1 courses if SEMESTER is 2, and S2 courses if SEMESTER is 1 | ||
|
||
if (pName === 'SELF') pName = 'S' | ||
if (pName === 'PRIME') pName = 'P' | ||
if (pName === 'Study Hall') pName = 'H' | ||
|
||
return periods.includes(pName); | ||
}); | ||
|
||
|
@@ -158,8 +171,11 @@ export const fetchMaterials = functions.https.onCall(async (data, context) => { | |
// Rip we have to flatten promises | ||
const responses = await Promise.all(promises); | ||
|
||
// TODO: type this better | ||
const sections: {[key: string]: {info: any, documents: any, assignments: any, pages: any, events: any}} = {}; | ||
// Return an object of type `SgyData` containing relevant data. | ||
const sections: SgyData = { | ||
grades: (await grades).section, | ||
gradYear: grad_year | ||
} | ||
|
||
// Unflattening smh my head my head my head | ||
for (let i = 0; i < courses.length; i++) { | ||
|
@@ -171,7 +187,7 @@ export const fetchMaterials = functions.https.onCall(async (data, context) => { | |
const pages = responses[4 * i + 2].page; | ||
const events = responses[4 * i + 3].event; | ||
|
||
sections[course.section_title.split(' ')[0][0]] = { | ||
sections[course.section_title.split(' ')[0][0] as SgyPeriod] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does sections[course.section_title.split(' ')[0][0]] work for study hall? I'm not sure what |
||
info: course, | ||
documents, | ||
assignments, | ||
|
@@ -180,8 +196,6 @@ export const fetchMaterials = functions.https.onCall(async (data, context) => { | |
}; | ||
} | ||
|
||
sections.grades = (await grades).section; | ||
|
||
return sections; | ||
|
||
} catch (e) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way to get graduation year via the schoology API? I recall when digging into obscure properties in people's
userData
objects on firestore that some people had agradYear
lumped into their schoology data. Is there a way we could fetch this instead of the gunn student course?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, but it would require a separate fetch to /users/me, and iirc the gradYear is also off by one year for no reason at all. Might just be better to just parse courses instead.