mirror of
https://github.com/Unleash/unleash.git
synced 2025-05-12 01:17:04 +02:00
I've tried to use/add the audit info to all events I could see/find. This makes this PR necessarily huge, because we do store quite a few events. I realise it might not be complete yet, but tests run green, and I think we now have a pattern to follow for other events.
173 lines
5.4 KiB
TypeScript
173 lines
5.4 KiB
TypeScript
import type { Response } from 'express';
|
|
import Controller from '../controller';
|
|
import type { FavoritesService, OpenApiService } from '../../services';
|
|
import type { Logger } from '../../logger';
|
|
import { type IUnleashConfig, type IUnleashServices, NONE } from '../../types';
|
|
import { emptyResponse, getStandardResponses } from '../../openapi';
|
|
import type { 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,
|
|
},
|
|
req.audit,
|
|
);
|
|
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,
|
|
},
|
|
req.audit,
|
|
);
|
|
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,
|
|
},
|
|
req.audit,
|
|
);
|
|
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,
|
|
},
|
|
req.audit,
|
|
);
|
|
res.status(200).end();
|
|
}
|
|
}
|