mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-31 00:16:47 +01: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:
parent
88bdef62b3
commit
65851ba51c
@ -88,9 +88,9 @@ export default class FavoritesController extends Controller {
|
||||
): Promise<void> {
|
||||
const { featureName } = req.params;
|
||||
const { user } = req;
|
||||
await this.favoritesService.addFavoriteFeature({
|
||||
await this.favoritesService.favoriteFeature({
|
||||
feature: featureName,
|
||||
userId: user.id,
|
||||
user,
|
||||
});
|
||||
res.status(200).end();
|
||||
}
|
||||
@ -101,9 +101,9 @@ export default class FavoritesController extends Controller {
|
||||
): Promise<void> {
|
||||
const { featureName } = req.params;
|
||||
const { user } = req;
|
||||
await this.favoritesService.removeFavoriteFeature({
|
||||
await this.favoritesService.unfavoriteFeature({
|
||||
feature: featureName,
|
||||
userId: user.id,
|
||||
user,
|
||||
});
|
||||
res.status(200).end();
|
||||
}
|
||||
@ -114,9 +114,9 @@ export default class FavoritesController extends Controller {
|
||||
): Promise<void> {
|
||||
const { projectId } = req.params;
|
||||
const { user } = req;
|
||||
await this.favoritesService.addFavoriteProject({
|
||||
await this.favoritesService.favoriteProject({
|
||||
project: projectId,
|
||||
userId: user.id,
|
||||
user,
|
||||
});
|
||||
res.status(200).end();
|
||||
}
|
||||
@ -127,9 +127,9 @@ export default class FavoritesController extends Controller {
|
||||
): Promise<void> {
|
||||
const { projectId } = req.params;
|
||||
const { user } = req;
|
||||
await this.favoritesService.removeFavoriteProject({
|
||||
await this.favoritesService.unfavoriteProject({
|
||||
project: projectId,
|
||||
userId: user.id,
|
||||
user: user,
|
||||
});
|
||||
res.status(200).end();
|
||||
}
|
||||
|
@ -1,15 +1,31 @@
|
||||
import { IUnleashConfig } from '../types/option';
|
||||
import { IUnleashStores } from '../types/stores';
|
||||
import { Logger } from '../logger';
|
||||
import {
|
||||
IFavoriteFeatureKey,
|
||||
IFavoriteFeaturesStore,
|
||||
} from '../types/stores/favorite-features';
|
||||
IEventStore,
|
||||
IFavoriteProjectsStore,
|
||||
IUnleashStores,
|
||||
} from '../types/stores';
|
||||
import { Logger } from '../logger';
|
||||
import { IFavoriteFeaturesStore } from '../types/stores/favorite-features';
|
||||
import { IFavoriteFeature, IFavoriteProject } from '../types/favorites';
|
||||
import {
|
||||
IFavoriteProjectKey,
|
||||
IFavoriteProjectsStore,
|
||||
} from '../types/stores/favorite-projects';
|
||||
FEATURE_FAVORITED,
|
||||
FEATURE_UNFAVORITED,
|
||||
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 {
|
||||
private config: IUnleashConfig;
|
||||
@ -20,13 +36,16 @@ export class FavoritesService {
|
||||
|
||||
private favoriteProjectsStore: IFavoriteProjectsStore;
|
||||
|
||||
private eventStore: IEventStore;
|
||||
|
||||
constructor(
|
||||
{
|
||||
favoriteFeaturesStore,
|
||||
favoriteProjectsStore,
|
||||
eventStore,
|
||||
}: Pick<
|
||||
IUnleashStores,
|
||||
'favoriteFeaturesStore' | 'favoriteProjectsStore'
|
||||
'favoriteFeaturesStore' | 'favoriteProjectsStore' | 'eventStore'
|
||||
>,
|
||||
config: IUnleashConfig,
|
||||
) {
|
||||
@ -34,37 +53,84 @@ export class FavoritesService {
|
||||
this.logger = config.getLogger('services/favorites-service.ts');
|
||||
this.favoriteFeaturesStore = favoriteFeaturesStore;
|
||||
this.favoriteProjectsStore = favoriteProjectsStore;
|
||||
this.eventStore = eventStore;
|
||||
}
|
||||
|
||||
async addFavoriteFeature(
|
||||
favorite: IFavoriteFeatureKey,
|
||||
): Promise<IFavoriteFeature> {
|
||||
return this.favoriteFeaturesStore.addFavoriteFeature(favorite);
|
||||
async favoriteFeature({
|
||||
feature,
|
||||
user,
|
||||
}: 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> {
|
||||
return this.favoriteFeaturesStore.delete(favorite);
|
||||
async unfavoriteFeature({
|
||||
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(
|
||||
favorite: IFavoriteProjectKey,
|
||||
): Promise<IFavoriteProject> {
|
||||
return this.favoriteProjectsStore.addFavoriteProject(favorite);
|
||||
async favoriteProject({
|
||||
project,
|
||||
user,
|
||||
}: 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> {
|
||||
return this.favoriteProjectsStore.delete(favorite);
|
||||
async unfavoriteProject({
|
||||
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(
|
||||
projectId: string,
|
||||
userId?: number,
|
||||
): Promise<boolean> {
|
||||
if (userId) {
|
||||
return this.favoriteProjectsStore.exists({
|
||||
project: projectId,
|
||||
userId,
|
||||
});
|
||||
async isFavoriteProject(favorite: IFavoriteProjectKey): Promise<boolean> {
|
||||
if (favorite.userId) {
|
||||
return this.favoriteProjectsStore.exists(favorite);
|
||||
}
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
@ -77,10 +77,10 @@ export default class ProjectHealthService {
|
||||
projectId,
|
||||
);
|
||||
|
||||
const favorite = await this.favoritesService.isFavoriteProject(
|
||||
projectId,
|
||||
const favorite = await this.favoritesService.isFavoriteProject({
|
||||
project: projectId,
|
||||
userId,
|
||||
);
|
||||
});
|
||||
return {
|
||||
name: project.name,
|
||||
description: project.description,
|
||||
|
@ -99,6 +99,11 @@ export const API_TOKEN_CREATED = 'api-token-created';
|
||||
export const API_TOKEN_UPDATED = 'api-token-updated';
|
||||
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 {
|
||||
type: string;
|
||||
createdBy: string;
|
||||
|
Loading…
Reference in New Issue
Block a user