Skip to content

Commit

Permalink
Merge pull request #158 from cornell-dti/archive-students
Browse files Browse the repository at this point in the history
Archive Students API Endpoint
  • Loading branch information
CollinWoo authored May 25, 2023
2 parents f9294a6 + fa5a6af commit 84ef5e8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
62 changes: 58 additions & 4 deletions backend/functions/src/student/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ async function removeStudentFromCourse(
courseId: string,
groupNumber: number
) {
//remove unmatched student
if (groupNumber === -1) {
const ref = courseRef.doc(courseId)

return ref
.update({
unmatched: admin.firestore.FieldValue.arrayRemove(email),
})
.catch((err) => {
logger.error(` error in removing ${email} from unmatched: ${err} `)
throw new Error(`error in removing ${email} from unmatched.`)
})
}

//remove matched student
const ref = courseRef
.doc(courseId)
.collection('groups')
Expand All @@ -66,9 +81,15 @@ async function removeStudentFromCourse(
//second condition is a sanity check
return ref.delete() // Fix this if we ever use this function, I don't think they want groups to ever be deleted fully
} else {
return ref.update({
members: admin.firestore.FieldValue.arrayRemove(email),
})
return ref
.update({
members: admin.firestore.FieldValue.arrayRemove(email),
updateTime: admin.firestore.FieldValue.serverTimestamp(),
})
.catch((err) => {
console.log(err)
throw new Error(`error in removing ${email} from group ${groupNumber}.`)
})
}
}

Expand Down Expand Up @@ -152,7 +173,15 @@ export const addStudentSurveyResponse = async (
// Find the existing courses this student is already in by checking group membership
const existingData = (await studentRef.doc(email).get()).data() //gets all the existing data
//studentCrses becomes courseIds of existingData.groups if available, otherwise []
const existingCourses = existingData ? existingData.groups : [] //gets the existing courses
const existingCourses = existingData
? existingData.groups.filter(
(course: { courseId: string; archived: string }) =>
!course.archived ||
!courseIdsWithNames
.map((course) => course.courseId)
.includes(course.courseId)
)
: [] //gets the existing courses
const existingCourseIds: string[] = existingCourses.map(
(course: { courseId: string }) => course.courseId
)
Expand All @@ -173,6 +202,7 @@ export const addStudentSurveyResponse = async (
notesModifyTime: surveyTimestamp, // Can't use serverTimestamp in arrays...
submissionTime: surveyTimestamp,
templateTimestamps: {},
archived: false,
flagged: false,
}))

Expand Down Expand Up @@ -318,3 +348,27 @@ export const updateStudentNotes = async (
`Updated notes for [${courseId}] in student [${email}] to [${notes}]`
)
}

async function archiveStudentInStudent(email: string, courseId: string) {
const studentDocRef = studentRef.doc(email)
const studentDoc = await studentDocRef.get()
if (!studentDoc.exists) {
throw new Error(`Student document for ${email} does not exist`)
}
const studentData = studentDoc.data() as FirestoreStudent
const groups = studentData.groups
const groupMembership = groups.find((group) => group.courseId === courseId)
if (!groupMembership) {
throw new Error(`Student ${email} does not have membership in ${courseId}`)
}

groupMembership.archived = true
await studentDocRef.update({ groups })
logger.info(`Archived [${courseId}] in student [${email}]]`)
return groupMembership.groupNumber
}

export async function archiveStudent(email: string, courseId: string) {
const groupNum = await archiveStudentInStudent(email, courseId)
await removeStudentFromCourse(email, courseId, groupNum)
}
11 changes: 11 additions & 0 deletions backend/functions/src/student/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getAllStudents,
removeStudent,
updateStudentNotes,
archiveStudent,
} from './functions'

router.get('/', (_, res) => {
Expand Down Expand Up @@ -72,4 +73,14 @@ router.delete('/', checkAuth, (req, res) => {
})
})

router.post('/archive', (req, res) => {
const { email, courseId } = req.body
archiveStudent(email, courseId)
.then((data) => res.status(200).send({ success: true, data }))
.catch((err) => {
console.log(err)
res.status(400).send({ success: false, err: err.message })
})
})

export default router
2 changes: 2 additions & 0 deletions backend/functions/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type GroupMembership = {
notesModifyTime: Date
submissionTime: Date
templateTimestamps: { [key: string]: Date }
archived: boolean
}

/** Information about an email template */
Expand Down Expand Up @@ -96,6 +97,7 @@ export type FirestoreGroupMembership = {
notesModifyTime: Timestamp
submissionTime: Timestamp
templateTimestamps: EmailTimestamps
archived: boolean
}

/** How email template data is stored in the database */
Expand Down

0 comments on commit 84ef5e8

Please sign in to comment.