csvCreator.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import Boards from './boards';
  2. export class CsvCreator {
  3. constructor(data) {
  4. // date to be used for timestamps during import
  5. this._nowDate = new Date();
  6. // index to help keep track of what information a column stores
  7. // each row represents a card
  8. this.fieldIndex = {};
  9. this.lists = {};
  10. // Map of members using username => wekanid
  11. this.members = data.membersMapping ? data.membersMapping : {};
  12. this.swimlane = null;
  13. }
  14. /**
  15. * If dateString is provided,
  16. * return the Date it represents.
  17. * If not, will return the date when it was first called.
  18. * This is useful for us, as we want all import operations to
  19. * have the exact same date for easier later retrieval.
  20. *
  21. * @param {String} dateString a properly formatted Date
  22. */
  23. _now(dateString) {
  24. if (dateString) {
  25. return new Date(dateString);
  26. }
  27. if (!this._nowDate) {
  28. this._nowDate = new Date();
  29. }
  30. return this._nowDate;
  31. }
  32. _user(wekanUserId) {
  33. if (wekanUserId && this.members[wekanUserId]) {
  34. return this.members[wekanUserId];
  35. }
  36. return Meteor.userId();
  37. }
  38. /**
  39. * Map the header row titles to an index to help assign proper values to the cards' fields
  40. * Valid headers (name of card fields):
  41. * title, description, status, owner, member, label, due date, start date, finish date, created at, updated at
  42. * Some header aliases can also be accepted.
  43. * Headers are NOT case-sensitive.
  44. *
  45. * @param {Array} headerRow array from row of headers of imported CSV/TSV for cards
  46. */
  47. mapHeadertoCardFieldIndex(headerRow) {
  48. const index = {};
  49. for (let i = 0; i < headerRow.length; i++) {
  50. switch (headerRow[i].trim().toLowerCase()) {
  51. case 'title':
  52. index.title = i;
  53. break;
  54. case 'description':
  55. index.description = i;
  56. break;
  57. case 'stage':
  58. case 'status':
  59. case 'state':
  60. index.stage = i;
  61. break;
  62. case 'owner':
  63. index.owner = i;
  64. break;
  65. case 'members':
  66. case 'member':
  67. index.members = i;
  68. break;
  69. case 'labels':
  70. case 'label':
  71. index.labels = i;
  72. break;
  73. case 'due date':
  74. case 'deadline':
  75. case 'due at':
  76. index.dueAt = i;
  77. break;
  78. case 'start date':
  79. case 'start at':
  80. index.startAt = i;
  81. break;
  82. case 'finish date':
  83. case 'end at':
  84. index.endAt = i;
  85. break;
  86. case 'creation date':
  87. case 'created at':
  88. index.createdAt = i;
  89. break;
  90. case 'update date':
  91. case 'updated at':
  92. case 'modified at':
  93. case 'modified on':
  94. index.modifiedAt = i;
  95. break;
  96. }
  97. }
  98. this.fieldIndex = index;
  99. }
  100. createBoard(csvData) {
  101. const boardToCreate = {
  102. archived: false,
  103. color: 'belize',
  104. createdAt: this._now(),
  105. labels: [],
  106. members: [
  107. {
  108. userId: Meteor.userId(),
  109. wekanId: Meteor.userId(),
  110. isActive: true,
  111. isAdmin: true,
  112. isNoComments: false,
  113. isCommentOnly: false,
  114. swimlaneId: false,
  115. },
  116. ],
  117. modifiedAt: this._now(),
  118. //default is private, should inform user.
  119. permission: 'private',
  120. slug: 'board',
  121. stars: 0,
  122. title: `Imported Board ${this._now()}`,
  123. };
  124. // create labels
  125. const labelsToCreate = new Set();
  126. for (let i = 1; i < csvData.length; i++) {
  127. if (csvData[i][this.fieldIndex.labels]) {
  128. for (const importedLabel of csvData[i][this.fieldIndex.labels].split(
  129. ' ',
  130. )) {
  131. if (importedLabel && importedLabel.length > 0) {
  132. labelsToCreate.add(importedLabel);
  133. }
  134. }
  135. }
  136. }
  137. for (const label of labelsToCreate) {
  138. let labelName, labelColor;
  139. if (label.indexOf('-') > -1) {
  140. labelName = label.split('-')[0];
  141. labelColor = label.split('-')[1];
  142. } else {
  143. labelName = label;
  144. }
  145. const labelToCreate = {
  146. _id: Random.id(6),
  147. color: labelColor ? labelColor : 'black',
  148. name: labelName,
  149. };
  150. boardToCreate.labels.push(labelToCreate);
  151. }
  152. const boardId = Boards.direct.insert(boardToCreate);
  153. Boards.direct.update(boardId, {
  154. $set: {
  155. modifiedAt: this._now(),
  156. },
  157. });
  158. // log activity
  159. Activities.direct.insert({
  160. activityType: 'importBoard',
  161. boardId,
  162. createdAt: this._now(),
  163. source: {
  164. id: boardId,
  165. system: 'CSV/TSV',
  166. },
  167. // We attribute the import to current user,
  168. // not the author from the original object.
  169. userId: this._user(),
  170. });
  171. return boardId;
  172. }
  173. createSwimlanes(boardId) {
  174. const swimlaneToCreate = {
  175. archived: false,
  176. boardId,
  177. createdAt: this._now(),
  178. title: 'Default',
  179. sort: 1,
  180. };
  181. const swimlaneId = Swimlanes.direct.insert(swimlaneToCreate);
  182. Swimlanes.direct.update(swimlaneId, { $set: { updatedAt: this._now() } });
  183. this.swimlane = swimlaneId;
  184. }
  185. createLists(csvData, boardId) {
  186. let numOfCreatedLists = 0;
  187. for (let i = 1; i < csvData.length; i++) {
  188. const listToCreate = {
  189. archived: false,
  190. boardId,
  191. createdAt: this._now(),
  192. };
  193. if (csvData[i][this.fieldIndex.stage]) {
  194. const existingList = Lists.find({
  195. title: csvData[i][this.fieldIndex.stage],
  196. boardId,
  197. }).fetch();
  198. if (existingList.length > 0) {
  199. continue;
  200. } else {
  201. listToCreate.title = csvData[i][this.fieldIndex.stage];
  202. }
  203. } else listToCreate.title = `Imported List ${this._now()}`;
  204. const listId = Lists.direct.insert(listToCreate);
  205. this.lists[csvData[i][this.fieldIndex.stage]] = listId;
  206. numOfCreatedLists++;
  207. Lists.direct.update(listId, {
  208. $set: {
  209. updatedAt: this._now(),
  210. sort: numOfCreatedLists,
  211. },
  212. });
  213. }
  214. }
  215. createCards(csvData, boardId) {
  216. for (let i = 1; i < csvData.length; i++) {
  217. const cardToCreate = {
  218. archived: false,
  219. boardId,
  220. createdAt: csvData[i][this.fieldIndex.createdAt]
  221. ? this._now(new Date(csvData[i][this.fieldIndex.createdAt]))
  222. : null,
  223. dateLastActivity: this._now(),
  224. description: csvData[i][this.fieldIndex.description],
  225. listId: this.lists[csvData[i][this.fieldIndex.stage]],
  226. swimlaneId: this.swimlane,
  227. sort: -1,
  228. title: csvData[i][this.fieldIndex.title],
  229. userId: this._user(),
  230. startAt: csvData[i][this.fieldIndex.startAt]
  231. ? this._now(new Date(csvData[i][this.fieldIndex.startAt]))
  232. : null,
  233. dueAt: csvData[i][this.fieldIndex.dueAt]
  234. ? this._now(new Date(csvData[i][this.fieldIndex.dueAt]))
  235. : null,
  236. endAt: csvData[i][this.fieldIndex.endAt]
  237. ? this._now(new Date(csvData[i][this.fieldIndex.endAt]))
  238. : null,
  239. spentTime: null,
  240. labelIds: [],
  241. modifiedAt: csvData[i][this.fieldIndex.modifiedAt]
  242. ? this._now(new Date(csvData[i][this.fieldIndex.modifiedAt]))
  243. : null,
  244. };
  245. // add the labels
  246. if (csvData[i][this.fieldIndex.labels]) {
  247. const board = Boards.findOne(boardId);
  248. for (const importedLabel of csvData[i][this.fieldIndex.labels].split(
  249. ' ',
  250. )) {
  251. if (importedLabel && importedLabel.length > 0) {
  252. let labelToApply;
  253. if (importedLabel.indexOf('-') === -1) {
  254. labelToApply = board.getLabel(importedLabel, 'black');
  255. } else {
  256. labelToApply = board.getLabel(
  257. importedLabel.split('-')[0],
  258. importedLabel.split('-')[1],
  259. );
  260. }
  261. cardToCreate.labelIds.push(labelToApply._id);
  262. }
  263. }
  264. }
  265. // add the members
  266. if (csvData[i][this.fieldIndex.members]) {
  267. const wekanMembers = [];
  268. for (const importedMember of csvData[i][this.fieldIndex.members].split(
  269. ' ',
  270. )) {
  271. if (this.members[importedMember]) {
  272. const wekanId = this.members[importedMember];
  273. if (!wekanMembers.find(wId => wId === wekanId)) {
  274. wekanMembers.push(wekanId);
  275. }
  276. }
  277. }
  278. if (wekanMembers.length > 0) {
  279. cardToCreate.members = wekanMembers;
  280. }
  281. }
  282. Cards.direct.insert(cardToCreate);
  283. }
  284. }
  285. create(board, currentBoardId) {
  286. const isSandstorm =
  287. Meteor.settings &&
  288. Meteor.settings.public &&
  289. Meteor.settings.public.sandstorm;
  290. if (isSandstorm && currentBoardId) {
  291. const currentBoard = Boards.findOne(currentBoardId);
  292. currentBoard.archive();
  293. }
  294. this.mapHeadertoCardFieldIndex(board[0]);
  295. const boardId = this.createBoard(board);
  296. this.createLists(board, boardId);
  297. this.createSwimlanes(boardId);
  298. this.createCards(board, boardId);
  299. return boardId;
  300. }
  301. }