1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-15 09:08:06 -05:00
shimapan/app/routes/api/stats.js

88 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-09-15 18:25:07 -04:00
const express = require('express');
const router = express.Router();
const ModelPath = '../../models/';
const Upload = require(ModelPath + 'Upload.js');
const View = require(ModelPath + 'View.js');
const verifyBody = require('../../util/verifyBody');
2019-01-02 16:47:18 -05:00
const authenticate = require('../../util/auth/authenticateRequest');
2018-09-15 18:25:07 -04:00
2019-01-01 15:35:56 -05:00
const uploadProps = [
{name: 'after', type: 'date', optional: true},
{name: 'before', type: 'date', optional: true},
{name: 'limit', type: 'number', min: 1, max: 10000, optional: true}
];
2019-01-01 16:48:08 -05:00
2019-01-02 16:47:18 -05:00
router.get('/uploads', authenticate('stats.get'), verifyBody(uploadProps), async (req, res) => {
2019-01-01 15:35:56 -05:00
let constraints = {uploader: req.username};
// Set date constraints if specified
if (req.body.after || req.body.before)
constraints.date = {};
if (req.body.after)
constraints.date.$gt = new Date(req.body.after);
if (req.body.before)
constraints.date.$lt = new Date(req.body.before);
// Create query
const query = Upload.find(constraints);
// Limit if specified
if (req.body.limit)
query.limit(req.body.limit);
// Fetch and transform results
let uploads = await query;
uploads = uploads.map(upload => {
return {
date: upload.date,
uid: upload.uid,
key: upload.uploaderKey,
2019-01-01 17:29:04 -05:00
originalName: upload.file.originalName,
size: upload.file.size,
mime: upload.file.mime
2019-01-01 15:35:56 -05:00
}
});
res.status(200).json(uploads);
2019-01-02 14:25:51 -05:00
});
2019-01-01 15:35:56 -05:00
2019-01-01 16:48:08 -05:00
2019-01-01 15:35:56 -05:00
const viewProps = [
{name: 'after', type: 'date', optional: true},
{name: 'before', type: 'date', optional: true},
{name: 'limit', type: 'number', min: 1, max: 10000, optional: true}
];
2019-01-01 16:48:08 -05:00
2019-01-02 16:47:18 -05:00
router.get('/views', authenticate('stats.get'), verifyBody(viewProps), async (req, res) => {
2019-01-01 15:35:56 -05:00
let constraints = {uploader: req.username};
// Set date constraints if specified
if (req.body.after || req.body.before)
constraints.date = {};
if (req.body.after)
constraints.date.$gt = new Date(req.body.after);
if (req.body.before)
constraints.date.$lt = new Date(req.body.before);
// Create query
const query = View.find(constraints);
// Limit if specified
if (req.body.limit)
query.limit(req.body.limit);
// Fetch and transform results
let views = await query;
views = views.map(view => {
return {
date: view.date,
uid: view.uid,
}
});
res.status(200).json(views);
2019-01-02 14:25:51 -05:00
});
2019-01-01 15:35:56 -05:00
2018-09-15 18:25:07 -04:00
module.exports = router;