import.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. Meteor.methods({
  2. importTrelloCard(trelloCard, listId, sortIndex) {
  3. // 1. check parameters are ok from a syntax point of view
  4. const DateString = Match.Where(function (dateAsString) {
  5. check(dateAsString, String);
  6. return moment(dateAsString, moment.ISO_8601).isValid();
  7. });
  8. try {
  9. check(trelloCard, Match.ObjectIncluding({
  10. name: String,
  11. desc: String,
  12. closed: Boolean,
  13. dateLastActivity: DateString,
  14. labels: [Match.ObjectIncluding({
  15. name: String,
  16. color: String,
  17. })],
  18. actions: [Match.ObjectIncluding({
  19. type: String,
  20. date: DateString,
  21. data: Object,
  22. })],
  23. members: [Object],
  24. }));
  25. check(listId, String);
  26. check(sortIndex, Number);
  27. } catch (e) {
  28. throw new Meteor.Error('error-json-schema');
  29. }
  30. // 2. check parameters are ok from a business point of view (exist &
  31. // authorized)
  32. const list = Lists.findOne(listId);
  33. if (!list) {
  34. throw new Meteor.Error('error-list-doesNotExist');
  35. }
  36. if (Meteor.isServer) {
  37. if (!allowIsBoardMember(Meteor.userId(), Boards.findOne(list.boardId))) {
  38. throw new Meteor.Error('error-board-notAMember');
  39. }
  40. }
  41. // 3. map all fields for the card to create
  42. const dateOfImport = new Date();
  43. const cardToCreate = {
  44. archived: trelloCard.closed,
  45. boardId: list.boardId,
  46. // this is a default date, we'll fetch the actual one from the actions array
  47. createdAt: dateOfImport,
  48. dateLastActivity: dateOfImport,
  49. description: trelloCard.desc,
  50. labelIds: [],
  51. listId: list._id,
  52. sort: sortIndex,
  53. title: trelloCard.name,
  54. // XXX use the original user?
  55. userId: Meteor.userId(),
  56. };
  57. // 4. find actual creation date
  58. const creationAction = trelloCard.actions.find((action) => {
  59. return action.type === 'createCard';
  60. });
  61. if (creationAction) {
  62. cardToCreate.createdAt = creationAction.date;
  63. }
  64. // 5. map labels - create missing ones
  65. trelloCard.labels.forEach((currentLabel) => {
  66. const { name, color } = currentLabel;
  67. // `addLabel` won't create dublicate labels (ie labels with the same name
  68. // and color) so here it is used more in a "enforceLabelExistence" way.
  69. list.board().addLabel(name, color);
  70. const { _id: labelId } = list.board().getLabel(name, color);
  71. if (labelId) {
  72. cardToCreate.labelIds.push(labelId);
  73. }
  74. });
  75. // 6. insert new card into list
  76. const cardId = Cards.direct.insert(cardToCreate);
  77. Activities.direct.insert({
  78. activityType: 'importCard',
  79. boardId: cardToCreate.boardId,
  80. cardId,
  81. createdAt: dateOfImport,
  82. listId: cardToCreate.listId,
  83. source: {
  84. id: trelloCard.id,
  85. system: 'Trello',
  86. url: trelloCard.url,
  87. },
  88. // we attribute the import to current user, not the one from the original
  89. // card
  90. userId: Meteor.userId(),
  91. });
  92. // 7. parse actions and add comments
  93. trelloCard.actions.forEach((currentAction) => {
  94. if (currentAction.type === 'commentCard') {
  95. const commentToCreate = {
  96. boardId: list.boardId,
  97. cardId,
  98. createdAt: currentAction.date,
  99. text: currentAction.data.text,
  100. // XXX use the original comment user instead
  101. userId: Meteor.userId(),
  102. };
  103. const commentId = CardComments.direct.insert(commentToCreate);
  104. Activities.direct.insert({
  105. activityType: 'addComment',
  106. boardId: commentToCreate.boardId,
  107. cardId: commentToCreate.cardId,
  108. commentId,
  109. createdAt: commentToCreate.createdAt,
  110. userId: commentToCreate.userId,
  111. });
  112. }
  113. });
  114. return cardId;
  115. },
  116. });