wekan/models/lists.js
Alexander Sulfrian a2888250f4 Models: Replace before.insert with autoValues
The before.insert hooks have the problem, that they are executed in a
different order if called from the client or from the server. If called
from the client, the before.insert hook is called before validation of
the schema, but if called from the server, the validation is called
first and fails.
2016-06-03 03:56:35 +02:00

115 lines
2.2 KiB
JavaScript

Lists = new Mongo.Collection('lists');
Lists.attachSchema(new SimpleSchema({
title: {
type: String,
},
archived: {
type: Boolean,
autoValue() { // eslint-disable-line consistent-return
if (this.isInsert && !this.isSet) {
return false;
}
},
},
boardId: {
type: String,
},
createdAt: {
type: Date,
autoValue() { // eslint-disable-line consistent-return
if (this.isInsert) {
return new Date();
} else {
this.unset();
}
},
},
sort: {
type: Number,
decimal: true,
// XXX We should probably provide a default
optional: true,
},
updatedAt: {
type: Date,
optional: true,
autoValue() { // eslint-disable-line consistent-return
if (this.isUpdate) {
return new Date();
} else {
this.unset();
}
},
},
}));
Lists.allow({
insert(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
},
update(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
},
remove(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
},
fetch: ['boardId'],
});
Lists.helpers({
cards() {
return Cards.find(Filter.mongoSelector({
listId: this._id,
archived: false,
}), { sort: ['sort'] });
},
allCards() {
return Cards.find({ listId: this._id });
},
board() {
return Boards.findOne(this.boardId);
},
});
Lists.mutations({
rename(title) {
return { $set: { title }};
},
archive() {
return { $set: { archived: true }};
},
restore() {
return { $set: { archived: false }};
},
});
Lists.hookOptions.after.update = { fetchPrevious: false };
if (Meteor.isServer) {
Lists.after.insert((userId, doc) => {
Activities.insert({
userId,
type: 'list',
activityType: 'createList',
boardId: doc.boardId,
listId: doc._id,
});
});
Lists.after.update((userId, doc) => {
if (doc.archived) {
Activities.insert({
userId,
type: 'list',
activityType: 'archivedList',
listId: doc._id,
boardId: doc.boardId,
});
}
});
}