exporter.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import moment from 'moment/min/moment-with-locales';
  3. const Papa = require('papaparse');
  4. import { TAPi18n } from '/imports/i18n';
  5. //const stringify = require('csv-stringify');
  6. //const stringify = require('csv-stringify');
  7. // exporter maybe is broken since Gridfs introduced, add fs and path
  8. export class Exporter {
  9. constructor(boardId, attachmentId) {
  10. this._boardId = boardId;
  11. this._attachmentId = attachmentId;
  12. }
  13. build() {
  14. const fs = Npm.require('fs');
  15. const os = Npm.require('os');
  16. const path = Npm.require('path');
  17. const byBoard = { boardId: this._boardId };
  18. const byBoardNoLinked = {
  19. boardId: this._boardId,
  20. linkedId: { $in: ['', null] },
  21. };
  22. // we do not want to retrieve boardId in related elements
  23. const noBoardId = {
  24. fields: {
  25. boardId: 0,
  26. },
  27. };
  28. const result = {
  29. _format: 'wekan-board-1.0.0',
  30. };
  31. _.extend(
  32. result,
  33. ReactiveCache.getBoard(this._boardId, {
  34. fields: {
  35. stars: 0,
  36. },
  37. }),
  38. );
  39. // [Old] for attachments we only export IDs and absolute url to original doc
  40. // [New] Encode attachment to base64
  41. const getBase64Data = function (doc, callback) {
  42. let buffer = Buffer.allocUnsafe(0);
  43. buffer.fill(0);
  44. // callback has the form function (err, res) {}
  45. const tmpFile = path.join(
  46. os.tmpdir(),
  47. `tmpexport${process.pid}${Math.random()}`,
  48. );
  49. const tmpWriteable = fs.createWriteStream(tmpFile);
  50. const readStream = doc.createReadStream();
  51. readStream.on('data', function (chunk) {
  52. buffer = Buffer.concat([buffer, chunk]);
  53. });
  54. readStream.on('error', function () {
  55. callback(null, null);
  56. });
  57. readStream.on('end', function () {
  58. // done
  59. fs.unlink(tmpFile, () => {
  60. //ignored
  61. });
  62. callback(null, buffer.toString('base64'));
  63. });
  64. readStream.pipe(tmpWriteable);
  65. };
  66. const getBase64DataSync = Meteor.wrapAsync(getBase64Data);
  67. const byBoardAndAttachment = this._attachmentId
  68. ? { boardId: this._boardId, _id: this._attachmentId }
  69. : byBoard;
  70. result.attachments = ReactiveCache.getAttachments(byBoardAndAttachment)
  71. .map((attachment) => {
  72. let filebase64 = null;
  73. filebase64 = getBase64DataSync(attachment);
  74. return {
  75. _id: attachment._id,
  76. cardId: attachment.meta.cardId,
  77. //url: FlowRouter.url(attachment.url()),
  78. file: filebase64,
  79. name: attachment.name,
  80. type: attachment.type,
  81. };
  82. });
  83. //When has a especific valid attachment return the single element
  84. if (this._attachmentId) {
  85. return result.attachments.length > 0 ? result.attachments[0] : {};
  86. }
  87. result.lists = ReactiveCache.getLists(byBoard, noBoardId);
  88. result.cards = ReactiveCache.getCards(byBoardNoLinked, noBoardId);
  89. result.swimlanes = ReactiveCache.getSwimlanes(byBoard, noBoardId);
  90. result.customFields = ReactiveCache.getCustomFields(
  91. { boardIds: this._boardId },
  92. { fields: { boardIds: 0 } },
  93. );
  94. result.comments = ReactiveCache.getCardComments(byBoard, noBoardId);
  95. result.activities = ReactiveCache.getActivities(byBoard, noBoardId);
  96. result.rules = ReactiveCache.getRules(byBoard, noBoardId);
  97. result.checklists = [];
  98. result.checklistItems = [];
  99. result.subtaskItems = [];
  100. result.triggers = [];
  101. result.actions = [];
  102. result.cards.forEach((card) => {
  103. result.checklists.push(
  104. ...ReactiveCache.getChecklists({
  105. cardId: card._id,
  106. }),
  107. );
  108. result.checklistItems.push(
  109. ...ReactiveCache.getChecklistItems({
  110. cardId: card._id,
  111. }),
  112. );
  113. result.subtaskItems.push(
  114. ...ReactiveCache.getCards({
  115. parentId: card._id,
  116. }),
  117. );
  118. });
  119. result.rules.forEach((rule) => {
  120. result.triggers.push(
  121. ...ReactiveCache.getTriggers(
  122. {
  123. _id: rule.triggerId,
  124. },
  125. noBoardId,
  126. ),
  127. );
  128. result.actions.push(
  129. ...ReactiveCache.getActions(
  130. {
  131. _id: rule.actionId,
  132. },
  133. noBoardId,
  134. ),
  135. );
  136. });
  137. // we also have to export some user data - as the other elements only
  138. // include id but we have to be careful:
  139. // 1- only exports users that are linked somehow to that board
  140. // 2- do not export any sensitive information
  141. const users = {};
  142. result.members.forEach((member) => {
  143. users[member.userId] = true;
  144. });
  145. result.lists.forEach((list) => {
  146. users[list.userId] = true;
  147. });
  148. result.cards.forEach((card) => {
  149. users[card.userId] = true;
  150. if (card.members) {
  151. card.members.forEach((memberId) => {
  152. users[memberId] = true;
  153. });
  154. }
  155. });
  156. result.comments.forEach((comment) => {
  157. users[comment.userId] = true;
  158. });
  159. result.activities.forEach((activity) => {
  160. users[activity.userId] = true;
  161. });
  162. result.checklists.forEach((checklist) => {
  163. users[checklist.userId] = true;
  164. });
  165. const byUserIds = {
  166. _id: {
  167. $in: Object.getOwnPropertyNames(users),
  168. },
  169. };
  170. // we use whitelist to be sure we do not expose inadvertently
  171. // some secret fields that gets added to User later.
  172. const userFields = {
  173. fields: {
  174. _id: 1,
  175. username: 1,
  176. 'profile.fullname': 1,
  177. 'profile.initials': 1,
  178. 'profile.avatarUrl': 1,
  179. },
  180. };
  181. result.users = ReactiveCache.getUsers(byUserIds, userFields)
  182. .map((user) => {
  183. // user avatar is stored as a relative url, we export absolute
  184. if ((user.profile || {}).avatarUrl) {
  185. user.profile.avatarUrl = FlowRouter.url(user.profile.avatarUrl);
  186. }
  187. return user;
  188. });
  189. return result;
  190. }
  191. buildCsv(userDelimiter = ',', userLanguage='en') {
  192. const result = this.build();
  193. const columnHeaders = [];
  194. const cardRows = [];
  195. const papaconfig = {
  196. quotes: true,
  197. quoteChar: '"',
  198. escapeChar: '"',
  199. delimiter: userDelimiter,
  200. header: true,
  201. newline: "\r\n",
  202. skipEmptyLines: false,
  203. escapeFormulae: true,
  204. };
  205. columnHeaders.push(
  206. TAPi18n.__('title','',userLanguage),
  207. TAPi18n.__('description','',userLanguage),
  208. TAPi18n.__('list','',userLanguage),
  209. TAPi18n.__('swimlane','',userLanguage),
  210. TAPi18n.__('owner','',userLanguage),
  211. TAPi18n.__('requested-by','',userLanguage),
  212. TAPi18n.__('assigned-by','',userLanguage),
  213. TAPi18n.__('members','',userLanguage),
  214. TAPi18n.__('assignee','',userLanguage),
  215. TAPi18n.__('labels','',userLanguage),
  216. TAPi18n.__('card-start','',userLanguage),
  217. TAPi18n.__('card-due','',userLanguage),
  218. TAPi18n.__('card-end','',userLanguage),
  219. TAPi18n.__('overtime-hours','',userLanguage),
  220. TAPi18n.__('spent-time-hours','',userLanguage),
  221. TAPi18n.__('createdAt','',userLanguage),
  222. TAPi18n.__('last-modified-at','',userLanguage),
  223. TAPi18n.__('last-activity','',userLanguage),
  224. TAPi18n.__('voting','',userLanguage),
  225. TAPi18n.__('archived','',userLanguage),
  226. );
  227. const customFieldMap = {};
  228. let i = 0;
  229. result.customFields.forEach((customField) => {
  230. customFieldMap[customField._id] = {
  231. position: i,
  232. type: customField.type,
  233. };
  234. if (customField.type === 'dropdown') {
  235. let options = '';
  236. customField.settings.dropdownItems.forEach((item) => {
  237. options = options === '' ? item.name : `${`${options}/${item.name}`}`;
  238. });
  239. columnHeaders.push(
  240. `CustomField-${customField.name}-${customField.type}-${options}`,
  241. );
  242. } else if (customField.type === 'currency') {
  243. columnHeaders.push(
  244. `CustomField-${customField.name}-${customField.type}-${customField.settings.currencyCode}`,
  245. );
  246. } else {
  247. columnHeaders.push(
  248. `CustomField-${customField.name}-${customField.type}`,
  249. );
  250. }
  251. i++;
  252. });
  253. //cardRows.push([[columnHeaders]]);
  254. cardRows.push(columnHeaders);
  255. result.cards.forEach((card) => {
  256. const currentRow = [];
  257. currentRow.push(card.title);
  258. currentRow.push(card.description);
  259. currentRow.push(
  260. result.lists.find(({ _id }) => _id === card.listId).title,
  261. );
  262. currentRow.push(
  263. result.swimlanes.find(({ _id }) => _id === card.swimlaneId).title,
  264. );
  265. currentRow.push(
  266. result.users.find(({ _id }) => _id === card.userId).username,
  267. );
  268. currentRow.push(card.requestedBy ? card.requestedBy : ' ');
  269. currentRow.push(card.assignedBy ? card.assignedBy : ' ');
  270. let usernames = '';
  271. card.members.forEach((memberId) => {
  272. const user = result.users.find(({ _id }) => _id === memberId);
  273. usernames = `${usernames + user.username} `;
  274. });
  275. currentRow.push(usernames.trim());
  276. let assignees = '';
  277. card.assignees.forEach((assigneeId) => {
  278. const user = result.users.find(({ _id }) => _id === assigneeId);
  279. assignees = `${assignees + user.username} `;
  280. });
  281. currentRow.push(assignees.trim());
  282. let labels = '';
  283. card.labelIds.forEach((labelId) => {
  284. const label = result.labels.find(({ _id }) => _id === labelId);
  285. labels = `${labels + label.name}-${label.color} `;
  286. });
  287. currentRow.push(labels.trim());
  288. currentRow.push(card.startAt ? moment(card.startAt).format() : ' ');
  289. currentRow.push(card.dueAt ? moment(card.dueAt).format() : ' ');
  290. currentRow.push(card.endAt ? moment(card.endAt).format() : ' ');
  291. currentRow.push(card.isOvertime ? 'true' : 'false');
  292. currentRow.push(card.spentTime);
  293. currentRow.push(card.createdAt ? moment(card.createdAt).format() : ' ');
  294. currentRow.push(card.modifiedAt ? moment(card.modifiedAt).format() : ' ');
  295. currentRow.push(
  296. card.dateLastActivity ? moment(card.dateLastActivity).format() : ' ',
  297. );
  298. if (card.vote && card.vote.question !== '') {
  299. let positiveVoters = '';
  300. let negativeVoters = '';
  301. card.vote.positive.forEach((userId) => {
  302. const user = result.users.find(({ _id }) => _id === userId);
  303. positiveVoters = `${positiveVoters + user.username} `;
  304. });
  305. card.vote.negative.forEach((userId) => {
  306. const user = result.users.find(({ _id }) => _id === userId);
  307. negativeVoters = `${negativeVoters + user.username} `;
  308. });
  309. const votingResult = `${
  310. card.vote.public
  311. ? `yes-${
  312. card.vote.positive.length
  313. }-${positiveVoters.trimRight()}-no-${
  314. card.vote.negative.length
  315. }-${negativeVoters.trimRight()}`
  316. : `yes-${card.vote.positive.length}-no-${card.vote.negative.length}`
  317. }`;
  318. currentRow.push(`${card.vote.question}-${votingResult}`);
  319. } else {
  320. currentRow.push(' ');
  321. }
  322. currentRow.push(card.archived ? 'true' : 'false');
  323. //Custom fields
  324. const customFieldValuesToPush = new Array(result.customFields.length);
  325. card.customFields.forEach((field) => {
  326. if (field.value !== null) {
  327. if (customFieldMap[field._id].type === 'date') {
  328. customFieldValuesToPush[customFieldMap[field._id].position] =
  329. moment(field.value).format();
  330. } else if (customFieldMap[field._id].type === 'dropdown') {
  331. const dropdownOptions = result.customFields.find(
  332. ({ _id }) => _id === field._id,
  333. ).settings.dropdownItems;
  334. const fieldObj = dropdownOptions.find(
  335. ({ _id }) => _id === field.value,
  336. );
  337. const fieldValue = (fieldObj && fieldObj.name) || null;
  338. customFieldValuesToPush[customFieldMap[field._id].position] =
  339. fieldValue;
  340. } else {
  341. customFieldValuesToPush[customFieldMap[field._id].position] =
  342. field.value;
  343. }
  344. }
  345. });
  346. for (
  347. let valueIndex = 0;
  348. valueIndex < customFieldValuesToPush.length;
  349. valueIndex++
  350. ) {
  351. if (!(valueIndex in customFieldValuesToPush)) {
  352. currentRow.push(' ');
  353. } else {
  354. currentRow.push(customFieldValuesToPush[valueIndex]);
  355. }
  356. }
  357. //cardRows.push([[currentRow]]);
  358. cardRows.push(currentRow);
  359. });
  360. return Papa.unparse(cardRows, papaconfig);
  361. }
  362. canExport(user) {
  363. const board = ReactiveCache.getBoard(this._boardId);
  364. return board && board.isVisibleBy(user);
  365. }
  366. }