mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 06:37:23 +01:00
Group Tasks Shared Completion (#10515)
* WIP(groups): add shared completion prop Also fix an issue where the Needs Approval toggle would not read/save correctly. * fix(groups): save group options on task create Also, correct count of assigned members when viewing user is among assignments * fix(groups): display correct messages in two places * fix(tasks): eliminate console error related to filtering Also localize a group plans string * WIP(groups): implement single completion for approval workflow * WIP(groups): Add shared completion handling to no-approval-needed flow * WIP(groups): cover approval flow case for all-assigned Also save new field on initial task creation * fix(tasks): use default sharedCompletion value when creating tasks * WIP(tests): non-working draft test * Added completed todo to group query * WIP(group-tasks): fix bugs, add tests * refactor(group-tasks): deleteMany op, add more tests * refactor(group-tasks): move shared completion handling to lib * WIP(group-tasks): broken refactor * WIP(group-tasks): await all the things * Turned complete master task to save * WIP(group-tasks): show completed * fix(filtering): don't try to filter if no list is passed * refactor(group-tasks): load completed to-dos on demand, not at start * fix(group-tasks): don't double up on repeat visits * fix(group-tasks): include brief explanation in dropdown * fix(group-tasks): improve wording some more
This commit is contained in:
61
website/server/libs/groupTasks.js
Normal file
61
website/server/libs/groupTasks.js
Normal file
@@ -0,0 +1,61 @@
|
||||
import * as Tasks from '../models/task';
|
||||
|
||||
const SHARED_COMPLETION = {
|
||||
default: 'recurringCompletion',
|
||||
single: 'singleCompletion',
|
||||
every: 'allAssignedCompletion',
|
||||
};
|
||||
|
||||
async function _completeMasterTask (masterTask) {
|
||||
masterTask.completed = true;
|
||||
await masterTask.save();
|
||||
}
|
||||
|
||||
async function _deleteUnfinishedTasks (groupMemberTask) {
|
||||
await Tasks.Task.deleteMany({
|
||||
'group.taskId': groupMemberTask.group.taskId,
|
||||
$and: [
|
||||
{userId: {$exists: true}},
|
||||
{userId: {$ne: groupMemberTask.userId}},
|
||||
],
|
||||
}).exec();
|
||||
}
|
||||
|
||||
async function _evaluateAllAssignedCompletion (masterTask) {
|
||||
let completions;
|
||||
if (masterTask.group.approval && masterTask.group.approval.required) {
|
||||
completions = await Tasks.Task.count({
|
||||
'group.taskId': masterTask._id,
|
||||
'group.approval.approved': true,
|
||||
}).exec();
|
||||
completions++;
|
||||
} else {
|
||||
completions = await Tasks.Task.count({
|
||||
'group.taskId': masterTask._id,
|
||||
completed: true,
|
||||
}).exec();
|
||||
}
|
||||
if (completions >= masterTask.group.assignedUsers.length) {
|
||||
await _completeMasterTask(masterTask);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSharedCompletion (groupMemberTask) {
|
||||
let masterTask = await Tasks.Task.findOne({
|
||||
_id: groupMemberTask.group.taskId,
|
||||
}).exec();
|
||||
|
||||
if (!masterTask || !masterTask.group || masterTask.type !== 'todo') return;
|
||||
|
||||
if (masterTask.group.sharedCompletion === SHARED_COMPLETION.single) {
|
||||
await _deleteUnfinishedTasks(groupMemberTask);
|
||||
await _completeMasterTask(masterTask);
|
||||
} else if (masterTask.group.sharedCompletion === SHARED_COMPLETION.every) {
|
||||
await _evaluateAllAssignedCompletion(masterTask);
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
SHARED_COMPLETION,
|
||||
handleSharedCompletion,
|
||||
};
|
||||
Reference in New Issue
Block a user