mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 23:27:26 +01:00
Added support for scoring group tasks down after approval (#9623)
* Added support for scoring group tasks down after approval * Fixed lint issues
This commit is contained in:
@@ -41,8 +41,9 @@ describe('POST /tasks/:id/score/:direction', () => {
|
|||||||
|
|
||||||
let memberTasks = await member.get('/tasks/user');
|
let memberTasks = await member.get('/tasks/user');
|
||||||
let syncedTask = find(memberTasks, findAssignedTask);
|
let syncedTask = find(memberTasks, findAssignedTask);
|
||||||
|
const direction = 'up';
|
||||||
|
|
||||||
await expect(member.post(`/tasks/${syncedTask._id}/score/up`))
|
await expect(member.post(`/tasks/${syncedTask._id}/score/${direction}`))
|
||||||
.to.eventually.be.rejected.and.to.eql({
|
.to.eventually.be.rejected.and.to.eql({
|
||||||
code: 401,
|
code: 401,
|
||||||
error: 'NotAuthorized',
|
error: 'NotAuthorized',
|
||||||
@@ -58,6 +59,7 @@ describe('POST /tasks/:id/score/:direction', () => {
|
|||||||
user: member.auth.local.username,
|
user: member.auth.local.username,
|
||||||
taskName: updatedTask.text,
|
taskName: updatedTask.text,
|
||||||
taskId: updatedTask._id,
|
taskId: updatedTask._id,
|
||||||
|
direction,
|
||||||
}, 'cs')); // This test only works if we have the notification translated
|
}, 'cs')); // This test only works if we have the notification translated
|
||||||
expect(user.notifications[1].data.groupId).to.equal(guild._id);
|
expect(user.notifications[1].data.groupId).to.equal(guild._id);
|
||||||
|
|
||||||
@@ -71,8 +73,9 @@ describe('POST /tasks/:id/score/:direction', () => {
|
|||||||
});
|
});
|
||||||
let memberTasks = await member.get('/tasks/user');
|
let memberTasks = await member.get('/tasks/user');
|
||||||
let syncedTask = find(memberTasks, findAssignedTask);
|
let syncedTask = find(memberTasks, findAssignedTask);
|
||||||
|
const direction = 'up';
|
||||||
|
|
||||||
await expect(member.post(`/tasks/${syncedTask._id}/score/up`))
|
await expect(member.post(`/tasks/${syncedTask._id}/score/${direction}`))
|
||||||
.to.eventually.be.rejected.and.to.eql({
|
.to.eventually.be.rejected.and.to.eql({
|
||||||
code: 401,
|
code: 401,
|
||||||
error: 'NotAuthorized',
|
error: 'NotAuthorized',
|
||||||
@@ -88,6 +91,7 @@ describe('POST /tasks/:id/score/:direction', () => {
|
|||||||
user: member.auth.local.username,
|
user: member.auth.local.username,
|
||||||
taskName: updatedTask.text,
|
taskName: updatedTask.text,
|
||||||
taskId: updatedTask._id,
|
taskId: updatedTask._id,
|
||||||
|
direction,
|
||||||
}));
|
}));
|
||||||
expect(user.notifications[1].data.groupId).to.equal(guild._id);
|
expect(user.notifications[1].data.groupId).to.equal(guild._id);
|
||||||
|
|
||||||
@@ -97,6 +101,7 @@ describe('POST /tasks/:id/score/:direction', () => {
|
|||||||
user: member.auth.local.username,
|
user: member.auth.local.username,
|
||||||
taskName: updatedTask.text,
|
taskName: updatedTask.text,
|
||||||
taskId: updatedTask._id,
|
taskId: updatedTask._id,
|
||||||
|
direction,
|
||||||
}));
|
}));
|
||||||
expect(member2.notifications[0].data.groupId).to.equal(guild._id);
|
expect(member2.notifications[0].data.groupId).to.equal(guild._id);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -568,10 +568,14 @@ export default {
|
|||||||
let approvedTasks = [];
|
let approvedTasks = [];
|
||||||
for (let i = 0; i < scoreTaskNotification.length; i++) {
|
for (let i = 0; i < scoreTaskNotification.length; i++) {
|
||||||
// Array with all approved tasks
|
// Array with all approved tasks
|
||||||
|
const scoreData = scoreTaskNotification[i].data;
|
||||||
|
let direction = 'up';
|
||||||
|
if (scoreData.direction) direction = scoreData.direction;
|
||||||
|
|
||||||
approvedTasks.push({
|
approvedTasks.push({
|
||||||
params: {
|
params: {
|
||||||
task: scoreTaskNotification[i].data.scoreTask,
|
task: scoreData.scoreTask,
|
||||||
direction: 'up',
|
direction,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -569,6 +569,7 @@ api.scoreTask = {
|
|||||||
}, manager.preferences.language),
|
}, manager.preferences.language),
|
||||||
groupId: group._id,
|
groupId: group._id,
|
||||||
taskId: task._id,
|
taskId: task._id,
|
||||||
|
direction,
|
||||||
});
|
});
|
||||||
managerPromises.push(manager.save());
|
managerPromises.push(manager.save());
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -299,20 +299,22 @@ api.approveTask = {
|
|||||||
task.group.approval.approvingUser = user._id;
|
task.group.approval.approvingUser = user._id;
|
||||||
task.group.approval.approved = true;
|
task.group.approval.approved = true;
|
||||||
|
|
||||||
assignedUser.addNotification('GROUP_TASK_APPROVED', {
|
// Get Managers
|
||||||
message: res.t('yourTaskHasBeenApproved', {taskText: task.text}),
|
const managerIds = Object.keys(group.managers);
|
||||||
groupId: group._id,
|
|
||||||
});
|
|
||||||
|
|
||||||
assignedUser.addNotification('SCORED_TASK', {
|
|
||||||
message: res.t('yourTaskHasBeenApproved', {taskText: task.text}),
|
|
||||||
scoreTask: task,
|
|
||||||
});
|
|
||||||
|
|
||||||
let managerIds = Object.keys(group.managers);
|
|
||||||
managerIds.push(group.leader);
|
managerIds.push(group.leader);
|
||||||
let managers = await User.find({_id: managerIds}, 'notifications').exec(); // Use this method so we can get access to notifications
|
const managers = await User.find({_id: managerIds}, 'notifications').exec(); // Use this method so we can get access to notifications
|
||||||
|
|
||||||
|
// Get task direction
|
||||||
|
const firstManagerNotifications = managers[0].notifications;
|
||||||
|
const firstNotificationIndex = findIndex(firstManagerNotifications, (notification) => {
|
||||||
|
return notification.data.taskId === task._id;
|
||||||
|
});
|
||||||
|
let direction = 'up';
|
||||||
|
if (firstManagerNotifications[firstNotificationIndex]) {
|
||||||
|
direction = firstManagerNotifications[firstNotificationIndex].direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove old notifications
|
||||||
let managerPromises = [];
|
let managerPromises = [];
|
||||||
managers.forEach((manager) => {
|
managers.forEach((manager) => {
|
||||||
let notificationIndex = findIndex(manager.notifications, function findNotification (notification) {
|
let notificationIndex = findIndex(manager.notifications, function findNotification (notification) {
|
||||||
@@ -325,6 +327,18 @@ api.approveTask = {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Add new notifications to user
|
||||||
|
assignedUser.addNotification('GROUP_TASK_APPROVED', {
|
||||||
|
message: res.t('yourTaskHasBeenApproved', {taskText: task.text}),
|
||||||
|
groupId: group._id,
|
||||||
|
});
|
||||||
|
|
||||||
|
assignedUser.addNotification('SCORED_TASK', {
|
||||||
|
message: res.t('yourTaskHasBeenApproved', {taskText: task.text}),
|
||||||
|
scoreTask: task,
|
||||||
|
direction,
|
||||||
|
});
|
||||||
|
|
||||||
managerPromises.push(task.save());
|
managerPromises.push(task.save());
|
||||||
managerPromises.push(assignedUser.save());
|
managerPromises.push(assignedUser.save());
|
||||||
await Bluebird.all(managerPromises);
|
await Bluebird.all(managerPromises);
|
||||||
|
|||||||
Reference in New Issue
Block a user