Schedule serverside

This commit is contained in:
tmshv committed 2017-05-26 18:41:11 +03:00
1 parent 06b3f481bd
commit fcf45614a3
11 files changed
+311 -107

No files matched your search

+62 -30
View File
@@ -1,18 +1,20 @@
import {c} from '../core/db'
import {select} from '../utils/fn'
import {createMap} from '../utils/common'
import {getCollective} from './collective'
import {scheduleDate, getPeriod as schedulePeriod} from '../models/schedule'
import {scheduleDate, getPeriod as schedulePeriod, getPeriodString} from '../models/schedule'
import {getCourse} from '../models/course'
const last = require('../lib/array').last
const sortBy = require('../lib/sort').sortBy
const SCHEDULES = 'schedules'
export async function getSchedule(period, semester) {
async function findSchedule(period, semester) {
const schedule = await c(SCHEDULES).findOne({
$or: [
{period: period},
{period: schedulePeriod(period)}
{period: period},
{period: schedulePeriod(period)}
],
semester: semester
})
@@ -20,19 +22,44 @@ export async function getSchedule(period, semester) {
return schedule ? migrate(schedule) : null
}
export async function getSchedules(fields = ['_id', 'id', 'period', 'semester']) {
const list = await c(SCHEDULES)
.find({})
.toArray()
async function findAllScheduleParams() {
const schedules = await c(SCHEDULES)
.find({}, {
period: 1,
semester: 1,
_id: 0,
})
.toArray()
const schedules = await Promise.all(list.map(migrate))
return schedules
.sort((a, b) => {
const ad = scheduleDate(a).getTime()
const bd = scheduleDate(b).getTime()
return ad - bd
})
.map(select(fields))
.map(i => ({
...i,
period: schedulePeriod(i.period),
}))
.sort(sortBy(
i => scheduleDate(i).getTime(),
true,
))
}
async function findDefaultScheduleParams() {
const schedules = await findAllScheduleParams()
return last(schedules
.map(i => ({
...i,
period: getPeriodString(i.period),
}))
)
}
function text({semester, period}) {
const word = semester => [['spring', 'Весна'], ['autumn', 'Осень']]
.find(i => i[0] === semester)
[1]
const periodIndex = semester === 'spring' ? 1 : 0
const year = period[periodIndex]
return `${word(semester)} ${year} года`
}
async function migrate(schedule) {
@@ -49,7 +76,7 @@ async function migrate(schedule) {
function migrate10to20(schedule) {
const dayNames = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
const time = time => time
.map(i => i.split(' '))
.map(i => i.split(' '))
const week = days => days.reduce((week, lesson, index) => {
if (lesson) {
@@ -66,23 +93,23 @@ function migrate10to20(schedule) {
version: '2.0',
period: schedulePeriod(schedule.period),
groups: schedule.schedule
.map(group => ({
name: group.group,
time: group.time,
week: week(group.week)
}))
.map(group => ({
name: group.group,
time: group.time,
week: week(group.week)
}))
})
}
async function migrate20to30(schedule) {
const days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
const days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat']//, 'sun']
const persons = await getCollective()
const person = createMap(i => i.id, persons)
const week = week => days
.map(day => week[day] || [])
.map(lessons => lessons.map(lesson))
.map(day => week[day] || [])
.map(lessons => lessons.map(lesson))
const lesson = i => ({
time: i.time,
@@ -107,10 +134,15 @@ async function migrate20to30(schedule) {
return Object.assign(schedule, {
version: '3.0',
groups: schedule.groups
.map(group => ({
name: group.name,
time: group.time,
week: week(group.week)
}))
.map(group => ({
name: group.name,
time: group.time,
week: week(group.week)
}))
})
}
exports.findSchedule = findSchedule
exports.findAllScheduleParams = findAllScheduleParams
exports.findDefaultScheduleParams = findDefaultScheduleParams
exports.text = text