import.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. Meteor.methods({
  2. importTrelloCard(trelloCard, listId, sortIndex) {
  3. // 1. check parameters are ok from a syntax point of view
  4. 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. if(Meteor.isServer) {
  29. console.log(e);
  30. }
  31. throw new Meteor.Error('error-json-schema');
  32. }
  33. // 2. check parameters are ok from a business point of view (exist & authorized)
  34. const list = Lists.findOne(listId);
  35. if(!list) {
  36. throw new Meteor.Error('error-list-doesNotExist');
  37. }
  38. if(Meteor.isServer) {
  39. if (!allowIsBoardMember(Meteor.userId(), Boards.findOne(list.boardId))) {
  40. throw new Meteor.Error('error-board-notAMember');
  41. }
  42. }
  43. // 3. map all fields for the card to create
  44. const dateOfImport = new Date();
  45. const cardToCreate = {
  46. archived: trelloCard.closed,
  47. boardId: list.boardId,
  48. // this is a default date, we'll fetch the actual one from the actions array
  49. createdAt: dateOfImport,
  50. dateLastActivity: dateOfImport,
  51. description: trelloCard.desc,
  52. listId: list._id,
  53. sort: sortIndex,
  54. title: trelloCard.name,
  55. // XXX use the original user?
  56. userId: Meteor.userId(),
  57. };
  58. // 4. find actual creation date
  59. const creationAction = trelloCard.actions.find((action) => {return action.type === 'createCard';});
  60. if(creationAction) {
  61. cardToCreate.createdAt = creationAction.date;
  62. }
  63. // 5. map labels - create missing ones
  64. trelloCard.labels.forEach((currentLabel) => {
  65. const color = currentLabel.color;
  66. const name = currentLabel.name;
  67. const existingLabel = list.board().getLabel(name, color);
  68. let labelId = undefined;
  69. if (existingLabel) {
  70. labelId = existingLabel._id;
  71. } else {
  72. let labelCreated = list.board().addLabel(name, color);
  73. // XXX currently mutations return no value so we have to fetch the label we just created
  74. // waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
  75. labelCreated = list.board().getLabel(name, color);
  76. labelId = labelCreated._id;
  77. }
  78. if(labelId) {
  79. if (!cardToCreate.labelIds) {
  80. cardToCreate.labelIds = [];
  81. }
  82. cardToCreate.labelIds.push(labelId);
  83. }
  84. });
  85. // 6. insert new card into list
  86. const cardId = Cards.direct.insert(cardToCreate);
  87. Activities.direct.insert({
  88. activityType: 'importCard',
  89. boardId: cardToCreate.boardId,
  90. cardId: cardId,
  91. createdAt: dateOfImport,
  92. listId: cardToCreate.listId,
  93. source: {
  94. id: trelloCard.id,
  95. system: 'Trello',
  96. url: trelloCard.url,
  97. },
  98. // we attribute the import to current user, not the one from the original card
  99. userId: Meteor.userId(),
  100. });
  101. // 7. parse actions and add comments
  102. trelloCard.actions.forEach((currentAction) => {
  103. if(currentAction.type === 'commentCard') {
  104. const commentToCreate = {
  105. boardId: list.boardId,
  106. cardId: cardId,
  107. createdAt: currentAction.date,
  108. text: currentAction.data.text,
  109. // XXX use the original comment user instead
  110. userId: Meteor.userId(),
  111. };
  112. const commentId = CardComments.direct.insert(commentToCreate);
  113. Activities.direct.insert({
  114. activityType: 'addComment',
  115. boardId: commentToCreate.boardId,
  116. cardId: commentToCreate.cardId,
  117. commentId: commentId,
  118. createdAt: commentToCreate.createdAt,
  119. userId: commentToCreate.userId,
  120. });
  121. }
  122. });
  123. return cardId;
  124. },
  125. });