import.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // XXX parse trelloCard.actions to determine creation date
  38. const cardToCreate = {
  39. title: trelloCard.name,
  40. description: trelloCard.desc,
  41. listId: list._id,
  42. boardId: list.boardId,
  43. userId: Meteor.userId(),
  44. sort: sortIndex,
  45. archived: trelloCard.closed,
  46. // XXX dateOfImport
  47. createdAt: dateOfImport,
  48. dateLastActivity: dateOfImport,
  49. };
  50. // 2. map labels
  51. trelloCard.labels.forEach((currentLabel) => {
  52. const color = currentLabel.color;
  53. const name = currentLabel.name;
  54. const existingLabel = list.board().getLabel(name, color);
  55. let labelId = undefined;
  56. if (existingLabel) {
  57. labelId = existingLabel._id;
  58. } else {
  59. let labelCreated = list.board().addLabel(name, color);
  60. // XXX currently mutations return no value so we have to fetch the label we just created
  61. // waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
  62. labelCreated = list.board().getLabel(name, color);
  63. labelId = labelCreated._id;
  64. }
  65. if(labelId) {
  66. if (!cardToCreate.labelIds) {
  67. cardToCreate.labelIds = [];
  68. }
  69. cardToCreate.labelIds.push(labelId);
  70. }
  71. });
  72. // 3. insert new card into list
  73. // XXX replace with direct MongoDB inserts
  74. const _id = Cards.direct.insert(cardToCreate);
  75. // XXX then add import activity
  76. // 4. parse actions and add comments
  77. trelloCard.actions.forEach((currentAction) => {
  78. if(currentAction.type === 'commentCard') {
  79. const commentToCreate = {
  80. boardId: list.boardId,
  81. cardId: _id,
  82. userId: Meteor.userId(),
  83. text: currentAction.data.text,
  84. createdAt: currentAction.date,
  85. };
  86. // console.log(commentToCreate);
  87. CardComments.direct.insert(commentToCreate);
  88. }
  89. // XXX add other type of activities?
  90. // XXX look for createCard to set create date > no do it BEFORE saving
  91. });
  92. return _id;
  93. },
  94. });