exporter.js 13 KB

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