mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-06 00:07:44 +01:00
b91df61994
## About the changes 1. Re-use existing methods in extract-user.ts:70f6a07f2c/src/lib/features/events/event-service.ts (L93-L101)
2. Move event-service and event-store to features/event 3. Add export default in previous paths for backward compatibility:70f6a07f2c/src/lib/services/event-service.ts (L1-L4)
and70f6a07f2c/src/lib/db/event-store.ts (L1-L4)
141 lines
3.9 KiB
TypeScript
141 lines
3.9 KiB
TypeScript
import { IUnleashConfig } from '../types/option';
|
|
import { IFavoriteProjectsStore, IUnleashStores } from '../types/stores';
|
|
import { Logger } from '../logger';
|
|
import { IFavoriteFeaturesStore } from '../types/stores/favorite-features';
|
|
import { IFavoriteFeature, IFavoriteProject } from '../types/favorites';
|
|
import {
|
|
FEATURE_FAVORITED,
|
|
FEATURE_UNFAVORITED,
|
|
PROJECT_FAVORITED,
|
|
PROJECT_UNFAVORITED,
|
|
} from '../types';
|
|
import { IUser } from '../types/user';
|
|
import { extractUsernameFromUser } from '../util';
|
|
import { IFavoriteProjectKey } from '../types/stores/favorite-projects';
|
|
import EventService from '../features/events/event-service';
|
|
|
|
export interface IFavoriteFeatureProps {
|
|
feature: string;
|
|
user: IUser;
|
|
}
|
|
|
|
export interface IFavoriteProjectProps {
|
|
project: string;
|
|
user: IUser;
|
|
}
|
|
|
|
export class FavoritesService {
|
|
private config: IUnleashConfig;
|
|
|
|
private logger: Logger;
|
|
|
|
private favoriteFeaturesStore: IFavoriteFeaturesStore;
|
|
|
|
private favoriteProjectsStore: IFavoriteProjectsStore;
|
|
|
|
private eventService: EventService;
|
|
|
|
constructor(
|
|
{
|
|
favoriteFeaturesStore,
|
|
favoriteProjectsStore,
|
|
}: Pick<
|
|
IUnleashStores,
|
|
'favoriteFeaturesStore' | 'favoriteProjectsStore'
|
|
>,
|
|
config: IUnleashConfig,
|
|
eventService: EventService,
|
|
) {
|
|
this.config = config;
|
|
this.logger = config.getLogger('services/favorites-service.ts');
|
|
this.favoriteFeaturesStore = favoriteFeaturesStore;
|
|
this.favoriteProjectsStore = favoriteProjectsStore;
|
|
this.eventService = eventService;
|
|
}
|
|
|
|
async favoriteFeature({
|
|
feature,
|
|
user,
|
|
}: IFavoriteFeatureProps): Promise<IFavoriteFeature> {
|
|
const data = await this.favoriteFeaturesStore.addFavoriteFeature({
|
|
feature: feature,
|
|
userId: user.id,
|
|
});
|
|
await this.eventService.storeEvent({
|
|
type: FEATURE_FAVORITED,
|
|
featureName: feature,
|
|
createdBy: extractUsernameFromUser(user),
|
|
createdByUserId: user.id,
|
|
data: {
|
|
feature,
|
|
},
|
|
});
|
|
return data;
|
|
}
|
|
|
|
async unfavoriteFeature({
|
|
feature,
|
|
user,
|
|
}: IFavoriteFeatureProps): Promise<void> {
|
|
const data = await this.favoriteFeaturesStore.delete({
|
|
feature: feature,
|
|
userId: user.id,
|
|
});
|
|
await this.eventService.storeEvent({
|
|
type: FEATURE_UNFAVORITED,
|
|
featureName: feature,
|
|
createdBy: extractUsernameFromUser(user),
|
|
createdByUserId: user.id,
|
|
data: {
|
|
feature,
|
|
},
|
|
});
|
|
return data;
|
|
}
|
|
|
|
async favoriteProject({
|
|
project,
|
|
user,
|
|
}: IFavoriteProjectProps): Promise<IFavoriteProject> {
|
|
const data = this.favoriteProjectsStore.addFavoriteProject({
|
|
project,
|
|
userId: user.id,
|
|
});
|
|
await this.eventService.storeEvent({
|
|
type: PROJECT_FAVORITED,
|
|
createdBy: extractUsernameFromUser(user),
|
|
createdByUserId: user.id,
|
|
data: {
|
|
project,
|
|
},
|
|
});
|
|
return data;
|
|
}
|
|
|
|
async unfavoriteProject({
|
|
project,
|
|
user,
|
|
}: IFavoriteProjectProps): Promise<void> {
|
|
const data = this.favoriteProjectsStore.delete({
|
|
project: project,
|
|
userId: user.id,
|
|
});
|
|
await this.eventService.storeEvent({
|
|
type: PROJECT_UNFAVORITED,
|
|
createdBy: extractUsernameFromUser(user),
|
|
createdByUserId: user.id,
|
|
data: {
|
|
project,
|
|
},
|
|
});
|
|
return data;
|
|
}
|
|
|
|
async isFavoriteProject(favorite: IFavoriteProjectKey): Promise<boolean> {
|
|
if (favorite.userId) {
|
|
return this.favoriteProjectsStore.exists(favorite);
|
|
}
|
|
return Promise.resolve(false);
|
|
}
|
|
}
|