mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
WIP(teams): data migration draft
This commit is contained in:
57
migrations/tasks/team-tasks-v2.js
Normal file
57
migrations/tasks/team-tasks-v2.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import clone from 'lodash/clone';
|
||||
import filter from 'lodash/filter';
|
||||
import find from 'lodash/find';
|
||||
import isArray from 'lodash/isArray';
|
||||
import { model as Group } from '../../website/server/models/group';
|
||||
import * as Tasks from '../../website/server/models/task';
|
||||
|
||||
async function updateTeamTasks (team) {
|
||||
const toSave = [];
|
||||
const teamTasks = await Tasks.Task.find({
|
||||
'group.id': team._id,
|
||||
}).exec();
|
||||
|
||||
const teamBoardTasks = filter(teamTasks, task => !task.userId);
|
||||
const teamUserTasks = filter(teamTasks, task => task.userId);
|
||||
|
||||
for (const boardTask of teamBoardTasks) {
|
||||
if (isArray(boardTask.group.assignedUsers)) {
|
||||
boardTask.group.approval = undefined;
|
||||
boardTask.group.assignedDate = undefined;
|
||||
boardTask.group.assigningUsername = undefined;
|
||||
boardTask.group.sharedCompletion = undefined;
|
||||
const assignedUsers = clone(boardTask.group.assignedUsers);
|
||||
boardTask.group.assignedUsers = {};
|
||||
for (const assignedUser of assignedUsers) {
|
||||
const userTask = find(teamUserTasks, task => task.userId === assignedUser
|
||||
&& task.group.taskId === boardTask._id);
|
||||
if (userTask) {
|
||||
boardTask.group.assignedUsers[assignedUser] = {
|
||||
assignedDate: userTask.group.assignedDate,
|
||||
assigningUsername: userTask.group.assigningUsername,
|
||||
completed: userTask.completed || false,
|
||||
completedDate: userTask.dateCompleted,
|
||||
};
|
||||
}
|
||||
}
|
||||
boardTask.markModified('group');
|
||||
toSave.push(boardTask.save());
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.all(toSave);
|
||||
}
|
||||
|
||||
export default async function processTeams () {
|
||||
const activeTeams = await Group.find({
|
||||
'purchased.plan.customerId': { $exists: true },
|
||||
$or: [
|
||||
{ 'purchased.plan.dateTerminated': { $exists: false } },
|
||||
{ 'purchased.plan.dateTerminated': null },
|
||||
{ 'purchased.plan.dateTerminated': { $gt: new Date() } },
|
||||
],
|
||||
}).exec();
|
||||
|
||||
const taskPromises = activeTeams.map(updateTeamTasks);
|
||||
return Promise.all(taskPromises);
|
||||
}
|
||||
@@ -127,7 +127,8 @@ export const TaskSchema = new Schema({
|
||||
|
||||
group: {
|
||||
id: { $type: String, ref: 'Group', validate: [v => validator.isUUID(v), 'Invalid uuid for group task.'] },
|
||||
broken: { $type: String, enum: ['GROUP_DELETED', 'TASK_DELETED', 'UNSUBSCRIBED'] },
|
||||
assignedDate: { $type: Date }, // To be removed
|
||||
assigningUsername: { $type: String }, // To be removed
|
||||
assignedUsers: {
|
||||
$type: Schema.Types.Mixed,
|
||||
// key is assigned UUID, with
|
||||
|
||||
Reference in New Issue
Block a user