| 586 | } |
| 587 | |
| 588 | onCourseInstancesSync (data) { |
| 589 | this.courseDistributionsRecent = [] |
| 590 | this.courseDistributions = [] |
| 591 | if (!data.courseInstances || !data.students || !data.prepaids) { return } |
| 592 | |
| 593 | const createCourseDistributions = numDays => { |
| 594 | // Find student furthest course |
| 595 | let courseName, user |
| 596 | const startDate = new Date() |
| 597 | startDate.setUTCDate(startDate.getUTCDate() - numDays) |
| 598 | const teacherStudentsMap = {} |
| 599 | const studentFurthestCourseMap = {} |
| 600 | const studentPaidStatusMap = {} |
| 601 | for (const courseInstance of Array.from(data.courseInstances)) { |
| 602 | if (utils.objectIdToDate(courseInstance._id) < startDate) { continue } |
| 603 | const { |
| 604 | courseID |
| 605 | } = courseInstance |
| 606 | if (this.courseOrderMap[courseID] == null) { |
| 607 | console.error(`ERROR: no course order for courseID=${courseID}`) |
| 608 | continue |
| 609 | } |
| 610 | const teacherID = courseInstance.ownerID |
| 611 | for (const studentID of Array.from(courseInstance.members)) { |
| 612 | studentPaidStatusMap[studentID] = 'free' |
| 613 | if (!studentFurthestCourseMap[studentID] || (studentFurthestCourseMap[studentID] < this.courseOrderMap[courseID])) { |
| 614 | studentFurthestCourseMap[studentID] = this.courseOrderMap[courseID] |
| 615 | } |
| 616 | if (teacherStudentsMap[teacherID] == null) { teacherStudentsMap[teacherID] = [] } |
| 617 | teacherStudentsMap[teacherID].push(studentID) |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | // Find paid students |
| 622 | const prepaidUserMap = {} |
| 623 | for (user of Array.from(data.students)) { |
| 624 | if (!studentPaidStatusMap[user._id]) { continue } |
| 625 | // since we use user.products in ozar too |
| 626 | const now = new Date() |
| 627 | const products = user.products.filter(p => (p.product === 'course') && (new Date(p.endDate) > now)) |
| 628 | for (const product of Array.from(products)) { |
| 629 | studentPaidStatusMap[user._id] = 'paid' |
| 630 | if (prepaidUserMap[product.prepaid] == null) { prepaidUserMap[product.prepaid] = [] } |
| 631 | prepaidUserMap[product.prepaid].push(user._id) |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | // Find trial students |
| 636 | for (const prepaid of Array.from(data.prepaids)) { |
| 637 | if (!prepaidUserMap[prepaid._id]) { continue } |
| 638 | if (prepaid.properties != null ? prepaid.properties.trialRequestID : undefined) { |
| 639 | for (const userID of Array.from(prepaidUserMap[prepaid._id])) { |
| 640 | studentPaidStatusMap[userID] = 'trial' |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | // Find teacher furthest course and paid status based on their students |