import.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. Meteor.methods({
  2. /**
  3. *
  4. */
  5. importTrelloCard(trelloCard, listId, sortIndex) {
  6. DateString = Match.Where(function (dateAsString) {
  7. check(dateAsString, String);
  8. //const date = new Date(dateAsString);
  9. //return (date.toString() !== 'Invalid Date') && !isNan(date);
  10. return moment(dateAsString, moment.ISO_8601).isValid();
  11. });
  12. check(trelloCard, Match.ObjectIncluding({
  13. name: String,
  14. desc: String,
  15. closed: Boolean,
  16. dateLastActivity: DateString,
  17. labels: [Match.ObjectIncluding({
  18. name: String,
  19. color: String,
  20. })],
  21. actions: [Match.ObjectIncluding({
  22. type: String,
  23. date: DateString,
  24. data: Object,
  25. })],
  26. members: [Object],
  27. }));
  28. check(listId, String);
  29. check(sortIndex, Number);
  30. const list = Lists.findOne(listId);
  31. if(!list) {
  32. throw 'exception-list-doesNotExist';
  33. }
  34. // XXX check we are allowed to run method
  35. // 1. map all fields for the card to create
  36. const dateOfImport = new Date();
  37. const cardToCreate = {
  38. title: trelloCard.name,
  39. description: trelloCard.desc,
  40. listId: list._id,
  41. boardId: list.boardId,
  42. userId: Meteor.userId(),
  43. sort: sortIndex,
  44. archived: trelloCard.closed,
  45. // this is a default date, we'll fetch the actual one from the actions array
  46. createdAt: dateOfImport,
  47. dateLastActivity: dateOfImport,
  48. };
  49. // find actual creation date
  50. const creationAction = trelloCard.actions.find((action) => {return action.type === 'createCard';});
  51. if(creationAction) {
  52. cardToCreate.createdAt = creationAction.date;
  53. }
  54. // 2. map labels
  55. trelloCard.labels.forEach((currentLabel) => {
  56. const color = currentLabel.color;
  57. const name = currentLabel.name;
  58. const existingLabel = list.board().getLabel(name, color);
  59. let labelId = undefined;
  60. if (existingLabel) {
  61. labelId = existingLabel._id;
  62. } else {
  63. let labelCreated = list.board().addLabel(name, color);
  64. // XXX currently mutations return no value so we have to fetch the label we just created
  65. // waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
  66. labelCreated = list.board().getLabel(name, color);
  67. labelId = labelCreated._id;
  68. }
  69. if(labelId) {
  70. if (!cardToCreate.labelIds) {
  71. cardToCreate.labelIds = [];
  72. }
  73. cardToCreate.labelIds.push(labelId);
  74. }
  75. });
  76. // 3. insert new card into list
  77. const cardId = Cards.direct.insert(cardToCreate);
  78. // XXX then add import activity
  79. // 4. parse actions and add comments
  80. trelloCard.actions.forEach((currentAction) => {
  81. if(currentAction.type === 'commentCard') {
  82. const commentToCreate = {
  83. boardId: list.boardId,
  84. cardId: cardId,
  85. createdAt: currentAction.date,
  86. text: currentAction.data.text,
  87. userId: Meteor.userId(),
  88. };
  89. const commentId = CardComments.direct.insert(commentToCreate);
  90. Activities.direct.insert({
  91. activityType: 'addComment',
  92. boardId: commentToCreate.boardId,
  93. cardId: commentToCreate.cardId,
  94. commentId: commentId,
  95. createdAt: commentToCreate.createdAt,
  96. userId: commentToCreate.userId,
  97. });
  98. }
  99. // XXX add other type of activities?
  100. });
  101. return cardId;
  102. },
  103. });