diff --git a/.eslintignore b/.eslintignore index d976020..3d3de82 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,5 @@ node_modules dist stats.json -cypress \ No newline at end of file +cypress +server/util/helper.js \ No newline at end of file diff --git a/server/models/user.js b/server/models/user.js index 4a6e5c0..5ce7949 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -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() { diff --git a/server/services/updaterService.js b/server/services/updaterService.js index 31fc705..50b00de 100644 --- a/server/services/updaterService.js +++ b/server/services/updaterService.js @@ -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 @@ -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) @@ -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}`) } @@ -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) }