diff --git a/src/const/common.ts b/src/const/common.ts index 20a3510c..14ea7491 100644 --- a/src/const/common.ts +++ b/src/const/common.ts @@ -84,6 +84,7 @@ export const RETURN_NAVIGATION_COLUMNS = [ ]; export const RETURN_FAVORITES_COLUMNS = [ + 'favorites.alias', 'entries.entryId', 'entries.scope', 'entries.type', diff --git a/src/controllers/favorites.ts b/src/controllers/favorites.ts index 365ef511..d6fb346a 100644 --- a/src/controllers/favorites.ts +++ b/src/controllers/favorites.ts @@ -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); }, }; diff --git a/src/db/models/favorite/index.ts b/src/db/models/favorite/index.ts index a1dff773..7c1c29be 100644 --- a/src/db/models/favorite/index.ts +++ b/src/db/models/favorite/index.ts @@ -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'; @@ -362,6 +367,35 @@ 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 result = await Favorite.query(this.primary) + .where({entryId}) + .update({alias: name}) + .returning('*') + .timeout(Model.DEFAULT_QUERY_TIMEOUT); + + ctx.log('RENAME_FAVORITE_SUCCESS'); + + return result; + } } export default Favorite; diff --git a/src/db/models/favorite/scheme.ts b/src/db/models/favorite/scheme.ts index 5a6b505f..34ff3fef 100644 --- a/src/db/models/favorite/scheme.ts +++ b/src/db/models/favorite/scheme.ts @@ -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({ @@ -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, + }, + }, +}); diff --git a/src/routes.ts b/src/routes.ts index f8a16b2c..2ec5e3d9 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -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)) { diff --git a/src/services/favorite.service.ts b/src/services/favorite.service.ts index e8c8dadd..3f10eec7 100644 --- a/src/services/favorite.service.ts +++ b/src/services/favorite.service.ts @@ -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, + }); + } } diff --git a/src/types/models/favorite.ts b/src/types/models/favorite.ts index 52867f5f..ec12f8dc 100644 --- a/src/types/models/favorite.ts +++ b/src/types/models/favorite.ts @@ -24,3 +24,9 @@ export interface DeleteFavoriteConfig extends BasicRequestParams { entryId?: any; ctx: CTX; } +export interface RenameFavoriteConfig extends BasicRequestParams { + entryId: string; + name: string; + ctx: CTX; + dlContext?: string; +} diff --git a/src/types/models/tables-columns.ts b/src/types/models/tables-columns.ts index 8f003943..1dc57e9e 100644 --- a/src/types/models/tables-columns.ts +++ b/src/types/models/tables-columns.ts @@ -87,5 +87,6 @@ export interface FavoriteColumns { entryId: string; tenantId: string; login: string; + alias: string | null; createdAt: string; } diff --git a/src/types/services.types.ts b/src/types/services.types.ts index a56be0f9..5d02ca57 100644 --- a/src/types/services.types.ts +++ b/src/types/services.types.ts @@ -98,6 +98,10 @@ export interface GetFavorite extends StdServiceParams { export interface AddFavorite extends StdServiceParams { entryId: string; } +export interface RenameFavorite extends StdServiceParams { + entryId: string; + name: string; +} export interface DeleteFavorite extends StdServiceParams { entryId: string; }