mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
make sure user.tasksOrder is update only when necessary
This commit is contained in:
@@ -54,6 +54,21 @@ describe('POST /tasks', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not update user.tasksOrder.{taskType} when the task is not saved because invalid', async () => {
|
||||||
|
let originalHabitsOrder = (await user.get('/user')).tasksOrder.habits;
|
||||||
|
return expect(user.post('/tasks', {
|
||||||
|
type: 'habit',
|
||||||
|
})).to.eventually.be.rejected.and.eql({ // this block is necessary
|
||||||
|
code: 400,
|
||||||
|
error: 'BadRequest',
|
||||||
|
message: 'habit validation failed',
|
||||||
|
}).then(async () => {
|
||||||
|
let updatedHabitsOrder = (await user.get('/user')).tasksOrder.habits;
|
||||||
|
|
||||||
|
expect(updatedHabitsOrder).to.eql(originalHabitsOrder);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('automatically sets "task.userId" to user\'s uuid', async () => {
|
it('automatically sets "task.userId" to user\'s uuid', async () => {
|
||||||
let task = await user.post('/tasks', {
|
let task = await user.post('/tasks', {
|
||||||
text: 'test habit',
|
text: 'test habit',
|
||||||
|
|||||||
@@ -32,16 +32,28 @@ api.createTask = {
|
|||||||
let user = res.locals.user;
|
let user = res.locals.user;
|
||||||
|
|
||||||
let toSave = tasksData.map(taskData => {
|
let toSave = tasksData.map(taskData => {
|
||||||
|
// Validate that task.type is valid
|
||||||
if (!taskData || Tasks.tasksTypes.indexOf(taskData.type) === -1) throw new BadRequest(res.t('invalidTaskType'));
|
if (!taskData || Tasks.tasksTypes.indexOf(taskData.type) === -1) throw new BadRequest(res.t('invalidTaskType'));
|
||||||
|
|
||||||
let taskType = taskData.type;
|
let taskType = taskData.type;
|
||||||
let newTask = new Tasks[taskType](Tasks.Task.sanitizeCreate(taskData));
|
let newTask = new Tasks[taskType](Tasks.Task.sanitizeCreate(taskData));
|
||||||
newTask.userId = user._id;
|
newTask.userId = user._id;
|
||||||
|
|
||||||
|
// Validate that the task is valid and throw if it isn't
|
||||||
|
// otherwise since we're saving user and task in parallel it could save the user with a tasksOrder that doens't match reality
|
||||||
|
let validationErrors = newTask.validateSync();
|
||||||
|
if (validationErrors) throw validationErrors;
|
||||||
|
|
||||||
|
// Otherwise update the user
|
||||||
user.tasksOrder[`${taskType}s`].unshift(newTask._id);
|
user.tasksOrder[`${taskType}s`].unshift(newTask._id);
|
||||||
|
|
||||||
return newTask.save();
|
return newTask;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// If all tasks are valid, save everything, withough running validation again
|
||||||
|
toSave = toSave.map(task => task.save({
|
||||||
|
validateBeforeSave: false,
|
||||||
|
}));
|
||||||
toSave.unshift(user.save());
|
toSave.unshift(user.save());
|
||||||
let results = await Q.all(toSave);
|
let results = await Q.all(toSave);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user