Skip to content

Commit

Permalink
fix: share.document can be null when document is deleted
Browse files Browse the repository at this point in the history
closes #3724
  • Loading branch information
tommoor committed Jul 2, 2022
1 parent f744d48 commit 0c30d2b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
16 changes: 9 additions & 7 deletions server/commands/documentLoader.ts
Expand Up @@ -64,7 +64,7 @@ export default async function loadDocument({
],
});

if (!share || share.document.archivedAt) {
if (!share || share.document?.archivedAt) {
throw InvalidRequestError("Document could not be found for shareId");
}

Expand Down Expand Up @@ -133,16 +133,18 @@ export default async function loadDocument({
// If we're attempting to load a document that isn't the document originally
// shared then includeChildDocuments must be enabled and the document must
// still be active and nested within the shared document
if (share.document.id !== document.id) {
if (share.documentId !== document.id) {
if (!share.includeChildDocuments) {
throw AuthorizationError();
}

const childDocumentIds = await share.document.getChildDocumentIds({
archivedAt: {
[Op.is]: null,
},
});
const childDocumentIds =
(await share.document?.getChildDocumentIds({
archivedAt: {
[Op.is]: null,
},
})) ?? [];

if (!childDocumentIds.includes(document.id)) {
throw AuthorizationError();
}
Expand Down
2 changes: 1 addition & 1 deletion server/models/Share.ts
Expand Up @@ -113,7 +113,7 @@ class Share extends IdModel {
teamId: string;

@BelongsTo(() => Document, "documentId")
document: Document;
document: Document | null;

@ForeignKey(() => Document)
@Column(DataType.UUID)
Expand Down
4 changes: 2 additions & 2 deletions server/presenters/share.ts
Expand Up @@ -5,8 +5,8 @@ export default function present(share: Share, isAdmin = false) {
const data = {
id: share.id,
documentId: share.documentId,
documentTitle: share.document.title,
documentUrl: share.document.url,
documentTitle: share.document?.title,
documentUrl: share.document?.url,
published: share.published,
url: `${share.team.url}/share/${share.id}`,
createdBy: presentUser(share.user),
Expand Down

0 comments on commit 0c30d2b

Please sign in to comment.