2021-08-10 23:35:05 +00:00
|
|
|
import { isLoggedIn } from '../../account/session';
|
|
|
|
import { asyncFilter } from '../../common/async-array-helpers';
|
|
|
|
import { database } from '../../common/database';
|
|
|
|
import * as models from '../../models';
|
2021-08-20 15:12:36 +00:00
|
|
|
import { isRemoteProject, RemoteProject } from '../../models/project';
|
2021-08-10 23:35:05 +00:00
|
|
|
import { isCollection, Workspace } from '../../models/workspace';
|
|
|
|
import { Team } from '../types';
|
2021-08-20 15:12:36 +00:00
|
|
|
import { initializeProjectFromTeam } from './initialize-model-from';
|
2021-08-10 23:35:05 +00:00
|
|
|
import { VCS } from './vcs';
|
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
export const logCollectionMovedToProject = (collection: Workspace, remoteProject: RemoteProject) => {
|
|
|
|
console.log('[sync] collection has been moved to the remote project to which it belongs', {
|
2021-08-10 23:35:05 +00:00
|
|
|
collection: {
|
|
|
|
id : collection._id,
|
|
|
|
name: collection.name,
|
|
|
|
},
|
2021-08-20 15:12:36 +00:00
|
|
|
project: {
|
|
|
|
id: remoteProject._id,
|
|
|
|
name: remoteProject.name,
|
2021-08-10 23:35:05 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
export const migrateCollectionsIntoRemoteProject = async (vcs: VCS) => {
|
|
|
|
console.log('[sync] checking for collections which need to be moved into a remote project');
|
2021-08-10 23:35:05 +00:00
|
|
|
|
|
|
|
// If not logged in, exit
|
|
|
|
if (!isLoggedIn()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const collections = (await models.workspace.all()).filter(isCollection);
|
2021-08-20 15:12:36 +00:00
|
|
|
const remoteProjects = (await models.project.all()).filter(isRemoteProject);
|
2021-08-10 23:35:05 +00:00
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
// Are there any collections that have sync setup but are not in a remote project?
|
|
|
|
const isNotInRemoteProject = (collection: Workspace) => !Boolean(remoteProjects.find(project => project._id === collection.parentId));
|
|
|
|
const hasLocalProject = (collection: Workspace) => vcs.hasBackendProjectForRootDocument(collection._id);
|
2021-08-10 23:35:05 +00:00
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
const needsMigration = await asyncFilter(collections, async coll => await hasLocalProject(coll) && isNotInRemoteProject(coll));
|
2021-08-10 23:35:05 +00:00
|
|
|
|
|
|
|
// If nothing to migrate, exit
|
|
|
|
if (!needsMigration.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
const remoteBackendProjectsInAnyTeam = await vcs.remoteBackendProjectsInAnyTeam();
|
|
|
|
const findRemoteBackendProject = (collection: Workspace) => remoteBackendProjectsInAnyTeam.find(project => project.rootDocumentId === collection._id);
|
|
|
|
const findRemoteProjectByTeam = (team: Team) => remoteProjects.find(project => project.remoteId === team.id);
|
2021-08-10 23:35:05 +00:00
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
const upsert: (Workspace | RemoteProject)[] = [];
|
2021-08-10 23:35:05 +00:00
|
|
|
|
|
|
|
for (const collection of needsMigration) {
|
2021-08-20 15:12:36 +00:00
|
|
|
const remoteBackendProject = findRemoteBackendProject(collection);
|
2021-08-10 23:35:05 +00:00
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
if (!remoteBackendProject) {
|
2021-08-30 16:18:21 +00:00
|
|
|
continue;
|
2021-08-10 23:35:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
let remoteProject = findRemoteProjectByTeam(remoteBackendProject.team);
|
2021-08-10 23:35:05 +00:00
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
if (!remoteProject) {
|
|
|
|
remoteProject = await initializeProjectFromTeam(remoteBackendProject.team);
|
|
|
|
upsert.push(remoteProject);
|
2021-08-10 23:35:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
collection.parentId = remoteProject._id;
|
2021-08-10 23:35:05 +00:00
|
|
|
upsert.push(collection);
|
2021-08-20 15:12:36 +00:00
|
|
|
logCollectionMovedToProject(collection, remoteProject);
|
2021-08-10 23:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (upsert.length) {
|
|
|
|
await database.batchModifyDocs({ upsert });
|
|
|
|
}
|
|
|
|
};
|