Skip to content

Commit

Permalink
another fix
Browse files Browse the repository at this point in the history
  • Loading branch information
paglias committed Feb 2, 2018
1 parent 3e8ff38 commit 3f5a637
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions website/server/models/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ export let TaskSchema = new Schema({
return !validator.isUUID(val);
},
msg: 'Task short names cannot be uuids.',
}, {
validator (alias) {
return new Promise((resolve, reject) => {
Task.findOne({ // eslint-disable-line no-use-before-define
_id: { $ne: this._id },
userId: this.userId,
alias,
}).exec().then((task) => {
let aliasAvailable = !task;

return aliasAvailable ? resolve() : reject();
}).catch(() => {
reject();
});
});
},
msg: 'Task alias already used on another task.',
}],
},
tags: [{
Expand Down Expand Up @@ -193,20 +210,6 @@ TaskSchema.methods.scoreChallengeTask = async function scoreChallengeTask (delta

export let Task = mongoose.model('Task', TaskSchema);

Task.schema.path('alias').validate(function valiateAliasNotTaken (alias, respond) {
Task.findOne({
_id: { $ne: this._id },
userId: this.userId,
alias,
}).exec().then((task) => {
let aliasAvailable = !task;

respond(aliasAvailable);
}).catch(() => {
respond(false);
});
}, 'Task alias already used on another task.');

// habits and dailies shared fields
let habitDailySchema = () => {
return {history: Array}; // [{date:Date, value:Number}], // this causes major performance problems
Expand Down

0 comments on commit 3f5a637

Please sign in to comment.