From 217e53d8b69a8cff6ac89f469bd116febf40c530 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 23 Jul 2023 13:06:34 -0400 Subject: [PATCH] fix: Enable toggling of insights while document is draft --- app/actions/definitions/documents.tsx | 2 +- app/models/Document.ts | 13 ++++++--- app/scenes/Document/components/Document.tsx | 2 +- app/scenes/Document/components/Insights.tsx | 5 ++-- app/scenes/DocumentPublish.tsx | 2 +- server/policies/document.ts | 31 +++++++++++---------- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx index 86ad7381a..5c1cdce1b 100644 --- a/app/actions/definitions/documents.tsx +++ b/app/actions/definitions/documents.tsx @@ -159,7 +159,7 @@ export const publishDocument = createAction({ } if (document?.collectionId) { - await document.save({ + await document.save(undefined, { publish: true, }); stores.toasts.showToast(t("Document published"), { diff --git a/app/models/Document.ts b/app/models/Document.ts index ebd0c2503..5db6a4a05 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -84,7 +84,6 @@ export default class Document extends ParanoidModel { /** * Whether team members can see who has viewed this document. */ - @Field @observable insightsEnabled: boolean; @@ -352,12 +351,18 @@ export default class Document extends ParanoidModel { templatize = () => this.store.templatize(this.id); @action - save = async (options?: SaveOptions | undefined) => { - const params = this.toAPI(); + save = async ( + fields: Partial | undefined, + options?: SaveOptions | undefined + ) => { + const params = fields ?? this.toAPI(); this.isSaving = true; try { - const model = await this.store.save({ ...params, id: this.id }, options); + const model = await this.store.save( + { ...params, ...fields, id: this.id }, + options + ); // if saving is successful set the new values on the model itself set(this, { ...params, ...model }); diff --git a/app/scenes/Document/components/Document.tsx b/app/scenes/Document/components/Document.tsx index 636a11506..a826b4077 100644 --- a/app/scenes/Document/components/Document.tsx +++ b/app/scenes/Document/components/Document.tsx @@ -305,7 +305,7 @@ class DocumentScene extends React.Component { this.isPublishing = !!options.publish; try { - const savedDocument = await document.save(options); + const savedDocument = await document.save(undefined, options); this.isEditorDirty = false; if (options.done) { diff --git a/app/scenes/Document/components/Insights.tsx b/app/scenes/Document/components/Insights.tsx index 714a9c999..4504105f4 100644 --- a/app/scenes/Document/components/Insights.tsx +++ b/app/scenes/Document/components/Insights.tsx @@ -160,8 +160,9 @@ function Insights() { { - document.insightsEnabled = ev.currentTarget.checked; - await document.save(); + await document.save({ + insightsEnabled: ev.currentTarget.checked, + }); }} /> diff --git a/app/scenes/DocumentPublish.tsx b/app/scenes/DocumentPublish.tsx index 3c807ddf6..90e19ff63 100644 --- a/app/scenes/DocumentPublish.tsx +++ b/app/scenes/DocumentPublish.tsx @@ -52,7 +52,7 @@ function DocumentPublish({ document }: Props) { } document.collectionId = collectionId; - await document.save({ publish: true }); + await document.save(undefined, { publish: true }); showToast(t("Document published"), { type: "success", diff --git a/server/policies/document.ts b/server/policies/document.ts index 8ef45d804..70993fad4 100644 --- a/server/policies/document.ts +++ b/server/policies/document.ts @@ -129,6 +129,23 @@ allow(User, "update", Document, (user, document) => { return user.teamId === document.teamId; }); +allow(User, "updateInsights", Document, (user, document) => { + if (!document || !document.isActive) { + return false; + } + + if (document.collectionId) { + invariant( + document.collection, + "collection is missing, did you forget to include in the query scope?" + ); + if (cannot(user, "update", document.collection)) { + return false; + } + } + return user.teamId === document.teamId; +}); + allow(User, "createChildDocument", Document, (user, document) => { if (!document || !document.isActive || document.isDraft) { return false; @@ -277,20 +294,6 @@ allow(User, "archive", Document, (user, document) => { return user.teamId === document.teamId; }); -allow(User, "updateInsights", Document, (user, document) => { - if (!document || !document.isActive || document.isDraft) { - return false; - } - invariant( - document.collection, - "collection is missing, did you forget to include in the query scope?" - ); - if (cannot(user, "update", document.collection)) { - return false; - } - return user.teamId === document.teamId; -}); - allow(User, "unarchive", Document, (user, document) => { if (!document) { return false;