Skip to content

Commit

Permalink
Added renameFavorite method which updates alias column in favorites t…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
Stanislav Kiselev committed Dec 6, 2023
1 parent 4669550 commit e449df2
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 2 deletions.
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
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);
},
};
36 changes: 35 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,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;
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,
},
},
});
5 changes: 5 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
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,
});
}
}
6 changes: 6 additions & 0 deletions src/types/models/favorite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/types/models/tables-columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ export interface FavoriteColumns {
entryId: string;
tenantId: string;
login: string;
alias: string | null;
createdAt: string;
}
4 changes: 4 additions & 0 deletions src/types/services.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit e449df2

Please sign in to comment.