triggers.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. Triggers = new Mongo.Collection('triggers');
  2. Triggers.mutations({
  3. rename(description) {
  4. return { $set: { description } };
  5. },
  6. });
  7. Triggers.allow({
  8. update: function () {
  9. // add custom authentication code here
  10. return true;
  11. },
  12. insert: function () {
  13. // add custom authentication code here
  14. return true;
  15. }
  16. });
  17. Triggers.helpers({
  18. fromList() {
  19. return Lists.findOne(this.fromId);
  20. },
  21. toList() {
  22. return Lists.findOne(this.toId);
  23. },
  24. findList(title) {
  25. return Lists.findOne({title:title});
  26. },
  27. labels() {
  28. const boardLabels = this.board().labels;
  29. const cardLabels = _.filter(boardLabels, (label) => {
  30. return _.contains(this.labelIds, label._id);
  31. });
  32. return cardLabels;
  33. }});
  34. if (Meteor.isServer) {
  35. Meteor.startup(() => {
  36. const rules = Triggers.findOne({});
  37. if(!rules){
  38. Triggers.insert({group: "cards", activityType: "moveCard","fromId":-1,"toId":-1 });
  39. }
  40. });
  41. }
  42. Activities.after.insert((userId, doc) => {
  43. const activity = Activities._transform(doc);
  44. const matchedTriggers = Triggers.find({activityType: activity.activityType,fromId:activity.oldListId,toId:activity.listId})
  45. if(matchedTriggers.count() > 0){
  46. const card = activity.card();
  47. const oldTitle = card.title;
  48. const fromListTitle = activity.oldList().title;
  49. Cards.direct.update({_id: card._id, listId: card.listId, boardId: card.boardId, archived: false},
  50. {$set: {title: "[From "+fromListTitle +"] "+ oldTitle}});
  51. }
  52. });