exporter.js 12 KB

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