exportExcel.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { TAPi18n } from '/imports/i18n';
  2. import { runOnServer } from './runOnServer';
  3. runOnServer(function() {
  4. // the ExporterExcel class is only available on server and in order to import
  5. // it here we use runOnServer to have it inside a function instead of an
  6. // if (Meteor.isServer) block
  7. import { ExporterExcel } from './server/ExporterExcel';
  8. import { Picker } from 'meteor/communitypackages:picker';
  9. // todo XXX once we have a real API in place, move that route there
  10. // todo XXX also share the route definition between the client and the server
  11. // so that we could use something like
  12. // `ApiRoutes.path('boards/exportExcel', boardId)``
  13. // on the client instead of copy/pasting the route path manually between the
  14. // client and the server.
  15. /**
  16. * @operation exportExcel
  17. * @tag Boards
  18. *
  19. * @summary This route is used to export the board Excel.
  20. *
  21. * @description If user is already logged-in, pass loginToken as param
  22. * "authToken": '/api/boards/:boardId/exportExcel?authToken=:token'
  23. *
  24. * See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
  25. * for detailed explanations
  26. *
  27. * @param {string} boardId the ID of the board we are exporting
  28. * @param {string} authToken the loginToken
  29. */
  30. Picker.route('/api/boards/:boardId/exportExcel', function (params, req, res) {
  31. const boardId = params.boardId;
  32. let user = null;
  33. let impersonateDone = false;
  34. let adminId = null;
  35. const loginToken = params.query.authToken;
  36. if (loginToken) {
  37. const hashToken = Accounts._hashLoginToken(loginToken);
  38. user = Meteor.users.findOne({
  39. 'services.resume.loginTokens.hashedToken': hashToken,
  40. });
  41. adminId = user._id.toString();
  42. impersonateDone = ImpersonatedUsers.findOne({
  43. adminId: adminId,
  44. });
  45. } else if (!Meteor.settings.public.sandstorm) {
  46. Authentication.checkUserId(req.userId);
  47. user = Users.findOne({
  48. _id: req.userId,
  49. isAdmin: true,
  50. });
  51. }
  52. let userLanguage = 'en';
  53. if(user && user.profile){
  54. userLanguage = user.profile.language
  55. }
  56. const exporterExcel = new ExporterExcel(boardId, userLanguage);
  57. if (exporterExcel.canExport(user) || impersonateDone) {
  58. if (impersonateDone) {
  59. ImpersonatedUsers.insert({
  60. adminId: adminId,
  61. boardId: boardId,
  62. reason: 'exportExcel',
  63. });
  64. }
  65. exporterExcel.build(res);
  66. } else {
  67. res.end(TAPi18n.__('user-can-not-export-excel'));
  68. }
  69. });
  70. });