Skip to content

Commit

Permalink
fix after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey-weber committed Nov 17, 2023
1 parent e9210c2 commit 319ecc9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 53 deletions.
20 changes: 7 additions & 13 deletions src/db/models/favorite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,36 +190,30 @@ class Favorite extends Model {
},
);

const workbookPermissionsMap = new Map<string, EntryPermissions>();
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) &&
includePermissionsInfo
) {
if (entry?.workbookId && workbooksMap.has(entry.workbookId)) {
const workbook = workbooksMap.get(entry.workbookId);

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

let isLocked = false;

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

if (!isReadPermission) {
isLocked = true;
Expand All @@ -229,7 +223,7 @@ class Favorite extends Model {
result.push({
...entry,
permissions: includePermissionsInfo
? workbookPermissionsMap.get(entry.workbookId)
? entryPermissionsMap.get(entry.entryId)
: undefined,
isLocked,
});
Expand Down
20 changes: 7 additions & 13 deletions src/db/models/navigation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,36 +253,30 @@ class Navigation extends Model {
},
);

const workbookPermissionsMap = new Map<string, EntryPermissions>();
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) &&
includePermissionsInfo
) {
if (entry?.workbookId && workbooksMap.has(entry.workbookId)) {
const workbook = workbooksMap.get(entry.workbookId);

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

let isLocked = false;

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

if (!isReadPermission) {
isLocked = true;
Expand All @@ -292,7 +286,7 @@ class Navigation extends Model {
result.push({
...entry,
permissions: includePermissionsInfo
? workbookPermissionsMap.get(entry.workbookId)
? entryPermissionsMap.get(entry.entryId)
: undefined,
isLocked,
});
Expand Down
2 changes: 0 additions & 2 deletions src/services/entry/actions/create-in-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ export async function createEntryInWorkbook(
workbook,
scope: resultEntry.scope,
});
}

if (includePermissionsInfo) {
resultEntry.permissions = permissions;
}

Expand Down
46 changes: 25 additions & 21 deletions src/services/entry/actions/get-entry-relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,7 @@ export async function getEntryRelations(ctx: CTX, params: GetEntryRelationsData)

let iamPermissions: Optional<EntryPermissions>;

if (entry.workbookId) {
const workbook = await getWorkbook(
{
ctx,
trx: Entry.replica,
},
{workbookId: entry.workbookId, includePermissionsInfo},
);

if (includePermissionsInfo) {
iamPermissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: entry.scope,
});
}
} else if (!isPrivateRoute) {
if (!entry.workbookId && !isPrivateRoute) {
await checkEntry(ctx, Entry.replica, {verifiableEntry: entry});
}

Expand All @@ -91,10 +75,30 @@ export async function getEntryRelations(ctx: CTX, params: GetEntryRelationsData)
});

if (entry.workbookId) {
relations = relations.map((item) => ({
...item,
permissions: iamPermissions,
})) as Entry[];
relations = (await Promise.all(
relations.map(async (item) => {
if (item.workbookId && includePermissionsInfo) {
const workbook = await getWorkbook(
{
ctx,
trx: Entry.replica,
},
{workbookId: item.workbookId, includePermissionsInfo},
);

iamPermissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: entry.scope,
});
}

return {
...item,
permissions: iamPermissions,
};
}),
)) as Entry[];
} else {
if (!isPrivateRoute && ctx.config.dlsEnabled) {
relations = await DLS.checkBulkPermission(
Expand Down
2 changes: 1 addition & 1 deletion src/services/new/entry/get-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const getEntry = async (
);

if (includePermissionsInfo) {
iamPermissions = await getEntryPermissionsByWorkbook({
iamPermissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: joinedEntryRevisionFavorite[EntryColumn.Scope],
Expand Down
1 change: 0 additions & 1 deletion src/services/new/workbook/get-workbook-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ export const getWorkbookContent = async (
if (includePermissionsInfo) {
permissions = getEntryPermissionsByWorkbook({
ctx,
trx: targetTrx,
workbook,
scope: entry.scope,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {TransactionOrKnex} from 'objection';
import {AppContext} from '@gravity-ui/nodekit';
import {Permissions} from '../../../../entities/workbook';
import type {WorkbookInstance} from '../../../../registry/common/entities/workbook/types';
Expand All @@ -12,7 +11,6 @@ export const getEntryPermissionsByWorkbook = ({
scope,
}: {
ctx: AppContext;
trx?: TransactionOrKnex;
workbook: WorkbookInstance;
scope?: EntryScopeType | null;
}) => {
Expand Down

0 comments on commit 319ecc9

Please sign in to comment.