wekanCreator.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. const DateString = Match.Where(function(dateAsString) {
  2. check(dateAsString, String);
  3. return moment(dateAsString, moment.ISO_8601).isValid();
  4. });
  5. export class WekanCreator {
  6. constructor(data) {
  7. // we log current date, to use the same timestamp for all our actions.
  8. // this helps to retrieve all elements performed by the same import.
  9. this._nowDate = new Date();
  10. // The object creation dates, indexed by Wekan id
  11. // (so we only parse actions once!)
  12. this.createdAt = {
  13. board: null,
  14. cards: {},
  15. lists: {},
  16. swimlanes: {},
  17. customFields: {},
  18. };
  19. // The object creator Wekan Id, indexed by the object Wekan id
  20. // (so we only parse actions once!)
  21. this.createdBy = {
  22. cards: {}, // only cards have a field for that
  23. };
  24. // Map of labels Wekan ID => Wekan ID
  25. this.labels = {};
  26. // Map of swimlanes Wekan ID => Wekan ID
  27. this.swimlanes = {};
  28. // Map of lists Wekan ID => Wekan ID
  29. this.lists = {};
  30. // Map of cards Wekan ID => Wekan ID
  31. this.cards = {};
  32. // Map of custom fields Wekan ID => Wekan ID
  33. this.customFields = {};
  34. // Map of comments Wekan ID => Wekan ID
  35. this.commentIds = {};
  36. // Map of attachments Wekan ID => Wekan ID
  37. this.attachmentIds = {};
  38. // Map of checklists Wekan ID => Wekan ID
  39. this.checklists = {};
  40. // Map of checklistItems Wekan ID => Wekan ID
  41. this.checklistItems = {};
  42. // The comments, indexed by Wekan card id (to map when importing cards)
  43. this.comments = {};
  44. // Map of rules Wekan ID => Wekan ID
  45. this.rules = {};
  46. // the members, indexed by Wekan member id => Wekan user ID
  47. this.members = data.membersMapping ? data.membersMapping : {};
  48. // Map of triggers Wekan ID => Wekan ID
  49. this.triggers = {};
  50. // Map of actions Wekan ID => Wekan ID
  51. this.actions = {};
  52. // maps a wekanCardId to an array of wekanAttachments
  53. this.attachments = {};
  54. }
  55. /**
  56. * If dateString is provided,
  57. * return the Date it represents.
  58. * If not, will return the date when it was first called.
  59. * This is useful for us, as we want all import operations to
  60. * have the exact same date for easier later retrieval.
  61. *
  62. * @param {String} dateString a properly formatted Date
  63. */
  64. _now(dateString) {
  65. if (dateString) {
  66. return new Date(dateString);
  67. }
  68. if (!this._nowDate) {
  69. this._nowDate = new Date();
  70. }
  71. return this._nowDate;
  72. }
  73. /**
  74. * if wekanUserId is provided and we have a mapping,
  75. * return it.
  76. * Otherwise return current logged user.
  77. * @param wekanUserId
  78. * @private
  79. */
  80. _user(wekanUserId) {
  81. if (wekanUserId && this.members[wekanUserId]) {
  82. return this.members[wekanUserId];
  83. }
  84. return Meteor.userId();
  85. }
  86. checkActivities(wekanActivities) {
  87. check(wekanActivities, [
  88. Match.ObjectIncluding({
  89. activityType: String,
  90. createdAt: DateString,
  91. }),
  92. ]);
  93. // XXX we could perform more thorough checks based on action type
  94. }
  95. checkBoard(wekanBoard) {
  96. check(
  97. wekanBoard,
  98. Match.ObjectIncluding({
  99. archived: Boolean,
  100. title: String,
  101. // XXX refine control by validating 'color' against a list of
  102. // allowed values (is it worth the maintenance?)
  103. color: String,
  104. permission: Match.Where(value => {
  105. return ['private', 'public'].indexOf(value) >= 0;
  106. }),
  107. }),
  108. );
  109. }
  110. checkCards(wekanCards) {
  111. check(wekanCards, [
  112. Match.ObjectIncluding({
  113. archived: Boolean,
  114. dateLastActivity: DateString,
  115. labelIds: [String],
  116. title: String,
  117. sort: Number,
  118. }),
  119. ]);
  120. }
  121. checkLabels(wekanLabels) {
  122. check(wekanLabels, [
  123. Match.ObjectIncluding({
  124. // XXX refine control by validating 'color' against a list of allowed
  125. // values (is it worth the maintenance?)
  126. color: String,
  127. }),
  128. ]);
  129. }
  130. checkLists(wekanLists) {
  131. check(wekanLists, [
  132. Match.ObjectIncluding({
  133. archived: Boolean,
  134. title: String,
  135. }),
  136. ]);
  137. }
  138. checkSwimlanes(wekanSwimlanes) {
  139. check(wekanSwimlanes, [
  140. Match.ObjectIncluding({
  141. archived: Boolean,
  142. title: String,
  143. }),
  144. ]);
  145. }
  146. checkChecklists(wekanChecklists) {
  147. check(wekanChecklists, [
  148. Match.ObjectIncluding({
  149. cardId: String,
  150. title: String,
  151. }),
  152. ]);
  153. }
  154. checkChecklistItems(wekanChecklistItems) {
  155. check(wekanChecklistItems, [
  156. Match.ObjectIncluding({
  157. cardId: String,
  158. title: String,
  159. }),
  160. ]);
  161. }
  162. checkRules(wekanRules) {
  163. check(wekanRules, [
  164. Match.ObjectIncluding({
  165. triggerId: String,
  166. actionId: String,
  167. title: String,
  168. }),
  169. ]);
  170. }
  171. checkTriggers(wekanTriggers) {
  172. // XXX More check based on trigger type
  173. check(wekanTriggers, [
  174. Match.ObjectIncluding({
  175. activityType: String,
  176. desc: String,
  177. }),
  178. ]);
  179. }
  180. getMembersToMap(data) {
  181. // we will work on the list itself (an ordered array of objects) when a
  182. // mapping is done, we add a 'wekan' field to the object representing the
  183. // imported member
  184. const membersToMap = data.members;
  185. const users = data.users;
  186. // auto-map based on username
  187. membersToMap.forEach(importedMember => {
  188. importedMember.id = importedMember.userId;
  189. delete importedMember.userId;
  190. const user = users.filter(user => {
  191. return user._id === importedMember.id;
  192. })[0];
  193. if (user.profile && user.profile.fullname) {
  194. importedMember.fullName = user.profile.fullname;
  195. }
  196. importedMember.username = user.username;
  197. const wekanUser = Users.findOne({ username: importedMember.username });
  198. if (wekanUser) {
  199. importedMember.wekanId = wekanUser._id;
  200. }
  201. });
  202. return membersToMap;
  203. }
  204. checkActions(wekanActions) {
  205. // XXX More check based on action type
  206. check(wekanActions, [
  207. Match.ObjectIncluding({
  208. actionType: String,
  209. desc: String,
  210. }),
  211. ]);
  212. }
  213. // You must call parseActions before calling this one.
  214. createBoardAndLabels(boardToImport) {
  215. const boardToCreate = {
  216. archived: boardToImport.archived,
  217. color: boardToImport.color,
  218. // very old boards won't have a creation activity so no creation date
  219. createdAt: this._now(boardToImport.createdAt),
  220. labels: [],
  221. members: [
  222. {
  223. userId: Meteor.userId(),
  224. wekanId: Meteor.userId(),
  225. isActive: true,
  226. isAdmin: true,
  227. isNoComments: false,
  228. isCommentOnly: false,
  229. swimlaneId: false,
  230. },
  231. ],
  232. presentParentTask: boardToImport.presentParentTask,
  233. // Standalone Export has modifiedAt missing, adding modifiedAt to fix it
  234. modifiedAt: this._now(boardToImport.modifiedAt),
  235. permission: boardToImport.permission,
  236. slug: getSlug(boardToImport.title) || 'board',
  237. stars: 0,
  238. title: Boards.uniqueTitle(boardToImport.title),
  239. };
  240. // now add other members
  241. if (boardToImport.members) {
  242. boardToImport.members.forEach(wekanMember => {
  243. // is it defined and do we already have it in our list?
  244. if (
  245. wekanMember.wekanId &&
  246. !boardToCreate.members.some(
  247. member => member.wekanId === wekanMember.wekanId,
  248. )
  249. )
  250. boardToCreate.members.push({
  251. ...wekanMember,
  252. userId: wekanMember.wekanId,
  253. });
  254. });
  255. }
  256. boardToImport.labels.forEach(label => {
  257. const labelToCreate = {
  258. _id: Random.id(6),
  259. color: label.color,
  260. name: label.name,
  261. };
  262. // We need to remember them by Wekan ID, as this is the only ref we have
  263. // when importing cards.
  264. this.labels[label._id] = labelToCreate._id;
  265. boardToCreate.labels.push(labelToCreate);
  266. });
  267. const boardId = Boards.direct.insert(boardToCreate);
  268. Boards.direct.update(boardId, {
  269. $set: {
  270. modifiedAt: this._now(),
  271. },
  272. });
  273. // log activity
  274. Activities.direct.insert({
  275. activityType: 'importBoard',
  276. boardId,
  277. createdAt: this._now(),
  278. source: {
  279. id: boardToImport.id,
  280. system: 'Wekan',
  281. },
  282. // We attribute the import to current user,
  283. // not the author from the original object.
  284. userId: this._user(),
  285. });
  286. return boardId;
  287. }
  288. /**
  289. * Create the Wekan cards corresponding to the supplied Wekan cards,
  290. * as well as all linked data: activities, comments, and attachments
  291. * @param wekanCards
  292. * @param boardId
  293. * @returns {Array}
  294. */
  295. createCards(wekanCards, boardId) {
  296. const result = [];
  297. wekanCards.forEach(card => {
  298. const cardToCreate = {
  299. archived: card.archived,
  300. boardId,
  301. // very old boards won't have a creation activity so no creation date
  302. createdAt: this._now(this.createdAt.cards[card._id]),
  303. dateLastActivity: this._now(),
  304. description: card.description,
  305. listId: this.lists[card.listId],
  306. swimlaneId: this.swimlanes[card.swimlaneId],
  307. sort: card.sort,
  308. title: card.title,
  309. // we attribute the card to its creator if available
  310. userId: this._user(this.createdBy.cards[card._id]),
  311. isOvertime: card.isOvertime || false,
  312. startAt: card.startAt ? this._now(card.startAt) : null,
  313. dueAt: card.dueAt ? this._now(card.dueAt) : null,
  314. spentTime: card.spentTime || null,
  315. };
  316. // add labels
  317. if (card.labelIds) {
  318. cardToCreate.labelIds = card.labelIds.map(wekanId => {
  319. return this.labels[wekanId];
  320. });
  321. }
  322. // add members {
  323. if (card.members) {
  324. const wekanMembers = [];
  325. // we can't just map, as some members may not have been mapped
  326. card.members.forEach(sourceMemberId => {
  327. if (this.members[sourceMemberId]) {
  328. const wekanId = this.members[sourceMemberId];
  329. // we may map multiple Wekan members to the same wekan user
  330. // in which case we risk adding the same user multiple times
  331. if (!wekanMembers.find(wId => wId === wekanId)) {
  332. wekanMembers.push(wekanId);
  333. }
  334. }
  335. return true;
  336. });
  337. if (wekanMembers.length > 0) {
  338. cardToCreate.members = wekanMembers;
  339. }
  340. }
  341. // add assignees
  342. if (card.assignees) {
  343. const wekanAssignees = [];
  344. // we can't just map, as some members may not have been mapped
  345. card.assignees.forEach(sourceMemberId => {
  346. if (this.members[sourceMemberId]) {
  347. const wekanId = this.members[sourceMemberId];
  348. // we may map multiple Wekan members to the same wekan user
  349. // in which case we risk adding the same user multiple times
  350. if (!wekanAssignees.find(wId => wId === wekanId)) {
  351. wekanAssignees.push(wekanId);
  352. }
  353. }
  354. return true;
  355. });
  356. if (wekanAssignees.length > 0) {
  357. cardToCreate.assignees = wekanAssignees;
  358. }
  359. }
  360. // set color
  361. if (card.color) {
  362. cardToCreate.color = card.color;
  363. }
  364. // add custom fields
  365. if (card.customFields) {
  366. cardToCreate.customFields = card.customFields.map(field => {
  367. return {
  368. _id: this.customFields[field._id],
  369. value: field.value,
  370. };
  371. });
  372. }
  373. // insert card
  374. const cardId = Cards.direct.insert(cardToCreate);
  375. // keep track of Wekan id => Wekan id
  376. this.cards[card._id] = cardId;
  377. // // log activity
  378. // Activities.direct.insert({
  379. // activityType: 'importCard',
  380. // boardId,
  381. // cardId,
  382. // createdAt: this._now(),
  383. // listId: cardToCreate.listId,
  384. // source: {
  385. // id: card._id,
  386. // system: 'Wekan',
  387. // },
  388. // // we attribute the import to current user,
  389. // // not the author of the original card
  390. // userId: this._user(),
  391. // });
  392. // add comments
  393. const comments = this.comments[card._id];
  394. if (comments) {
  395. comments.forEach(comment => {
  396. const commentToCreate = {
  397. boardId,
  398. cardId,
  399. createdAt: this._now(comment.createdAt),
  400. text: comment.text,
  401. // we attribute the comment to the original author, default to current user
  402. userId: this._user(comment.userId),
  403. };
  404. // dateLastActivity will be set from activity insert, no need to
  405. // update it ourselves
  406. const commentId = CardComments.direct.insert(commentToCreate);
  407. this.commentIds[comment._id] = commentId;
  408. // Activities.direct.insert({
  409. // activityType: 'addComment',
  410. // boardId: commentToCreate.boardId,
  411. // cardId: commentToCreate.cardId,
  412. // commentId,
  413. // createdAt: this._now(commentToCreate.createdAt),
  414. // // we attribute the addComment (not the import)
  415. // // to the original author - it is needed by some UI elements.
  416. // userId: commentToCreate.userId,
  417. // });
  418. });
  419. }
  420. const attachments = this.attachments[card._id];
  421. const wekanCoverId = card.coverId;
  422. if (attachments) {
  423. attachments.forEach(att => {
  424. const file = new FS.File();
  425. // Simulating file.attachData on the client generates multiple errors
  426. // - HEAD returns null, which causes exception down the line
  427. // - the template then tries to display the url to the attachment which causes other errors
  428. // so we make it server only, and let UI catch up once it is done, forget about latency comp.
  429. const self = this;
  430. if (Meteor.isServer) {
  431. if (att.url) {
  432. file.attachData(att.url, function(error) {
  433. file.boardId = boardId;
  434. file.cardId = cardId;
  435. file.userId = self._user(att.userId);
  436. // The field source will only be used to prevent adding
  437. // attachments' related activities automatically
  438. file.source = 'import';
  439. if (error) {
  440. throw error;
  441. } else {
  442. const wekanAtt = Attachments.insert(file, () => {
  443. // we do nothing
  444. });
  445. self.attachmentIds[att._id] = wekanAtt._id;
  446. //
  447. if (wekanCoverId === att._id) {
  448. Cards.direct.update(cardId, {
  449. $set: {
  450. coverId: wekanAtt._id,
  451. },
  452. });
  453. }
  454. }
  455. });
  456. } else if (att.file) {
  457. //If attribute type is null or empty string is set, assume binary stream
  458. att.type =
  459. !att.type || att.type.trim().length === 0
  460. ? 'application/octet-stream'
  461. : att.type;
  462. file.attachData(
  463. Buffer.from(att.file, 'base64'),
  464. {
  465. type: att.type,
  466. },
  467. error => {
  468. file.name(att.name);
  469. file.boardId = boardId;
  470. file.cardId = cardId;
  471. file.userId = self._user(att.userId);
  472. // The field source will only be used to prevent adding
  473. // attachments' related activities automatically
  474. file.source = 'import';
  475. if (error) {
  476. throw error;
  477. } else {
  478. const wekanAtt = Attachments.insert(file, () => {
  479. // we do nothing
  480. });
  481. this.attachmentIds[att._id] = wekanAtt._id;
  482. //
  483. if (wekanCoverId === att._id) {
  484. Cards.direct.update(cardId, {
  485. $set: {
  486. coverId: wekanAtt._id,
  487. },
  488. });
  489. }
  490. }
  491. },
  492. );
  493. }
  494. }
  495. // todo XXX set cover - if need be
  496. });
  497. }
  498. result.push(cardId);
  499. });
  500. return result;
  501. }
  502. /**
  503. * Create the Wekan custom fields corresponding to the supplied Wekan
  504. * custom fields.
  505. * @param wekanCustomFields
  506. * @param boardId
  507. */
  508. createCustomFields(wekanCustomFields, boardId) {
  509. wekanCustomFields.forEach((field, fieldIndex) => {
  510. const fieldToCreate = {
  511. boardIds: [boardId],
  512. name: field.name,
  513. type: field.type,
  514. settings: field.settings,
  515. showOnCard: field.showOnCard,
  516. showLabelOnMiniCard: field.showLabelOnMiniCard,
  517. automaticallyOnCard: field.automaticallyOnCard,
  518. alwaysOnCard: field.alwaysOnCard,
  519. //use date "now" if now created at date is provided (e.g. for very old boards)
  520. createdAt: this._now(this.createdAt.customFields[field._id]),
  521. modifiedAt: field.modifiedAt,
  522. };
  523. //insert copy of custom field
  524. const fieldId = CustomFields.direct.insert(fieldToCreate);
  525. //set modified date to now
  526. CustomFields.direct.update(fieldId, {
  527. $set: {
  528. modifiedAt: this._now(),
  529. },
  530. });
  531. //store mapping of old id to new id
  532. this.customFields[field._id] = fieldId;
  533. });
  534. }
  535. // Create labels if they do not exist and load this.labels.
  536. createLabels(wekanLabels, board) {
  537. wekanLabels.forEach(label => {
  538. const color = label.color;
  539. const name = label.name;
  540. const existingLabel = board.getLabel(name, color);
  541. if (existingLabel) {
  542. this.labels[label.id] = existingLabel._id;
  543. } else {
  544. const idLabelCreated = board.pushLabel(name, color);
  545. this.labels[label.id] = idLabelCreated;
  546. }
  547. });
  548. }
  549. createLists(wekanLists, boardId) {
  550. wekanLists.forEach((list, listIndex) => {
  551. const listToCreate = {
  552. archived: list.archived,
  553. boardId,
  554. // We are being defensing here by providing a default date (now) if the
  555. // creation date wasn't found on the action log. This happen on old
  556. // Wekan boards (eg from 2013) that didn't log the 'createList' action
  557. // we require.
  558. createdAt: this._now(this.createdAt.lists[list.id]),
  559. title: list.title,
  560. sort: list.sort ? list.sort : listIndex,
  561. };
  562. const listId = Lists.direct.insert(listToCreate);
  563. Lists.direct.update(listId, {
  564. $set: {
  565. updatedAt: this._now(),
  566. },
  567. });
  568. this.lists[list._id] = listId;
  569. // // log activity
  570. // Activities.direct.insert({
  571. // activityType: 'importList',
  572. // boardId,
  573. // createdAt: this._now(),
  574. // listId,
  575. // source: {
  576. // id: list._id,
  577. // system: 'Wekan',
  578. // },
  579. // // We attribute the import to current user,
  580. // // not the creator of the original object
  581. // userId: this._user(),
  582. // });
  583. });
  584. }
  585. createSwimlanes(wekanSwimlanes, boardId) {
  586. wekanSwimlanes.forEach((swimlane, swimlaneIndex) => {
  587. const swimlaneToCreate = {
  588. archived: swimlane.archived,
  589. boardId,
  590. // We are being defensing here by providing a default date (now) if the
  591. // creation date wasn't found on the action log. This happen on old
  592. // Wekan boards (eg from 2013) that didn't log the 'createList' action
  593. // we require.
  594. createdAt: this._now(this.createdAt.swimlanes[swimlane._id]),
  595. title: swimlane.title,
  596. sort: swimlane.sort ? swimlane.sort : swimlaneIndex,
  597. };
  598. // set color
  599. if (swimlane.color) {
  600. swimlaneToCreate.color = swimlane.color;
  601. }
  602. const swimlaneId = Swimlanes.direct.insert(swimlaneToCreate);
  603. Swimlanes.direct.update(swimlaneId, {
  604. $set: {
  605. updatedAt: this._now(),
  606. },
  607. });
  608. this.swimlanes[swimlane._id] = swimlaneId;
  609. });
  610. }
  611. createSubtasks(wekanCards) {
  612. wekanCards.forEach(card => {
  613. // get new id of card (in created / new board)
  614. const cardIdInNewBoard = this.cards[card._id];
  615. //If there is a mapped parent card, use the mapped card
  616. // this means, the card and parent were in the same source board
  617. //If there is no mapped parent card, use the original parent id,
  618. // this should handle cases where source and parent are in different boards
  619. // Note: This can only handle board cloning (within the same wekan instance).
  620. // When importing boards between instances the IDs are definitely
  621. // lost if source and parent are two different boards
  622. // This is not the place to fix it, the entire subtask system needs to be rethought there.
  623. const parentIdInNewBoard = this.cards[card.parentId]
  624. ? this.cards[card.parentId]
  625. : card.parentId;
  626. //if the parent card exists, proceed
  627. if (Cards.findOne(parentIdInNewBoard)) {
  628. //set parent id of the card in the new board to the new id of the parent
  629. Cards.direct.update(cardIdInNewBoard, {
  630. $set: {
  631. parentId: parentIdInNewBoard,
  632. },
  633. });
  634. }
  635. });
  636. }
  637. createChecklists(wekanChecklists) {
  638. const result = [];
  639. wekanChecklists.forEach((checklist, checklistIndex) => {
  640. // Create the checklist
  641. const checklistToCreate = {
  642. cardId: this.cards[checklist.cardId],
  643. title: checklist.title,
  644. createdAt: checklist.createdAt,
  645. sort: checklist.sort ? checklist.sort : checklistIndex,
  646. };
  647. const checklistId = Checklists.direct.insert(checklistToCreate);
  648. this.checklists[checklist._id] = checklistId;
  649. result.push(checklistId);
  650. });
  651. return result;
  652. }
  653. createTriggers(wekanTriggers, boardId) {
  654. wekanTriggers.forEach(trigger => {
  655. if (trigger.hasOwnProperty('labelId')) {
  656. trigger.labelId = this.labels[trigger.labelId];
  657. }
  658. if (trigger.hasOwnProperty('memberId')) {
  659. trigger.memberId = this.members[trigger.memberId];
  660. }
  661. trigger.boardId = boardId;
  662. const oldId = trigger._id;
  663. delete trigger._id;
  664. this.triggers[oldId] = Triggers.direct.insert(trigger);
  665. });
  666. }
  667. createActions(wekanActions, boardId) {
  668. wekanActions.forEach(action => {
  669. if (action.hasOwnProperty('labelId')) {
  670. action.labelId = this.labels[action.labelId];
  671. }
  672. if (action.hasOwnProperty('memberId')) {
  673. action.memberId = this.members[action.memberId];
  674. }
  675. action.boardId = boardId;
  676. const oldId = action._id;
  677. delete action._id;
  678. this.actions[oldId] = Actions.direct.insert(action);
  679. });
  680. }
  681. createRules(wekanRules, boardId) {
  682. wekanRules.forEach(rule => {
  683. // Create the rule
  684. rule.boardId = boardId;
  685. rule.triggerId = this.triggers[rule.triggerId];
  686. rule.actionId = this.actions[rule.actionId];
  687. delete rule._id;
  688. Rules.direct.insert(rule);
  689. });
  690. }
  691. createChecklistItems(wekanChecklistItems) {
  692. wekanChecklistItems.forEach((checklistitem, checklistitemIndex) => {
  693. //Check if the checklist for this item (still) exists
  694. //If a checklist was deleted, but items remain, the import would error out here
  695. //Leading to no further checklist items being imported
  696. if (this.checklists[checklistitem.checklistId]) {
  697. // Create the checklistItem
  698. const checklistItemTocreate = {
  699. title: checklistitem.title,
  700. checklistId: this.checklists[checklistitem.checklistId],
  701. cardId: this.cards[checklistitem.cardId],
  702. sort: checklistitem.sort ? checklistitem.sort : checklistitemIndex,
  703. isFinished: checklistitem.isFinished,
  704. };
  705. const checklistItemId = ChecklistItems.direct.insert(
  706. checklistItemTocreate,
  707. );
  708. this.checklistItems[checklistitem._id] = checklistItemId;
  709. }
  710. });
  711. }
  712. parseActivities(wekanBoard) {
  713. wekanBoard.activities.forEach(activity => {
  714. switch (activity.activityType) {
  715. case 'addAttachment': {
  716. // We have to be cautious, because the attachment could have been removed later.
  717. // In that case Wekan still reports its addition, but removes its 'url' field.
  718. // So we test for that
  719. const wekanAttachment = wekanBoard.attachments.filter(attachment => {
  720. return attachment._id === activity.attachmentId;
  721. })[0];
  722. if (typeof wekanAttachment !== 'undefined' && wekanAttachment) {
  723. if (wekanAttachment.url || wekanAttachment.file) {
  724. // we cannot actually create the Wekan attachment, because we don't yet
  725. // have the cards to attach it to, so we store it in the instance variable.
  726. const wekanCardId = activity.cardId;
  727. if (!this.attachments[wekanCardId]) {
  728. this.attachments[wekanCardId] = [];
  729. }
  730. this.attachments[wekanCardId].push(wekanAttachment);
  731. }
  732. }
  733. break;
  734. }
  735. case 'addComment': {
  736. const wekanComment = wekanBoard.comments.filter(comment => {
  737. return comment._id === activity.commentId;
  738. })[0];
  739. const id = activity.cardId;
  740. if (!this.comments[id]) {
  741. this.comments[id] = [];
  742. }
  743. this.comments[id].push(wekanComment);
  744. break;
  745. }
  746. case 'createBoard': {
  747. this.createdAt.board = activity.createdAt;
  748. break;
  749. }
  750. case 'createCard': {
  751. const cardId = activity.cardId;
  752. this.createdAt.cards[cardId] = activity.createdAt;
  753. this.createdBy.cards[cardId] = activity.userId;
  754. break;
  755. }
  756. case 'createList': {
  757. const listId = activity.listId;
  758. this.createdAt.lists[listId] = activity.createdAt;
  759. break;
  760. }
  761. case 'createSwimlane': {
  762. const swimlaneId = activity.swimlaneId;
  763. this.createdAt.swimlanes[swimlaneId] = activity.createdAt;
  764. break;
  765. }
  766. case 'createCustomField': {
  767. const customFieldId = activity.customFieldId;
  768. this.createdAt.customFields[customFieldId] = activity.createdAt;
  769. break;
  770. }
  771. }
  772. });
  773. }
  774. importActivities(activities, boardId) {
  775. activities.forEach(activity => {
  776. switch (activity.activityType) {
  777. // Board related activities
  778. // TODO: addBoardMember, removeBoardMember
  779. case 'createBoard': {
  780. Activities.direct.insert({
  781. userId: this._user(activity.userId),
  782. type: 'board',
  783. activityTypeId: boardId,
  784. activityType: activity.activityType,
  785. boardId,
  786. createdAt: this._now(activity.createdAt),
  787. });
  788. break;
  789. }
  790. // List related activities
  791. // TODO: removeList, archivedList
  792. case 'createList': {
  793. Activities.direct.insert({
  794. userId: this._user(activity.userId),
  795. type: 'list',
  796. activityType: activity.activityType,
  797. listId: this.lists[activity.listId],
  798. boardId,
  799. createdAt: this._now(activity.createdAt),
  800. });
  801. break;
  802. }
  803. // Card related activities
  804. // TODO: archivedCard, restoredCard, joinMember, unjoinMember
  805. case 'createCard': {
  806. Activities.direct.insert({
  807. userId: this._user(activity.userId),
  808. activityType: activity.activityType,
  809. listId: this.lists[activity.listId],
  810. cardId: this.cards[activity.cardId],
  811. boardId,
  812. createdAt: this._now(activity.createdAt),
  813. });
  814. break;
  815. }
  816. case 'moveCard': {
  817. Activities.direct.insert({
  818. userId: this._user(activity.userId),
  819. oldListId: this.lists[activity.oldListId],
  820. activityType: activity.activityType,
  821. listId: this.lists[activity.listId],
  822. cardId: this.cards[activity.cardId],
  823. boardId,
  824. createdAt: this._now(activity.createdAt),
  825. });
  826. break;
  827. }
  828. // Comment related activities
  829. case 'addComment': {
  830. Activities.direct.insert({
  831. userId: this._user(activity.userId),
  832. activityType: activity.activityType,
  833. cardId: this.cards[activity.cardId],
  834. commentId: this.commentIds[activity.commentId],
  835. boardId,
  836. createdAt: this._now(activity.createdAt),
  837. });
  838. break;
  839. }
  840. // Attachment related activities
  841. case 'addAttachment': {
  842. Activities.direct.insert({
  843. userId: this._user(activity.userId),
  844. type: 'card',
  845. activityType: activity.activityType,
  846. attachmentId: this.attachmentIds[activity.attachmentId],
  847. cardId: this.cards[activity.cardId],
  848. boardId,
  849. createdAt: this._now(activity.createdAt),
  850. });
  851. break;
  852. }
  853. // Checklist related activities
  854. case 'addChecklist': {
  855. Activities.direct.insert({
  856. userId: this._user(activity.userId),
  857. activityType: activity.activityType,
  858. cardId: this.cards[activity.cardId],
  859. checklistId: this.checklists[activity.checklistId],
  860. boardId,
  861. createdAt: this._now(activity.createdAt),
  862. });
  863. break;
  864. }
  865. case 'addChecklistItem': {
  866. Activities.direct.insert({
  867. userId: this._user(activity.userId),
  868. activityType: activity.activityType,
  869. cardId: this.cards[activity.cardId],
  870. checklistId: this.checklists[activity.checklistId],
  871. checklistItemId: activity.checklistItemId.replace(
  872. activity.checklistId,
  873. this.checklists[activity.checklistId],
  874. ),
  875. boardId,
  876. createdAt: this._now(activity.createdAt),
  877. });
  878. break;
  879. }
  880. }
  881. });
  882. }
  883. //check(board) {
  884. check() {
  885. //try {
  886. // check(data, {
  887. // membersMapping: Match.Optional(Object),
  888. // });
  889. // this.checkActivities(board.activities);
  890. // this.checkBoard(board);
  891. // this.checkLabels(board.labels);
  892. // this.checkLists(board.lists);
  893. // this.checkSwimlanes(board.swimlanes);
  894. // this.checkCards(board.cards);
  895. //this.checkChecklists(board.checklists);
  896. // this.checkRules(board.rules);
  897. // this.checkActions(board.actions);
  898. //this.checkTriggers(board.triggers);
  899. //this.checkChecklistItems(board.checklistItems);
  900. //} catch (e) {
  901. // throw new Meteor.Error('error-json-schema');
  902. // }
  903. }
  904. create(board, currentBoardId) {
  905. // TODO : Make isSandstorm variable global
  906. const isSandstorm =
  907. Meteor.settings &&
  908. Meteor.settings.public &&
  909. Meteor.settings.public.sandstorm;
  910. if (isSandstorm && currentBoardId) {
  911. const currentBoard = Boards.findOne(currentBoardId);
  912. currentBoard.archive();
  913. }
  914. this.parseActivities(board);
  915. const boardId = this.createBoardAndLabels(board);
  916. this.createLists(board.lists, boardId);
  917. this.createSwimlanes(board.swimlanes, boardId);
  918. this.createCustomFields(board.customFields, boardId);
  919. this.createCards(board.cards, boardId);
  920. this.createSubtasks(board.cards);
  921. this.createChecklists(board.checklists);
  922. this.createChecklistItems(board.checklistItems);
  923. this.importActivities(board.activities, boardId);
  924. this.createTriggers(board.triggers, boardId);
  925. this.createActions(board.actions, boardId);
  926. this.createRules(board.rules, boardId);
  927. // XXX add members
  928. return boardId;
  929. }
  930. }