The Open Source kanban (built with Meteor). Keep variable/table/field names camelCase. For translations, only add Pull Request changes to wekan/i18n/en.i18n.json , other translations are done at https://transifex.com/wekan/wekan only.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wekan/server/rulesHelper.js

337 lines
9.4 KiB

RulesHelper = {
executeRules(activity) {
7 years ago
const matchingRules = this.findMatchingRules(activity);
for (let i = 0; i < matchingRules.length; i++) {
7 years ago
const action = matchingRules[i].getAction();
if (action !== undefined) {
this.performAction(activity, action);
}
7 years ago
}
},
findMatchingRules(activity) {
7 years ago
const activityType = activity.activityType;
if (TriggersDef[activityType] === undefined) {
7 years ago
return [];
}
const matchingFields = TriggersDef[activityType].matchingFields;
const matchingMap = this.buildMatchingFieldsMap(activity, matchingFields);
const matchingTriggers = Triggers.find(matchingMap);
const matchingRules = [];
matchingTriggers.forEach(function(trigger) {
const rule = trigger.getRule();
// Check that for some unknown reason there are some leftover triggers
// not connected to any rules
if (rule !== undefined) {
matchingRules.push(trigger.getRule());
}
7 years ago
});
return matchingRules;
},
buildMatchingFieldsMap(activity, matchingFields) {
const matchingMap = { activityType: activity.activityType };
for (let i = 0; i < matchingFields.length; i++) {
7 years ago
// Creating a matching map with the actual field of the activity
// and with the wildcard (for example: trigger when a card is added
// in any [*] board
matchingMap[matchingFields[i]] = {
$in: [activity[matchingFields[i]], '*'],
};
7 years ago
}
return matchingMap;
},
performAction(activity, action) {
const card = Cards.findOne({ _id: activity.cardId });
7 years ago
const boardId = activity.boardId;
if (
action.actionType === 'moveCardToTop' ||
action.actionType === 'moveCardToBottom'
) {
7 years ago
let list;
let listId;
if (action.listName === '*') {
7 years ago
list = card.list();
if (boardId !== action.boardId) {
list = Lists.findOne({ title: list.title, boardId: action.boardId });
}
} else {
list = Lists.findOne({
title: action.listName,
boardId: action.boardId,
});
7 years ago
}
if (list === undefined) {
listId = '';
} else {
7 years ago
listId = list._id;
}
let swimlane;
let swimlaneId;
if (action.swimlaneName === '*') {
swimlane = Swimlanes.findOne(card.swimlaneId);
if (boardId !== action.boardId) {
swimlane = Swimlanes.findOne({
title: swimlane.title,
boardId: action.boardId,
});
}
} else {
swimlane = Swimlanes.findOne({
title: action.swimlaneName,
boardId: action.boardId,
});
}
if (swimlane === undefined) {
swimlaneId = Swimlanes.findOne({
title: 'Default',
boardId: action.boardId,
})._id;
} else {
swimlaneId = swimlane._id;
}
if (action.actionType === 'moveCardToTop') {
const minOrder = _.min(
list.cardsUnfiltered(swimlaneId).map(c => c.sort),
);
card.move(action.boardId, swimlaneId, listId, minOrder - 1);
} else {
const maxOrder = _.max(
list.cardsUnfiltered(swimlaneId).map(c => c.sort),
);
card.move(action.boardId, swimlaneId, listId, maxOrder + 1);
}
7 years ago
}
if (action.actionType === 'sendEmail') {
const to = action.emailTo;
const text = action.emailMsg || '';
const subject = action.emailSubject || '';
7 years ago
try {
Email.send({
to,
7 years ago
from: Accounts.emailTemplates.from,
subject,
text,
7 years ago
});
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
7 years ago
return;
}
}
if (action.actionType === 'setDate') {
try {
const currentDateTime = new Date();
switch (action.dateField) {
case 'startAt': {
const resStart = card.getStart();
if (typeof resStart === 'undefined') {
card.setStart(currentDateTime);
}
break;
}
case 'endAt': {
const resEnd = card.getEnd();
if (typeof resEnd === 'undefined') {
card.setEnd(currentDateTime);
}
break;
}
case 'dueAt': {
const resDue = card.getDue();
if (typeof resDue === 'undefined') {
card.setDue(currentDateTime);
}
break;
}
case 'receivedAt': {
const resReceived = card.getReceived();
if (typeof resReceived === 'undefined') {
card.setReceived(currentDateTime);
}
break;
}
}
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
return;
}
}
if (action.actionType === 'updateDate') {
const currentDateTimeUpdate = new Date();
switch (action.dateField) {
case 'startAt': {
card.setStart(currentDateTimeUpdate);
break;
}
case 'endAt': {
card.setEnd(currentDateTimeUpdate);
break;
}
case 'dueAt': {
card.setDue(currentDateTimeUpdate);
break;
}
case 'receivedAt': {
card.setReceived(currentDateTimeUpdate);
break;
}
}
}
if (action.actionType === 'removeDate') {
switch (action.dateField) {
case 'startAt': {
card.unsetStart();
break;
}
case 'endAt': {
card.unsetEnd();
break;
}
case 'dueAt': {
card.unsetDue();
break;
}
case 'receivedAt': {
card.unsetReceived();
break;
}
}
}
if (action.actionType === 'archive') {
7 years ago
card.archive();
}
if (action.actionType === 'unarchive') {
7 years ago
card.restore();
}
if (action.actionType === 'setColor') {
card.setColor(action.selectedColor);
}
if (action.actionType === 'addLabel') {
7 years ago
card.addLabel(action.labelId);
}
if (action.actionType === 'removeLabel') {
7 years ago
card.removeLabel(action.labelId);
}
if (action.actionType === 'addMember') {
const memberId = Users.findOne({ username: action.username })._id;
7 years ago
card.assignMember(memberId);
}
if (action.actionType === 'removeMember') {
if (action.username === '*') {
7 years ago
const members = card.members;
for (let i = 0; i < members.length; i++) {
7 years ago
card.unassignMember(members[i]);
}
} else {
const memberId = Users.findOne({ username: action.username })._id;
7 years ago
card.unassignMember(memberId);
}
}
if (action.actionType === 'checkAll') {
const checkList = Checklists.findOne({
title: action.checklistName,
cardId: card._id,
});
7 years ago
checkList.checkAllItems();
}
if (action.actionType === 'uncheckAll') {
const checkList = Checklists.findOne({
title: action.checklistName,
cardId: card._id,
});
7 years ago
checkList.uncheckAllItems();
}
if (action.actionType === 'checkItem') {
const checkList = Checklists.findOne({
title: action.checklistName,
cardId: card._id,
});
const checkItem = ChecklistItems.findOne({
title: action.checkItemName,
checkListId: checkList._id,
});
7 years ago
checkItem.check();
}
if (action.actionType === 'uncheckItem') {
const checkList = Checklists.findOne({
title: action.checklistName,
cardId: card._id,
});
const checkItem = ChecklistItems.findOne({
title: action.checkItemName,
checkListId: checkList._id,
});
7 years ago
checkItem.uncheck();
}
if (action.actionType === 'addChecklist') {
Checklists.insert({
title: action.checklistName,
cardId: card._id,
sort: 0,
});
7 years ago
}
if (action.actionType === 'removeChecklist') {
Checklists.remove({
title: action.checklistName,
cardId: card._id,
sort: 0,
});
7 years ago
}
if (action.actionType === 'addSwimlane') {
Swimlanes.insert({
title: action.swimlaneName,
6 years ago
boardId,
sort: 0,
});
}
if (action.actionType === 'addChecklistWithItems') {
const checkListId = Checklists.insert({
title: action.checklistName,
cardId: card._id,
sort: 0,
});
6 years ago
const itemsArray = action.checklistItems.split(',');
const checkList = Checklists.findOne({ _id: checkListId });
for (let i = 0; i < itemsArray.length; i++) {
ChecklistItems.insert({
title: itemsArray[i],
checklistId: checkListId,
cardId: card._id,
sort: checkList.itemCount(),
});
6 years ago
}
}
if (action.actionType === 'createCard') {
const list = Lists.findOne({ title: action.listName, boardId });
let listId = '';
let swimlaneId = '';
const swimlane = Swimlanes.findOne({
title: action.swimlaneName,
boardId,
});
if (list === undefined) {
listId = '';
} else {
listId = list._id;
}
if (swimlane === undefined) {
swimlaneId = Swimlanes.findOne({ title: 'Default', boardId })._id;
} else {
swimlaneId = swimlane._id;
}
Cards.insert({
title: action.cardName,
listId,
swimlaneId,
sort: 0,
boardId,
});
}
7 years ago
},
};