Files
outline/server/queues/tasks/DuplicateCollectionDocumentsTask.ts
Tom Moor bc992fb22a refactor: Add jobId hook to BaseTask for deduplicating queued jobs (#13890)
* refactor: Add jobId hook to BaseTask for deduplicating queued jobs

* fix: Drop jobId from APIImportTask, it re-schedules itself while active
2026-09-25 23:44:16 +00:00

76 lines
2.2 KiB
TypeScript

import { Op } from "sequelize";
import { documentsDuplicator } 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> {
protected jobId({ collectionId }: Props) {
return `duplicate-collection:${collectionId}`;
}
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,
});
// The whole collection is duplicated as one unit so that links between
// documents in different trees are remapped to the copies.
await documentsDuplicator(ctx, {
documents: sorted,
collection,
recursive: true,
});
});
}
}