wekanCreator.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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 && Meteor.isServer) {
  426. attachments.forEach(att => {
  427. // Simulating file.attachData on the client generates multiple errors
  428. // - HEAD returns null, which causes exception down the line
  429. // - the template then tries to display the url to the attachment which causes other errors
  430. // so we make it server only, and let UI catch up once it is done, forget about latency comp.
  431. const self = this;
  432. if (att.url || att.file) {
  433. Attachment.load(
  434. att.url ? att.url : Buffer.from(att.file, 'base64'),
  435. { type: att.type ? att.ype : undefined },
  436. (error, fileObj) => {
  437. if (error) {
  438. throw error;
  439. }
  440. fileObj.boardId = boardId;
  441. fileObj.cardId = cardId;
  442. fileObj.userId = self._user(att.userId);
  443. // The field source will only be used to prevent adding
  444. // attachments' related activities automatically
  445. fileObj.source = 'import';
  446. self.attachmentIds[att._id] = fileObj._id;
  447. if (wekanCoverId === att._id) {
  448. Cards.direct.update(cardId, {
  449. $set: { coverId: fileObj._id },
  450. });
  451. }
  452. },
  453. );
  454. }
  455. });
  456. }
  457. result.push(cardId);
  458. });
  459. return result;
  460. }
  461. /**
  462. * Create the Wekan custom fields corresponding to the supplied Wekan
  463. * custom fields.
  464. * @param wekanCustomFields
  465. * @param boardId
  466. */
  467. createCustomFields(wekanCustomFields, boardId) {
  468. wekanCustomFields.forEach((field, fieldIndex) => {
  469. const fieldToCreate = {
  470. boardIds: [boardId],
  471. name: field.name,
  472. type: field.type,
  473. settings: field.settings,
  474. showOnCard: field.showOnCard,
  475. showLabelOnMiniCard: field.showLabelOnMiniCard,
  476. automaticallyOnCard: field.automaticallyOnCard,
  477. alwaysOnCard: field.alwaysOnCard,
  478. //use date "now" if now created at date is provided (e.g. for very old boards)
  479. createdAt: this._now(this.createdAt.customFields[field._id]),
  480. modifiedAt: field.modifiedAt,
  481. };
  482. //insert copy of custom field
  483. const fieldId = CustomFields.direct.insert(fieldToCreate);
  484. //set modified date to now
  485. CustomFields.direct.update(fieldId, {
  486. $set: {
  487. modifiedAt: this._now(),
  488. },
  489. });
  490. //store mapping of old id to new id
  491. this.customFields[field._id] = fieldId;
  492. });
  493. }
  494. // Create labels if they do not exist and load this.labels.
  495. createLabels(wekanLabels, board) {
  496. wekanLabels.forEach(label => {
  497. const color = label.color;
  498. const name = label.name;
  499. const existingLabel = board.getLabel(name, color);
  500. if (existingLabel) {
  501. this.labels[label.id] = existingLabel._id;
  502. } else {
  503. const idLabelCreated = board.pushLabel(name, color);
  504. this.labels[label.id] = idLabelCreated;
  505. }
  506. });
  507. }
  508. createLists(wekanLists, boardId) {
  509. wekanLists.forEach((list, listIndex) => {
  510. const listToCreate = {
  511. archived: list.archived,
  512. boardId,
  513. // We are being defensing here by providing a default date (now) if the
  514. // creation date wasn't found on the action log. This happen on old
  515. // Wekan boards (eg from 2013) that didn't log the 'createList' action
  516. // we require.
  517. createdAt: this._now(this.createdAt.lists[list.id]),
  518. title: list.title,
  519. sort: list.sort ? list.sort : listIndex,
  520. };
  521. const listId = Lists.direct.insert(listToCreate);
  522. Lists.direct.update(listId, {
  523. $set: {
  524. updatedAt: this._now(),
  525. },
  526. });
  527. this.lists[list._id] = listId;
  528. // // log activity
  529. // Activities.direct.insert({
  530. // activityType: 'importList',
  531. // boardId,
  532. // createdAt: this._now(),
  533. // listId,
  534. // source: {
  535. // id: list._id,
  536. // system: 'Wekan',
  537. // },
  538. // // We attribute the import to current user,
  539. // // not the creator of the original object
  540. // userId: this._user(),
  541. // });
  542. });
  543. }
  544. createSwimlanes(wekanSwimlanes, boardId) {
  545. wekanSwimlanes.forEach((swimlane, swimlaneIndex) => {
  546. const swimlaneToCreate = {
  547. archived: swimlane.archived,
  548. boardId,
  549. // We are being defensing here by providing a default date (now) if the
  550. // creation date wasn't found on the action log. This happen on old
  551. // Wekan boards (eg from 2013) that didn't log the 'createList' action
  552. // we require.
  553. createdAt: this._now(this.createdAt.swimlanes[swimlane._id]),
  554. title: swimlane.title,
  555. sort: swimlane.sort ? swimlane.sort : swimlaneIndex,
  556. };
  557. // set color
  558. if (swimlane.color) {
  559. swimlaneToCreate.color = swimlane.color;
  560. }
  561. const swimlaneId = Swimlanes.direct.insert(swimlaneToCreate);
  562. Swimlanes.direct.update(swimlaneId, {
  563. $set: {
  564. updatedAt: this._now(),
  565. },
  566. });
  567. this.swimlanes[swimlane._id] = swimlaneId;
  568. });
  569. }
  570. createSubtasks(wekanCards) {
  571. wekanCards.forEach(card => {
  572. // get new id of card (in created / new board)
  573. const cardIdInNewBoard = this.cards[card._id];
  574. //If there is a mapped parent card, use the mapped card
  575. // this means, the card and parent were in the same source board
  576. //If there is no mapped parent card, use the original parent id,
  577. // this should handle cases where source and parent are in different boards
  578. // Note: This can only handle board cloning (within the same wekan instance).
  579. // When importing boards between instances the IDs are definitely
  580. // lost if source and parent are two different boards
  581. // This is not the place to fix it, the entire subtask system needs to be rethought there.
  582. const parentIdInNewBoard = this.cards[card.parentId]
  583. ? this.cards[card.parentId]
  584. : card.parentId;
  585. //if the parent card exists, proceed
  586. if (Cards.findOne(parentIdInNewBoard)) {
  587. //set parent id of the card in the new board to the new id of the parent
  588. Cards.direct.update(cardIdInNewBoard, {
  589. $set: {
  590. parentId: parentIdInNewBoard,
  591. },
  592. });
  593. }
  594. });
  595. }
  596. createChecklists(wekanChecklists) {
  597. const result = [];
  598. wekanChecklists.forEach((checklist, checklistIndex) => {
  599. // Create the checklist
  600. const checklistToCreate = {
  601. cardId: this.cards[checklist.cardId],
  602. title: checklist.title,
  603. createdAt: checklist.createdAt,
  604. sort: checklist.sort ? checklist.sort : checklistIndex,
  605. };
  606. const checklistId = Checklists.direct.insert(checklistToCreate);
  607. this.checklists[checklist._id] = checklistId;
  608. result.push(checklistId);
  609. });
  610. return result;
  611. }
  612. createTriggers(wekanTriggers, boardId) {
  613. wekanTriggers.forEach(trigger => {
  614. if (trigger.hasOwnProperty('labelId')) {
  615. trigger.labelId = this.labels[trigger.labelId];
  616. }
  617. if (trigger.hasOwnProperty('memberId')) {
  618. trigger.memberId = this.members[trigger.memberId];
  619. }
  620. trigger.boardId = boardId;
  621. const oldId = trigger._id;
  622. delete trigger._id;
  623. this.triggers[oldId] = Triggers.direct.insert(trigger);
  624. });
  625. }
  626. createActions(wekanActions, boardId) {
  627. wekanActions.forEach(action => {
  628. if (action.hasOwnProperty('labelId')) {
  629. action.labelId = this.labels[action.labelId];
  630. }
  631. if (action.hasOwnProperty('memberId')) {
  632. action.memberId = this.members[action.memberId];
  633. }
  634. action.boardId = boardId;
  635. const oldId = action._id;
  636. delete action._id;
  637. this.actions[oldId] = Actions.direct.insert(action);
  638. });
  639. }
  640. createRules(wekanRules, boardId) {
  641. wekanRules.forEach(rule => {
  642. // Create the rule
  643. rule.boardId = boardId;
  644. rule.triggerId = this.triggers[rule.triggerId];
  645. rule.actionId = this.actions[rule.actionId];
  646. delete rule._id;
  647. Rules.direct.insert(rule);
  648. });
  649. }
  650. createChecklistItems(wekanChecklistItems) {
  651. wekanChecklistItems.forEach((checklistitem, checklistitemIndex) => {
  652. //Check if the checklist for this item (still) exists
  653. //If a checklist was deleted, but items remain, the import would error out here
  654. //Leading to no further checklist items being imported
  655. if (this.checklists[checklistitem.checklistId]) {
  656. // Create the checklistItem
  657. const checklistItemTocreate = {
  658. title: checklistitem.title,
  659. checklistId: this.checklists[checklistitem.checklistId],
  660. cardId: this.cards[checklistitem.cardId],
  661. sort: checklistitem.sort ? checklistitem.sort : checklistitemIndex,
  662. isFinished: checklistitem.isFinished,
  663. };
  664. const checklistItemId = ChecklistItems.direct.insert(
  665. checklistItemTocreate,
  666. );
  667. this.checklistItems[checklistitem._id] = checklistItemId;
  668. }
  669. });
  670. }
  671. parseActivities(wekanBoard) {
  672. wekanBoard.activities.forEach(activity => {
  673. switch (activity.activityType) {
  674. case 'addAttachment': {
  675. // We have to be cautious, because the attachment could have been removed later.
  676. // In that case Wekan still reports its addition, but removes its 'url' field.
  677. // So we test for that
  678. const wekanAttachment = wekanBoard.attachments.filter(attachment => {
  679. return attachment._id === activity.attachmentId;
  680. })[0];
  681. if (typeof wekanAttachment !== 'undefined' && wekanAttachment) {
  682. if (wekanAttachment.url || wekanAttachment.file) {
  683. // we cannot actually create the Wekan attachment, because we don't yet
  684. // have the cards to attach it to, so we store it in the instance variable.
  685. const wekanCardId = activity.cardId;
  686. if (!this.attachments[wekanCardId]) {
  687. this.attachments[wekanCardId] = [];
  688. }
  689. this.attachments[wekanCardId].push(wekanAttachment);
  690. }
  691. }
  692. break;
  693. }
  694. case 'addComment': {
  695. const wekanComment = wekanBoard.comments.filter(comment => {
  696. return comment._id === activity.commentId;
  697. })[0];
  698. const id = activity.cardId;
  699. if (!this.comments[id]) {
  700. this.comments[id] = [];
  701. }
  702. this.comments[id].push(wekanComment);
  703. break;
  704. }
  705. case 'createBoard': {
  706. this.createdAt.board = activity.createdAt;
  707. break;
  708. }
  709. case 'createCard': {
  710. const cardId = activity.cardId;
  711. this.createdAt.cards[cardId] = activity.createdAt;
  712. this.createdBy.cards[cardId] = activity.userId;
  713. break;
  714. }
  715. case 'createList': {
  716. const listId = activity.listId;
  717. this.createdAt.lists[listId] = activity.createdAt;
  718. break;
  719. }
  720. case 'createSwimlane': {
  721. const swimlaneId = activity.swimlaneId;
  722. this.createdAt.swimlanes[swimlaneId] = activity.createdAt;
  723. break;
  724. }
  725. case 'createCustomField': {
  726. const customFieldId = activity.customFieldId;
  727. this.createdAt.customFields[customFieldId] = activity.createdAt;
  728. break;
  729. }
  730. }
  731. });
  732. }
  733. importActivities(activities, boardId) {
  734. activities.forEach(activity => {
  735. switch (activity.activityType) {
  736. // Board related activities
  737. // TODO: addBoardMember, removeBoardMember
  738. case 'createBoard': {
  739. Activities.direct.insert({
  740. userId: this._user(activity.userId),
  741. type: 'board',
  742. activityTypeId: boardId,
  743. activityType: activity.activityType,
  744. boardId,
  745. createdAt: this._now(activity.createdAt),
  746. });
  747. break;
  748. }
  749. // List related activities
  750. // TODO: removeList, archivedList
  751. case 'createList': {
  752. Activities.direct.insert({
  753. userId: this._user(activity.userId),
  754. type: 'list',
  755. activityType: activity.activityType,
  756. listId: this.lists[activity.listId],
  757. boardId,
  758. createdAt: this._now(activity.createdAt),
  759. });
  760. break;
  761. }
  762. // Card related activities
  763. // TODO: archivedCard, restoredCard, joinMember, unjoinMember
  764. case 'createCard': {
  765. Activities.direct.insert({
  766. userId: this._user(activity.userId),
  767. activityType: activity.activityType,
  768. listId: this.lists[activity.listId],
  769. cardId: this.cards[activity.cardId],
  770. boardId,
  771. createdAt: this._now(activity.createdAt),
  772. });
  773. break;
  774. }
  775. case 'moveCard': {
  776. Activities.direct.insert({
  777. userId: this._user(activity.userId),
  778. oldListId: this.lists[activity.oldListId],
  779. activityType: activity.activityType,
  780. listId: this.lists[activity.listId],
  781. cardId: this.cards[activity.cardId],
  782. boardId,
  783. createdAt: this._now(activity.createdAt),
  784. });
  785. break;
  786. }
  787. // Comment related activities
  788. case 'addComment': {
  789. Activities.direct.insert({
  790. userId: this._user(activity.userId),
  791. activityType: activity.activityType,
  792. cardId: this.cards[activity.cardId],
  793. commentId: this.commentIds[activity.commentId],
  794. boardId,
  795. createdAt: this._now(activity.createdAt),
  796. });
  797. break;
  798. }
  799. // Attachment related activities
  800. case 'addAttachment': {
  801. Activities.direct.insert({
  802. userId: this._user(activity.userId),
  803. type: 'card',
  804. activityType: activity.activityType,
  805. attachmentId: this.attachmentIds[activity.attachmentId],
  806. cardId: this.cards[activity.cardId],
  807. boardId,
  808. createdAt: this._now(activity.createdAt),
  809. });
  810. break;
  811. }
  812. // Checklist related activities
  813. case 'addChecklist': {
  814. Activities.direct.insert({
  815. userId: this._user(activity.userId),
  816. activityType: activity.activityType,
  817. cardId: this.cards[activity.cardId],
  818. checklistId: this.checklists[activity.checklistId],
  819. boardId,
  820. createdAt: this._now(activity.createdAt),
  821. });
  822. break;
  823. }
  824. case 'addChecklistItem': {
  825. Activities.direct.insert({
  826. userId: this._user(activity.userId),
  827. activityType: activity.activityType,
  828. cardId: this.cards[activity.cardId],
  829. checklistId: this.checklists[activity.checklistId],
  830. checklistItemId: activity.checklistItemId.replace(
  831. activity.checklistId,
  832. this.checklists[activity.checklistId],
  833. ),
  834. boardId,
  835. createdAt: this._now(activity.createdAt),
  836. });
  837. break;
  838. }
  839. }
  840. });
  841. }
  842. //check(board) {
  843. check() {
  844. //try {
  845. // check(data, {
  846. // membersMapping: Match.Optional(Object),
  847. // });
  848. // this.checkActivities(board.activities);
  849. // this.checkBoard(board);
  850. // this.checkLabels(board.labels);
  851. // this.checkLists(board.lists);
  852. // this.checkSwimlanes(board.swimlanes);
  853. // this.checkCards(board.cards);
  854. //this.checkChecklists(board.checklists);
  855. // this.checkRules(board.rules);
  856. // this.checkActions(board.actions);
  857. //this.checkTriggers(board.triggers);
  858. //this.checkChecklistItems(board.checklistItems);
  859. //} catch (e) {
  860. // throw new Meteor.Error('error-json-schema');
  861. // }
  862. }
  863. create(board, currentBoardId) {
  864. // TODO : Make isSandstorm variable global
  865. const isSandstorm =
  866. Meteor.settings &&
  867. Meteor.settings.public &&
  868. Meteor.settings.public.sandstorm;
  869. if (isSandstorm && currentBoardId) {
  870. const currentBoard = Boards.findOne(currentBoardId);
  871. currentBoard.archive();
  872. }
  873. this.parseActivities(board);
  874. const boardId = this.createBoardAndLabels(board);
  875. this.createLists(board.lists, boardId);
  876. this.createSwimlanes(board.swimlanes, boardId);
  877. this.createCustomFields(board.customFields, boardId);
  878. this.createCards(board.cards, boardId);
  879. this.createSubtasks(board.cards);
  880. this.createChecklists(board.checklists);
  881. this.createChecklistItems(board.checklistItems);
  882. this.importActivities(board.activities, boardId);
  883. this.createTriggers(board.triggers, boardId);
  884. this.createActions(board.actions, boardId);
  885. this.createRules(board.rules, boardId);
  886. // XXX add members
  887. return boardId;
  888. }
  889. }