From c033701577cef14d69b8e39a8ab2246481d4236c Mon Sep 17 00:00:00 2001 From: Sergei Samokhvalov Date: Thu, 11 Jan 2024 17:14:12 +0300 Subject: [PATCH] Add error for move folder (#51) * Add error for move folder * Add checkFileConnectionsExistence to Utils * Fix imports --- src/components/error-response-presenter.ts | 2 ++ src/const/us-error-constants.ts | 2 ++ src/db/models/new/entry/index.ts | 18 +----------------- src/db/models/new/entry/types.ts | 16 ++++++++++++++++ src/services/entry/actions/copy-to-workbook.ts | 3 ++- .../new/entry/copy-entry-to-workbook.ts | 3 ++- src/services/new/workbook/copy-workbook.ts | 7 ++----- .../new/workbook/get-workbook-content.ts | 2 +- .../utils/get-entry-permissions-by-workbook.ts | 2 +- src/utils/utils.ts | 14 ++++++++++++++ 10 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 src/db/models/new/entry/types.ts diff --git a/src/components/error-response-presenter.ts b/src/components/error-response-presenter.ts index 07c2982d..6cb5f685 100644 --- a/src/components/error-response-presenter.ts +++ b/src/components/error-response-presenter.ts @@ -65,6 +65,8 @@ export default (error: AppError | DBError) => { }, }; } + case US_ERRORS.FOLDER_ALREADY_EXIST_IN_TENANT: + case US_ERRORS.FOLDER_MOVE_FILE_CONNECTION_ERROR: case US_ERRORS.ENTRY_IS_ALREADY_IN_WORKBOOK: case US_ERRORS.ENTRY_IS_NOT_IN_WORKBOOK: case US_ERRORS.FOLDER_DESTINATION_NOT_EXIST: diff --git a/src/const/us-error-constants.ts b/src/const/us-error-constants.ts index 26e4753e..6d38978c 100644 --- a/src/const/us-error-constants.ts +++ b/src/const/us-error-constants.ts @@ -41,6 +41,8 @@ const US_ERRORS = { LOCK_TOKEN_REQUIRED: 'LOCK_TOKEN_REQUIRED', NOT_EXIST_LOCKED_ENTRY: 'NOT_EXIST_LOCKED_ENTRY', NOT_EXIST_FOLDER: 'NOT_EXIST_FOLDER', + FOLDER_ALREADY_EXIST_IN_TENANT: 'FOLDER_ALREADY_EXIST_IN_TENANT', + FOLDER_MOVE_FILE_CONNECTION_ERROR: 'FOLDER_MOVE_FILE_CONNECTION_ERROR', SCOPE_NOT_ALLOWED: 'SCOPE_NOT_ALLOWED', NOT_EXIST_STATE_BY_HASH: 'NOT_EXIST_STATE_BY_HASH', TENANTS_PER_CLOUD_QUOTA_EXCEEDED: 'TENANTS_PER_CLOUD_QUOTA_EXCEEDED', diff --git a/src/db/models/new/entry/index.ts b/src/db/models/new/entry/index.ts index d75f9eb4..263fdc44 100644 --- a/src/db/models/new/entry/index.ts +++ b/src/db/models/new/entry/index.ts @@ -2,23 +2,7 @@ import {Model} from '../../..'; import {RevisionModel} from '../revision'; import {WorkbookModel} from '../workbook'; import {Favorite} from '../favorite'; - -export enum EntryScope { - Connection = 'connection', - Dataset = 'dataset', - Widget = 'widget', - Dash = 'dash', - Folder = 'folder', - Config = 'config', - Pdf = 'pdf', -} - -// "type" is a string field and value can be any string, this enum is only used for checks in code -export enum EntryType { - File = 'file', - GsheetsV2 = 'gsheets_v2', - // types can be added as needed -} +import {EntryScope} from './types'; export const EntryColumn = { Scope: 'scope', diff --git a/src/db/models/new/entry/types.ts b/src/db/models/new/entry/types.ts new file mode 100644 index 00000000..ac571403 --- /dev/null +++ b/src/db/models/new/entry/types.ts @@ -0,0 +1,16 @@ +export enum EntryScope { + Connection = 'connection', + Dataset = 'dataset', + Widget = 'widget', + Dash = 'dash', + Folder = 'folder', + Config = 'config', + Pdf = 'pdf', +} + +// "type" is a string field and value can be any string, this enum is only used for checks in code +export enum EntryType { + File = 'file', + GsheetsV2 = 'gsheets_v2', + // types can be added as needed +} diff --git a/src/services/entry/actions/copy-to-workbook.ts b/src/services/entry/actions/copy-to-workbook.ts index 26823042..96342473 100644 --- a/src/services/entry/actions/copy-to-workbook.ts +++ b/src/services/entry/actions/copy-to-workbook.ts @@ -2,7 +2,8 @@ import {TransactionOrKnex} from 'objection'; import {AppError} from '@gravity-ui/nodekit'; import {getId} from '../../../db'; -import {Entry, EntryColumn, EntryScope, EntryType} from '../../../db/models/new/entry'; +import {Entry, EntryColumn} from '../../../db/models/new/entry'; +import {EntryScope, EntryType} from '../../../db/models/new/entry/types'; import {JoinedEntryRevision} from '../../../db/presentations/joined-entry-revision'; import {WorkbookModel} from '../../../db/models/new/workbook'; import {CTX} from '../../../types/models'; diff --git a/src/services/new/entry/copy-entry-to-workbook.ts b/src/services/new/entry/copy-entry-to-workbook.ts index 6fa1d170..2f82572d 100644 --- a/src/services/new/entry/copy-entry-to-workbook.ts +++ b/src/services/new/entry/copy-entry-to-workbook.ts @@ -2,7 +2,8 @@ import {AppError} from '@gravity-ui/nodekit'; import {transaction} from 'objection'; import {getId} from '../../../db'; import OldEntry from '../../../db/models/entry'; -import {Entry, EntryScope} from '../../../db/models/new/entry'; +import {EntryScope} from '../../../db/models/new/entry/types'; +import {Entry} from '../../../db/models/new/entry'; import {RevisionModel, RevisionModelColumn} from '../../../db/models/new/revision'; import {SyncLinks} from '../../../types/models/link'; import {US_ERRORS, BiTrackingLogs} from '../../../const'; diff --git a/src/services/new/workbook/copy-workbook.ts b/src/services/new/workbook/copy-workbook.ts index c2563cba..a17b9659 100644 --- a/src/services/new/workbook/copy-workbook.ts +++ b/src/services/new/workbook/copy-workbook.ts @@ -9,7 +9,7 @@ import {WorkbookModel, WorkbookModelColumn} from '../../../db/models/new/workboo import {JoinedEntryRevisionColumns} from '../../../db/presentations/joined-entry-revision'; import {RevisionModel} from '../../../db/models/new/revision'; import Link from '../../../db/models/links'; -import {Entry, EntryScope, EntryType} from '../../../db/models/new/entry'; +import {Entry} from '../../../db/models/new/entry'; import {WorkbookPermission} from '../../../entities/workbook'; import Utils, {logInfo} from '../../../utils'; import {copyToWorkbook} from '../../entry/actions'; @@ -160,10 +160,7 @@ export const copyWorkbook = async ( }) .timeout(Entry.DEFAULT_QUERY_TIMEOUT); - const fileConnectionTypes: string[] = [EntryType.File, EntryType.GsheetsV2]; - const fileConnectionExists = originEntries.some((entry) => { - return entry.scope === EntryScope.Connection && fileConnectionTypes.includes(entry.type); - }); + const fileConnectionExists = Utils.checkFileConnectionsExistence(originEntries); if (fileConnectionExists) { throw new AppError(US_ERRORS.WORKBOOK_COPY_FILE_CONNECTION_ERROR, { diff --git a/src/services/new/workbook/get-workbook-content.ts b/src/services/new/workbook/get-workbook-content.ts index dab1f615..5ad6e564 100644 --- a/src/services/new/workbook/get-workbook-content.ts +++ b/src/services/new/workbook/get-workbook-content.ts @@ -1,7 +1,7 @@ import {makeSchemaValidator} from '../../../components/validation-schema-compiler'; import {RETURN_NAVIGATION_COLUMNS, DEFAULT_PAGE, DEFAULT_PAGE_SIZE} from '../../../const'; import Navigation from '../../../db/models/navigation'; -import {EntryScope} from '../../../db/models/new/entry'; +import {EntryScope} from '../../../db/models/new/entry/types'; import Utils, {logInfo} from '../../../utils'; import {UsPermission} from '../../../types/models'; import {ServiceArgs} from '../types'; diff --git a/src/services/new/workbook/utils/get-entry-permissions-by-workbook.ts b/src/services/new/workbook/utils/get-entry-permissions-by-workbook.ts index 8219ea84..65cb94fc 100644 --- a/src/services/new/workbook/utils/get-entry-permissions-by-workbook.ts +++ b/src/services/new/workbook/utils/get-entry-permissions-by-workbook.ts @@ -3,7 +3,7 @@ import {Permissions} from '../../../../entities/workbook'; import type {WorkbookInstance} from '../../../../registry/common/entities/workbook/types'; import {Feature, isEnabledFeature} from '../../../../components/features'; import type {EntryScope as EntryScopeType} from '../../../../types/models'; -import {EntryScope} from '../../../../db/models/new/entry'; +import {EntryScope} from '../../../../db/models/new/entry/types'; export const getEntryPermissionsByWorkbook = ({ ctx, diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 9e6f808d..2085a953 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -5,6 +5,9 @@ import {EntryScope, USAPIResponse} from '../types/models'; import {ID_VARIABLES, CODING_BASE, TRUE_FLAGS, COPY_START, COPY_END} from '../const'; +import type {Entry} from '../db/models/new/entry'; +import {EntryScope as EntryScopeEnum, EntryType} from '../db/models/new/entry/types'; + const PROFILES: { [key: string]: any; } = {}; @@ -347,4 +350,15 @@ export class Utils { .map((word, index) => (index === 0 ? word : word[0].toUpperCase() + word.slice(1))) .join(''); } + + static checkFileConnectionsExistence(entries: Entry[]) { + const fileConnectionTypes: string[] = [EntryType.File, EntryType.GsheetsV2]; + + return entries.some((entry) => { + return ( + entry.scope === EntryScopeEnum.Connection && + fileConnectionTypes.includes(entry.type) + ); + }); + } }