1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-08 01:15:49 +02:00

Add favorite events (#2572)

This PR adds 4 new events

1. FAVORITE_FEATURE_ADDED
2.  FAVORITE_FEATURE_REMOVED
3. FAVORITE_PROJECT_ADDED
4. FAVORITE_PROJECT_REMOVED
This commit is contained in:
sjaanus 2022-11-30 13:26:17 +01:00 committed by GitHub
parent 88bdef62b3
commit 65851ba51c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 112 additions and 41 deletions

View File

@ -88,9 +88,9 @@ export default class FavoritesController extends Controller {
): Promise<void> { ): Promise<void> {
const { featureName } = req.params; const { featureName } = req.params;
const { user } = req; const { user } = req;
await this.favoritesService.addFavoriteFeature({ await this.favoritesService.favoriteFeature({
feature: featureName, feature: featureName,
userId: user.id, user,
}); });
res.status(200).end(); res.status(200).end();
} }
@ -101,9 +101,9 @@ export default class FavoritesController extends Controller {
): Promise<void> { ): Promise<void> {
const { featureName } = req.params; const { featureName } = req.params;
const { user } = req; const { user } = req;
await this.favoritesService.removeFavoriteFeature({ await this.favoritesService.unfavoriteFeature({
feature: featureName, feature: featureName,
userId: user.id, user,
}); });
res.status(200).end(); res.status(200).end();
} }
@ -114,9 +114,9 @@ export default class FavoritesController extends Controller {
): Promise<void> { ): Promise<void> {
const { projectId } = req.params; const { projectId } = req.params;
const { user } = req; const { user } = req;
await this.favoritesService.addFavoriteProject({ await this.favoritesService.favoriteProject({
project: projectId, project: projectId,
userId: user.id, user,
}); });
res.status(200).end(); res.status(200).end();
} }
@ -127,9 +127,9 @@ export default class FavoritesController extends Controller {
): Promise<void> { ): Promise<void> {
const { projectId } = req.params; const { projectId } = req.params;
const { user } = req; const { user } = req;
await this.favoritesService.removeFavoriteProject({ await this.favoritesService.unfavoriteProject({
project: projectId, project: projectId,
userId: user.id, user: user,
}); });
res.status(200).end(); res.status(200).end();
} }

View File

