1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-24 01:18:01 +02:00
unleash.unleash/src/lib/routes/admin-api/favorites.ts
andreas-unleash d5ef1dda0a
chore: openapai favorite endpoints (#4189)
<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->
Adds description and summary to `favorite` endpoints
## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #
[1-1098](https://linear.app/unleash/issue/1-1098/openapi-features-favorites)

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
Co-authored-by: Thomas Heartman <thomas@getunleash.ai>
2023-07-10 14:13:00 +03:00

161 lines
5.2 KiB
TypeScript

import { Response } from 'express';
import Controller from '../controller';
import { FavoritesService, OpenApiService } from '../../services';
import { Logger } from '../../logger';
import { IUnleashConfig, IUnleashServices, NONE } from '../../types';
import { emptyResponse, getStandardResponses } from '../../openapi';
import { IAuthRequest } from '../unleash-types';
export default class FavoritesController extends Controller {
private favoritesService: FavoritesService;
private logger: Logger;
private openApiService: OpenApiService;
constructor(
config: IUnleashConfig,
{
favoritesService,
openApiService,
}: Pick<IUnleashServices, 'favoritesService' | 'openApiService'>,
) {
super(config);
this.logger = config.getLogger('/routes/favorites-controller');
this.favoritesService = favoritesService;
this.openApiService = openApiService;
this.route({
method: 'post',
path: '/:projectId/features/:featureName/favorites',
handler: this.addFavoriteFeature,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Features'],
operationId: 'addFavoriteFeature',
summary: 'Add feature to favorites',
description:
'This endpoint marks the feature in the url as favorite',
responses: {
200: emptyResponse,
...getStandardResponses(401, 404),
},
}),
],
});
this.route({
method: 'delete',
path: '/:projectId/features/:featureName/favorites',
handler: this.removeFavoriteFeature,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Features'],
operationId: 'removeFavoriteFeature',
summary: 'Remove feature from favorites',
description:
'This endpoint removes the feature in the url from favorites',
responses: {
200: emptyResponse,
...getStandardResponses(401, 404),
},
}),
],
});
this.route({
method: 'post',
path: '/:projectId/favorites',
handler: this.addFavoriteProject,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Features'],
summary: 'Add project to favorites',
description:
'This endpoint marks the project in the url as favorite',
operationId: 'addFavoriteProject',
responses: {
200: emptyResponse,
...getStandardResponses(401, 404),
},
}),
],
});
this.route({
method: 'delete',
path: '/:projectId/favorites',
handler: this.removeFavoriteProject,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Features'],
summary: 'Remove project from favorites',
description:
'This endpoint removes the project in the url from favorites',
operationId: 'removeFavoriteProject',
responses: {
200: emptyResponse,
...getStandardResponses(401, 404),
},
}),
],
});
}
async addFavoriteFeature(
req: IAuthRequest<{ featureName: string }>,
res: Response,
): Promise<void> {
const { featureName } = req.params;
const { user } = req;
await this.favoritesService.favoriteFeature({
feature: featureName,
user,
});
res.status(200).end();
}
async removeFavoriteFeature(
req: IAuthRequest<{ featureName: string }>,
res: Response,
): Promise<void> {
const { featureName } = req.params;
const { user } = req;
await this.favoritesService.unfavoriteFeature({
feature: featureName,
user,
});
res.status(200).end();
}
async addFavoriteProject(
req: IAuthRequest<{ projectId: string }>,
res: Response,
): Promise<void> {
const { projectId } = req.params;
const { user } = req;
await this.favoritesService.favoriteProject({
project: projectId,
user,
});
res.status(200).end();
}
async removeFavoriteProject(
req: IAuthRequest<{ projectId: string }>,
res: Response,
): Promise<void> {
const { projectId } = req.params;
const { user } = req;
await this.favoritesService.unfavoriteProject({
project: projectId,
user: user,
});
res.status(200).end();
}
}