wekanCreator.js 31 KB

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