2015-10-14 17:57:58 +02:00
|
|
|
Meteor.methods({
|
|
|
|
|
importTrelloCard(trelloCard, listId, sortIndex) {
|
2015-10-14 19:28:30 +02:00
|
|
|
// 1. check parameters are ok from a syntax point of view
|
2015-10-14 23:50:12 +02:00
|
|
|
const DateString = Match.Where(function (dateAsString) {
|
2015-10-14 17:57:58 +02:00
|
|
|
check(dateAsString, String);
|
|
|
|
|
return moment(dateAsString, moment.ISO_8601).isValid();
|
|
|
|
|
});
|
2015-10-14 22:09:32 +02:00
|
|
|
try {
|
|
|
|
|
check(trelloCard, Match.ObjectIncluding({
|
2015-10-14 17:57:58 +02:00
|
|
|
name: String,
|
2015-10-14 22:09:32 +02:00
|
|
|
desc: String,
|
|
|
|
|
closed: Boolean,
|
|
|
|
|
dateLastActivity: DateString,
|
|
|
|
|
labels: [Match.ObjectIncluding({
|
|
|
|
|
name: String,
|
|
|
|
|
color: String,
|
|
|
|
|
})],
|
|
|
|
|
actions: [Match.ObjectIncluding({
|
|
|
|
|
type: String,
|
|
|
|
|
date: DateString,
|
|
|
|
|
data: Object,
|
|
|
|
|
})],
|
|
|
|
|
members: [Object],
|
|
|
|
|
}));
|
|
|
|
|
check(listId, String);
|
|
|
|
|
check(sortIndex, Number);
|
2015-10-16 17:49:25 +02:00
|
|
|
} catch (e) {
|
2015-10-14 22:09:32 +02:00
|
|
|
throw new Meteor.Error('error-json-schema');
|
|
|
|
|
}
|
2015-10-14 17:57:58 +02:00
|
|
|
|
2015-10-16 17:49:25 +02:00
|
|
|
// 2. check parameters are ok from a business point of view (exist &
|
|
|
|
|
// authorized)
|
2015-10-14 17:57:58 +02:00
|
|
|
const list = Lists.findOne(listId);
|
2015-10-16 17:49:25 +02:00
|
|
|
if (!list) {
|
2015-10-14 22:09:32 +02:00
|
|
|
throw new Meteor.Error('error-list-doesNotExist');
|
2015-10-14 17:57:58 +02:00
|
|
|
}
|
2015-10-16 17:49:25 +02:00
|
|
|
if (Meteor.isServer) {
|
2015-10-14 19:28:30 +02:00
|
|
|
if (!allowIsBoardMember(Meteor.userId(), Boards.findOne(list.boardId))) {
|
2015-10-14 22:09:32 +02:00
|
|
|
throw new Meteor.Error('error-board-notAMember');
|
2015-10-14 19:28:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-10-14 17:57:58 +02:00
|
|
|
|
2015-10-14 19:28:30 +02:00
|
|
|
// 3. map all fields for the card to create
|
2015-10-14 17:57:58 +02:00
|
|
|
const dateOfImport = new Date();
|
|
|
|
|
const cardToCreate = {
|
|
|
|
|
archived: trelloCard.closed,
|
2015-10-14 19:54:40 +02:00
|
|
|
boardId: list.boardId,
|
2015-10-14 18:23:46 +02:00
|
|
|
// this is a default date, we'll fetch the actual one from the actions array
|
2015-10-14 17:57:58 +02:00
|
|
|
createdAt: dateOfImport,
|
|
|
|
|
dateLastActivity: dateOfImport,
|
2015-10-14 19:54:40 +02:00
|
|
|
description: trelloCard.desc,
|
2015-10-16 17:49:25 +02:00
|
|
|
labelIds: [],
|
2015-10-14 19:54:40 +02:00
|
|
|
listId: list._id,
|
|
|
|
|
sort: sortIndex,
|
|
|
|
|
title: trelloCard.name,
|
|
|
|
|
// XXX use the original user?
|
|
|
|
|
userId: Meteor.userId(),
|
2015-10-14 17:57:58 +02:00
|
|
|
};
|
2015-10-14 19:28:30 +02:00
|
|
|
|
|
|
|
|
// 4. find actual creation date
|
2015-10-14 23:50:12 +02:00
|
|
|
const creationAction = trelloCard.actions.find((action) => {
|
|
|
|
|
return action.type === 'createCard';
|
|
|
|
|
});
|
2015-10-16 17:49:25 +02:00
|
|
|
if (creationAction) {
|
2015-10-14 18:23:46 +02:00
|
|
|
cardToCreate.createdAt = creationAction.date;
|
|
|
|
|
}
|
2015-10-14 19:28:30 +02:00
|
|
|
|
|
|
|
|
// 5. map labels - create missing ones
|
2015-10-14 17:57:58 +02:00
|
|
|
trelloCard.labels.forEach((currentLabel) => {
|
2015-10-16 17:49:25 +02:00
|
|
|
const { name, color } = currentLabel;
|
|
|
|
|
// `addLabel` won't create dublicate labels (ie labels with the same name
|
|
|
|
|
// and color) so here it is used more in a "enforceLabelExistence" way.
|
|
|
|
|
list.board().addLabel(name, color);
|
|
|
|
|
const { _id: labelId } = list.board().getLabel(name, color);
|
|
|
|
|
if (labelId) {
|
2015-10-14 17:57:58 +02:00
|
|
|
cardToCreate.labelIds.push(labelId);
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-10-14 19:28:30 +02:00
|
|
|
|
|
|
|
|
// 6. insert new card into list
|
2015-10-14 18:23:46 +02:00
|
|
|
const cardId = Cards.direct.insert(cardToCreate);
|
2015-10-14 19:54:40 +02:00
|
|
|
Activities.direct.insert({
|
|
|
|
|
activityType: 'importCard',
|
|
|
|
|
boardId: cardToCreate.boardId,
|
2015-10-14 23:50:12 +02:00
|
|
|
cardId,
|
2015-10-14 19:54:40 +02:00
|
|
|
createdAt: dateOfImport,
|
|
|
|
|
listId: cardToCreate.listId,
|
|
|
|
|
source: {
|
|
|
|
|
id: trelloCard.id,
|
|
|
|
|
system: 'Trello',
|
|
|
|
|
url: trelloCard.url,
|
|
|
|
|
},
|
2015-10-16 17:49:25 +02:00
|
|
|
// we attribute the import to current user, not the one from the original
|
|
|
|
|
// card
|
2015-10-14 19:54:40 +02:00
|
|
|
userId: Meteor.userId(),
|
|
|
|
|
});
|
2015-10-14 19:28:30 +02:00
|
|
|
|
|
|
|
|
// 7. parse actions and add comments
|
2015-10-14 17:57:58 +02:00
|
|
|
trelloCard.actions.forEach((currentAction) => {
|
2015-10-16 17:49:25 +02:00
|
|
|
if (currentAction.type === 'commentCard') {
|
2015-10-14 17:57:58 +02:00
|
|
|
const commentToCreate = {
|
|
|
|
|
boardId: list.boardId,
|
2015-10-14 23:50:12 +02:00
|
|
|
cardId,
|
2015-10-14 17:57:58 +02:00
|
|
|
createdAt: currentAction.date,
|
2015-10-14 18:23:46 +02:00
|
|
|
text: currentAction.data.text,
|
2015-10-14 19:54:40 +02:00
|
|
|
// XXX use the original comment user instead
|
2015-10-14 18:23:46 +02:00
|
|
|
userId: Meteor.userId(),
|
2015-10-14 17:57:58 +02:00
|
|
|
};
|
2015-10-14 18:23:46 +02:00
|
|
|
const commentId = CardComments.direct.insert(commentToCreate);
|
|
|
|
|
Activities.direct.insert({
|
|
|
|
|
activityType: 'addComment',
|
|
|
|
|
boardId: commentToCreate.boardId,
|
|
|
|
|
cardId: commentToCreate.cardId,
|
2015-10-14 23:50:12 +02:00
|
|
|
commentId,
|
2015-10-14 18:23:46 +02:00
|
|
|
createdAt: commentToCreate.createdAt,
|
|
|
|
|
userId: commentToCreate.userId,
|
|
|
|
|
});
|
2015-10-14 17:57:58 +02:00
|
|
|
}
|
|
|
|
|
});
|
2015-10-14 18:23:46 +02:00
|
|
|
return cardId;
|
2015-10-14 17:57:58 +02:00
|
|
|
},
|
|
|
|
|
});
|