mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
An import that stopped halfway - a restart, a hung writer, a failure - left a partial board nothing recorded. Each board import, copy, Trello zip and Trello API import now writes an importRuns record first, with the id its new board will have; every creator inserts the board with that id and the run's id stamped on it. A heartbeat keeps the run alive; the method deadline now stops the writer at its next step. A scan at startup and every 5 minutes flags a stopped or failed run once and records it in Admin Panel -> Problems -> Recovery, where an administrator keeps the partial board or discards it: only the board carrying the run's stamp is removed, through the board's own removal, then whatever else has its id. Discard is refused while the import's Scrum stage is still written or recovered, and the Scrum resume is refused while the import is discarded. Resume is deliberately not offered: the source file is not kept, for its size, its personal data and the anonymization it would undo; discard and import again instead. Thanks to xet7 !
415 lines
13 KiB
JavaScript
415 lines
13 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { importedTableRows } from './lib/importedTableRows';
|
|
import { plannedBoardFields } from './lib/importPipeline';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import Activities from '/models/activities';
|
|
import Boards from './boards';
|
|
import Cards from '/models/cards';
|
|
import CustomFields from '/models/customFields';
|
|
import Lists from '/models/lists';
|
|
import Swimlanes from '/models/swimlanes';
|
|
|
|
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++) {
|
|
// #6620: a sparse/short CSV row (a trailing empty column some CSV
|
|
// parsers report as a hole rather than '') yields undefined here -
|
|
// guard so an odd header row is skipped instead of crashing the
|
|
// import with "toLowerCase is not a function".
|
|
const header = typeof headerRow[i] === 'string' ? headerRow[i] : '';
|
|
switch (header.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 (header.toLowerCase().startsWith('customfield')) {
|
|
if (headerRow[i].split('-')[2] === 'dropdown' || headerRow[i].split('-')[2] === 'dropdownMultiSelect') {
|
|
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;
|
|
}
|
|
async createCustomFields(boardId) {
|
|
for (const customField of this.fieldIndex.customFields) {
|
|
let settings = {};
|
|
if (customField.type === 'dropdown' || customField.type === 'dropdownMultiSelect') {
|
|
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 = await CustomFields.direct.insertAsync({
|
|
name: customField.name,
|
|
type: customField.type,
|
|
settings,
|
|
showOnCard: false,
|
|
automaticallyOnCard: false,
|
|
alwaysOnCard: false,
|
|
showLabelOnMiniCard: false,
|
|
boardIds: [boardId],
|
|
});
|
|
customField.id = id;
|
|
customField.settings = settings;
|
|
}
|
|
}
|
|
|
|
async createBoard(csvData) {
|
|
const boardToCreate = {
|
|
...plannedBoardFields(this),
|
|
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 = await Boards.direct.insertAsync(boardToCreate);
|
|
await Boards.direct.updateAsync(boardId, {
|
|
$set: {
|
|
modifiedAt: this._now(),
|
|
},
|
|
});
|
|
// log activity
|
|
await Activities.direct.insertAsync({
|
|
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;
|
|
}
|
|
|
|
async createSwimlanes(boardId) {
|
|
const swimlaneToCreate = {
|
|
archived: false,
|
|
boardId,
|
|
createdAt: this._now(),
|
|
title: 'Default',
|
|
sort: 1,
|
|
};
|
|
const swimlaneId = await Swimlanes.direct.insertAsync(swimlaneToCreate);
|
|
await Swimlanes.direct.updateAsync(swimlaneId, { $set: { updatedAt: this._now() } });
|
|
this.swimlane = swimlaneId;
|
|
}
|
|
|
|
async 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 = await ReactiveCache.getLists({
|
|
title: csvData[i][this.fieldIndex.stage],
|
|
boardId,
|
|
});
|
|
if (existingList.length > 0) {
|
|
continue;
|
|
} else {
|
|
listToCreate.title = csvData[i][this.fieldIndex.stage];
|
|
}
|
|
} else listToCreate.title = `Imported List ${this._now()}`;
|
|
|
|
const listId = await Lists.direct.insertAsync(listToCreate);
|
|
this.lists[csvData[i][this.fieldIndex.stage]] = listId;
|
|
numOfCreatedLists++;
|
|
await Lists.direct.updateAsync(listId, {
|
|
$set: {
|
|
updatedAt: this._now(),
|
|
sort: numOfCreatedLists,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
async createCards(csvData, boardId) {
|
|
for (let i = 1; i < csvData.length; i++) {
|
|
const cardToCreate = {
|
|
archived: false,
|
|
boardId,
|
|
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(),
|
|
spentTime: null,
|
|
labelIds: [],
|
|
};
|
|
// Date columns are optional: only set them when the column exists for this
|
|
// row and is non-empty (this.fieldIndex.<x> is undefined when the column is
|
|
// absent, so the cell lookup yields undefined).
|
|
const createdAtCell = csvData[i][this.fieldIndex.createdAt];
|
|
if (createdAtCell && createdAtCell.length !== 0) {
|
|
cardToCreate.createdAt = this._now(new Date(createdAtCell));
|
|
}
|
|
const startAtCell = csvData[i][this.fieldIndex.startAt];
|
|
if (startAtCell && startAtCell.length !== 0) {
|
|
cardToCreate.startAt = this._now(new Date(startAtCell));
|
|
}
|
|
const dueAtCell = csvData[i][this.fieldIndex.dueAt];
|
|
if (dueAtCell && dueAtCell.length !== 0) {
|
|
cardToCreate.dueAt = this._now(new Date(dueAtCell));
|
|
}
|
|
const endAtCell = csvData[i][this.fieldIndex.endAt];
|
|
if (endAtCell && endAtCell.length !== 0) {
|
|
cardToCreate.endAt = this._now(new Date(endAtCell));
|
|
}
|
|
const modifiedAtCell = csvData[i][this.fieldIndex.modifiedAt];
|
|
if (modifiedAtCell && modifiedAtCell.length !== 0) {
|
|
cardToCreate.modifiedAt = this._now(new Date(modifiedAtCell));
|
|
}
|
|
// add the labels
|
|
if (csvData[i][this.fieldIndex.labels]) {
|
|
const board = await ReactiveCache.getBoard(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;
|
|
});
|
|
}
|
|
await Cards.direct.insertAsync(cardToCreate);
|
|
}
|
|
}
|
|
|
|
async create(board, currentBoardId) {
|
|
board = importedTableRows(board);
|
|
const isSandstorm =
|
|
Meteor.settings &&
|
|
Meteor.settings.public &&
|
|
Meteor.settings.public.sandstorm;
|
|
if (isSandstorm && currentBoardId) {
|
|
const currentBoard = await ReactiveCache.getBoard(currentBoardId);
|
|
await currentBoard.archive();
|
|
}
|
|
this.mapHeadertoCardFieldIndex(board[0]);
|
|
const boardId = await this.createBoard(board);
|
|
await this.createLists(board, boardId);
|
|
await this.createSwimlanes(boardId);
|
|
await this.createCustomFields(boardId);
|
|
await this.createCards(board, boardId);
|
|
return boardId;
|
|
}
|
|
}
|