import.js 4.1 KB

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