summaryrefslogtreecommitdiffstats
path: root/models/customFields.js
diff options
context:
space:
mode:
authorJustin Reynolds <justinr1234@gmail.com>2019-06-26 17:47:27 -0500
committerJustin Reynolds <justinr1234@gmail.com>2019-06-27 09:13:20 -0500
commitc60e80d25baa6a81b28f6090ca848553d20b2bb7 (patch)
tree64bcea66932f4b4635d5df6901f18d5ba6db0a37 /models/customFields.js
parentfb728baf0c87bae5fa39d92089b667ff1ed69fa6 (diff)
downloadwekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.tar.gz
wekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.tar.bz2
wekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.zip
Add createdAt and modifiedAt to all collections
Diffstat (limited to 'models/customFields.js')
-rw-r--r--models/customFields.js297
1 files changed, 181 insertions, 116 deletions
diff --git a/models/customFields.js b/models/customFields.js
index 83033cb4..8b51c0a3 100644
--- a/models/customFields.js
+++ b/models/customFields.js
@@ -3,74 +3,100 @@ CustomFields = new Mongo.Collection('customFields');
/**
* A custom field on a card in the board
*/
-CustomFields.attachSchema(new SimpleSchema({
- boardIds: {
- /**
- * the ID of the board
- */
- type: [String],
- },
- name: {
- /**
- * name of the custom field
- */
- type: String,
- },
- type: {
- /**
- * type of the custom field
- */
- type: String,
- allowedValues: ['text', 'number', 'date', 'dropdown'],
- },
- settings: {
- /**
- * settings of the custom field
- */
- type: Object,
- },
- 'settings.dropdownItems': {
- /**
- * list of drop down items objects
- */
- type: [Object],
- optional: true,
- },
- 'settings.dropdownItems.$': {
- type: new SimpleSchema({
- _id: {
- /**
- * ID of the drop down item
- */
- type: String,
+CustomFields.attachSchema(
+ new SimpleSchema({
+ boardIds: {
+ /**
+ * the ID of the board
+ */
+ type: [String],
+ },
+ name: {
+ /**
+ * name of the custom field
+ */
+ type: String,
+ },
+ type: {
+ /**
+ * type of the custom field
+ */
+ type: String,
+ allowedValues: ['text', 'number', 'date', 'dropdown'],
+ },
+ settings: {
+ /**
+ * settings of the custom field
+ */
+ type: Object,
+ },
+ 'settings.dropdownItems': {
+ /**
+ * list of drop down items objects
+ */
+ type: [Object],
+ optional: true,
+ },
+ 'settings.dropdownItems.$': {
+ type: new SimpleSchema({
+ _id: {
+ /**
+ * ID of the drop down item
+ */
+ type: String,
+ },
+ name: {
+ /**
+ * name of the drop down item
+ */
+ type: String,
+ },
+ }),
+ },
+ showOnCard: {
+ /**
+ * should we show on the cards this custom field
+ */
+ type: Boolean,
+ },
+ automaticallyOnCard: {
+ /**
+ * should the custom fields automatically be added on cards?
+ */
+ type: Boolean,
+ },
+ showLabelOnMiniCard: {
+ /**
+ * should the label of the custom field be shown on minicards?
+ */
+ type: Boolean,
+ },
+ createdAt: {
+ type: Date,
+ optional: true,
+ // eslint-disable-next-line consistent-return
+ autoValue() {
+ if (this.isInsert) {
+ return new Date();
+ } else {
+ this.unset();
+ }
},
- name: {
- /**
- * name of the drop down item
- */
- type: String,
+ },
+ modifiedAt: {
+ type: Date,
+ denyUpdate: false,
+ // eslint-disable-next-line consistent-return
+ autoValue() {
+ if (this.isInsert || this.isUpsert || this.isUpdate) {
+ return new Date();
+ } else {
+ this.unset();
+ }
},
- }),
- },
- showOnCard: {
- /**
- * should we show on the cards this custom field
- */
- type: Boolean,
- },
- automaticallyOnCard: {
- /**
- * should the custom fields automatically be added on cards?
- */
- type: Boolean,
- },
- showLabelOnMiniCard: {
- /**
- * should the label of the custom field be shown on minicards?
- */
- type: Boolean,
- },
-}));
+ },
+ })
+);
CustomFields.mutations({
addBoard(boardId) {
@@ -88,19 +114,28 @@ CustomFields.mutations({
CustomFields.allow({
insert(userId, doc) {
- return allowIsAnyBoardMember(userId, Boards.find({
- _id: {$in: doc.boardIds},
- }).fetch());
+ return allowIsAnyBoardMember(
+ userId,
+ Boards.find({
+ _id: { $in: doc.boardIds },
+ }).fetch()
+ );
},
update(userId, doc) {
- return allowIsAnyBoardMember(userId, Boards.find({
- _id: {$in: doc.boardIds},
- }).fetch());
+ return allowIsAnyBoardMember(
+ userId,
+ Boards.find({
+ _id: { $in: doc.boardIds },
+ }).fetch()
+ );
},
remove(userId, doc) {
- return allowIsAnyBoardMember(userId, Boards.find({
- _id: {$in: doc.boardIds},
- }).fetch());
+ return allowIsAnyBoardMember(
+ userId,
+ Boards.find({
+ _id: { $in: doc.boardIds },
+ }).fetch()
+ );
},
fetch: ['userId', 'boardIds'],
});
@@ -108,7 +143,7 @@ CustomFields.allow({
// not sure if we need this?
//CustomFields.hookOptions.after.update = { fetchPrevious: false };
-function customFieldCreation(userId, doc){
+function customFieldCreation(userId, doc) {
Activities.insert({
userId,
activityType: 'createCustomField',
@@ -142,6 +177,7 @@ function customFieldEdit(userId, doc){
if (Meteor.isServer) {
Meteor.startup(() => {
+ CustomFields._collection._ensureIndex({ modifiedAt: -1 });
CustomFields._collection._ensureIndex({ boardIds: 1 });
});
@@ -149,12 +185,17 @@ if (Meteor.isServer) {
customFieldCreation(userId, doc);
});
+ CustomFields.before.update((userId, doc, fieldNames, modifier, options) => {
+ modifier.$set = modifier.$set || {};
+ modifier.$set.modifiedAt = Date.now();
+ });
+
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}
+ { boardId: modifier.$pull.boardIds, 'customFields._id': doc._id },
+ { $pull: { customFields: { _id: doc._id } } },
+ { multi: true }
);
customFieldEdit(userId, doc);
Activities.remove({
@@ -180,9 +221,9 @@ if (Meteor.isServer) {
});
Cards.update(
- {boardId: {$in: doc.boardIds}, 'customFields._id': doc._id},
- {$pull: {'customFields': {'_id': doc._id}}},
- {multi: true}
+ { boardId: { $in: doc.boardIds }, 'customFields._id': doc._id },
+ { $pull: { customFields: { _id: doc._id } } },
+ { multi: true }
);
});
}
@@ -198,18 +239,23 @@ if (Meteor.isServer) {
* name: string,
* type: string}]
*/
- JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function (req, res) {
- Authentication.checkUserId( req.userId);
+ JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function(
+ req,
+ res
+ ) {
+ Authentication.checkUserId(req.userId);
const paramBoardId = req.params.boardId;
JsonRoutes.sendResult(res, {
code: 200,
- data: CustomFields.find({ boardIds: {$in: [paramBoardId]} }).map(function (cf) {
- return {
- _id: cf._id,
- name: cf.name,
- type: cf.type,
- };
- }),
+ data: CustomFields.find({ boardIds: { $in: [paramBoardId] } }).map(
+ function(cf) {
+ return {
+ _id: cf._id,
+ name: cf.name,
+ type: cf.type,
+ };
+ }
+ ),
});
});
@@ -221,15 +267,22 @@ if (Meteor.isServer) {
* @param {string} customFieldId the ID of the custom field
* @return_type CustomFields
*/
- JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
- Authentication.checkUserId( req.userId);
- const paramBoardId = req.params.boardId;
- const paramCustomFieldId = req.params.customFieldId;
- JsonRoutes.sendResult(res, {
- code: 200,
- data: CustomFields.findOne({ _id: paramCustomFieldId, boardIds: {$in: [paramBoardId]} }),
- });
- });
+ JsonRoutes.add(
+ 'GET',
+ '/api/boards/:boardId/custom-fields/:customFieldId',
+ function(req, res) {
+ Authentication.checkUserId(req.userId);
+ const paramBoardId = req.params.boardId;
+ const paramCustomFieldId = req.params.customFieldId;
+ JsonRoutes.sendResult(res, {
+ code: 200,
+ data: CustomFields.findOne({
+ _id: paramCustomFieldId,
+ boardIds: { $in: [paramBoardId] },
+ }),
+ });
+ }
+ );
/**
* @operation new_custom_field
@@ -244,8 +297,11 @@ if (Meteor.isServer) {
* @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
* @return_type {_id: string}
*/
- JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function (req, res) {
- Authentication.checkUserId( req.userId);
+ JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function(
+ req,
+ res
+ ) {
+ Authentication.checkUserId(req.userId);
const paramBoardId = req.params.boardId;
const id = CustomFields.direct.insert({
name: req.body.name,
@@ -254,10 +310,13 @@ if (Meteor.isServer) {
showOnCard: req.body.showOnCard,
automaticallyOnCard: req.body.automaticallyOnCard,
showLabelOnMiniCard: req.body.showLabelOnMiniCard,
- boardIds: {$in: [paramBoardId]},
+ boardIds: { $in: [paramBoardId] },
});
- const customField = CustomFields.findOne({_id: id, boardIds: {$in: [paramBoardId]} });
+ const customField = CustomFields.findOne({
+ _id: id,
+ boardIds: { $in: [paramBoardId] },
+ });
customFieldCreation(req.body.authorId, customField);
JsonRoutes.sendResult(res, {
@@ -278,16 +337,22 @@ if (Meteor.isServer) {
* @param {string} customFieldId the ID of the custom field
* @return_type {_id: string}
*/
- JsonRoutes.add('DELETE', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
- Authentication.checkUserId( req.userId);
- const paramBoardId = req.params.boardId;
- const id = req.params.customFieldId;
- CustomFields.remove({ _id: id, boardIds: {$in: [paramBoardId]} });
- JsonRoutes.sendResult(res, {
- code: 200,
- data: {
- _id: id,
- },
- });
- });
+ JsonRoutes.add(
+ 'DELETE',
+ '/api/boards/:boardId/custom-fields/:customFieldId',
+ function(req, res) {
+ Authentication.checkUserId(req.userId);
+ const paramBoardId = req.params.boardId;
+ const id = req.params.customFieldId;
+ CustomFields.remove({ _id: id, boardIds: { $in: [paramBoardId] } });
+ JsonRoutes.sendResult(res, {
+ code: 200,
+ data: {
+ _id: id,
+ },
+ });
+ }
+ );
}
+
+export default CustomFields;