2022-10-11 10:26:09 -04:00
|
|
|
/**
|
|
|
|
* This script fixes problems found by the find_malformed_filetrees.js script.
|
|
|
|
*
|
|
|
|
* The script takes two arguments: the project id and the problemtatic path.
|
|
|
|
* This is the output format of each line in the find_malformed_filetrees.js
|
|
|
|
* script.
|
|
|
|
*/
|
2024-08-02 12:27:31 -04:00
|
|
|
const { ObjectId } = require('mongodb-legacy')
|
2022-04-12 10:04:16 -04:00
|
|
|
const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
|
2022-10-11 10:26:09 -04:00
|
|
|
const ProjectLocator = require('../app/src/Features/Project/ProjectLocator')
|
2022-04-12 10:04:16 -04:00
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const { projectId, mongoPath } = parseArgs()
|
|
|
|
await waitForDb()
|
|
|
|
|
|
|
|
let modifiedCount
|
2022-10-11 10:26:09 -04:00
|
|
|
if (isRootFolder(mongoPath)) {
|
2022-04-12 10:04:16 -04:00
|
|
|
modifiedCount = await fixRootFolder(projectId)
|
2022-10-11 10:26:09 -04:00
|
|
|
} else if (isArrayElement(mongoPath)) {
|
|
|
|
modifiedCount = await removeNulls(projectId, parentPath(mongoPath))
|
|
|
|
} else if (isArray(mongoPath)) {
|
|
|
|
modifiedCount = await fixArray(projectId, mongoPath)
|
|
|
|
} else if (isFolderId(mongoPath)) {
|
|
|
|
modifiedCount = await fixFolderId(projectId, mongoPath)
|
|
|
|
} else if (isDocOrFileId(mongoPath)) {
|
|
|
|
modifiedCount = await removeElementsWithoutIds(
|
|
|
|
projectId,
|
|
|
|
parentPath(parentPath(mongoPath))
|
|
|
|
)
|
|
|
|
} else if (isName(mongoPath)) {
|
|
|
|
modifiedCount = await fixName(projectId, mongoPath)
|
2022-04-12 10:04:16 -04:00
|
|
|
} else {
|
|
|
|
console.error(`Unexpected mongo path: ${mongoPath}`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`${modifiedCount} project(s) modified`)
|
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseArgs() {
|
|
|
|
const args = process.argv.slice(2)
|
|
|
|
if (args.length !== 2) {
|
|
|
|
console.error('Usage: fix_malformed_filetree.js PROJECT_ID MONGO_PATH')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
const [projectId, mongoPath] = args
|
2023-12-18 05:54:01 -05:00
|
|
|
return { projectId: new ObjectId(projectId), mongoPath }
|
2022-04-12 10:04:16 -04:00
|
|
|
}
|
|
|
|
|
2022-10-11 10:26:09 -04:00
|
|
|
function isRootFolder(path) {
|
|
|
|
return path === 'rootFolder.0'
|
|
|
|
}
|
|
|
|
|
|
|
|
function isArray(path) {
|
|
|
|
return /\.(docs|folders|fileRefs)$/.test(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isArrayElement(path) {
|
2022-04-12 10:04:16 -04:00
|
|
|
return /\.\d+$/.test(path)
|
|
|
|
}
|
|
|
|
|
2022-10-11 10:26:09 -04:00
|
|
|
function isFolderId(path) {
|
|
|
|
return /\.folders\.\d+\._id$/.test(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDocOrFileId(path) {
|
|
|
|
return /\.(docs|fileRefs)\.\d+\._id$/.test(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isName(path) {
|
|
|
|
return /\.name$/.test(path)
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:04:16 -04:00
|
|
|
function parentPath(path) {
|
|
|
|
return path.slice(0, path.lastIndexOf('.'))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the root folder structure is missing, set it up
|
|
|
|
*/
|
|
|
|
async function fixRootFolder(projectId) {
|
|
|
|
const result = await db.projects.updateOne(
|
|
|
|
{ _id: projectId, rootFolder: [] },
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
rootFolder: [
|
|
|
|
{
|
2023-12-18 05:54:01 -05:00
|
|
|
_id: new ObjectId(),
|
2022-04-12 10:04:16 -04:00
|
|
|
name: 'rootFolder',
|
|
|
|
folders: [],
|
|
|
|
docs: [],
|
|
|
|
fileRefs: [],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return result.modifiedCount
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-11 10:26:09 -04:00
|
|
|
* Remove all nulls from the given docs/files/folders array
|
2022-04-12 10:04:16 -04:00
|
|
|
*/
|
2022-10-11 10:26:09 -04:00
|
|
|
async function removeNulls(projectId, path) {
|
2022-04-12 10:04:16 -04:00
|
|
|
const result = await db.projects.updateOne(
|
2022-10-11 10:26:09 -04:00
|
|
|
{ _id: projectId, [path]: { $type: 'array' } },
|
|
|
|
{ $pull: { [path]: null } }
|
2022-04-12 10:04:16 -04:00
|
|
|
)
|
|
|
|
return result.modifiedCount
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the element at the given path is not an array, set it to an empty array
|
|
|
|
*/
|
2022-10-11 10:26:09 -04:00
|
|
|
async function fixArray(projectId, path) {
|
2022-04-12 10:04:16 -04:00
|
|
|
const result = await db.projects.updateOne(
|
|
|
|
{ _id: projectId, [path]: { $not: { $type: 'array' } } },
|
|
|
|
{ $set: { [path]: [] } }
|
|
|
|
)
|
|
|
|
return result.modifiedCount
|
|
|
|
}
|
|
|
|
|
2022-10-11 10:26:09 -04:00
|
|
|
/**
|
|
|
|
* Generate a missing id for a folder
|
|
|
|
*/
|
|
|
|
async function fixFolderId(projectId, path) {
|
|
|
|
const result = await db.projects.updateOne(
|
|
|
|
{ _id: projectId, [path]: { $exists: false } },
|
2023-12-18 05:54:01 -05:00
|
|
|
{ $set: { [path]: new ObjectId() } }
|
2022-10-11 10:26:09 -04:00
|
|
|
)
|
|
|
|
return result.modifiedCount
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove elements that don't have ids in the array at the given path
|
|
|
|
*/
|
|
|
|
async function removeElementsWithoutIds(projectId, path) {
|
|
|
|
const result = await db.projects.updateOne(
|
|
|
|
{ _id: projectId, [path]: { $type: 'array' } },
|
|
|
|
{ $pull: { [path]: { _id: null } } }
|
|
|
|
)
|
|
|
|
return result.modifiedCount
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Give a name to a file/doc/folder that doesn't have one
|
|
|
|
*/
|
|
|
|
async function fixName(projectId, path) {
|
|
|
|
const project = await db.projects.findOne(
|
|
|
|
{ _id: projectId },
|
|
|
|
{ projection: { rootFolder: 1 } }
|
|
|
|
)
|
|
|
|
const arrayPath = parentPath(parentPath(path))
|
|
|
|
const array = ProjectLocator.findElementByMongoPath(project, arrayPath)
|
|
|
|
const existingNames = new Set(array.map(x => x.name))
|
|
|
|
const name = findUniqueName(existingNames)
|
|
|
|
const result = await db.projects.updateOne(
|
2022-10-13 11:40:24 -04:00
|
|
|
{ _id: projectId, [path]: { $in: [null, ''] } },
|
2022-10-11 10:26:09 -04:00
|
|
|
{ $set: { [path]: name } }
|
|
|
|
)
|
|
|
|
return result.modifiedCount
|
|
|
|
}
|
|
|
|
|
|
|
|
function findUniqueName(existingFilenames) {
|
|
|
|
let index = 0
|
|
|
|
let filename = 'untitled'
|
|
|
|
while (existingFilenames.has(filename)) {
|
|
|
|
index += 1
|
|
|
|
filename = `untitled-${index}`
|
|
|
|
}
|
|
|
|
return filename
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:04:16 -04:00
|
|
|
main()
|
|
|
|
.then(() => {
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|