wekanCreator.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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: boardToImport.title,
  239. };
  240. // now add other members
  241. if (boardToImport.members) {
  242. boardToImport.members.forEach(wekanMember => {
  243. // do we already have it in our list?
  244. if (
  245. !boardToCreate.members.some(
  246. member => member.wekanId === wekanMember.wekanId,
  247. )
  248. )
  249. boardToCreate.members.push({
  250. ...wekanMember,
  251. userId: wekanMember.wekanId,
  252. });
  253. });
  254. }
  255. boardToImport.labels.forEach(label => {
  256. const labelToCreate = {
  257. _id: Random.id(6),
  258. color: label.color,
  259. name: label.name,
  260. };
  261. // We need to remember them by Wekan ID, as this is the only ref we have
  262. // when importing cards.
  263. this.labels[label._id] = labelToCreate._id;
  264. boardToCreate.labels.push(labelToCreate);
  265. });
  266. const boardId = Boards.direct.insert(boardToCreate);
  267. Boards.direct.update(boardId, {
  268. $set: {
  269. modifiedAt: this._now(),
  270. },
  271. });
  272. // log activity
  273. Activities.direct.insert({
  274. activityType: 'importBoard',
  275. boardId,
  276. createdAt: this._now(),
  277. source: {
  278. id: boardToImport.id,
  279. system: 'Wekan',
  280. },
  281. // We attribute the import to current user,
  282. // not the author from the original object.
  283. userId: this._user(),
  284. });
  285. return boardId;
  286. }
  287. /**
  288. * Create the Wekan cards corresponding to the supplied Wekan cards,
  289. * as well as all linked data: activities, comments, and attachments
  290. * @param wekanCards
  291. * @param boardId
  292. * @returns {Array}
  293. */
  294. createCards(wekanCards, boardId) {
  295. const result = [];
  296. wekanCards.forEach(card => {
  297. const cardToCreate = {
  298. archived: card.archived,
  299. boardId,
  300. // very old boards won't have a creation activity so no creation date
  301. createdAt: this._now(this.createdAt.cards[card._id]),
  302. dateLastActivity: this._now(),
  303. description: card.description,
  304. listId: this.lists[card.listId],
  305. swimlaneId: this.swimlanes[card.swimlaneId],
  306. sort: card.sort,
  307. title: card.title,
  308. // we attribute the card to its creator if available
  309. userId: this._user(this.createdBy.cards[card._id]),
  310. isOvertime: card.isOvertime || false,
  311. startAt: card.startAt ? this._now(card.startAt) : null,
  312. dueAt: card.dueAt ? this._now(card.dueAt) : null,
  313. spentTime: card.spentTime || null,
  314. };
  315. // add labels
  316. if (card.labelIds) {
  317. cardToCreate.labelIds = card.labelIds.map(wekanId => {
  318. return this.labels[wekanId];
  319. });
  320. }
  321. // add members {
  322. if (card.members) {
  323. const wekanMembers = [];
  324. // we can't just map, as some members may not have been mapped
  325. card.members.forEach(sourceMemberId => {
  326. if (this.members[sourceMemberId]) {
  327. const wekanId = this.members[sourceMemberId];
  328. // we may map multiple Wekan members to the same wekan user
  329. // in which case we risk adding the same user multiple times
  330. if (!wekanMembers.find(wId => wId === wekanId)) {
  331. wekanMembers.push(wekanId);
  332. }
  333. }
  334. return true;
  335. });
  336. if (wekanMembers.length > 0) {
  337. cardToCreate.members = wekanMembers;
  338. }
  339. }
  340. // add assignees
  341. if (card.assignees) {
  342. const wekanAssignees = [];
  343. // we can't just map, as some members may not have been mapped
  344. card.assignees.forEach(sourceMemberId => {
  345. if (this.members[sourceMemberId]) {
  346. const wekanId = this.members[sourceMemberId];
  347. // we may map multiple Wekan members to the same wekan user
  348. // in which case we risk adding the same user multiple times
  349. if (!wekanAssignees.find(wId => wId === wekanId)) {
  350. wekanAssignees.push(wekanId);
  351. }
  352. }
  353. return true;
  354. });
  355. if (wekanAssignees.length > 0) {
  356. cardToCreate.assignees = wekanAssignees;
  357. }
  358. }
  359. // set color
  360. if (card.color) {
  361. cardToCreate.color = card.color;
  362. }
  363. // add custom fields
  364. if (card.customFields) {
  365. cardToCreate.customFields = card.customFields.map(field => {
  366. return {
  367. _id: this.customFields[field._id],
  368. value: field.value,
  369. };
  370. });
  371. }
  372. // insert card
  373. const cardId = Cards.direct.insert(cardToCreate);
  374. // keep track of Wekan id => Wekan id
  375. this.cards[card._id] = cardId;
  376. // // log activity
  377. // Activities.direct.insert({
  378. // activityType: 'importCard',
  379. // boardId,
  380. // cardId,
  381. // createdAt: this._now(),
  382. // listId: cardToCreate.listId,
  383. // source: {
  384. // id: card._id,
  385. // system: 'Wekan',
  386. // },
  387. // // we attribute the import to current user,
  388. // // not the author of the original card
  389. // userId: this._user(),
  390. // });
  391. // add comments
  392. const comments = this.comments[card._id];
  393. if (comments) {
  394. comments.forEach(comment => {
  395. const commentToCreate = {
  396. boardId,
  397. cardId,
  398. createdAt: this._now(comment.createdAt),
  399. text: comment.text,
  400. // we attribute the comment to the original author, default to current user
  401. userId: this._user(comment.userId),
  402. };
  403. // dateLastActivity will be set from activity insert, no need to
  404. // update it ourselves
  405. const commentId = CardComments.direct.insert(commentToCreate);
  406. this.commentIds[comment._id] = commentId;
  407. // Activities.direct.insert({
  408. // activityType: 'addComment',
  409. // boardId: commentToCreate.boardId,
  410. // cardId: commentToCreate.cardId,
  411. // commentId,
  412. // createdAt: this._now(commentToCreate.createdAt),
  413. // // we attribute the addComment (not the import)
  414. // // to the original author - it is needed by some UI elements.
  415. // userId: commentToCreate.userId,
  416. // });
  417. });
  418. }
  419. const attachments = this.attachments[card._id];
  420. const wekanCoverId = card.coverId;
  421. if (attachments) {
  422. attachments.forEach(att => {
  423. const file = new FS.File();
  424. // Simulating file.attachData on the client generates multiple errors
  425. // - HEAD returns null, which causes exception down the line
  426. // - the template then tries to display the url to the attachment which causes other errors
  427. // so we make it server only, and let UI catch up once it is done, forget about latency comp.
  428. const self = this;
  429. if (Meteor.isServer) {
  430. if (att.url) {
  431. file.attachData(att.url, function(error) {
  432. file.boardId = boardId;
  433. file.cardId = cardId;
  434. file.userId = self._user(att.userId);
  435. // The field source will only be used to prevent adding
  436. // attachments' related activities automatically
  437. file.source = 'import';
  438. if (error) {
  439. throw error;
  440. } else {
  441. const wekanAtt = Attachments.insert(file, () => {
  442. // we do nothing
  443. });
  444. self.attachmentIds[att._id] = wekanAtt._id;
  445. //
  446. if (wekanCoverId === att._id) {
  447. Cards.direct.update(cardId, {
  448. $set: {
  449. coverId: wekanAtt._id,
  450. },
  451. });
  452. }
  453. }
  454. });
  455. } else if (att.file) {
  456. //If attribute type is null or empty string is set, assume binary stream
  457. att.type =
  458. !att.type || att.type.trim().length === 0
  459. ? 'application/octet-stream'
  460. : att.type;
  461. file.attachData(
  462. Buffer.from(att.file, 'base64'),
  463. {
  464. type: att.type,
  465. },
  466. error => {
  467. file.name(att.name);
  468. file.boardId = boardId;
  469. file.cardId = cardId;
  470. file.userId = self._user(att.userId);
  471. // The field source will only be used to prevent adding
  472. // attachments' related activities automatically
  473. file.source = 'import';
  474. if (error) {
  475. throw error;
  476. } else {
  477. const wekanAtt = Attachments.insert(file, () => {
  478. // we do nothing
  479. });
  480. this.attachmentIds[att._id] = wekanAtt._id;
  481. //
  482. if (wekanCoverId === att._id) {
  483. Cards.direct.update(cardId, {
  484. $set: {
  485. coverId: wekanAtt._id,
  486. },
  487. });
  488. }
  489. }
  490. },
  491. );
  492. }
  493. }
  494. // todo XXX set cover - if need be
  495. });
  496. }
  497. result.push(cardId);
  498. });
  499. return result;
  500. }
  501. /**
  502. * Create the Wekan custom fields corresponding to the supplied Wekan
  503. * custom fields.
  504. * @param wekanCustomFields
  505. * @param boardId
  506. */
  507. createCustomFields(wekanCustomFields, boardId) {
  508. wekanCustomFields.forEach((field, fieldIndex) => {
  509. const fieldToCreate = {
  510. boardIds: [boardId],
  511. name: field.name,
  512. type: field.type,
  513. settings: field.settings,
  514. showOnCard: field.showOnCard,
  515. showLabelOnMiniCard: field.showLabelOnMiniCard,
  516. automaticallyOnCard: field.automaticallyOnCard,
  517. //use date "now" if now created at date is provided (e.g. for very old boards)
  518. createdAt: this._now(this.createdAt.customFields[field._id]),
  519. modifiedAt: field.modifiedAt,
  520. };
  521. //insert copy of custom field
  522. const fieldId = CustomFields.direct.insert(fieldToCreate);
  523. //set modified date to now
  524. CustomFields.direct.update(fieldId, {
  525. $set: {
  526. modifiedAt: this._now(),
  527. },
  528. });
  529. //store mapping of old id to new id
  530. this.customFields[field._id] = fieldId;
  531. });
  532. }
  533. // Create labels if they do not exist and load this.labels.
  534. createLabels(wekanLabels, board) {
  535. wekanLabels.forEach(label => {
  536. const color = label.color;
  537. const name = label.name;
  538. const existingLabel = board.getLabel(name, color);
  539. if (existingLabel) {
  540. this.labels[label.id] = existingLabel._id;
  541. } else {
  542. const idLabelCreated = board.pushLabel(name, color);
  543. this.labels[label.id] = idLabelCreated;
  544. }
  545. });
  546. }
  547. createLists(wekanLists, boardId) {
  548. wekanLists.forEach((list, listIndex) => {
  549. const listToCreate = {
  550. archived: list.archived,
  551. boardId,
  552. // We are being defensing here by providing a default date (now) if the
  553. // creation date wasn't found on the action log. This happen on old
  554. // Wekan boards (eg from 2013) that didn't log the 'createList' action
  555. // we require.
  556. createdAt: this._now(this.createdAt.lists[list.id]),
  557. title: list.title,
  558. sort: list.sort ? list.sort : listIndex,
  559. };
  560. const listId = Lists.direct.insert(listToCreate);
  561. Lists.direct.update(listId, {
  562. $set: {
  563. updatedAt: this._now(),
  564. },
  565. });
  566. this.lists[list._id] = listId;
  567. // // log activity
  568. // Activities.direct.insert({
  569. // activityType: 'importList',
  570. // boardId,
  571. // createdAt: this._now(),
  572. // listId,
  573. // source: {
  574. // id: list._id,
  575. // system: 'Wekan',
  576. // },
  577. // // We attribute the import to current user,
  578. // // not the creator of the original object
  579. // userId: this._user(),
  580. // });
  581. });
  582. }
  583. createSwimlanes(wekanSwimlanes, boardId) {
  584. wekanSwimlanes.forEach((swimlane, swimlaneIndex) => {
  585. const swimlaneToCreate = {
  586. archived: swimlane.archived,
  587. boardId,
  588. // We are being defensing here by providing a default date (now) if the
  589. // creation date wasn't found on the action log. This happen on old
  590. // Wekan boards (eg from 2013) that didn't log the 'createList' action
  591. // we require.
  592. createdAt: this._now(this.createdAt.swimlanes[swimlane._id]),
  593. title: swimlane.title,
  594. sort: swimlane.sort ? swimlane.sort : swimlaneIndex,
  595. };
  596. // set color
  597. if (swimlane.color) {
  598. swimlaneToCreate.color = swimlane.color;
  599. }
  600. const swimlaneId = Swimlanes.direct.insert(swimlaneToCreate);
  601. Swimlanes.direct.update(swimlaneId, {
  602. $set: {
  603. updatedAt: this._now(),
  604. },
  605. });
  606. this.swimlanes[swimlane._id] = swimlaneId;
  607. });
  608. }
  609. createSubtasks(wekanCards) {
  610. wekanCards.forEach(card => {
  611. // get new id of card (in created / new board)
  612. const cardIdInNewBoard = this.cards[card._id];
  613. //If there is a mapped parent card, use the mapped card
  614. // this means, the card and parent were in the same source board
  615. //If there is no mapped parent card, use the original parent id,
  616. // this should handle cases where source and parent are in different boards
  617. // Note: This can only handle board cloning (within the same wekan instance).
  618. // When importing boards between instances the IDs are definitely
  619. // lost if source and parent are two different boards
  620. // This is not the place to fix it, the entire subtask system needs to be rethought there.
  621. const parentIdInNewBoard = this.cards[card.parentId]
  622. ? this.cards[card.parentId]
  623. : card.parentId;
  624. //if the parent card exists, proceed
  625. if (Cards.findOne(parentIdInNewBoard)) {
  626. //set parent id of the card in the new board to the new id of the parent
  627. Cards.direct.update(cardIdInNewBoard, {
  628. $set: {
  629. parentId: parentIdInNewBoard,
  630. },
  631. });
  632. }
  633. });
  634. }
  635. createChecklists(wekanChecklists) {
  636. const result = [];
  637. wekanChecklists.forEach((checklist, checklistIndex) => {
  638. // Create the checklist
  639. const checklistToCreate = {
  640. cardId: this.cards[checklist.cardId],
  641. title: checklist.title,
  642. createdAt: checklist.createdAt,
  643. sort: checklist.sort ? checklist.sort : checklistIndex,
  644. };
  645. const checklistId = Checklists.direct.insert(checklistToCreate);
  646. this.checklists[checklist._id] = checklistId;
  647. result.push(checklistId);
  648. });
  649. return result;
  650. }
  651. createTriggers(wekanTriggers, boardId) {
  652. wekanTriggers.forEach(trigger => {
  653. if (trigger.hasOwnProperty('labelId')) {
  654. trigger.labelId = this.labels[trigger.labelId];
  655. }
  656. if (trigger.hasOwnProperty('memberId')) {
  657. trigger.memberId = this.members[trigger.memberId];
  658. }
  659. trigger.boardId = boardId;
  660. const oldId = trigger._id;
  661. delete trigger._id;
  662. this.triggers[oldId] = Triggers.direct.insert(trigger);
  663. });
  664. }
  665. createActions(wekanActions, boardId) {
  666. wekanActions.forEach(action => {
  667. if (action.hasOwnProperty('labelId')) {
  668. action.labelId = this.labels[action.labelId];
  669. }
  670. if (action.hasOwnProperty('memberId')) {
  671. action.memberId = this.members[action.memberId];
  672. }
  673. action.boardId = boardId;
  674. const oldId = action._id;
  675. delete action._id;
  676. this.actions[oldId] = Actions.direct.insert(action);
  677. });
  678. }
  679. createRules(wekanRules, boardId) {
  680. wekanRules.forEach(rule => {
  681. // Create the rule
  682. rule.boardId = boardId;
  683. rule.triggerId = this.triggers[rule.triggerId];
  684. rule.actionId = this.actions[rule.actionId];
  685. delete rule._id;
  686. Rules.direct.insert(rule);
  687. });
  688. }
  689. createChecklistItems(wekanChecklistItems) {
  690. wekanChecklistItems.forEach((checklistitem, checklistitemIndex) => {
  691. //Check if the checklist for this item (still) exists
  692. //If a checklist was deleted, but items remain, the import would error out here
  693. //Leading to no further checklist items being imported
  694. if (this.checklists[checklistitem.checklistId]) {
  695. // Create the checklistItem
  696. const checklistItemTocreate = {
  697. title: checklistitem.title,
  698. checklistId: this.checklists[checklistitem.checklistId],
  699. cardId: this.cards[checklistitem.cardId],
  700. sort: checklistitem.sort ? checklistitem.sort : checklistitemIndex,
  701. isFinished: checklistitem.isFinished,
  702. };
  703. const checklistItemId = ChecklistItems.direct.insert(
  704. checklistItemTocreate,
  705. );
  706. this.checklistItems[checklistitem._id] = checklistItemId;
  707. }
  708. });
  709. }
  710. parseActivities(wekanBoard) {
  711. wekanBoard.activities.forEach(activity => {
  712. switch (activity.activityType) {
  713. case 'addAttachment': {
  714. // We have to be cautious, because the attachment could have been removed later.
  715. // In that case Wekan still reports its addition, but removes its 'url' field.
  716. // So we test for that
  717. const wekanAttachment = wekanBoard.attachments.filter(attachment => {
  718. return attachment._id === activity.attachmentId;
  719. })[0];
  720. if (typeof wekanAttachment !== 'undefined' && wekanAttachment) {
  721. if (wekanAttachment.url || wekanAttachment.file) {
  722. // we cannot actually create the Wekan attachment, because we don't yet
  723. // have the cards to attach it to, so we store it in the instance variable.
  724. const wekanCardId = activity.cardId;
  725. if (!this.attachments[wekanCardId]) {
  726. this.attachments[wekanCardId] = [];
  727. }
  728. this.attachments[wekanCardId].push(wekanAttachment);
  729. }
  730. }
  731. break;
  732. }
  733. case 'addComment': {
  734. const wekanComment = wekanBoard.comments.filter(comment => {
  735. return comment._id === activity.commentId;
  736. })[0];
  737. const id = activity.cardId;
  738. if (!this.comments[id]) {
  739. this.comments[id] = [];
  740. }
  741. this.comments[id].push(wekanComment);
  742. break;
  743. }
  744. case 'createBoard': {
  745. this.createdAt.board = activity.createdAt;
  746. break;
  747. }
  748. case 'createCard': {
  749. const cardId = activity.cardId;
  750. this.createdAt.cards[cardId] = activity.createdAt;
  751. this.createdBy.cards[cardId] = activity.userId;
  752. break;
  753. }
  754. case 'createList': {
  755. const listId = activity.listId;
  756. this.createdAt.lists[listId] = activity.createdAt;
  757. break;
  758. }
  759. case 'createSwimlane': {
  760. const swimlaneId = activity.swimlaneId;
  761. this.createdAt.swimlanes[swimlaneId] = activity.createdAt;
  762. break;
  763. }
  764. case 'createCustomField': {
  765. const customFieldId = activity.customFieldId;
  766. this.createdAt.customFields[customFieldId] = activity.createdAt;
  767. break;
  768. }
  769. }
  770. });
  771. }
  772. importActivities(activities, boardId) {
  773. activities.forEach(activity => {
  774. switch (activity.activityType) {
  775. // Board related activities
  776. // TODO: addBoardMember, removeBoardMember
  777. case 'createBoard': {
  778. Activities.direct.insert({
  779. userId: this._user(activity.userId),
  780. type: 'board',
  781. activityTypeId: boardId,
  782. activityType: activity.activityType,
  783. boardId,
  784. createdAt: this._now(activity.createdAt),
  785. });
  786. break;
  787. }
  788. // List related activities
  789. // TODO: removeList, archivedList
  790. case 'createList': {
  791. Activities.direct.insert({
  792. userId: this._user(activity.userId),
  793. type: 'list',
  794. activityType: activity.activityType,
  795. listId: this.lists[activity.listId],
  796. boardId,
  797. createdAt: this._now(activity.createdAt),
  798. });
  799. break;
  800. }
  801. // Card related activities
  802. // TODO: archivedCard, restoredCard, joinMember, unjoinMember
  803. case 'createCard': {
  804. Activities.direct.insert({
  805. userId: this._user(activity.userId),
  806. activityType: activity.activityType,
  807. listId: this.lists[activity.listId],
  808. cardId: this.cards[activity.cardId],
  809. boardId,
  810. createdAt: this._now(activity.createdAt),
  811. });
  812. break;
  813. }
  814. case 'moveCard': {
  815. Activities.direct.insert({
  816. userId: this._user(activity.userId),
  817. oldListId: this.lists[activity.oldListId],
  818. activityType: activity.activityType,
  819. listId: this.lists[activity.listId],
  820. cardId: this.cards[activity.cardId],
  821. boardId,
  822. createdAt: this._now(activity.createdAt),
  823. });
  824. break;
  825. }
  826. // Comment related activities
  827. case 'addComment': {
  828. Activities.direct.insert({
  829. userId: this._user(activity.userId),
  830. activityType: activity.activityType,
  831. cardId: this.cards[activity.cardId],
  832. commentId: this.commentIds[activity.commentId],
  833. boardId,
  834. createdAt: this._now(activity.createdAt),
  835. });
  836. break;
  837. }
  838. // Attachment related activities
  839. case 'addAttachment': {
  840. Activities.direct.insert({
  841. userId: this._user(activity.userId),
  842. type: 'card',
  843. activityType: activity.activityType,
  844. attachmentId: this.attachmentIds[activity.attachmentId],
  845. cardId: this.cards[activity.cardId],
  846. boardId,
  847. createdAt: this._now(activity.createdAt),
  848. });
  849. break;
  850. }
  851. // Checklist related activities
  852. case 'addChecklist': {
  853. Activities.direct.insert({
  854. userId: this._user(activity.userId),
  855. activityType: activity.activityType,
  856. cardId: this.cards[activity.cardId],
  857. checklistId: this.checklists[activity.checklistId],
  858. boardId,
  859. createdAt: this._now(activity.createdAt),
  860. });
  861. break;
  862. }
  863. case 'addChecklistItem': {
  864. Activities.direct.insert({
  865. userId: this._user(activity.userId),
  866. activityType: activity.activityType,
  867. cardId: this.cards[activity.cardId],
  868. checklistId: this.checklists[activity.checklistId],
  869. checklistItemId: activity.checklistItemId.replace(
  870. activity.checklistId,
  871. this.checklists[activity.checklistId],
  872. ),
  873. boardId,
  874. createdAt: this._now(activity.createdAt),
  875. });
  876. break;
  877. }
  878. }
  879. });
  880. }
  881. //check(board) {
  882. check() {
  883. //try {
  884. // check(data, {
  885. // membersMapping: Match.Optional(Object),
  886. // });
  887. // this.checkActivities(board.activities);
  888. // this.checkBoard(board);
  889. // this.checkLabels(board.labels);
  890. // this.checkLists(board.lists);
  891. // this.checkSwimlanes(board.swimlanes);
  892. // this.checkCards(board.cards);
  893. //this.checkChecklists(board.checklists);
  894. // this.checkRules(board.rules);
  895. // this.checkActions(board.actions);
  896. //this.checkTriggers(board.triggers);
  897. //this.checkChecklistItems(board.checklistItems);
  898. //} catch (e) {
  899. // throw new Meteor.Error('error-json-schema');
  900. // }
  901. }
  902. create(board, currentBoardId) {
  903. // TODO : Make isSandstorm variable global
  904. const isSandstorm =
  905. Meteor.settings &&
  906. Meteor.settings.public &&
  907. Meteor.settings.public.sandstorm;
  908. if (isSandstorm && currentBoardId) {
  909. const currentBoard = Boards.findOne(currentBoardId);
  910. currentBoard.archive();
  911. }
  912. this.parseActivities(board);
  913. const boardId = this.createBoardAndLabels(board);
  914. this.createLists(board.lists, boardId);
  915. this.createSwimlanes(board.swimlanes, boardId);
  916. this.createCustomFields(board.customFields, boardId);
  917. this.createCards(board.cards, boardId);
  918. this.createSubtasks(board.cards);
  919. this.createChecklists(board.checklists);
  920. this.createChecklistItems(board.checklistItems);
  921. this.importActivities(board.activities, boardId);
  922. this.createTriggers(board.triggers, boardId);
  923. this.createActions(board.actions, boardId);
  924. this.createRules(board.rules, boardId);
  925. // XXX add members
  926. return boardId;
  927. }
  928. }