Files
outline/server/queues/tasks/DetachDraftsFromCollectionTask.ts
T
Hemachandar 142985c6d7 Move Document event writing to model layer (#9790)
* documents.restore, documents.unarchive

* documents.templatize

* documents.archive

* documents.unpublish

* documents.create, documents.update

* documents.title_change event

* documents.move

* documents.delete

* tsc, tests

* tsc

* Copilot feedback

---------

Co-authored-by: Tom Moor <tom@getoutline.com>
2025-11-23 20:40:45 +01:00

58 lines
1.3 KiB
TypeScript

import { Op } from "sequelize";
import documentMover from "@server/commands/documentMover";
import { Collection, Document, User } from "@server/models";
import { sequelize } from "@server/storage/database";
import BaseTask from "./BaseTask";
import { createContext } from "@server/context";
type Props = {
collectionId: string;
actorId: string;
ip: string | null;
};
export default class DetachDraftsFromCollectionTask extends BaseTask<Props> {
async perform(props: Props) {
const [collection, actor] = await Promise.all([
Collection.findByPk(props.collectionId, {
paranoid: false,
}),
User.findByPk(props.actorId),
]);
if (
!actor ||
!collection ||
!(collection.deletedAt || collection.archivedAt)
) {
return;
}
const documents = await Document.scope("withDrafts").findAll({
where: {
collectionId: props.collectionId,
template: false,
publishedAt: {
[Op.is]: null,
},
},
paranoid: false,
});
return sequelize.transaction(async (transaction) => {
const ctx = createContext({
user: actor,
ip: props.ip,
transaction,
});
for (const document of documents) {
await documentMover(ctx, {
document,
collectionId: null,
});
}
});
}
}