summaryrefslogtreecommitdiffstats
path: root/models/csvCreator.js
blob: 72b8a3b44667231f813a9d182c3b57efc609f1af (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
import Boards from './boards';

export class CsvCreator {
  constructor(data) {
    // date to be used for timestamps during import
    this._nowDate = new Date();
    // index to help keep track of what information a column stores
    // each row represents a card
    this.fieldIndex = {};
    this.lists = {};
    // Map of members using username => wekanid
    this.members = data.membersMapping ? data.membersMapping : {};
    this.swimlane = null;
  }

  /**
   * If dateString is provided,
   * return the Date it represents.
   * If not, will return the date when it was first called.
   * This is useful for us, as we want all import operations to
   * have the exact same date for easier later retrieval.
   *
   * @param {String} dateString a properly formatted Date
   */
  _now(dateString) {
    if (dateString) {
      return new Date(dateString);
    }
    if (!this._nowDate) {
      this._nowDate = new Date();
    }
    return this._nowDate;
  }

  _user(wekanUserId) {
    if (wekanUserId && this.members[wekanUserId]) {
      return this.members[wekanUserId];
    }
    return Meteor.userId();
  }

  /**
   * Map the header row titles to an index to help assign proper values to the cards' fields
   * Valid headers (name of card fields):
   * title, description, status, owner, member, label, due date, start date, finish date, created at, updated at
   * Some header aliases can also be accepted.
   * Headers are NOT case-sensitive.
   *
   * @param {Array} headerRow array from row of headers of imported CSV/TSV for cards
   */
  mapHeadertoCardFieldIndex(headerRow) {
    const index = {};
    index.customFields = [];
    for (let i = 0; i < headerRow.length; i++) {
      switch (headerRow[i].trim().toLowerCase()) {
        case 'title':
          index.title = i;
          break;
        case 'description':
          index.description = i;
          break;
        case 'stage':
        case 'status':
        case 'state':
          index.stage = i;
          break;
        case 'owner':
          index.owner = i;
          break;
        case 'members':
        case 'member':
          index.members = i;
          break;
        case 'labels':
        case 'label':
          index.labels = i;
          break;
        case 'due date':
        case 'deadline':
        case 'due at':
          index.dueAt = i;
          break;
        case 'start date':
        case 'start at':
          index.startAt = i;
          break;
        case 'finish date':
        case 'end at':
          index.endAt = i;
          break;
        case 'creation date':
        case 'created at':
          index.createdAt = i;
          break;
        case 'update date':
        case 'updated at':
        case 'modified at':
        case 'modified on':
          index.modifiedAt = i;
          break;
      }
      if (headerRow[i].toLowerCase().startsWith('customfield')) {
        if (headerRow[i].split('-')[2] === 'dropdown') {
          index.customFields.push({
            name: headerRow[i].split('-')[1],
            type: headerRow[i].split('-')[2],
            options: headerRow[i].split('-')[3].split('/'),
            position: i,
          });
        } else if (headerRow[i].split('-')[2] === 'currency') {
          index.customFields.push({
            name: headerRow[i].split('-')[1],
            type: headerRow[i].split('-')[2],
            currencyCode: headerRow[i].split('-')[3],
            position: i,
          });
        } else {
          index.customFields.push({
            name: headerRow[i].split('-')[1],
            type: headerRow[i].split('-')[2],
            position: i,
          });
        }
      }
    }
    this.fieldIndex = index;
  }
  createCustomFields(boardId) {
    this.fieldIndex.customFields.forEach(customField => {
      let settings = {};
      if (customField.type === 'dropdown') {
        settings = {
          dropdownItems: customField.options.map(option => {
            return { _id: Random.id(6), name: option };
          }),
        };
      } else if (customField.type === 'currency') {
        settings = {
          currencyCode: customField.currencyCode,
        };
      } else {
        settings = {};
      }
      const id = CustomFields.direct.insert({
        name: customField.name,
        type: customField.type,
        settings,
        showOnCard: false,
        automaticallyOnCard: false,
        showLabelOnMiniCard: false,
        boardIds: [boardId],
      });
      customField.id = id;
      customField.settings = settings;
    });
  }

  createBoard(csvData) {
    const boardToCreate = {
      archived: false,
      color: 'belize',
      createdAt: this._now(),
      labels: [],
      members: [
        {
          userId: Meteor.userId(),
          wekanId: Meteor.userId(),
          isActive: true,
          isAdmin: true,
          isNoComments: false,
          isCommentOnly: false,
          swimlaneId: false,
        },
      ],
      modifiedAt: this._now(),
      //default is private, should inform user.
      permission: 'private',
      slug: 'board',
      stars: 0,
      title: `Imported Board ${this._now()}`,
    };

    // create labels
    const labelsToCreate = new Set();
    for (let i = 1; i < csvData.length; i++) {
      if (csvData[i][this.fieldIndex.labels]) {
        for (const importedLabel of csvData[i][this.fieldIndex.labels].split(
          ' ',
        )) {
          if (importedLabel && importedLabel.length > 0) {
            labelsToCreate.add(importedLabel);
          }
        }
      }
    }
    for (const label of labelsToCreate) {
      let labelName, labelColor;
      if (label.indexOf('-') > -1) {
        labelName = label.split('-')[0];
        labelColor = label.split('-')[1];
      } else {
        labelName = label;
      }
      const labelToCreate = {
        _id: Random.id(6),
        color: labelColor ? labelColor : 'black',
        name: labelName,
      };
      boardToCreate.labels.push(labelToCreate);
    }

    const boardId = Boards.direct.insert(boardToCreate);
    Boards.direct.update(boardId, {
      $set: {
        modifiedAt: this._now(),
      },
    });
    // log activity
    Activities.direct.insert({
      activityType: 'importBoard',
      boardId,
      createdAt: this._now(),
      source: {
        id: boardId,
        system: 'CSV/TSV',
      },
      // We attribute the import to current user,
      // not the author from the original object.
      userId: this._user(),
    });
    return boardId;
  }

  createSwimlanes(boardId) {
    const swimlaneToCreate = {
      archived: false,
      boardId,
      createdAt: this._now(),
      title: 'Default',
      sort: 1,
    };
    const swimlaneId = Swimlanes.direct.insert(swimlaneToCreate);
    Swimlanes.direct.update(swimlaneId, { $set: { updatedAt: this._now() } });
    this.swimlane = swimlaneId;
  }

  createLists(csvData, boardId) {
    let numOfCreatedLists = 0;
    for (let i = 1; i < csvData.length; i++) {
      const listToCreate = {
        archived: false,
        boardId,
        createdAt: this._now(),
      };
      if (csvData[i][this.fieldIndex.stage]) {
        const existingList = Lists.find({
          title: csvData[i][this.fieldIndex.stage],
          boardId,
        }).fetch();
        if (existingList.length > 0) {
          continue;
        } else {
          listToCreate.title = csvData[i][this.fieldIndex.stage];
        }
      } else listToCreate.title = `Imported List ${this._now()}`;

      const listId = Lists.direct.insert(listToCreate);
      this.lists[csvData[i][this.fieldIndex.stage]] = listId;
      numOfCreatedLists++;
      Lists.direct.update(listId, {
        $set: {
          updatedAt: this._now(),
          sort: numOfCreatedLists,
        },
      });
    }
  }

  createCards(csvData, boardId) {
    for (let i = 1; i < csvData.length; i++) {
      const cardToCreate = {
        archived: false,
        boardId,
        createdAt:
          csvData[i][this.fieldIndex.createdAt] !== ' ' || ''
            ? this._now(new Date(csvData[i][this.fieldIndex.createdAt]))
            : null,
        dateLastActivity: this._now(),
        description: csvData[i][this.fieldIndex.description],
        listId: this.lists[csvData[i][this.fieldIndex.stage]],
        swimlaneId: this.swimlane,
        sort: -1,
        title: csvData[i][this.fieldIndex.title],
        userId: this._user(),
        startAt:
          csvData[i][this.fieldIndex.startAt] !== ' ' || ''
            ? this._now(new Date(csvData[i][this.fieldIndex.startAt]))
            : null,
        dueAt:
          csvData[i][this.fieldIndex.dueAt] !== ' ' || ''
            ? this._now(new Date(csvData[i][this.fieldIndex.dueAt]))
            : null,
        endAt:
          csvData[i][this.fieldIndex.endAt] !== ' ' || ''
            ? this._now(new Date(csvData[i][this.fieldIndex.endAt]))
            : null,
        spentTime: null,
        labelIds: [],
        modifiedAt:
          csvData[i][this.fieldIndex.modifiedAt] !== ' ' || ''
            ? this._now(new Date(csvData[i][this.fieldIndex.modifiedAt]))
            : null,
      };
      // add the labels
      if (csvData[i][this.fieldIndex.labels]) {
        const board = Boards.findOne(boardId);
        for (const importedLabel of csvData[i][this.fieldIndex.labels].split(
          ' ',
        )) {
          if (importedLabel && importedLabel.length > 0) {
            let labelToApply;
            if (importedLabel.indexOf('-') === -1) {
              labelToApply = board.getLabel(importedLabel, 'black');
            } else {
              labelToApply = board.getLabel(
                importedLabel.split('-')[0],
                importedLabel.split('-')[1],
              );
            }
            cardToCreate.labelIds.push(labelToApply._id);
          }
        }
      }
      // add the members
      if (csvData[i][this.fieldIndex.members]) {
        const wekanMembers = [];
        for (const importedMember of csvData[i][this.fieldIndex.members].split(
          ' ',
        )) {
          if (this.members[importedMember]) {
            const wekanId = this.members[importedMember];
            if (!wekanMembers.find(wId => wId === wekanId)) {
              wekanMembers.push(wekanId);
            }
          }
        }
        if (wekanMembers.length > 0) {
          cardToCreate.members = wekanMembers;
        }
      }
      // add the custom fields
      if (this.fieldIndex.customFields.length > 0) {
        const customFields = [];
        this.fieldIndex.customFields.forEach(customField => {
          if (csvData[i][customField.position] !== ' ') {
            if (customField.type === 'dropdown') {
              customFields.push({
                _id: customField.id,
                value: customField.settings.dropdownItems.find(
                  ({ name }) => name === csvData[i][customField.position],
                )._id,
              });
            } else {
              customFields.push({
                _id: customField.id,
                value: csvData[i][customField.position],
              });
            }
          }
          cardToCreate.customFields = customFields;
        });
        Cards.direct.insert(cardToCreate);
      }
    }
  }

  create(board, currentBoardId) {
    const isSandstorm =
      Meteor.settings &&
      Meteor.settings.public &&
      Meteor.settings.public.sandstorm;
    if (isSandstorm && currentBoardId) {
      const currentBoard = Boards.findOne(currentBoardId);
      currentBoard.archive();
    }
    this.mapHeadertoCardFieldIndex(board[0]);
    const boardId = this.createBoard(board);
    this.createLists(board, boardId);
    this.createSwimlanes(boardId);
    this.createCustomFields(boardId);
    this.createCards(board, boardId);
    return boardId;
  }
}