summaryrefslogtreecommitdiffstats
path: root/models/exporter.js
blob: b6188ece3062e7707bf2995fe565b46435b8d119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
const stringify = require('csv-stringify');

// exporter maybe is broken since Gridfs introduced, add fs and path
export class Exporter {
  constructor(boardId) {
    this._boardId = boardId;
  }

  build() {
    const fs = Npm.require('fs');
    const os = Npm.require('os');
    const path = Npm.require('path');

    const byBoard = { boardId: this._boardId };
    const byBoardNoLinked = {
      boardId: this._boardId,
      linkedId: { $in: ['', null] },
    };
    // we do not want to retrieve boardId in related elements
    const noBoardId = {
      fields: {
        boardId: 0,
      },
    };
    const result = {
      _format: 'wekan-board-1.0.0',
    };
    _.extend(
      result,
      Boards.findOne(this._boardId, {
        fields: {
          stars: 0,
        },
      }),
    );
    result.lists = Lists.find(byBoard, noBoardId).fetch();
    result.cards = Cards.find(byBoardNoLinked, noBoardId).fetch();
    result.swimlanes = Swimlanes.find(byBoard, noBoardId).fetch();
    result.customFields = CustomFields.find(
      { boardIds: this._boardId },
      { fields: { boardIds: 0 } },
    ).fetch();
    result.comments = CardComments.find(byBoard, noBoardId).fetch();
    result.activities = Activities.find(byBoard, noBoardId).fetch();
    result.rules = Rules.find(byBoard, noBoardId).fetch();
    result.checklists = [];
    result.checklistItems = [];
    result.subtaskItems = [];
    result.triggers = [];
    result.actions = [];
    result.cards.forEach(card => {
      result.checklists.push(
        ...Checklists.find({
          cardId: card._id,
        }).fetch(),
      );
      result.checklistItems.push(
        ...ChecklistItems.find({
          cardId: card._id,
        }).fetch(),
      );
      result.subtaskItems.push(
        ...Cards.find({
          parentId: card._id,
        }).fetch(),
      );
    });
    result.rules.forEach(rule => {
      result.triggers.push(
        ...Triggers.find(
          {
            _id: rule.triggerId,
          },
          noBoardId,
        ).fetch(),
      );
      result.actions.push(
        ...Actions.find(
          {
            _id: rule.actionId,
          },
          noBoardId,
        ).fetch(),
      );
    });

    // [Old] for attachments we only export IDs and absolute url to original doc
    // [New] Encode attachment to base64

    const getBase64Data = function(doc, callback) {
      let buffer = Buffer.allocUnsafe(0);
      buffer.fill(0);

      // callback has the form function (err, res) {}
      const tmpFile = path.join(
        os.tmpdir(),
        `tmpexport${process.pid}${Math.random()}`,
      );
      const tmpWriteable = fs.createWriteStream(tmpFile);
      const readStream = doc.createReadStream();
      readStream.on('data', function(chunk) {
        buffer = Buffer.concat([buffer, chunk]);
      });

      readStream.on('error', function() {
        callback(null, null);
      });
      readStream.on('end', function() {
        // done
        fs.unlink(tmpFile, () => {
          //ignored
        });

        callback(null, buffer.toString('base64'));
      });
      readStream.pipe(tmpWriteable);
    };
    const getBase64DataSync = Meteor.wrapAsync(getBase64Data);
    result.attachments = Attachments.find(byBoard)
      .fetch()
      .map(attachment => {
        let filebase64 = null;
        filebase64 = getBase64DataSync(attachment);

        return {
          _id: attachment._id,
          cardId: attachment.cardId,
          //url: FlowRouter.url(attachment.url()),
          file: filebase64,
          name: attachment.original.name,
          type: attachment.original.type,
        };
      });

    // we also have to export some user data - as the other elements only
    // include id but we have to be careful:
    // 1- only exports users that are linked somehow to that board
    // 2- do not export any sensitive information
    const users = {};
    result.members.forEach(member => {
      users[member.userId] = true;
    });
    result.lists.forEach(list => {
      users[list.userId] = true;
    });
    result.cards.forEach(card => {
      users[card.userId] = true;
      if (card.members) {
        card.members.forEach(memberId => {
          users[memberId] = true;
        });
      }
    });
    result.comments.forEach(comment => {
      users[comment.userId] = true;
    });
    result.activities.forEach(activity => {
      users[activity.userId] = true;
    });
    result.checklists.forEach(checklist => {
      users[checklist.userId] = true;
    });
    const byUserIds = {
      _id: {
        $in: Object.getOwnPropertyNames(users),
      },
    };
    // we use whitelist to be sure we do not expose inadvertently
    // some secret fields that gets added to User later.
    const userFields = {
      fields: {
        _id: 1,
        username: 1,
        'profile.fullname': 1,
        'profile.initials': 1,
        'profile.avatarUrl': 1,
      },
    };
    result.users = Users.find(byUserIds, userFields)
      .fetch()
      .map(user => {
        // user avatar is stored as a relative url, we export absolute
        if ((user.profile || {}).avatarUrl) {
          user.profile.avatarUrl = FlowRouter.url(user.profile.avatarUrl);
        }
        return user;
      });
    return result;
  }

