rulesHelper.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. RulesHelper = {
  2. executeRules(activity){
  3. const matchingRules = this.findMatchingRules(activity);
  4. for(let i = 0; i< matchingRules.length; i++){
  5. const action = matchingRules[i].getAction();
  6. if(action !== undefined){
  7. this.performAction(activity, action);
  8. }
  9. }
  10. },
  11. findMatchingRules(activity){
  12. const activityType = activity.activityType;
  13. if(TriggersDef[activityType] === undefined){
  14. return [];
  15. }
  16. const matchingFields = TriggersDef[activityType].matchingFields;
  17. const matchingMap = this.buildMatchingFieldsMap(activity, matchingFields);
  18. const matchingTriggers = Triggers.find(matchingMap);
  19. const matchingRules = [];
  20. matchingTriggers.forEach(function(trigger){
  21. const rule = trigger.getRule();
  22. // Check that for some unknown reason there are some leftover triggers
  23. // not connected to any rules
  24. if(rule !== undefined){
  25. matchingRules.push(trigger.getRule());
  26. }
  27. });
  28. return matchingRules;
  29. },
  30. buildMatchingFieldsMap(activity, matchingFields){
  31. const matchingMap = {'activityType':activity.activityType};
  32. for(let i = 0; i< matchingFields.length; i++){
  33. // Creating a matching map with the actual field of the activity
  34. // and with the wildcard (for example: trigger when a card is added
  35. // in any [*] board
  36. matchingMap[matchingFields[i]] = { $in: [activity[matchingFields[i]], '*']};
  37. }
  38. return matchingMap;
  39. },
  40. performAction(activity, action){
  41. const card = Cards.findOne({_id:activity.cardId});
  42. const boardId = activity.boardId;
  43. if(action.actionType === 'moveCardToTop'){
  44. let listId;
  45. let list;
  46. if(action.listTitle === '*'){
  47. listId = card.listId;
  48. list = card.list();
  49. }else{
  50. list = Lists.findOne({title: action.listTitle, boardId });
  51. listId = list._id;
  52. }
  53. const minOrder = _.min(list.cardsUnfiltered(card.swimlaneId).map((c) => c.sort));
  54. card.move(boardId, card.swimlaneId, listId, minOrder - 1);
  55. }
  56. if(action.actionType === 'moveCardToBottom'){
  57. let listId;
  58. let list;
  59. if(action.listTitle === '*'){
  60. listId = card.listId;
  61. list = card.list();
  62. }else{
  63. list = Lists.findOne({title: action.listTitle, boardId});
  64. listId = list._id;
  65. }
  66. const maxOrder = _.max(list.cardsUnfiltered(card.swimlaneId).map((c) => c.sort));
  67. card.move(boardId, card.swimlaneId, listId, maxOrder + 1);
  68. }
  69. if(action.actionType === 'sendEmail'){
  70. const to = action.emailTo;
  71. const text = action.emailMsg || '';
  72. const subject = action.emailSubject || '';
  73. try {
  74. Email.send({
  75. to,
  76. from: Accounts.emailTemplates.from,
  77. subject,
  78. text,
  79. });
  80. } catch (e) {
  81. // eslint-disable-next-line no-console
  82. console.error(e);
  83. return;
  84. }
  85. }
  86. if(action.actionType === 'setDate') {
  87. try {
  88. const currentDateTime = new Date();
  89. switch (action.dateField){
  90. case 'startAt': {
  91. const resStart = card.getStart();
  92. if (typeof resStart === 'undefined') {
  93. card.setStart(currentDateTime);
  94. }
  95. break;
  96. }
  97. case 'endAt': {
  98. const resEnd = card.getEnd();
  99. if (typeof resEnd === 'undefined') {
  100. card.setEnd(currentDateTime);
  101. }
  102. break;
  103. }
  104. case 'dueAt': {
  105. const resDue = card.getDue();
  106. if (typeof resDue === 'undefined') {
  107. card.setDue(currentDateTime);
  108. }
  109. break;
  110. }
  111. case 'receivedAt': {
  112. const resReceived = card.getReceived();
  113. if (typeof resReceived === 'undefined') {
  114. card.setReceived(currentDateTime);
  115. }
  116. break;
  117. }
  118. }
  119. } catch (e) {
  120. // eslint-disable-next-line no-console
  121. console.error(e);
  122. return;
  123. }
  124. }
  125. if(action.actionType === 'updateDate'){
  126. const currentDateTimeUpdate = new Date();
  127. switch (action.dateField){
  128. case 'startAt': {
  129. card.setStart(currentDateTimeUpdate);
  130. break;
  131. }
  132. case 'endAt': {
  133. card.setEnd(currentDateTimeUpdate);
  134. break;
  135. }
  136. case 'dueAt': {
  137. card.setDue(currentDateTimeUpdate);
  138. break;
  139. }
  140. case 'receivedAt': {
  141. card.setReceived(currentDateTimeUpdate);
  142. break;
  143. }
  144. }
  145. }
  146. if(action.actionType === 'removeDate'){
  147. switch (action.dateField){
  148. case 'startAt': {
  149. card.unsetStart();
  150. break;
  151. }
  152. case 'endAt': {
  153. card.unsetEnd();
  154. break;
  155. }
  156. case 'dueAt': {
  157. card.unsetDue();
  158. break;
  159. }
  160. case 'receivedAt': {
  161. card.unsetReceived();
  162. break;
  163. }
  164. }
  165. }
  166. if(action.actionType === 'archive'){
  167. card.archive();
  168. }
  169. if(action.actionType === 'unarchive'){
  170. card.restore();
  171. }
  172. if(action.actionType === 'setColor'){
  173. card.setColor(action.selectedColor);
  174. }
  175. if(action.actionType === 'addLabel'){
  176. card.addLabel(action.labelId);
  177. }
  178. if(action.actionType === 'removeLabel'){
  179. card.removeLabel(action.labelId);
  180. }
  181. if(action.actionType === 'addMember'){
  182. const memberId = Users.findOne({username:action.username})._id;
  183. card.assignMember(memberId);
  184. }
  185. if(action.actionType === 'removeMember'){
  186. if(action.username === '*'){
  187. const members = card.members;
  188. for(let i = 0; i< members.length; i++){
  189. card.unassignMember(members[i]);
  190. }
  191. }else{
  192. const memberId = Users.findOne({username:action.username})._id;
  193. card.unassignMember(memberId);
  194. }
  195. }
  196. if(action.actionType === 'checkAll'){
  197. const checkList = Checklists.findOne({'title':action.checklistName, 'cardId':card._id});
  198. checkList.checkAllItems();
  199. }
  200. if(action.actionType === 'uncheckAll'){
  201. const checkList = Checklists.findOne({'title':action.checklistName, 'cardId':card._id});
  202. checkList.uncheckAllItems();
  203. }
  204. if(action.actionType === 'checkItem'){
  205. const checkList = Checklists.findOne({'title':action.checklistName, 'cardId':card._id});
  206. const checkItem = ChecklistItems.findOne({'title':action.checkItemName, 'checkListId':checkList._id});
  207. checkItem.check();
  208. }
  209. if(action.actionType === 'uncheckItem'){
  210. const checkList = Checklists.findOne({'title':action.checklistName, 'cardId':card._id});
  211. const checkItem = ChecklistItems.findOne({'title':action.checkItemName, 'checkListId':checkList._id});
  212. checkItem.uncheck();
  213. }
  214. if(action.actionType === 'addChecklist'){
  215. Checklists.insert({'title':action.checklistName, 'cardId':card._id, 'sort':0});
  216. }
  217. if(action.actionType === 'removeChecklist'){
  218. Checklists.remove({'title':action.checklistName, 'cardId':card._id, 'sort':0});
  219. }
  220. if(action.actionType === 'addSwimlane'){
  221. Swimlanes.insert({
  222. title: action.swimlaneName,
  223. boardId,
  224. sort: 0,
  225. });
  226. }
  227. if(action.actionType === 'addChecklistWithItems'){
  228. const checkListId = Checklists.insert({'title':action.checklistName, 'cardId':card._id, 'sort':0});
  229. const itemsArray = action.checklistItems.split(',');
  230. const checkList = Checklists.findOne({_id:checkListId});
  231. for(let i = 0; i <itemsArray.length; i++){
  232. ChecklistItems.insert({title:itemsArray[i], checklistId:checkListId, cardId:card._id, 'sort':checkList.itemCount()});
  233. }
  234. }
  235. if(action.actionType === 'createCard'){
  236. const list = Lists.findOne({title:action.listName, boardId});
  237. let listId = '';
  238. let swimlaneId = '';
  239. const swimlane = Swimlanes.findOne({title:action.swimlaneName, boardId});
  240. if(list === undefined){
  241. listId = '';
  242. }else{
  243. listId = list._id;
  244. }
  245. if(swimlane === undefined){
  246. swimlaneId = Swimlanes.findOne({title:'Default', boardId})._id;
  247. }else{
  248. swimlaneId = swimlane._id;
  249. }
  250. Cards.insert({title:action.cardName, listId, swimlaneId, sort:0, boardId});
  251. }
  252. },
  253. };