Skip to content

Commit

Permalink
Merge branch 'main' into CHARTS-8756-fix-routes-for-mass-move
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey-weber committed Dec 10, 2023
2 parents ac256f9 + 25bc819 commit 7a66a35
Show file tree
Hide file tree
Showing 20 changed files with 346 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/components/error-response-presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,14 @@ export default (error: AppError | DBError) => {
},
};
}
case US_ERRORS.WORKBOOK_IS_ALREADY_RESTORED: {
return {
code: 400,
response: {
message: 'The workbook is alredy restored',
},
};
}
case US_ERRORS.WORKBOOK_ENTITY_ERROR: {
return {
code: 500,
Expand Down
1 change: 1 addition & 0 deletions src/const/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const RETURN_NAVIGATION_COLUMNS = [
];

export const RETURN_FAVORITES_COLUMNS = [
'favorites.alias',
'entries.entryId',
'entries.scope',
'entries.type',
Expand Down
1 change: 1 addition & 0 deletions src/const/us-error-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const US_ERRORS = {
COLLECTION_NOT_EXISTS: 'COLLECTION_NOT_EXISTS',
COLLECTION_CIRCULAR_REFERENCE_ERROR: 'COLLECTION_CIRCULAR_REFERENCE_ERROR',
WORKBOOK_NOT_EXISTS: 'WORKBOOK_NOT_EXISTS',
WORKBOOK_IS_ALREADY_RESTORED: 'WORKBOOK_IS_ALREADY_RESTORED',
WORKBOOK_ENTITY_ERROR: 'WORKBOOK_ENTITY_ERROR',
WORKBOOK_COPY_FILE_CONNECTION_ERROR: 'WORKBOOK_COPY_FILE_CONNECTION_ERROR',
WORKBOOK_OPERATION_FORBIDDEN: 'WORKBOOK_OPERATION_FORBIDDEN',
Expand Down
13 changes: 13 additions & 0 deletions src/controllers/favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ export default {

const {code, response} = prepareResponse({data: result});

res.status(code).send(response);
},
renameFavorite: async (req: Request, res: Response) => {
const {params, body} = req;

const result = await FavoriteService.rename({
entryId: params.entryId,
name: body.name,
ctx: req.ctx,
});

const {code, response} = prepareResponse({data: result});

res.status(code).send(response);
},
};
19 changes: 19 additions & 0 deletions src/controllers/workbooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getAllWorkbooks,
OrderField,
OrderDirection,
restoreWorkbook,
} from '../services/new/workbook';
import {
formatWorkbookModel,
Expand All @@ -25,6 +26,7 @@ import {
formatWorkbookModelWithOperation,
formatWorkbooksList,
formatSetWorkbookIsTemplate,
formatRestoreWorkbook,
} from '../services/new/workbook/formatters';

export default {
Expand Down Expand Up @@ -260,4 +262,21 @@ export default {
const {code, response} = prepareResponse({data: formattedResponse});
res.status(code).send(response);
},

restore: async (req: Request, res: Response) => {
const {params} = req;

const result = await restoreWorkbook(
{
ctx: req.ctx,
},
{
workbookId: params.workbookId,
},
);

const formattedResponse = formatRestoreWorkbook(result);
const {code, response} = prepareResponse({data: formattedResponse});
res.status(code).send(response);
},
};
39 changes: 38 additions & 1 deletion src/db/models/favorite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {AppError} from '@gravity-ui/nodekit';
import * as MT from '../../../types/models';
import {DlsActions} from '../../../types/models';
import Utils from '../../../utils';
import {validateGetFavorites, validateAddFavorite, validateDeleteFavorite} from './scheme';
import {
validateGetFavorites,
validateAddFavorite,
validateDeleteFavorite,
validateRenameFavorite,
} from './scheme';
import {RETURN_FAVORITES_COLUMNS} from '../../../const';
import {registry} from '../../../registry';

Expand Down Expand Up @@ -362,6 +367,38 @@ class Favorite extends Model {

return result;
}

static async rename({
tenantId,
entryId,
name,
requestedBy,
ctx,
dlContext,
}: MT.RenameFavoriteConfig) {
ctx.log('RENAME_FAVORITE_REQUEST', {
tenantId,
entryId,
name,
requestedBy,
dlContext,
});

validateRenameFavorite({entryId, name});

const {login} = requestedBy;

const result = await Favorite.query(this.primary)
.update({alias: name})
.where({entryId, tenantId, login})
.returning('*')
.first()
.timeout(Model.DEFAULT_QUERY_TIMEOUT);

ctx.log('RENAME_FAVORITE_SUCCESS');

return result;
}
}

export default Favorite;
15 changes: 14 additions & 1 deletion src/db/models/favorite/scheme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import compileSchema from '../../../components/validation-schema-compiler';
import compileSchema, {makeSchemaValidator} from '../../../components/validation-schema-compiler';
import {AJV_PATTERN_KEYS_NOT_OBJECT} from '../../../const';

export const validateGetFavorites = compileSchema({
Expand Down Expand Up @@ -76,3 +76,16 @@ export const validateDeleteFavorite = compileSchema({
},
},
});
export const validateRenameFavorite = makeSchemaValidator({
type: 'object',
required: ['entryId', 'name'],
properties: {
entryId: {
type: 'string',
},
name: {
type: ['string', 'null'],
verifyEntryName: true,
},
},
});
1 change: 1 addition & 0 deletions src/db/models/new/favorite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class Favorite extends Model {
entryId!: string;
tenantId!: string;
login!: string;
alias!: string | null;
createdAt!: string;

entry?: Entry;
Expand Down
13 changes: 13 additions & 0 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ export function getRoutes(nodekit: NodeKit, options: GetRoutesOptions) {
handler: favoritesController.deleteFavorite,
write: true,
}),
renameFavorite: makeRoute({
route: 'POST /v1/favorites/:entryId/rename',
handler: favoritesController.renameFavorite,
write: true,
}),
};

if (isEnabledFeature(ctx, Feature.CollectionsEnabled)) {
Expand Down Expand Up @@ -333,6 +338,14 @@ export function getRoutes(nodekit: NodeKit, options: GetRoutesOptions) {
write: true,
}),

privateRestoreWorkbook: makeRoute({
route: 'POST /private/v2/workbooks/:workbookId/restore',
handler: workbooksController.restore,
authPolicy: AuthPolicy.disabled,
private: true,
write: true,
}),

privateGetAllWorkbooks: makeRoute({
route: 'GET /private/all-workbooks',
handler: workbooksController.getAll,
Expand Down
14 changes: 14 additions & 0 deletions src/services/favorite.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ export default class FavoriteService {
ctx,
});
}

static async rename({entryId, name, ctx}: ST.RenameFavorite) {
const {requestId, tenantId, user, dlContext} = ctx.get('info');

return await Favorite.rename({
requestId,
tenantId,
entryId,
name,
requestedBy: user,
dlContext,
ctx,
});
}
}
12 changes: 8 additions & 4 deletions src/services/new/collection/utils/get-parents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface GetCollectionParentIds extends Ctx {
}

