Skip to content
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

Fetch externalServices' courses and use it to detect a new school year start. #260

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions client/app/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ class @App
NotificationsManager.hideAll()

# == Modals ==
Template.newSchoolYearModal.helpers classes: -> classes()

Template.newSchoolYearModal.events
"change": (event) ->
target = $(event.target)
checked = target.is ":checked"
classId = target.attr "classid"

target.find("span").css color: if checked then "lightred" else "white"

Template.addTicketModal.helpers
body: -> Session.get('addTicketModalContent') ? ''

Expand Down
29 changes: 0 additions & 29 deletions client/app/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,35 +92,6 @@ <h4 class="modal-title">Vakken</h4>
</div>
</template>

<template name="newSchoolYearModal">
<div class="modal fade" aria-hidden="true" id="newSchoolYearModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<h4 class="modal-title">Nieuw Schooljaar</h4>
</div>
<div class="modal-body">
<h4 style="text-align: center; margin-bottom: 20px;">Welken vakken moeten er weg?<small><br>Vakken kan je zometeen toevoegen.</small></h4>
{{#each classes}}
<div class="classRemoveRow">
<label>
<input type="checkbox" classId="{{_id._str}}">
<span id="checkboxLabel">
{{name}}
</span>
</label>
</div>
{{/each}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="goButton">Okido</button>
</div>
</div>
</div>
</div>
</template>

<template name="addTicketModal">
<div class="modal fade" aria-hidden="true" id="addTicketModal">
<div class="modal-dialog">
Expand Down
29 changes: 5 additions & 24 deletions client/app/setup/setup.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ setupItems = [
name: 'externalServices'
async: no
onDone: (cb) ->
# well, this externalServices global shit stuff is a fucking mess.

schoolId = _(externalServices.get())
.map (s) -> s.profileData()?.schoolId
.find _.negate _.isUndefined

schoolId ?= getUserField Meteor.userId(), 'profile.schoolId'

done = (success) ->
if success?
addProgress 'externalServices', -> cb yes
Expand Down Expand Up @@ -193,26 +197,7 @@ setupItems = [
}

{
# TODO: implement this, it should open a modal that asks
# if the current schoolyear is over, if so we can ask the user to follow the setup
# with stuff as `externalServices` and `externalClasses` again.
name: 'newSchoolYear'
func: ->
return undefined

alertModal(
"Hey!",
Locals["nl-NL"].NewSchoolYear(),
DialogButtons.Ok,
{ main: "verder" },
{ main: "btn-primary" },
{ main: (->) },
no
)
}

{
name: 'first-use'
name: 'final'
func: ->
addProgress 'first-use', ->
name = getUserField Meteor.userId(), 'profile.firstName'
Expand All @@ -231,10 +216,6 @@ running = undefined
setupProgress = getUserField Meteor.userId(), 'setupProgress'
return undefined unless setupProgress?

setupProgress = setupProgress.concat [
'newSchoolYear' # TODO: Dunno how're going to do this shit
]

running = _.filter setupItems, (item) -> item.name not in setupProgress

if running.length > 0
Expand Down
63 changes: 63 additions & 0 deletions lib/classes/course.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###*
# @class Course
# @constructor
# @param {Date} from
# @param {Date} to
# @param {String} profile
# @param {String} userId
###
class @Course
constructor: (@from, @to, @profile, @userId) ->
###*
# @property typeId
# @type Number|undefined
# @default undefined
###
@typeId = undefined

###*
# @property externalId
# @type mixed
# @default undefined
###
@externalId = undefined

###*
# @property fetchedBy
# @type String|undefined
# @default undefined
###
@fetchedBy = undefined

###*
# @property lastUpdated
# @type Date|undefined
# @default undefined
###
@lastUpdated = undefined

###*
# @method inside
# @param {Date} date
# @return {Boolean}
###
inside: (date) -> @from <= date <= @to

@schema: new SimpleSchema
from:
type: Date
to:
type: Date
profile:
type: String
userId:
type: String
typeId:
type: null
optional: yes
externalId:
type: null
optional: yes
fetchedBy:
type: String
optional: yes
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ export * from './functions/studyUtils.coffee'
export * from './functions/messages.coffee'
export * from './functions/profileData.coffee'
export * from './functions/serviceUpdates.coffee'
export * from './functions/courses.coffee'
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Services, ExternalServicesConnector } from '../connector.coffee'

###*
# @method getCourses
# @param {String} userId
# @return {Course[]}
###
export getCourses = (userId) ->
check userId, String
res = []

services = _.filter Services, (s) -> s.getCourses? and s.active userId
for service in services
try
res = res.concat service.getCourses userId
catch e
ExternalServicesConnector.handleServiceError service.name, userId, e

res
26 changes: 25 additions & 1 deletion packages/magister-binding/magister-binding.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global AbsenceInfo, Schools, Grade, StudyUtil, GradePeriod,
ExternalPerson, CalendarItem, Assignment, ExternalFile, getClassInfos,
Message, ms, MagisterBinding, ServiceUpdate */
Message, ms, MagisterBinding, ServiceUpdate, Course */

// One heck of a binding this is.

Expand Down Expand Up @@ -249,6 +249,30 @@ function getCurrentCourse (magister) {
return fut.wait();
}

/**
* @method getCourses
* @param {String} userId
* @return {Course}
*/
MagisterBinding.getCourses = function (userId) {
check(userId, String);
const magister = getMagisterObject(userId);
const courses = Meteor.wrapAsync(magister.courses, magister)();

return courses.map(function (c) {
const course = new Course(c.begin(), c.end(), c.profile(), userId);

course.profile = c.profile();
course.typeId = c.type().id;
course.externalId = prefixId(magister, c.id());
course.fetchedBy = MagisterBinding.name;

course.lastUpdated = new Date();

return course;
});
};

/**
* Get the grades for the given userId from Magister.
* @method getGrades
Expand Down
44 changes: 44 additions & 0 deletions server/cronJobs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,47 @@ SyncedCron.add
Date.today()
Date.today().addDays 14
)

SyncedCron.add
name: 'Handle new schoolyears'
schedule: (parser) -> parser.recur().on(4).hour()
job: ->
userIds = Meteor.users.find({
'profile.firstName': $ne: ''
}, {
fields:
_id: 1
'profile.firstName': 1
emails: 1
}).map (u) -> u._id

for userId in userIds
courses = functions.getCourses userId
current = _.find courses, (c) -> c.inside new Date
next = _.find courses, (c) -> c.from > new Date

if current? and Date.today().addDays(-1) < current.start
###
loginUrl = 'https://app.simplyHomework.nl/login'
sendMail user, 'Nieuw schooljaar', """
Hey #{user.profile.firstName}!

Zo te zien is het nieuwe schooljaar zojuist voor je begonnen.
We hopen dat simplyHomework je dit jaar weer kan helpen met school! :)

Je moet wel even eerst de setup doorlopen (ongeveer 2 minuten) op: <a href='#{loginurl}'>#{loginurl}</a>

Success dit schooljaar!
"""
###
Meteor.users.update(
userId
$pullAll: setupProgress: [
'externalServices'
'extractInfo'
'getExternalClasses'
]
)

# REVIEW: do we want to send a message here? If so, what?
# else unless next?