2017-08-25 02:59:20 +02:00
|
|
|
CustomFields = new Mongo.Collection('customFields');
|
|
|
|
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* A custom field on a card in the board
|
|
|
|
*/
|
2017-08-25 02:59:20 +02:00
|
|
|
CustomFields.attachSchema(new SimpleSchema({
|
2019-03-08 23:39:33 +01:00
|
|
|
boardIds: {
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* the ID of the board
|
|
|
|
*/
|
2019-03-08 23:39:33 +01:00
|
|
|
type: [String],
|
2017-08-25 02:59:20 +02:00
|
|
|
},
|
|
|
|
name: {
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* name of the custom field
|
|
|
|
*/
|
2017-08-25 02:59:20 +02:00
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
type: {
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* type of the custom field
|
|
|
|
*/
|
2017-08-25 02:59:20 +02:00
|
|
|
type: String,
|
2018-05-18 10:24:51 +02:00
|
|
|
allowedValues: ['text', 'number', 'date', 'dropdown'],
|
2017-09-18 00:46:17 +02:00
|
|
|
},
|
|
|
|
settings: {
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* settings of the custom field
|
|
|
|
*/
|
2017-09-18 00:46:17 +02:00
|
|
|
type: Object,
|
|
|
|
},
|
|
|
|
'settings.dropdownItems': {
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* list of drop down items objects
|
|
|
|
*/
|
2017-09-18 00:46:17 +02:00
|
|
|
type: [Object],
|
2018-05-18 10:24:51 +02:00
|
|
|
optional: true,
|
2017-09-18 00:46:17 +02:00
|
|
|
},
|
|
|
|
'settings.dropdownItems.$': {
|
|
|
|
type: new SimpleSchema({
|
|
|
|
_id: {
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* ID of the drop down item
|
|
|
|
*/
|
2017-09-18 00:46:17 +02:00
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
name: {
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* name of the drop down item
|
|
|
|
*/
|
2017-09-18 00:46:17 +02:00
|
|
|
type: String,
|
|
|
|
},
|
2018-05-18 10:24:51 +02:00
|
|
|
}),
|
2017-08-25 02:59:20 +02:00
|
|
|
},
|
|
|
|
showOnCard: {
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* should we show on the cards this custom field
|
|
|
|
*/
|
2017-08-25 02:59:20 +02:00
|
|
|
type: Boolean,
|
2018-05-18 10:24:51 +02:00
|
|
|
},
|
2018-11-05 21:46:57 +01:00
|
|
|
automaticallyOnCard: {
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* should the custom fields automatically be added on cards?
|
|
|
|
*/
|
2018-11-05 21:46:57 +01:00
|
|
|
type: Boolean,
|
|
|
|
},
|
|
|
|
showLabelOnMiniCard: {
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* should the label of the custom field be shown on minicards?
|
|
|
|
*/
|
2018-11-05 21:46:57 +01:00
|
|
|
type: Boolean,
|
|
|
|
},
|
2017-08-25 02:59:20 +02:00
|
|
|
}));
|
|
|
|
|
2019-03-16 22:43:47 +01:00
|
|
|
CustomFields.mutations({
|
|
|
|
addBoard(boardId) {
|
|
|
|
if (boardId) {
|
|
|
|
return {
|
|
|
|
$push: {
|
|
|
|
boardIds: boardId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-08-25 02:59:20 +02:00
|
|
|
CustomFields.allow({
|
|
|
|
insert(userId, doc) {
|
2019-03-16 22:43:47 +01:00
|
|
|
return allowIsAnyBoardMember(userId, Boards.find({
|
|
|
|
_id: {$in: doc.boardIds},
|
|
|
|
}).fetch());
|
2017-08-25 02:59:20 +02:00
|
|
|
},
|
|
|
|
update(userId, doc) {
|
2019-03-16 22:43:47 +01:00
|
|
|
return allowIsAnyBoardMember(userId, Boards.find({
|
|
|
|
_id: {$in: doc.boardIds},
|
|
|
|
}).fetch());
|
2017-08-25 02:59:20 +02:00
|
|
|
},
|
|
|
|
remove(userId, doc) {
|
2019-03-16 22:43:47 +01:00
|
|
|
return allowIsAnyBoardMember(userId, Boards.find({
|
|
|
|
_id: {$in: doc.boardIds},
|
|
|
|
}).fetch());
|
2017-08-25 02:59:20 +02:00
|
|
|
},
|
2019-03-16 22:43:47 +01:00
|
|
|
fetch: ['userId', 'boardIds'],
|
2017-08-25 02:59:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// not sure if we need this?
|
|
|
|
//CustomFields.hookOptions.after.update = { fetchPrevious: false };
|
|
|
|
|
|
|
|
function customFieldCreation(userId, doc){
|
|
|
|
Activities.insert({
|
|
|
|
userId,
|
|
|
|
activityType: 'createCustomField',
|
2019-03-16 22:43:47 +01:00
|
|
|
boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
|
2017-08-25 02:59:20 +02:00
|
|
|
customFieldId: doc._id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-27 04:06:21 +03:00
|
|
|
function customFieldDeletion(userId, doc){
|
|
|
|
Activities.insert({
|
|
|
|
userId,
|
|
|
|
activityType: 'deleteCustomField',
|
|
|
|
boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
|
|
|
|
customFieldId: doc._id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// This has some bug, it does not show edited customField value at Outgoing Webhook,
|
|
|
|
// instead it shows undefined, and no listId and swimlaneId.
|
|
|
|
function customFieldEdit(userId, doc){
|
|
|
|
const card = Cards.findOne(doc.cardId);
|
|
|
|
Activities.insert({
|
|
|
|
userId,
|
|
|
|
activityType: 'editCustomField',
|
|
|
|
boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
|
|
|
|
customFieldId: doc._id,
|
|
|
|
listId: card.listId,
|
|
|
|
swimlaneId: card.swimlaneId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-25 02:59:20 +02:00
|
|
|
if (Meteor.isServer) {
|
2019-02-28 11:44:29 -06:00
|
|
|
Meteor.startup(() => {
|
2019-03-16 22:43:47 +01:00
|
|
|
CustomFields._collection._ensureIndex({ boardIds: 1 });
|
2019-02-28 11:44:29 -06:00
|
|
|
});
|
2017-08-25 02:59:20 +02:00
|
|
|
|
|
|
|
CustomFields.after.insert((userId, doc) => {
|
|
|
|
customFieldCreation(userId, doc);
|
|
|
|
});
|
|
|
|
|
2019-03-16 22:43:47 +01:00
|
|
|
CustomFields.before.update((userId, doc, fieldNames, modifier) => {
|
|
|
|
if (_.contains(fieldNames, 'boardIds') && modifier.$pull) {
|
|
|
|
Cards.update(
|
|
|
|
{boardId: modifier.$pull.boardIds, 'customFields._id': doc._id},
|
|
|
|
{$pull: {'customFields': {'_id': doc._id}}},
|
|
|
|
{multi: true}
|
|
|
|
);
|
2019-06-27 04:06:21 +03:00
|
|
|
customFieldEdit(userId, doc);
|
2019-03-16 22:43:47 +01:00
|
|
|
Activities.remove({
|
|
|
|
customFieldId: doc._id,
|
|
|
|
boardId: modifier.$pull.boardIds,
|
2019-06-27 04:06:21 +03:00
|
|
|
listId: card.listId,
|
|
|
|
swimlaneId: card.swimlaneId,
|
2019-03-16 22:43:47 +01:00
|
|
|
});
|
|
|
|
} else if (_.contains(fieldNames, 'boardIds') && modifier.$push) {
|
|
|
|
Activities.insert({
|
|
|
|
userId,
|
|
|
|
activityType: 'createCustomField',
|
|
|
|
boardId: modifier.$push.boardIds,
|
|
|
|
customFieldId: doc._id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
CustomFields.before.remove((userId, doc) => {
|
2019-06-27 04:06:21 +03:00
|
|
|
customFieldDeletion(userId, doc);
|
2017-08-27 22:31:24 +02:00
|
|
|
Activities.remove({
|
|
|
|
customFieldId: doc._id,
|
|
|
|
});
|
2018-10-12 11:18:11 +02:00
|
|
|
|
|
|
|
Cards.update(
|
2019-03-16 22:43:47 +01:00
|
|
|
{boardId: {$in: doc.boardIds}, 'customFields._id': doc._id},
|
2018-10-12 11:18:11 +02:00
|
|
|
{$pull: {'customFields': {'_id': doc._id}}},
|
|
|
|
{multi: true}
|
|
|
|
);
|
2017-08-25 02:59:20 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//CUSTOM FIELD REST API
|
|
|
|
if (Meteor.isServer) {
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* @operation get_all_custom_fields
|
|
|
|
* @summary Get the list of Custom Fields attached to a board
|
|
|
|
*
|
|
|
|
* @param {string} boardID the ID of the board
|
|
|
|
* @return_type [{_id: string,
|
|
|
|
* name: string,
|
|
|
|
* type: string}]
|
|
|
|
*/
|
2018-05-18 10:24:51 +02:00
|
|
|
JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function (req, res) {
|
2017-08-25 02:59:20 +02:00
|
|
|
Authentication.checkUserId( req.userId);
|
|
|
|
const paramBoardId = req.params.boardId;
|
|
|
|
JsonRoutes.sendResult(res, {
|
|
|
|
code: 200,
|
2019-03-08 23:39:33 +01:00
|
|
|
data: CustomFields.find({ boardIds: {$in: [paramBoardId]} }).map(function (cf) {
|
2018-07-02 18:34:46 +02:00
|
|
|
return {
|
|
|
|
_id: cf._id,
|
|
|
|
name: cf.name,
|
|
|
|
type: cf.type,
|
|
|
|
};
|
|
|
|
}),
|
2017-08-25 02:59:20 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* @operation get_custom_field
|
|
|
|
* @summary Get a Custom Fields attached to a board
|
|
|
|
*
|
|
|
|
* @param {string} boardID the ID of the board
|
|
|
|
* @param {string} customFieldId the ID of the custom field
|
|
|
|
* @return_type CustomFields
|
|
|
|
*/
|
2018-05-18 10:24:51 +02:00
|
|
|
JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
|
2017-08-25 02:59:20 +02:00
|
|
|
Authentication.checkUserId( req.userId);
|
|
|
|
const paramBoardId = req.params.boardId;
|
|
|
|
const paramCustomFieldId = req.params.customFieldId;
|
|
|
|
JsonRoutes.sendResult(res, {
|
|
|
|
code: 200,
|
2019-03-08 23:39:33 +01:00
|
|
|
data: CustomFields.findOne({ _id: paramCustomFieldId, boardIds: {$in: [paramBoardId]} }),
|
2017-08-25 02:59:20 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* @operation new_custom_field
|
|
|
|
* @summary Create a Custom Field
|
|
|
|
*
|
|
|
|
* @param {string} boardID the ID of the board
|
|
|
|
* @param {string} name the name of the custom field
|
|
|
|
* @param {string} type the type of the custom field
|
|
|
|
* @param {string} settings the settings object of the custom field
|
|
|
|
* @param {boolean} showOnCard should we show the custom field on cards?
|
|
|
|
* @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards?
|
|
|
|
* @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
|
|
|
|
* @return_type {_id: string}
|
|
|
|
*/
|
2018-05-18 10:24:51 +02:00
|
|
|
JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function (req, res) {
|
2017-08-25 02:59:20 +02:00
|
|
|
Authentication.checkUserId( req.userId);
|
|
|
|
const paramBoardId = req.params.boardId;
|
|
|
|
const id = CustomFields.direct.insert({
|
|
|
|
name: req.body.name,
|
|
|
|
type: req.body.type,
|
2017-09-18 00:46:17 +02:00
|
|
|
settings: req.body.settings,
|
2017-08-25 02:59:20 +02:00
|
|
|
showOnCard: req.body.showOnCard,
|
2018-11-05 21:46:57 +01:00
|
|
|
automaticallyOnCard: req.body.automaticallyOnCard,
|
|
|
|
showLabelOnMiniCard: req.body.showLabelOnMiniCard,
|
2019-03-16 22:43:47 +01:00
|
|
|
boardIds: {$in: [paramBoardId]},
|
2017-08-25 02:59:20 +02:00
|
|
|
});
|
|
|
|
|
2019-03-08 23:39:33 +01:00
|
|
|
const customField = CustomFields.findOne({_id: id, boardIds: {$in: [paramBoardId]} });
|
2017-08-25 02:59:20 +02:00
|
|
|
customFieldCreation(req.body.authorId, customField);
|
|
|
|
|
|
|
|
JsonRoutes.sendResult(res, {
|
|
|
|
code: 200,
|
|
|
|
data: {
|
|
|
|
_id: id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-26 07:27:24 +02:00
|
|
|
/**
|
|
|
|
* @operation delete_custom_field
|
|
|
|
* @summary Delete a Custom Fields attached to a board
|
|
|
|
*
|
|
|
|
* @description The Custom Field can't be retrieved after this operation
|
|
|
|
*
|
|
|
|
* @param {string} boardID the ID of the board
|
|
|
|
* @param {string} customFieldId the ID of the custom field
|
|
|
|
* @return_type {_id: string}
|
|
|
|
*/
|
2018-05-18 10:24:51 +02:00
|
|
|
JsonRoutes.add('DELETE', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
|
2017-08-25 02:59:20 +02:00
|
|
|
Authentication.checkUserId( req.userId);
|
|
|
|
const paramBoardId = req.params.boardId;
|
|
|
|
const id = req.params.customFieldId;
|
2019-03-16 22:43:47 +01:00
|
|
|
CustomFields.remove({ _id: id, boardIds: {$in: [paramBoardId]} });
|
2017-08-25 02:59:20 +02:00
|
|
|
JsonRoutes.sendResult(res, {
|
|
|
|
code: 200,
|
|
|
|
data: {
|
|
|
|
_id: id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|