2020-05-25 17:54:51 +03:00
|
|
|
import { Exporter } from './exporter';
|
2015-12-16 21:54:35 +01:00
|
|
|
/* global JsonRoutes */
|
2017-04-27 20:49:24 +03:00
|
|
|
if (Meteor.isServer) {
|
2015-12-17 13:11:33 +01:00
|
|
|
// todo XXX once we have a real API in place, move that route there
|
2015-12-17 23:57:28 +01:00
|
|
|
// todo XXX also share the route definition between the client and the server
|
2016-01-05 13:37:15 +01:00
|
|
|
// so that we could use something like
|
|
|
|
// `ApiRoutes.path('boards/export', boardId)``
|
|
|
|
// on the client instead of copy/pasting the route path manually between the
|
|
|
|
// client and the server.
|
2019-05-14 09:37:04 +02:00
|
|
|
/**
|
2020-05-25 17:54:51 +03:00
|
|
|
* @operation exportJson
|
2019-05-14 09:37:04 +02:00
|
|
|
* @tag Boards
|
|
|
|
*
|
2020-05-25 17:54:51 +03:00
|
|
|
* @summary This route is used to export the board to a json file format.
|
2019-05-14 09:37:04 +02:00
|
|
|
*
|
|
|
|
* @description If user is already logged-in, pass loginToken as param
|
|
|
|
* "authToken": '/api/boards/:boardId/export?authToken=:token'
|
2015-12-17 13:11:33 +01:00
|
|
|
*
|
|
|
|
* See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
|
|
|
|
* for detailed explanations
|
2019-05-14 09:37:04 +02:00
|
|
|
*
|
|
|
|
* @param {string} boardId the ID of the board we are exporting
|
|
|
|
* @param {string} authToken the loginToken
|
2015-12-17 13:11:33 +01:00
|
|
|
*/
|
2018-09-14 19:20:24 +02:00
|
|
|
JsonRoutes.add('get', '/api/boards/:boardId/export', function(req, res) {
|
2017-06-12 09:30:03 +02:00
|
|
|
const boardId = req.params.boardId;
|
|
|
|
let user = null;
|
|
|
|
const loginToken = req.query.authToken;
|
|
|
|
if (loginToken) {
|
|
|
|
const hashToken = Accounts._hashLoginToken(loginToken);
|
|
|
|
user = Meteor.users.findOne({
|
|
|
|
'services.resume.loginTokens.hashedToken': hashToken,
|
|
|
|
});
|
2019-05-14 09:37:04 +02:00
|
|
|
} else if (!Meteor.settings.public.sandstorm) {
|
|
|
|
Authentication.checkUserId(req.userId);
|
|
|
|
user = Users.findOne({ _id: req.userId, isAdmin: true });
|
2017-06-12 09:30:03 +02:00
|
|
|
}
|
|
|
|
const exporter = new Exporter(boardId);
|
2019-04-06 09:00:13 +03:00
|
|
|
if (exporter.canExport(user)) {
|
2018-09-14 19:20:24 +02:00
|
|
|
JsonRoutes.sendResult(res, {
|
|
|
|
code: 200,
|
2018-09-16 01:50:36 +03:00
|
|
|
data: exporter.build(),
|
2018-09-14 19:20:24 +02:00
|
|
|
});
|
2017-06-12 09:30:03 +02:00
|
|
|
} else {
|
|
|
|
// we could send an explicit error message, but on the other hand the only
|
|
|
|
// way to get there is by hacking the UI so let's keep it raw.
|
|
|
|
JsonRoutes.sendResult(res, 403);
|
|
|
|
}
|
|
|
|
});
|
2020-01-23 01:16:56 -05:00
|
|
|
|
2020-05-25 17:54:51 +03:00
|
|
|
/**
|
|
|
|
* @operation exportCSV/TSV
|
|
|
|
* @tag Boards
|
|
|
|
*
|
|
|
|
* @summary This route is used to export the board to a CSV or TSV file format.
|
|
|
|
*
|
|
|
|
* @description If user is already logged-in, pass loginToken as param
|
|
|
|
*
|
|
|
|
* See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
|
|
|
|
* for detailed explanations
|
|
|
|
*
|
|
|
|
* @param {string} boardId the ID of the board we are exporting
|
|
|
|
* @param {string} authToken the loginToken
|
|
|
|
* @param {string} delimiter delimiter to use while building export. Default is comma ','
|
|
|
|
*/
|
|
|
|
Picker.route('/api/boards/:boardId/export/csv', function(params, req, res) {
|
|
|
|
const boardId = params.boardId;
|
|
|
|
let user = null;
|
|
|
|
const loginToken = params.query.authToken;
|
|
|
|
if (loginToken) {
|
|
|
|
const hashToken = Accounts._hashLoginToken(loginToken);
|
|
|
|
user = Meteor.users.findOne({
|
|
|
|
'services.resume.loginTokens.hashedToken': hashToken,
|
2017-07-15 22:10:46 +01:00
|
|
|
});
|
2020-05-25 17:54:51 +03:00
|
|
|
} else if (!Meteor.settings.public.sandstorm) {
|
|
|
|
Authentication.checkUserId(req.userId);
|
|
|
|
user = Users.findOne({
|
|
|
|
_id: req.userId,
|
|
|
|
isAdmin: true,
|
2019-06-28 12:52:09 -05:00
|
|
|
});
|
2020-05-25 17:54:51 +03:00
|
|
|
}
|
|
|
|
const exporter = new Exporter(boardId);
|
|
|
|
if (exporter.canExport(user)) {
|
|
|
|
body = params.query.delimiter
|
|
|
|
? exporter.buildCsv(params.query.delimiter)
|
|
|
|
: exporter.buildCsv();
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Length': body[0].length,
|
|
|
|
'Content-Type': params.query.delimiter ? 'text/csv' : 'text/tsv',
|
2019-06-28 12:52:09 -05:00
|
|
|
});
|
2020-05-25 17:54:51 +03:00
|
|
|
res.write(body[0]);
|
|
|
|
res.end();
|
|
|
|
} else {
|
|
|
|
res.writeHead(403);
|
|
|
|
res.end('Permission Error');
|
|
|
|
}
|
|
|
|
});
|
2015-12-09 00:35:45 +01:00
|
|
|
}
|