1
0
mirror of https://github.com/matt-fidd/stratos.git synced 2026-01-01 22:59:28 +00:00
Files
stratos/routes/report.js

109 lines
2.1 KiB
JavaScript

'use strict';
const express = require('express');
const router = express.Router();
const User = require('../lib/User');
const validator = require('../lib/validator');
router.all(/\/reports.*/, (req, res, next) => {
if (!req.session.userType === 'account')
return res.redirect('/admin');
next();
});
router.get('/reports', async (req, res) => {
const u = await new User(req.db, req.session.userId);
const classes = await u.getClasses();
const tests = await u.getTests();
let studentIds = [];
let students = [];
classes.forEach(c => studentIds.push(...c.studentIds));
classes.forEach(c => students.push(...c.students));
studentIds = studentIds.map((s, i) =>
studentIds.indexOf(s) === i ? s : '');
students = students.filter((_, i) => studentIds[i] !== '');
return res.render('reports', {
...req.hbsContext,
title: 'Stratos - Reports',
current: 'Reports',
types: [
{
key: 'student',
value: 'Student'
},
{
key: 'class',
value: 'Class'
},
{
key: 'test',
value: 'Test'
}
],
targets: JSON.stringify({
student: students.map(s => ({
id: s.id, name: s.shortName
})),
class: classes.map(c => ({ id: c.id, name: c.name })),
test: tests.map(t => ({
id: t.id,
name: `${t.template.name} - ` +
`${t.class.name} - ` +
`${t.getDateString()}`
}))
})
});
});
router.post('/report/generate', async (req, res) => {
const u = await new User(req.db, req.session.userId);
const classes = await u.getClasses();
const tests = await u.getTests();
const studentIds = [];
classes.forEach(c => studentIds.push(...c.studentIds));
const extractIds = (obj) => obj.id;
const ids = [
...classes.map(extractIds),
...tests.map(extractIds),
...studentIds
];
let fields;
try {
fields = validator.validate(req.body,
[
'type',
'target'
],
{
values: {
type: [ 'student', 'class', 'test' ],
target: ids
}
}
).fields;
} catch (e) {
console.error(e);
return res.status(400).json({ status: 'Invalid' });
}
fields.get('type');
return res.redirect('/admin/reports');
});
module.exports = {
root: '/admin',
router: router
};