mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-29 08:07:48 +01:00
fix todolist removal
This commit is contained in:
1
app.js
1
app.js
@@ -53,6 +53,7 @@ app.use((error, req, res, next) => {
|
|||||||
res.status(400);
|
res.status(400);
|
||||||
res.json({ success: false, error });
|
res.json({ success: false, error });
|
||||||
break;
|
break;
|
||||||
|
case 'AuthenticationError':
|
||||||
case 'UnauthorizedError':
|
case 'UnauthorizedError':
|
||||||
res.status(401);
|
res.status(401);
|
||||||
res.json({ success: false, error });
|
res.json({ success: false, error });
|
||||||
|
|||||||
@@ -22,12 +22,20 @@ TodoListSchema.pre('save', async function () {
|
|||||||
TodoListSchema.pre('remove', async function () {
|
TodoListSchema.pre('remove', async function () {
|
||||||
const user = await this.model('User').findById(this.user);
|
const user = await this.model('User').findById(this.user);
|
||||||
user.lists.splice(user.lists.indexOf(this._id), 1);
|
user.lists.splice(user.lists.indexOf(this._id), 1);
|
||||||
await user.save();
|
|
||||||
|
|
||||||
|
// removing todos in parallel can cause VersionError
|
||||||
|
// so we remove todos from user
|
||||||
const todos = await this.model('Todo')
|
const todos = await this.model('Todo')
|
||||||
.find({ list: this._id })
|
.find({ list: this._id })
|
||||||
.exec();
|
.exec();
|
||||||
await Promise.all(todos.map(todo => todo.remove()));
|
const ids = todos.map(todo => todo._id);
|
||||||
|
user.todos = user.todos.filter(todo => ids.includes(todo._id));
|
||||||
|
await user.save();
|
||||||
|
// and remove them from db
|
||||||
|
await this.model('Todo')
|
||||||
|
.find({ list: this._id })
|
||||||
|
.remove()
|
||||||
|
.exec();
|
||||||
});
|
});
|
||||||
|
|
||||||
TodoListSchema.methods.toJson = function () {
|
TodoListSchema.methods.toJson = function () {
|
||||||
|
|||||||
@@ -22,10 +22,14 @@ UserSchema.plugin(passportLocalMongoose);
|
|||||||
UserSchema.plugin(uniqueValidator);
|
UserSchema.plugin(uniqueValidator);
|
||||||
|
|
||||||
UserSchema.pre('remove', async function () {
|
UserSchema.pre('remove', async function () {
|
||||||
const lists = await this.model('TodoList')
|
await this.model('TodoList')
|
||||||
.find({ user: this._id })
|
.find({ user: this._id })
|
||||||
|
.remove()
|
||||||
|
.exec();
|
||||||
|
await this.model('Todo')
|
||||||
|
.find({ user: this._id })
|
||||||
|
.remove()
|
||||||
.exec();
|
.exec();
|
||||||
await Promise.all(lists.map(list => list.remove()));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
UserSchema.methods.generateJwt = function () {
|
UserSchema.methods.generateJwt = function () {
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ router.delete(
|
|||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
'/login',
|
'/login',
|
||||||
passport.authenticate('local', { session: false }),
|
passport.authenticate('local', { session: false, failWithError: true }),
|
||||||
asyncHelper(async (req, res) => {
|
asyncHelper(async (req, res) => {
|
||||||
res.json({ success: true, data: req.user.toAuthJson() });
|
res.json({ success: true, data: req.user.toAuthJson() });
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ describe('test users', () => {
|
|||||||
})
|
})
|
||||||
.set('Content-Type', 'application/json')
|
.set('Content-Type', 'application/json')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
.expect(400);
|
.expect(401);
|
||||||
});
|
});
|
||||||
test('should not login user with wrong password', async () => {
|
test('should not login user with wrong password', async () => {
|
||||||
await request(server)
|
await request(server)
|
||||||
|
|||||||
Reference in New Issue
Block a user