@ -1,15 +1,31 @@
import { IUnleashConfig } from '../types/option'; import { IUnleashConfig } from '../types/option';
import { IUnleashStores } from '../types/stores';
import { Logger } from '../logger';
import { import {
IFavoriteFeatureKey, IEventStore,
IFavoriteFeaturesStore, IFavoriteProjectsStore,
} from '../types/stores/favorite-features'; IUnleashStores,
} from '../types/stores';
import { Logger } from '../logger';
import { IFavoriteFeaturesStore } from '../types/stores/favorite-features';
import { IFavoriteFeature, IFavoriteProject } from '../types/favorites'; import { IFavoriteFeature, IFavoriteProject } from '../types/favorites';
import { import {
IFavoriteProjectKey, FEATURE_FAVORITED,
IFavoriteProjectsStore, FEATURE_UNFAVORITED,
} from '../types/stores/favorite-projects'; PROJECT_FAVORITED,
PROJECT_UNFAVORITED,
} from '../types';
import User from '../types/user';
import { extractUsernameFromUser } from '../util';
import { IFavoriteProjectKey } from '../types/stores/favorite-projects';
export interface IFavoriteFeatureProps {
feature: string;
user: User;
}
export interface IFavoriteProjectProps {
project: string;
user: User;
}
export class FavoritesService { export class FavoritesService {
private config: IUnleashConfig; private config: IUnleashConfig;
@ -20,13 +36,16 @@ export class FavoritesService {
private favoriteProjectsStore: IFavoriteProjectsStore; private favoriteProjectsStore: IFavoriteProjectsStore;
private eventStore: IEventStore;
constructor( constructor(
{ {
favoriteFeaturesStore, favoriteFeaturesStore,
favoriteProjectsStore, favoriteProjectsStore,
eventStore,
}: Pick< }: Pick<
IUnleashStores, IUnleashStores,
'favoriteFeaturesStore' | 'favoriteProjectsStore' 'favoriteFeaturesStore' | 'favoriteProjectsStore' | 'eventStore'
>, >,
config: IUnleashConfig, config: IUnleashConfig,
) { ) {
@ -34,37 +53,84 @@ export class FavoritesService {
this.logger = config.getLogger('services/favorites-service.ts'); this.logger = config.getLogger('services/favorites-service.ts');
this.favoriteFeaturesStore = favoriteFeaturesStore; this.favoriteFeaturesStore = favoriteFeaturesStore;
this.favoriteProjectsStore = favoriteProjectsStore; this.favoriteProjectsStore = favoriteProjectsStore;
this.eventStore = eventStore;
} }
async addFavoriteFeature( async favoriteFeature({
favorite: IFavoriteFeatureKey, feature,
): Promise<IFavoriteFeature> { user,
return this.favoriteFeaturesStore.addFavoriteFeature(favorite); }: IFavoriteFeatureProps): Promise<IFavoriteFeature> {
const data = await this.favoriteFeaturesStore.addFavoriteFeature({
feature: feature,
userId: user.id,
});
await this.eventStore.store({
type: FEATURE_FAVORITED,
createdBy: extractUsernameFromUser(user),
data: {
feature,
},
});
return data;
} }
async removeFavoriteFeature(favorite: IFavoriteFeatureKey): Promise<void> { async unfavoriteFeature({
return this.favoriteFeaturesStore.delete(favorite); feature,
user,
}: IFavoriteFeatureProps): Promise<void> {
const data = await this.favoriteFeaturesStore.delete({
feature: feature,
userId: user.id,
});
await this.eventStore.store({
type: FEATURE_UNFAVORITED,
createdBy: extractUsernameFromUser(user),
data: {
feature,
},
});
return data;
} }
async addFavoriteProject( async favoriteProject({
favorite: IFavoriteProjectKey, project,
): Promise<IFavoriteProject> { user,
return this.favoriteProjectsStore.addFavoriteProject(favorite); }: IFavoriteProjectProps): Promise<IFavoriteProject> {
const data = this.favoriteProjectsStore.addFavoriteProject({
project,
userId: user.id,
});
await this.eventStore.store({
type: PROJECT_FAVORITED,
createdBy: extractUsernameFromUser(user),
data: {
project,
},
});
return data;
} }
async removeFavoriteProject(favorite: IFavoriteProjectKey): Promise<void> { async unfavoriteProject({
return this.favoriteProjectsStore.delete(favorite); project,
user,
}: IFavoriteProjectProps): Promise<void> {
const data = this.favoriteProjectsStore.delete({
project: project,
userId: user.id,
});
await this.eventStore.store({
type: PROJECT_UNFAVORITED,
createdBy: extractUsernameFromUser(user),
data: {
project,
},
});
return data;
} }
async isFavoriteProject( async isFavoriteProject(favorite: IFavoriteProjectKey): Promise<boolean> {
projectId: string, if (favorite.userId) {
userId?: number, return this.favoriteProjectsStore.exists(favorite);
): Promise<boolean> {
if (userId) {
return this.favoriteProjectsStore.exists({
project: projectId,
userId,
});
} }
return Promise.resolve(false); return Promise.resolve(false);
} }

View File

@ -77,10 +77,10 @@ export default class ProjectHealthService {
projectId, projectId,
); );
const favorite = await this.favoritesService.isFavoriteProject( const favorite = await this.favoritesService.isFavoriteProject({
projectId, project: projectId,
userId, userId,
); });
return { return {
name: project.name, name: project.name,
description: project.description, description: project.description,

View File

@ -99,6 +99,11 @@ export const API_TOKEN_CREATED = 'api-token-created';
export const API_TOKEN_UPDATED = 'api-token-updated'; export const API_TOKEN_UPDATED = 'api-token-updated';
export const API_TOKEN_DELETED = 'api-token-deleted'; export const API_TOKEN_DELETED = 'api-token-deleted';
export const FEATURE_FAVORITED = 'feature-favorited';
export const FEATURE_UNFAVORITED = 'feature-unfavorited';
export const PROJECT_FAVORITED = 'project-favorited';
export const PROJECT_UNFAVORITED = 'project-unfavorited';
export interface IBaseEvent { export interface IBaseEvent {
type: string; type: string;
createdBy: string; createdBy: string;