direct hook to bypass the checking step with API request

This commit is contained in:
zago 2017-07-19 10:27:23 +02:00
parent cbcf10a02b
commit e2ee005a5f
2 changed files with 186 additions and 141 deletions

View file

@ -56,6 +56,16 @@ CardComments.helpers({
CardComments.hookOptions.after.update = { fetchPrevious: false }; CardComments.hookOptions.after.update = { fetchPrevious: false };
function commentCreation(userId, doc){
Activities.insert({
userId,
activityType: 'addComment',
boardId: doc.boardId,
cardId: doc.cardId,
commentId: doc._id,
});
}
if (Meteor.isServer) { if (Meteor.isServer) {
// Comments are often fetched within a card, so we create an index to make these // Comments are often fetched within a card, so we create an index to make these
// queries more efficient. // queries more efficient.
@ -64,13 +74,7 @@ if (Meteor.isServer) {
}); });
CardComments.after.insert((userId, doc) => { CardComments.after.insert((userId, doc) => {
Activities.insert({ commentCreation(userId, doc);
userId,
activityType: 'addComment',
boardId: doc.boardId,
cardId: doc.cardId,
commentId: doc._id,
});
}); });
CardComments.after.remove((userId, doc) => { CardComments.after.remove((userId, doc) => {
@ -114,12 +118,16 @@ if (Meteor.isServer) {
Authentication.checkUserId( req.userId); Authentication.checkUserId( req.userId);
const paramBoardId = req.params.boardId; const paramBoardId = req.params.boardId;
const paramCardId = req.params.cardId; const paramCardId = req.params.cardId;
const id = CardComments.insert({ const id = CardComments.direct.insert({
userId: req.body.authorId, userId: req.body.authorId,
text: req.body.comment, text: req.body.comment,
cardId: paramCardId, cardId: paramCardId,
boardId: paramBoardId, boardId: paramBoardId,
}); });
const cardComment = CardComments.findOne({_id: id, cardId:paramCardId, boardId: paramBoardId });
commentCreation(req.body.authorId, cardComment);
JsonRoutes.sendResult(res, { JsonRoutes.sendResult(res, {
code: 200, code: 200,
data: { data: {

View file

@ -263,25 +263,23 @@ Cards.mutations({
}, },
}); });
if (Meteor.isServer) {
// Cards are often fetched within a board, so we create an index to make these
// queries more efficient.
Meteor.startup(() => {
Cards._collection._ensureIndex({ boardId: 1, createdAt: -1 });
});
Cards.after.insert((userId, doc) => { //FUNCTIONS FOR creation of Activities
function cardMove(userId, doc, fieldNames, oldListId) {
if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
Activities.insert({ Activities.insert({
userId, userId,
activityType: 'createCard', oldListId,
boardId: doc.boardId, activityType: 'moveCard',
listId: doc.listId, listId: doc.listId,
boardId: doc.boardId,
cardId: doc._id, cardId: doc._id,
}); });
}); }
}
// New activity for card (un)archivage function cardState(userId, doc, fieldNames) {
Cards.after.update((userId, doc, fieldNames) => {
if (_.contains(fieldNames, 'archived')) { if (_.contains(fieldNames, 'archived')) {
if (doc.archived) { if (doc.archived) {
Activities.insert({ Activities.insert({
@ -301,25 +299,9 @@ if (Meteor.isServer) {
}); });
} }
} }
});
// New activity for card moves
Cards.after.update(function (userId, doc, fieldNames) {
const oldListId = this.previous.listId;
if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
Activities.insert({
userId,
oldListId,
activityType: 'moveCard',
listId: doc.listId,
boardId: doc.boardId,
cardId: doc._id,
});
} }
});
// Add a new activity if we add or remove a member to the card function cardMembers(userId, doc, fieldNames, modifier) {
Cards.before.update((userId, doc, fieldNames, modifier) => {
if (!_.contains(fieldNames, 'members')) if (!_.contains(fieldNames, 'members'))
return; return;
let memberId; let memberId;
@ -351,11 +333,19 @@ if (Meteor.isServer) {
}); });
} }
} }
}); }
// Remove all activities associated with a card if we remove the card function cardCreation(userId, doc) {
// Remove also card_comments / checklists / attachments Activities.insert({
Cards.after.remove((userId, doc) => { userId,
activityType: 'createCard',
boardId: doc.boardId,
listId: doc.listId,
cardId: doc._id,
});
}
function cardRemover(userId, doc) {
Activities.remove({ Activities.remove({
cardId: doc._id, cardId: doc._id,
}); });
@ -368,6 +358,40 @@ if (Meteor.isServer) {
Attachments.remove({ Attachments.remove({
cardId: doc._id, cardId: doc._id,
}); });
}
if (Meteor.isServer) {
// Cards are often fetched within a board, so we create an index to make these
// queries more efficient.
Meteor.startup(() => {
Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
});
Cards.after.insert((userId, doc) => {
cardCreation(userId, doc);
});
// New activity for card (un)archivage
Cards.after.update((userId, doc, fieldNames) => {
cardState(userId, doc, fieldNames);
});
//New activity for card moves
Cards.after.update(function (userId, doc, fieldNames) {
const oldListId = this.previous.listId;
cardMove(userId, doc, fieldNames, oldListId);
});
// Add a new activity if we add or remove a member to the card
Cards.before.update((userId, doc, fieldNames, modifier) => {
cardMembers(userId, doc, fieldNames, modifier);
});
// Remove all activities associated with a card if we remove the card
// Remove also card_comments / checklists / attachments
Cards.after.remove((userId, doc) => {
cardRemover(userId, doc);
}); });
} }
//LISTS REST API //LISTS REST API
@ -405,7 +429,7 @@ if (Meteor.isServer) {
const paramListId = req.params.listId; const paramListId = req.params.listId;
const check = Users.findOne({_id: req.body.authorId}); const check = Users.findOne({_id: req.body.authorId});
if (typeof check !== 'undefined') { if (typeof check !== 'undefined') {
const id = Cards.insert({ const id = Cards.direct.insert({
title: req.body.title, title: req.body.title,
boardId: paramBoardId, boardId: paramBoardId,
listId: paramListId, listId: paramListId,
@ -420,6 +444,10 @@ if (Meteor.isServer) {
_id: id, _id: id,
}, },
}); });
const card = Cards.findOne({_id:id});
cardCreation(req.body.authorId, card);
} else { } else {
JsonRoutes.sendResult(res, { JsonRoutes.sendResult(res, {
code: 401, code: 401,
@ -432,19 +460,24 @@ if (Meteor.isServer) {
const paramBoardId = req.params.boardId; const paramBoardId = req.params.boardId;
const paramCardId = req.params.cardId; const paramCardId = req.params.cardId;
const paramListId = req.params.listId; const paramListId = req.params.listId;
if (req.body.hasOwnProperty('title')) { if (req.body.hasOwnProperty('title')) {
const newTitle = req.body.title; const newTitle = req.body.title;
Cards.update({ _id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false }, Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
{$set: {title: newTitle}}); {$set: {title: newTitle}});
} }
if (req.body.hasOwnProperty('listId')) { if (req.body.hasOwnProperty('listId')) {
const newParamListId = req.body.listId; const newParamListId = req.body.listId;
Cards.update({ _id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false }, Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
{$set: {listId: newParamListId}}); {$set: {listId: newParamListId}});
const card = Cards.findOne({_id: paramCardId} );
cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
} }
if (req.body.hasOwnProperty('description')) { if (req.body.hasOwnProperty('description')) {
const newDescription = req.body.description; const newDescription = req.body.description;
Cards.update({ _id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false }, Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
{$set: {description: newDescription}}); {$set: {description: newDescription}});
} }
JsonRoutes.sendResult(res, { JsonRoutes.sendResult(res, {
@ -461,12 +494,16 @@ if (Meteor.isServer) {
const paramBoardId = req.params.boardId; const paramBoardId = req.params.boardId;
const paramListId = req.params.listId; const paramListId = req.params.listId;
const paramCardId = req.params.cardId; const paramCardId = req.params.cardId;
Cards.remove({ _id: paramCardId, listId: paramListId, boardId: paramBoardId });
Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
const card = Cards.find({_id: paramCardId} );
cardRemover(req.body.authorId, card);
JsonRoutes.sendResult(res, { JsonRoutes.sendResult(res, {
code: 200, code: 200,
data: { data: {
_id: paramCardId, _id: paramCardId,
}, },
}); });
}); });
} }