Skip to content

Commit

Permalink
Don't allow to delete workbook templates & collections with them (#71)
Browse files Browse the repository at this point in the history
* Don't allow to delete workbook templates & collections with them

* Don't allow to delete workbook templates & collections with them

---------

Co-authored-by: Stanislav Kiselev <“[email protected]”>
  • Loading branch information
stankis and Stanislav Kiselev authored Feb 2, 2024
1 parent 4ec7171 commit db9f33d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/components/error-response-presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,24 @@ export default (error: AppError | DBError) => {
},
};
}
case US_ERRORS.WORKBOOK_TEMPLATE_CANT_BE_DELETED: {
return {
code: 403,
response: {
code,
message: "Workbook template can't be deleted",
},
};
}
case US_ERRORS.COLLECTION_WITH_WORKBOOK_TEMPLATE_CANT_BE_DELETED: {
return {
code: 403,
response: {
code,
message: "Collection with workbook template can't be deleted",
},
};
}
default:
return {
code: 500,
Expand Down
3 changes: 3 additions & 0 deletions src/const/us-error-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ const US_ERRORS = {
MOVE_TENANT_CONTENT_ERROR: 'MOVE_TENANT_CONTENT_ERROR',
INCORRECT_WORKBOOK_ID_HEADER: 'INCORRECT_WORKBOOK_ID_HEADER',
WORKBOOK_ISOLATION_INTERRUPTION: 'WORKBOOK_ISOLATION_INTERRUPTION',
WORKBOOK_TEMPLATE_CANT_BE_DELETED: 'WORKBOOK_TEMPLATE_CANT_BE_DELETED',
COLLECTION_WITH_WORKBOOK_TEMPLATE_CANT_BE_DELETED:
'COLLECTION_WITH_WORKBOOK_TEMPLATE_CANT_BE_DELETED',
};

export const ERROR_BY_DLS_STATUS_CODE: {[key: number]: string} = {
Expand Down
11 changes: 10 additions & 1 deletion src/services/new/collection/delete-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {ServiceArgs} from '../types';
import {getPrimary} from '../utils';
import {deleteWorkbook} from '../workbook';
import {makeSchemaValidator} from '../../../components/validation-schema-compiler';
import {CURRENT_TIMESTAMP} from '../../../const';
import {CURRENT_TIMESTAMP, US_ERRORS} from '../../../const';
import {raw, transaction} from 'objection';
import {CollectionModel, CollectionModelColumn} from '../../../db/models/new/collection';
import {WorkbookModel, WorkbookModelColumn} from '../../../db/models/new/workbook';
import Utils, {logInfo} from '../../../utils';
import {CollectionPermission} from '../../../entities/collection';
import {AppError} from '@gravity-ui/nodekit';

const validateArgs = makeSchemaValidator({
type: 'object',
Expand Down Expand Up @@ -128,6 +129,14 @@ export const deleteCollection = async (
})
.timeout(WorkbookModel.DEFAULT_QUERY_TIMEOUT);

for (const workbook of workbooksForDelete) {
if (workbook.isTemplate) {
throw new AppError("Collection with workbook template can't be deleted", {
code: US_ERRORS.COLLECTION_WITH_WORKBOOK_TEMPLATE_CANT_BE_DELETED,
});
}
}

// TODO: Rewrite the deletion for the optimal number of requests
await Promise.all(
workbooksForDelete.map((workbook) => {
Expand Down
6 changes: 6 additions & 0 deletions src/services/new/workbook/delete-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export const deleteWorkbook = async (
{workbookId},
);

if (workbook.model.isTemplate) {
throw new AppError("Workbook template can't be deleted", {
code: US_ERRORS.WORKBOOK_TEMPLATE_CANT_BE_DELETED,
});
}

if (accessServiceEnabled && !skipCheckPermissions) {
let parentIds: string[] = [];

Expand Down

0 comments on commit db9f33d

Please sign in to comment.