From f4366a01b3eb0ef34d4f89522feb7d74f258bf50 Mon Sep 17 00:00:00 2001 From: Paul Fitzpatrick Date: Thu, 21 Jan 2021 13:43:18 -0500 Subject: [PATCH] (core) tweak meaning of newRec to be state at end of bundle Summary: This redefines `newRec` to be the state at the end of a bundle, for the purposes of modifying a document. Updates and adds tests for creation/updates of rows that are now more intuitive hopefully. Test Plan: added tests Reviewers: dsagal Reviewed By: dsagal Differential Revision: https://phab.getgrist.com/D2707 --- app/server/lib/GranularAccess.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/app/server/lib/GranularAccess.ts b/app/server/lib/GranularAccess.ts index 1be5fcbf..2cb35648 100644 --- a/app/server/lib/GranularAccess.ts +++ b/app/server/lib/GranularAccess.ts @@ -628,10 +628,28 @@ export class GranularAccess { // For the moment, only deal with Record-related actions. // TODO: process table/column schema changes more carefully. if (isSchemaAction(a)) { return; } + const {rowsBefore, rowsAfter} = await this._getRowsBeforeAndAfter(idx); + await this._filterRowsAndCells(docSession, rowsBefore, rowsAfter, a, accessFn); + } + + private async _getRowsBeforeAndAfter(idx: number) { if (!this._rowSnapshots) { throw new Error('Logic error: actions not available'); } const allRowSnapshots = await this._rowSnapshots.get(); - const [rowsBefore, rowsAfter] = allRowSnapshots[idx]; - await this._filterRowsAndCells(docSession, rowsBefore, rowsAfter, a, accessFn); + const rowsBefore = allRowSnapshots[idx][0]; + // When determining whether to apply an action, we choose to make newRec refer to the + // state at the end of the entire bundle. So we look for the last pair of row snapshots + // for the same tableId. + // TODO: this would need to be elaborated to work well on bundles containing table + // renames. + const tableId = getTableId(rowsBefore); + for (const snapshots of allRowSnapshots.reverse()) { + const rowsAfter = snapshots[1]; + if (getTableId(rowsAfter) === tableId) { + return {rowsBefore, rowsAfter}; + } + } + // Should not be possible to arrive here. + throw new Error('Logic error: no matching snapshot'); } /**