Files
outline/server/commands/documentLoader.ts
Hemachandar 888add7920 Use shareLoader for loading public docs in documents.info endpoint (#9822)
* Use `shareLoader` for loading public docs in `documents.info` endpoint

* cleanup sharedCache in documents store

* remove shareId path param for authenticated routes

* require auth in document loader

---------

Co-authored-by: Tom Moor <tom@getoutline.com>
2025-08-07 22:41:04 -04:00

43 lines
854 B
TypeScript

import { NotFoundError, PaymentRequiredError } from "@server/errors";
import { Document, User } from "@server/models";
import { authorize } from "@server/policies";
type Props = {
id: string;
user: User;
includeState?: boolean;
};
export default async function loadDocument({
id,
user,
includeState,
}: Props): Promise<Document> {
const document = await Document.findByPk(id, {
userId: user ? user.id : undefined,
paranoid: false,
includeState,
});
if (!document) {
throw NotFoundError();
}
if (document.deletedAt) {
// don't send data if user cannot restore deleted doc
if (user) {
authorize(user, "restore", document);
}
} else {
if (user) {
authorize(user, "read", document);
}
}
if (document.isTrialImport) {
throw PaymentRequiredError();
}
return document;
}