Skip to content

Commit

Permalink
Add new methods for make collections and workbooks map (#205)
Browse files Browse the repository at this point in the history
* Add new method for make map collections and workbooks

* Refactor

* Refactor
  • Loading branch information
Sergey-weber authored Nov 14, 2024
1 parent 386354c commit 40c6a73
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 63 deletions.
7 changes: 6 additions & 1 deletion api/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ export {
} from '../src/services/entry/actions/get-related-entries';

export {getCollection} from '../src/services/new/collection/get-collection';
export {getParentIds} from '../src/services/new/collection/utils/get-parents';
export {
getParentIds,
makeWorkbooksWithParentsMap,
makeCollectionsWithParentsMap,
} from '../src/services/new/collection/utils/get-parents';

export {getWorkbook, createWorkbook, setWorkbookIsTemplate} from '../src/services/new/workbook';
export {checkWorkbookPermission} from '../src/services/new/workbook/utils/check-workbook-permission';
export {copyEntriesToWorkbook} from '../src/services/new/entry';
Expand Down
33 changes: 5 additions & 28 deletions src/services/new/collection/delete-collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import {CollectionPermission} from '../../../entities/collection';
import {AppError} from '@gravity-ui/nodekit';
import {getCollectionsListByIds} from './get-collections-list-by-ids';
import {markCollectionsAsDeleted} from './utils/mark-collections-as-deleted';
import {getParents, getParentsIdsFromMap} from './utils';
import {registry} from '../../../registry';
import {CollectionInstance} from '../../../registry/common/entities/collection/types';
import {makeCollectionsWithParentsMap} from './utils';

const validateArgs = makeSchemaValidator({
type: 'object',
Expand Down Expand Up @@ -89,31 +87,10 @@ export const deleteCollections = async (

const collectionsForDeleteIds = collectionsForDelete.map((item) => item.collectionId);

const collectionsMap = new Map<CollectionInstance, string[]>();

const parents = await getParents({
ctx,
trx: getReplica(trx),
collectionIds: collectionsForDeleteIds,
});

const parentsMap = new Map<string, Nullable<string>>();

parents.forEach((parent: CollectionModel) => {
parentsMap.set(parent.collectionId, parent.parentId);
});

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

collectionsForDelete.forEach((model) => {
const parentId = model.parentId;

const parentsForCollection = getParentsIdsFromMap(parentId, parentsMap);

const collection = new Collection({ctx, model});

collectionsMap.set(collection, parentsForCollection);
});
const collectionsMap = await makeCollectionsWithParentsMap(
{ctx, trx},
{models: collectionsForDelete},
);

const workbooksForDelete = await WorkbookModel.query(getReplica(trx))
.select()
Expand Down
83 changes: 83 additions & 0 deletions src/services/new/collection/utils/get-parents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import {TransactionOrKnex} from 'objection';
import {AppContext} from '@gravity-ui/nodekit';
import {getReplica} from '../../utils';
import {CollectionModel, CollectionModelColumn} from '../../../../db/models/new/collection';
import {WorkbookInstance} from '../../../../registry/common/entities/workbook/types';
import {CollectionInstance} from '../../../../registry/common/entities/collection/types';
import {ServiceArgs} from '../../types';
import {registry} from '../../../../registry';
import {WorkbookModel} from '../../../../db/models/new/workbook';

interface Ctx {
ctx: AppContext;
Expand Down Expand Up @@ -93,3 +98,81 @@ export const getParentsIdsFromMap = (

return arr;
};

const makeParentsMap = (collectionModels: CollectionModel[]) => {
const parentsMap = new Map<string, Nullable<string>>();

collectionModels.forEach((parent: CollectionModel) => {
parentsMap.set(parent.collectionId, parent.parentId);
});

return parentsMap;
};

export const makeWorkbooksWithParentsMap = async (
{trx, ctx}: ServiceArgs,
{
models,
}: {
models: WorkbookModel[];
},
): Promise<Map<WorkbookInstance, string[]>> => {
const workbooksWithParentsMap = new Map<WorkbookInstance, string[]>();
const collectionIds = models.map((item) => item.collectionId).filter((item) => Boolean(item));

const parents = await getParents({
ctx,
trx: getReplica(trx),
collectionIds,
});

const parentsMap = makeParentsMap(parents);

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

models.forEach((model) => {
const collectionId = model.collectionId;

const parentsForWorkbook = getParentsIdsFromMap(collectionId, parentsMap);

const workbook = new Workbook({ctx, model});

workbooksWithParentsMap.set(workbook, parentsForWorkbook);
});

return workbooksWithParentsMap;
};

export const makeCollectionsWithParentsMap = async (
{ctx, trx}: ServiceArgs,
{
models,
}: {
models: CollectionModel[];
},
): Promise<Map<CollectionInstance, string[]>> => {
const collectionsWithParentsMap = new Map<CollectionInstance, string[]>();
const collectionIds = models.map((item) => item.collectionId).filter((item) => Boolean(item));

const parents = await getParents({
ctx,
trx: getReplica(trx),
collectionIds,
});

const parentsMap = makeParentsMap(parents);

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

models.forEach((model) => {
const parentId = model.parentId;

const parentsForCollection = getParentsIdsFromMap(parentId, parentsMap);

const collection = new Collection({ctx, model});

collectionsWithParentsMap.set(collection, parentsForCollection);
});

return collectionsWithParentsMap;
};
38 changes: 4 additions & 34 deletions src/services/new/workbook/get-workbooks-list-by-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import {getReplica} from '../utils';
import Utils from '../../../utils';
import {registry} from '../../../registry';

import {getParents, getParentsIdsFromMap} from '../collection/utils';
import {makeWorkbooksWithParentsMap} from '../collection/utils';

import {WorkbookPermission} from '../../../entities/workbook';

import {Feature, isEnabledFeature} from '../../../components/features';

import {CollectionModel} from '../../../db/models/new/collection';
import {WorkbookInstance} from '../../../registry/common/entities/workbook/types';

const validateArgs = makeSchemaValidator({
Expand Down Expand Up @@ -76,41 +75,12 @@ export const getWorkbooksListByIds = async (
return workbookList.map((model) => new Workbook({ctx, model}));
}

const collectionIds = workbookList
.map((workbook) => workbook.collectionId)
.filter((item) => Boolean(item));

const parents = await getParents({
ctx,
trx: targetTrx,
collectionIds,
});

const workbooksMap = new Map<WorkbookModel, string[]>();
const workbooksMap = await makeWorkbooksWithParentsMap({ctx, trx}, {models: workbookList});
const acceptedWorkbooksMap = new Map<WorkbookModel, string[]>();

const parentsMap = new Map<string, Nullable<string>>();

parents.forEach((parent: CollectionModel) => {
parentsMap.set(parent.collectionId, parent.parentId);
});

workbookList.forEach((model) => {
const collectionId = model.collectionId;

const parentsforWorkbook = getParentsIdsFromMap(collectionId, parentsMap);

workbooksMap.set(model, parentsforWorkbook);
});

const checkPermissionPromises: Promise<WorkbookInstance | void>[] = [];

workbooksMap.forEach((parentIds, workbookModel) => {
const workbook = new Workbook({
ctx,
model: workbookModel,
});

workbooksMap.forEach((parentIds, workbook) => {
const promise = workbook
.checkPermission({
parentIds,
Expand All @@ -119,7 +89,7 @@ export const getWorkbooksListByIds = async (
: WorkbookPermission.View,
})
.then(() => {
acceptedWorkbooksMap.set(workbookModel, parentIds);
acceptedWorkbooksMap.set(workbook.model, parentIds);

return workbook;
})
Expand Down

0 comments on commit 40c6a73

Please sign in to comment.