From 51f52fee23f4a71c8e89c62ef6223d9248fee1bd Mon Sep 17 00:00:00 2001 From: Bryan Mutai Date: Sun, 24 May 2020 17:48:49 +0300 Subject: enable exporting and importing custom fields. --- models/csvCreator.js | 102 +++++++++++++++++++++++++++++++++++++++++++-------- models/exporter.js | 79 +++++++++++++++++++++++++++++++-------- 2 files changed, 150 insertions(+), 31 deletions(-) (limited to 'models') diff --git a/models/csvCreator.js b/models/csvCreator.js index 025a3179..696d2205 100644 --- a/models/csvCreator.js +++ b/models/csvCreator.js @@ -50,6 +50,7 @@ export class CsvCreator { */ mapHeadertoCardFieldIndex(headerRow) { const index = {}; + index.customFields = []; for (let i = 0; i < headerRow.length; i++) { switch (headerRow[i].trim().toLowerCase()) { case 'title': @@ -98,9 +99,50 @@ export class CsvCreator { 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 { + 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 { + 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 = { @@ -228,9 +270,10 @@ export class CsvCreator { const cardToCreate = { archived: false, boardId, - createdAt: csvData[i][this.fieldIndex.createdAt] - ? this._now(new Date(csvData[i][this.fieldIndex.createdAt])) - : null, + 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]], @@ -238,20 +281,24 @@ export class CsvCreator { 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, + 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, + 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]) { @@ -290,7 +337,29 @@ export class CsvCreator { cardToCreate.members = wekanMembers; } } - Cards.direct.insert(cardToCreate); + // 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); + } } } @@ -307,6 +376,7 @@ export class CsvCreator { const boardId = this.createBoard(board); this.createLists(board, boardId); this.createSwimlanes(boardId); + this.createCustomFields(boardId); this.createCards(board, boardId); return boardId; } diff --git a/models/exporter.js b/models/exporter.js index 3fa8ca3a..cbcfa6cb 100644 --- a/models/exporter.js +++ b/models/exporter.js @@ -37,8 +37,8 @@ export class Exporter { result.cards = Cards.find(byBoardNoLinked, noBoardId).fetch(); result.swimlanes = Swimlanes.find(byBoard, noBoardId).fetch(); result.customFields = CustomFields.find( - { boardIds: { $in: [this.boardId] } }, - { fields: { boardId: 0 } }, + { boardIds: this.boardId }, + { fields: { boardIds: 0 } }, ).fetch(); result.comments = CardComments.find(byBoard, noBoardId).fetch(); result.activities = Activities.find(byBoard, noBoardId).fetch(); @@ -214,7 +214,29 @@ export class Exporter { 'Vote', 'Archived', ); - + const customFieldMap = {}; + let i = 0; + result.customFields.forEach(customField => { + customFieldMap[customField._id] = i; + 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 { + columnHeaders.push( + `CustomField-${customField.name}-${customField.type}`, + ); + } + i++; + }); /* TODO: Try to get translations working. These currently only bring English translations. TAPi18n.__('title'), @@ -290,21 +312,15 @@ export class Exporter { labels = `${labels + label.name}-${label.color} `; }); currentRow.push(labels.trim()); - currentRow.push(card.startAt ? moment(card.startAt).format('LLLL') : ' '); - currentRow.push(card.dueAt ? moment(card.dueAt).format('LLLL') : ' '); - currentRow.push(card.endAt ? moment(card.endAt).format('LLLL') : ' '); + 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.createdAt ? moment(card.createdAt).format('LLLL') : ' ', - ); - currentRow.push( - card.modifiedAt ? moment(card.modifiedAt).format('LLLL') : ' ', - ); - currentRow.push( - card.dateLastActivity - ? moment(card.dateLastActivity).format('LLLL') - : ' ', + card.dateLastActivity ? moment(card.dateLastActivity).format() : ' ', ); if (card.vote.question) { let positiveVoters = ''; @@ -331,6 +347,39 @@ export class Exporter { currentRow.push(' '); } currentRow.push(card.archived ? 'true' : 'false'); + //Custom fields + const customFieldValuesToPush = new Array(result.customFields.length); + card.customFields.forEach(field => { + 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(); -- cgit v1.2.3-1-g7c22