Skip to content

Commit

Permalink
reclaim moar tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mluukkai committed Feb 1, 2024
1 parent 509d365 commit 7fa48e0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
stats.json
cypress
cypress
server/util/helper.js
28 changes: 23 additions & 5 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,36 @@ class User extends Model {
}

const studyrights = await this.getStudyRights()
const matluRight = studyrights.find(({ faculty_code, valid }) => faculty_code === 'H50' && validNow(valid))
const validMatluRights = studyrights.filter(({ faculty_code, valid }) => faculty_code === 'H50' && validNow(valid))

if (!matluRight) {
if (!validMatluRights || validMatluRights.length === 0) {
return false
}

const semesterEnrollments = (await this.getSemesterEnrollments()).data
const flattenEnrolments = semesterEnrollments[matluRight.id]
// console.log(semesterEnrollments)

const status = flattenEnrolments.some(({ semester_code, semester_enrollment_type_code }) => semester_code === currentSemester && [1, 3].includes(semester_enrollment_type_code))
const matluEnrollments = validMatluRights.reduce((set, right) => set.concat(semesterEnrollments[right.id]), [])

return status
// const flattenedEnrollments = semesterEnrollments[validMatluRights[0].id]
// console.log(matluEnrollments)

const ACCEPTABLE_TYPES = [1, 3]
const currentAndLegit = ({ semester_code, semester_enrollment_type_code }) => semester_code === currentSemester && ACCEPTABLE_TYPES.includes(semester_enrollment_type_code)

/*
const leg = matluEnrollments.some(currentAndLegit)
if (!leg) {
const reg = matluEnrollments.find(s => s.semester_code === currentSemester)
// console.log(reg.termRegistrationType)
if (!['NONATTENDING', 'NEGLECTED'].includes(reg.termRegistrationType)) {
console.log(this.studentNumber, reg.termRegistrationType)
}
}
*/

return matluEnrollments.some(currentAndLegit)
}

async getStudyRights() {
Expand Down
42 changes: 31 additions & 11 deletions server/services/updaterService.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ const updateStudentEligibility = async (studentNumber) => {
// await completionChecker(updatedStudent)
}

const isSpring = new Date().getMonth() < 7

const isAbsent = async (student, currentSemester) => {
const isPresent = await student.isEnrolled(currentSemester)
return !isPresent
Expand All @@ -149,7 +151,7 @@ const isAbsent = async (student, currentSemester) => {
const getFallSemesterCode = year => (year - 1950) * 2 + 1
const getSpringSemesterCode = year => (year - 1950) * 2
const getCurrentYear = () => (inProduction ? new Date().getFullYear() : 2019)
const getCurrentSemesterCode = (new Date().getMonth() < 8 ? getSpringSemesterCode(new Date().getFullYear()) : getFallSemesterCode(new Date().getFullYear()))
const getCurrentSemesterCode = (isSpring ? getSpringSemesterCode(new Date().getFullYear()) : getFallSemesterCode(new Date().getFullYear()))

const getFirstYearCredits = async (student) => {
const semesterCode = getFallSemesterCode(student.signupYear)
Expand Down Expand Up @@ -268,28 +270,45 @@ const reclaimYear = async (signup_year) => {
const student = deviceHolders[i]
// eslint-disable-next-line no-await-in-loop

const isSpring = new Date().getMonth() < 7
const deviceHeldForAYear = currentYear - (isSpring ? 1 : 0) - new Date(student.deviceGivenAt).getFullYear() > 0
const deviceGiven = new Date(student.deviceGivenAt).getFullYear()
const deviceHeldForAYear = currentYear - (isSpring ? 1 : 0) - deviceGiven > 0
if (deviceHeldForAYear) {
// eslint-disable-next-line no-await-in-loop
const firstYearCredits = await getFirstYearCredits(student)
// eslint-disable-next-line no-await-in-loop
await student.update({ firstYearCredits })
}
const creditsUnderLimit = deviceHeldForAYear && student.firstYearCredits < FIRST_YEAR_CREDIT_LIMIT
const creditsUnderLimit = deviceHeldForAYear && deviceGiven > 2021 && student.firstYearCredits < FIRST_YEAR_CREDIT_LIMIT

// eslint-disable-next-line no-await-in-loop
const enrolledInFaculty = await student.isEnrolled(semester_code)
if (!enrolledInFaculty || creditsUnderLimit) {
// console.log(student.studentNumber, !enrolledInFaculty, creditsUnderLimit)
counter.enroll += !enrolledInFaculty
counter.limit += creditsUnderLimit
counter.reclaim += !enrolledInFaculty || creditsUnderLimit
}

const loanExpiredThisYear = differenceInYears(new Date(`${currentYear}`), new Date(student.deviceGivenAt)) === 5
const absent = !enrolledInFaculty

const semester = isSpring ? 'SPRING' : 'AUTUM'

if (loanExpiredThisYear || absent) {
// eslint-disable-next-line no-await-in-loop
await ReclaimCase.create({
userId: student.id,
status: 'OPEN',
absent,
loanExpired: loanExpiredThisYear,
creditsUnderLimit,
year: currentYear,
semester,
})
}
}

const percentage = (100 * (counter.reclaim / deviceHolders.length)).toFixed(1)
console.log(signup_year, counter.reclaim, deviceHolders.length, percentage, counter.enroll, counter.limit)
console.log(signup_year, counter.reclaim, deviceHolders.length, Number(percentage), counter.enroll, counter.limit)

// logger.info(`Checking reclaim status for ${deviceHolders.length} students, year ${currentYear}`)
}
Expand All @@ -300,17 +319,18 @@ const runReclaimStatusUpdater = async () => {
where: {
deviceSerial: { [Op.ne]: null },
deviceReturned: false,
signup_year: 2020,
signup_year: 2022,
},
})
const student = deviceHolders.find(d => d.studentNumber === '014299629')
const firstYearCredits = await getFirstYearCredits(student)
console.log(student.studentNumber, firstYearCredits)
const semester_code = getCurrentSemesterCode
const student = deviceHolders.find(d => d.studentNumber === '015372244')
const enrolledInFaculty = await student.isEnrolled(semester_code)
console.log(enrolledInFaculty)
*/

for (let year = 2019; year < 2024; year++) {
// for (let year = 2019; year < 2024; year++) {
for (let year = 2019; year < 2020; year++) {
// eslint-disable-next-line no-await-in-loop
await reclaimYear(year)
}
Expand Down

0 comments on commit 7fa48e0

Please sign in to comment.