mirror of
https://github.com/outline/outline.git
synced 2026-06-13 03:14:59 +03:00
bf45e97641
* Update types * fix circular dep * type imports * lint type imports and --fix
44 lines
892 B
TypeScript
44 lines
892 B
TypeScript
import { NotFoundError, PaymentRequiredError } from "@server/errors";
|
|
import type { User } from "@server/models";
|
|
import { Document } 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;
|
|
}
|