mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 23:57:49 +01:00
clean up,
tests, fixes
This commit is contained in:
@@ -13,13 +13,15 @@ const TodoSchema = Schema({
|
||||
});
|
||||
|
||||
TodoSchema.pre('save', async function () {
|
||||
const user = await this.model('User').findById(this.user);
|
||||
user.todos.push(this._id);
|
||||
await user.save();
|
||||
if (this.isNew) {
|
||||
const user = await this.model('User').findById(this.user);
|
||||
user.todos.push(this._id);
|
||||
await user.save();
|
||||
|
||||
const list = await this.model('TodoList').findById(this.list);
|
||||
list.todos.push(this._id);
|
||||
await list.save();
|
||||
const list = await this.model('TodoList').findById(this.list);
|
||||
list.todos.push(this._id);
|
||||
await list.save();
|
||||
}
|
||||
});
|
||||
|
||||
TodoSchema.pre('remove', async function () {
|
||||
|
||||
@@ -12,16 +12,22 @@ const TodoListSchema = Schema({
|
||||
});
|
||||
|
||||
TodoListSchema.pre('save', async function () {
|
||||
const user = await this.model('User').findById(this.user);
|
||||
user.lists.push(this._id);
|
||||
await user.save();
|
||||
if (this.isNew) {
|
||||
const user = await this.model('User').findById(this.user);
|
||||
user.lists.push(this._id);
|
||||
await user.save();
|
||||
}
|
||||
});
|
||||
|
||||
TodoListSchema.pre('remove', async function () {
|
||||
const user = await this.model('User').findById(this.user);
|
||||
user.lists.splice(user.todos.indexOf(this._id), 1);
|
||||
user.lists.splice(user.lists.indexOf(this._id), 1);
|
||||
await user.save();
|
||||
await this.model('Todo').remove({ list: this._id });
|
||||
|
||||
const todos = await this.model('Todo')
|
||||
.find({ list: this._id })
|
||||
.exec();
|
||||
await Promise.all(todos.map(todo => todo.remove()));
|
||||
});
|
||||
|
||||
TodoListSchema.methods.toJson = function () {
|
||||
|
||||
@@ -22,8 +22,15 @@ UserSchema.plugin(passportLocalMongoose);
|
||||
UserSchema.plugin(uniqueValidator);
|
||||
|
||||
UserSchema.pre('remove', async function () {
|
||||
await this.model('TodoList').remove({ user: this._id });
|
||||
await this.model('Todo').remove({ user: this._id });
|
||||
await this.model('TodoList')
|
||||
.find({ user: this._id })
|
||||
.remove()
|
||||
.exec();
|
||||
|
||||
await this.model('Todo')
|
||||
.find({ user: this._id })
|
||||
.remove()
|
||||
.exec();
|
||||
});
|
||||
|
||||
UserSchema.methods.generateJwt = function () {
|
||||
|
||||
Reference in New Issue
Block a user