Files
outline/server/queues/tasks/DuplicateCollectionDocumentsTask.ts
T
Tom Moor ade792f709 feat: Add ability to duplicate collection (#13197)
* feat: Add ability to duplicate collection

* Add confirmation dialog
2026-07-29 21:28:59 -04:00

72 lines
2.0 KiB
TypeScript

import { Op } from "sequelize";
import documentDuplicator from "@server/commands/documentDuplicator";
import { createContext } from "@server/context";
import { Collection, Document, User } from "@server/models";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import { sequelize } from "@server/storage/database";
import { BaseTask } from "./base/BaseTask";
type Props = {
/** The collection to duplicate documents into */
collectionId: string;
/** The collection to duplicate documents from */
originalCollectionId: string;
/** The user that initiated the duplication */
actorId: string;
/** The IP address of the user that initiated the duplication */
ip: string | null;
};
export default class DuplicateCollectionDocumentsTask extends BaseTask<Props> {
async perform(props: Props) {
const [collection, original, actor] = await Promise.all([
Collection.findByPk(props.collectionId),
Collection.findByPk(props.originalCollectionId),
User.findByPk(props.actorId),
]);
if (!actor || !original || !collection?.isActive) {
return;
}
const rootDocuments = await Document.findAll({
where: {
teamId: original.teamId,
collectionId: original.id,
parentDocumentId: {
[Op.is]: null,
},
archivedAt: {
[Op.is]: null,
},
},
});
if (!rootDocuments.length) {
return;
}
const structure = await original.getCachedDocumentStructure();
const sorted = DocumentHelper.sortDocumentsByStructure(
rootDocuments,
structure ?? []
).reverse(); // we have to reverse since the root documents will be added in reverse order
return sequelize.transaction(async (transaction) => {
const ctx = createContext({
user: actor,
ip: props.ip,
transaction,
});
for (const document of sorted) {
await documentDuplicator(ctx, {
document,
collection,
recursive: true,
});
}
});
}
}