Skip to content

Commit

Permalink
Add method - getEntriesWithPermissionsOnly (#168)
Browse files Browse the repository at this point in the history
* Add method - getEntriesWithPermissionsOnly

* Change call getEntriesWithPermissionsOnly from registry

* Fix includePermissionsInfo

* Change getEntriesWithPermissionsOnly

* Refactor

* Fix types

* Fix after review

* Hide locked workbook entries in flat lists

* Fix bug with includePermissionsInfo

* Refactor

* Change type on generic and move order result

* Refactor

* Fix

* Fix type

* Add support report scope for entries
  • Loading branch information
Sergey-weber authored Sep 12, 2024
1 parent 675d401 commit 7c70c2a
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 299 deletions.
2 changes: 2 additions & 0 deletions api/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ export {
export {formatWorkbookModel} from '../src/services/new/workbook/formatters';

export {crossSyncCopiedJoinedEntryRevisions} from '../src/services/new/workbook';

export {filterEntriesByPermission} from '../src/services/new/entry/utils';
148 changes: 14 additions & 134 deletions src/db/models/favorite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@ import {RETURN_FAVORITES_COLUMNS, US_ERRORS} from '../../../const';
import {registry} from '../../../registry';

import {getWorkbook} from '../../../services/new/workbook';
import {filterEntriesByPermission} from '../../../services/new/entry/utils';

import {getWorkbooksListByIds} from '../../../services/new/workbook/get-workbooks-list-by-ids';

import {getEntryPermissionsByWorkbook} from '../../../services/new/workbook/utils';

import {EntryPermissions} from '../../../services/new/entry/types';

import {WorkbookInstance} from '../../../registry/common/entities/workbook/types';

interface Favorite extends MT.FavoriteColumns {}
interface FavoriteFields extends MT.FavoriteColumns {
isLocked?: boolean;
permissions?: MT.UsPermission;
}
interface Favorite extends FavoriteFields {}
class Favorite extends Model {
static get tableName() {
return 'favorites';
Expand Down Expand Up @@ -74,8 +71,6 @@ class Favorite extends Model {
dlContext,
});

const {DLS} = registry.common.classes.get();

const {login} = requestedBy;

const {isValid, validationErrors} = validateGetFavorites({
Expand All @@ -97,8 +92,6 @@ class Favorite extends Model {
});
}

let result: any[] = [];

const entries = await Favorite.query(this.replica)
.select(RETURN_FAVORITES_COLUMNS)
.join('entries', 'favorites.entryId', 'entries.entryId')
Expand Down Expand Up @@ -159,132 +152,19 @@ class Favorite extends Model {
curPage: entries,
});

const workbookEntries: Favorite[] = [];
const entryWithoutWorkbook: Favorite[] = [];

entries.forEach((entry) => {
if (entry.workbookId) {
workbookEntries.push(entry);
} else {
entryWithoutWorkbook.push(entry);
}
});

// TODO: use originatePermissions
if (entryWithoutWorkbook.length > 0) {
if (ctx.config.dlsEnabled) {
result = await DLS.checkBulkPermission(
{ctx},
{
entities: entryWithoutWorkbook,
action: MT.DlsActions.Read,
includePermissionsInfo,
},
);
} else {
result = entryWithoutWorkbook.map((entry) => ({
...entry,
...(includePermissionsInfo && {
permissions: {
execute: true,
read: true,
edit: true,
admin: true,
},
}),
}));
}
}

if (workbookEntries.length > 0) {
if (ctx.config.accessServiceEnabled) {
const workbookList = await getWorkbooksListByIds(
{ctx},
{
workbookIds: workbookEntries.map((entry) => entry.workbookId) as string[],
includePermissionsInfo,
},
);

const entryPermissionsMap = new Map<string, EntryPermissions>();
const workbooksMap = new Map<string, WorkbookInstance>();

workbookList.forEach((workbook) => {
workbooksMap.set(workbook.model.workbookId, workbook);
});

workbookEntries.forEach((entry) => {
if (entry?.workbookId && workbooksMap.has(entry.workbookId)) {
const workbook = workbooksMap.get(entry.workbookId);

if (workbook && includePermissionsInfo) {
const permissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: entry.scope,
});
entryPermissionsMap.set(entry.entryId, permissions);
}

let isLocked = false;

if (entryPermissionsMap.has(entry.entryId)) {
const isReadPermission = entryPermissionsMap.get(entry.entryId)?.read;

if (!isReadPermission) {
isLocked = true;
}
}

result.push({
...entry,
permissions: includePermissionsInfo
? entryPermissionsMap.get(entry.entryId)
: undefined,
isLocked,
});
}
});
} else {
result = [
...result,
...workbookEntries.map((entry) => ({
...entry,
isLocked: false,
...(includePermissionsInfo && {
permissions: {
execute: true,
read: true,
edit: true,
admin: true,
},
}),
})),
];
}
}

const mapResult = new Map<string, Favorite>();

result.forEach((entry) => {
mapResult.set(entry.entryId, entry);
});

const orderedResult: Favorite[] = [];

entries.forEach((entry) => {
const model = mapResult.get(entry.entryId);

if (model) {
orderedResult.push(model);
}
});
const entriesWithPermissionsOnly = await filterEntriesByPermission(
{ctx},
{
entries: entries,
includePermissionsInfo,
},
);

ctx.log('GET_FAVORITES_SUCCESS');

const data: MT.PaginationEntriesResponse = {
nextPageToken,
entries: orderedResult,
entries: entriesWithPermissionsOnly,
};

return data;
Expand Down
Loading

0 comments on commit 7c70c2a

Please sign in to comment.