  buildCsv(delimiter = ',') {
    const result = this.build();
    const columnHeaders = [];
    const cardRows = [];
    columnHeaders.push(
      'Title',
      'Description',
      'Status',
      'Swimlane',
      'Owner',
      'Requested by',
      'Assigned by',
      'Members',
      'Assignees',
      'Labels',
      'Start at',
      'Due at',
      'End at',
      'Over time',
      'Spent time (hours)',
      'Created at',
      'Last modified at',
      'Last activity',
      'Vote',
      'Archived',
    );
    const customFieldMap = {};
    let i = 0;
    result.customFields.forEach(customField => {
      customFieldMap[customField._id] = {
        position: i,
        type: customField.type,
      };
      if (customField.type === 'dropdown') {
        let options = '';
        customField.settings.dropdownItems.forEach(item => {
          options = options === '' ? item.name : `${`${options}/${item.name}`}`;
        });
        columnHeaders.push(
          `CustomField-${customField.name}-${customField.type}-${options}`,
        );
      } else if (customField.type === 'currency') {
        columnHeaders.push(
          `CustomField-${customField.name}-${customField.type}-${customField.settings.currencyCode}`,
        );
      } else {
        columnHeaders.push(
          `CustomField-${customField.name}-${customField.type}`,
        );
      }
      i++;
    });
    /* TODO: Try to get translations working.
             These currently only bring English translations.
    TAPi18n.__('title'),
    TAPi18n.__('description'),
    TAPi18n.__('status'),
    TAPi18n.__('swimlane'),
    TAPi18n.__('owner'),
    TAPi18n.__('requested-by'),
    TAPi18n.__('assigned-by'),
    TAPi18n.__('members'),
    TAPi18n.__('assignee'),
    TAPi18n.__('labels'),
    TAPi18n.__('card-start'),
    TAPi18n.__('card-due'),
    TAPi18n.__('card-end'),
    TAPi18n.__('overtime-hours'),
    TAPi18n.__('spent-time-hours'),
    TAPi18n.__('createdAt'),
    TAPi18n.__('last-modified-at'),
    TAPi18n.__('last-activity'),
    TAPi18n.__('voting'),
    TAPi18n.__('archived'),
    */

    const stringifier = stringify({
      header: true,
      delimiter,
      columns: columnHeaders,
    });

    stringifier.on('readable', function() {
      let row;
      while ((row = stringifier.read())) {
        cardRows.push(row);
      }
    });

    stringifier.on('error', function(err) {
      // eslint-disable-next-line no-console
      console.error(err.message);
    });

    result.cards.forEach(card => {
      const currentRow = [];
      currentRow.push(card.title);
      currentRow.push(card.description);
      currentRow.push(
        result.lists.find(({ _id }) => _id === card.listId).title,
      );
      currentRow.push(
        result.swimlanes.find(({ _id }) => _id === card.swimlaneId).title,
      );
      currentRow.push(
        result.users.find(({ _id }) => _id === card.userId).username,
      );
      currentRow.push(card.requestedBy ? card.requestedBy : ' ');
      currentRow.push(card.assignedBy ? card.assignedBy : ' ');
      let usernames = '';
      card.members.forEach(memberId => {
        const user = result.users.find(({ _id }) => _id === memberId);
        usernames = `${usernames + user.username} `;
      });
      currentRow.push(usernames.trim());
      let assignees = '';
      card.assignees.forEach(assigneeId => {
        const user = result.users.find(({ _id }) => _id === assigneeId);
        assignees = `${assignees + user.username} `;
      });
      currentRow.push(assignees.trim());
      let labels = '';
      card.labelIds.forEach(labelId => {
        const label = result.labels.find(({ _id }) => _id === labelId);
        labels = `${labels + label.name}-${label.color} `;
      });
      currentRow.push(labels.trim());
      currentRow.push(card.startAt ? moment(card.startAt).format() : ' ');
      currentRow.push(card.dueAt ? moment(card.dueAt).format() : ' ');
      currentRow.push(card.endAt ? moment(card.endAt).format() : ' ');
      currentRow.push(card.isOvertime ? 'true' : 'false');
      currentRow.push(card.spentTime);
      currentRow.push(card.createdAt ? moment(card.createdAt).format() : ' ');
      currentRow.push(card.modifiedAt ? moment(card.modifiedAt).format() : ' ');
      currentRow.push(
        card.dateLastActivity ? moment(card.dateLastActivity).format() : ' ',
      );
      if (card.vote && card.vote.question !== '') {
        let positiveVoters = '';
        let negativeVoters = '';
        card.vote.positive.forEach(userId => {
          const user = result.users.find(({ _id }) => _id === userId);
          positiveVoters = `${positiveVoters + user.username} `;
        });
        card.vote.negative.forEach(userId => {
          const user = result.users.find(({ _id }) => _id === userId);
          negativeVoters = `${negativeVoters + user.username} `;
        });
        const votingResult = `${
          card.vote.public
            ? `yes-${
                card.vote.positive.length
              }-${positiveVoters.trimRight()}-no-${
                card.vote.negative.length
              }-${negativeVoters.trimRight()}`
            : `yes-${card.vote.positive.length}-no-${card.vote.negative.length}`
        }`;
        currentRow.push(`${card.vote.question}-${votingResult}`);
      } else {
        currentRow.push(' ');
      }
      currentRow.push(card.archived ? 'true' : 'false');
      //Custom fields
      const customFieldValuesToPush = new Array(result.customFields.length);
      card.customFields.forEach(field => {
        if (field.value !== null) {
          if (customFieldMap[field._id].type === 'date') {
            customFieldValuesToPush[
              customFieldMap[field._id].position
            ] = moment(field.value).format();
          } else if (customFieldMap[field._id].type === 'dropdown') {
            const dropdownOptions = result.customFields.find(
              ({ _id }) => _id === field._id,
            ).settings.dropdownItems;
            const fieldValue = dropdownOptions.find(
              ({ _id }) => _id === field.value,
            ).name;
            customFieldValuesToPush[
              customFieldMap[field._id].position
            ] = fieldValue;
          } else {
            customFieldValuesToPush[customFieldMap[field._id].position] =
              field.value;
          }
        }
      });
      for (
        let valueIndex = 0;
        valueIndex < customFieldValuesToPush.length;
        valueIndex++
      ) {
        if (!(valueIndex in customFieldValuesToPush)) {
          currentRow.push(' ');
        } else {
          currentRow.push(customFieldValuesToPush[valueIndex]);
        }
      }
      stringifier.write(currentRow);
    });
    stringifier.end();
    return cardRows[0];
  }

  canExport(user) {
    const board = Boards.findOne(this._boardId);
    return board && board.isVisibleBy(user);
  }
}