diff options
author | Justin Reynolds <justinr1234@gmail.com> | 2019-06-26 17:47:27 -0500 |
---|---|---|
committer | Justin Reynolds <justinr1234@gmail.com> | 2019-06-27 09:13:20 -0500 |
commit | c60e80d25baa6a81b28f6090ca848553d20b2bb7 (patch) | |
tree | 64bcea66932f4b4635d5df6901f18d5ba6db0a37 /models/checklistItems.js | |
parent | fb728baf0c87bae5fa39d92089b667ff1ed69fa6 (diff) | |
download | wekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.tar.gz wekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.tar.bz2 wekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.zip |
Add createdAt and modifiedAt to all collections
Diffstat (limited to 'models/checklistItems.js')
-rw-r--r-- | models/checklistItems.js | 229 |
1 files changed, 140 insertions, 89 deletions
diff --git a/models/checklistItems.js b/models/checklistItems.js index df56c475..d548e681 100644 --- a/models/checklistItems.js +++ b/models/checklistItems.js @@ -3,40 +3,66 @@ ChecklistItems = new Mongo.Collection('checklistItems'); /** * An item in a checklist */ -ChecklistItems.attachSchema(new SimpleSchema({ - title: { - /** - * the text of the item - */ - type: String, - }, - sort: { - /** - * the sorting field of the item - */ - type: Number, - decimal: true, - }, - isFinished: { - /** - * Is the item checked? - */ - type: Boolean, - defaultValue: false, - }, - checklistId: { - /** - * the checklist ID the item is attached to - */ - type: String, - }, - cardId: { - /** - * the card ID the item is attached to - */ - type: String, - }, -})); +ChecklistItems.attachSchema( + new SimpleSchema({ + title: { + /** + * the text of the item + */ + type: String, + }, + sort: { + /** + * the sorting field of the item + */ + type: Number, + decimal: true, + }, + isFinished: { + /** + * Is the item checked? + */ + type: Boolean, + defaultValue: false, + }, + checklistId: { + /** + * the checklist ID the item is attached to + */ + type: String, + }, + cardId: { + /** + * the card ID the item is attached to + */ + type: String, + }, + createdAt: { + type: Date, + optional: true, + // eslint-disable-next-line consistent-return + autoValue() { + if (this.isInsert) { + return new Date(); + } else { + this.unset(); + } + }, + }, + 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(); + } + }, + }, + }) +); ChecklistItems.allow({ insert(userId, doc) { @@ -62,10 +88,10 @@ ChecklistItems.mutations({ setTitle(title) { return { $set: { title } }; }, - check(){ + check() { return { $set: { isFinished: true } }; }, - uncheck(){ + uncheck() { return { $set: { isFinished: false } }; }, toggleItem() { @@ -79,7 +105,7 @@ ChecklistItems.mutations({ sort: sortIndex, }; - return {$set: mutatedFields}; + return { $set: mutatedFields }; }, }); @@ -106,13 +132,13 @@ function itemRemover(userId, doc) { }); } -function publishCheckActivity(userId, doc){ +function publishCheckActivity(userId, doc) { const card = Cards.findOne(doc.cardId); const boardId = card.boardId; let activityType; - if(doc.isFinished){ + if (doc.isFinished) { activityType = 'checkedItem'; - }else{ + } else { activityType = 'uncheckedItem'; } const act = { @@ -122,19 +148,19 @@ function publishCheckActivity(userId, doc){ boardId, checklistId: doc.checklistId, checklistItemId: doc._id, - checklistItemName:doc.title, + checklistItemName: doc.title, listId: card.listId, swimlaneId: card.swimlaneId, }; Activities.insert(act); } -function publishChekListCompleted(userId, doc){ +function publishChekListCompleted(userId, doc) { const card = Cards.findOne(doc.cardId); const boardId = card.boardId; const checklistId = doc.checklistId; - const checkList = Checklists.findOne({_id:checklistId}); - if(checkList.isFinished()){ + const checkList = Checklists.findOne({ _id: checklistId }); + if (checkList.isFinished()) { const act = { userId, activityType: 'completeChecklist', @@ -149,11 +175,11 @@ function publishChekListCompleted(userId, doc){ } } -function publishChekListUncompleted(userId, doc){ +function publishChekListUncompleted(userId, doc) { const card = Cards.findOne(doc.cardId); const boardId = card.boardId; const checklistId = doc.checklistId; - const checkList = Checklists.findOne({_id:checklistId}); + const checkList = Checklists.findOne({ _id: checklistId }); // BUGS in IFTTT Rules: https://github.com/wekan/wekan/issues/1972 // Currently in checklist all are set as uncompleted/not checked, // IFTTT Rule does not move card to other list. @@ -167,7 +193,7 @@ function publishChekListUncompleted(userId, doc){ // find . | xargs grep 'count' -sl | grep -v .meteor | grep -v node_modules | grep -v .build // Maybe something related here? // wekan/client/components/rules/triggers/checklistTriggers.js - if(checkList.isFinished()){ + if (checkList.isFinished()) { const act = { userId, activityType: 'uncompleteChecklist', @@ -185,6 +211,7 @@ function publishChekListUncompleted(userId, doc){ // Activities if (Meteor.isServer) { Meteor.startup(() => { + ChecklistItems._collection._ensureIndex({ modifiedAt: -1 }); ChecklistItems._collection._ensureIndex({ checklistId: 1 }); ChecklistItems._collection._ensureIndex({ cardId: 1 }); }); @@ -198,6 +225,10 @@ if (Meteor.isServer) { publishChekListUncompleted(userId, doc, fieldNames); }); + ChecklistItems.before.update((userId, doc, fieldNames, modifier, options) => { + modifier.$set = modifier.$set || {}; + modifier.$set.modifiedAt = Date.now(); + }); ChecklistItems.after.insert((userId, doc) => { itemCreation(userId, doc); @@ -214,7 +245,7 @@ if (Meteor.isServer) { boardId, checklistId: doc.checklistId, checklistItemId: doc._id, - checklistItemName:doc.title, + checklistItemName: doc.title, listId: card.listId, swimlaneId: card.swimlaneId, }); @@ -233,21 +264,25 @@ if (Meteor.isServer) { * @param {string} itemId the ID of the item * @return_type ChecklistItems */ - JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) { - Authentication.checkUserId( req.userId); - const paramItemId = req.params.itemId; - const checklistItem = ChecklistItems.findOne({ _id: paramItemId }); - if (checklistItem) { - JsonRoutes.sendResult(res, { - code: 200, - data: checklistItem, - }); - } else { - JsonRoutes.sendResult(res, { - code: 500, - }); + JsonRoutes.add( + 'GET', + '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', + function(req, res) { + Authentication.checkUserId(req.userId); + const paramItemId = req.params.itemId; + const checklistItem = ChecklistItems.findOne({ _id: paramItemId }); + if (checklistItem) { + JsonRoutes.sendResult(res, { + code: 200, + data: checklistItem, + }); + } else { + JsonRoutes.sendResult(res, { + code: 500, + }); + } } - }); + ); /** * @operation edit_checklist_item @@ -262,25 +297,35 @@ if (Meteor.isServer) { * @param {string} [title] the new text of the item * @return_type {_id: string} */ - JsonRoutes.add('PUT', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) { - Authentication.checkUserId( req.userId); + JsonRoutes.add( + 'PUT', + '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', + function(req, res) { + Authentication.checkUserId(req.userId); - const paramItemId = req.params.itemId; + const paramItemId = req.params.itemId; - if (req.body.hasOwnProperty('isFinished')) { - ChecklistItems.direct.update({_id: paramItemId}, {$set: {isFinished: req.body.isFinished}}); - } - if (req.body.hasOwnProperty('title')) { - ChecklistItems.direct.update({_id: paramItemId}, {$set: {title: req.body.title}}); - } + if (req.body.hasOwnProperty('isFinished')) { + ChecklistItems.direct.update( + { _id: paramItemId }, + { $set: { isFinished: req.body.isFinished } } + ); + } + if (req.body.hasOwnProperty('title')) { + ChecklistItems.direct.update( + { _id: paramItemId }, + { $set: { title: req.body.title } } + ); + } - JsonRoutes.sendResult(res, { - code: 200, - data: { - _id: paramItemId, - }, - }); - }); + JsonRoutes.sendResult(res, { + code: 200, + data: { + _id: paramItemId, + }, + }); + } + ); /** * @operation delete_checklist_item @@ -295,15 +340,21 @@ if (Meteor.isServer) { * @param {string} itemId the ID of the item to be removed * @return_type {_id: string} */ - JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', function (req, res) { - Authentication.checkUserId( req.userId); - const paramItemId = req.params.itemId; - ChecklistItems.direct.remove({ _id: paramItemId }); - JsonRoutes.sendResult(res, { - code: 200, - data: { - _id: paramItemId, - }, - }); - }); + JsonRoutes.add( + 'DELETE', + '/api/boards/:boardId/cards/:cardId/checklists/:checklistId/items/:itemId', + function(req, res) { + Authentication.checkUserId(req.userId); + const paramItemId = req.params.itemId; + ChecklistItems.direct.remove({ _id: paramItemId }); + JsonRoutes.sendResult(res, { + code: 200, + data: { + _id: paramItemId, + }, + }); + } + ); } + +export default ChecklistItems; |