export const getParents = async ({ctx, trx, collectionIds}: GetCollectionsParentIds) => {
const {tenantId, projectId} = ctx.get('info');
const {tenantId, projectId, onlyMirrored} = ctx.get('info');

const targetTrx = getReplica(trx);

Expand All @@ -28,7 +28,7 @@ export const getParents = async ({ctx, trx, collectionIds}: GetCollectionsParent
qb1.select()
.from(CollectionModel.tableName)
.where({
[CollectionModelColumn.TenantId]: tenantId,
...(onlyMirrored ? {} : {[CollectionModelColumn.TenantId]: tenantId}),
[CollectionModelColumn.ProjectId]: projectId,
[CollectionModelColumn.DeletedAt]: null,
})
Expand All @@ -37,8 +37,12 @@ export const getParents = async ({ctx, trx, collectionIds}: GetCollectionsParent
qb2.select(`${CollectionModel.tableName}.*`)
.from(CollectionModel.tableName)
.where({
[`${CollectionModel.tableName}.${CollectionModelColumn.TenantId}`]:
tenantId,
...(onlyMirrored
? {}
: {
[`${CollectionModel.tableName}.${CollectionModelColumn.TenantId}`]:
tenantId,
}),
[`${CollectionModel.tableName}.${CollectionModelColumn.ProjectId}`]:
projectId,
[`${CollectionModel.tableName}.${CollectionModelColumn.DeletedAt}`]:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {WorkbookModel} from '../../../../db/models/new/workbook';

export const formatRestoreWorkbook = (workbookModel: WorkbookModel) => {
return {
workbookId: workbookModel.workbookId,
};
};
1 change: 1 addition & 0 deletions src/services/new/workbook/formatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './format-workbook-model';
export * from './format-workbook-model-with-operation';
export * from './format-workbook-models-list';
export * from './format-set-workbook-is-template';
export * from './format-restore-workbook';
6 changes: 3 additions & 3 deletions src/services/new/workbook/get-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const getWorkbook = async <T extends WorkbookInstance = WorkbookInstance>
validateArgs(args);
}

const {tenantId, projectId, isPrivateRoute} = ctx.get('info');
const {tenantId, projectId, isPrivateRoute, onlyMirrored} = ctx.get('info');

const {accessServiceEnabled} = ctx.config;

Expand All @@ -55,7 +55,7 @@ export const getWorkbook = async <T extends WorkbookInstance = WorkbookInstance>
.where({
[WorkbookModelColumn.DeletedAt]: null,
[WorkbookModelColumn.WorkbookId]: workbookId,
...(isPrivateRoute
...(isPrivateRoute || onlyMirrored
? {}
: {
[WorkbookModelColumn.TenantId]: tenantId,
Expand All @@ -78,7 +78,7 @@ export const getWorkbook = async <T extends WorkbookInstance = WorkbookInstance>
model,
});

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

if (workbook.model.collectionId !== null) {
Expand Down
1 change: 1 addition & 0 deletions src/services/new/workbook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './set-workbook-is-template';
export * from './copy-workbook';
export * from './copy-workbook-template';
export * from './get-all-workbooks';
export * from './restore-workbook';
Loading

0 comments on commit 7a66a35

Please sign in to comment.