1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/services/favorites-service.ts

72 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-11-29 16:06:08 +01:00
import { IUnleashConfig } from '../types/option';
import { IUnleashStores } from '../types/stores';
import { Logger } from '../logger';
import {
IFavoriteFeatureKey,
IFavoriteFeaturesStore,
} from '../types/stores/favorite-features';
import { IFavoriteFeature, IFavoriteProject } from '../types/favorites';
import {
IFavoriteProjectKey,
IFavoriteProjectsStore,
} from '../types/stores/favorite-projects';
2022-11-29 16:06:08 +01:00
export class FavoritesService {
private config: IUnleashConfig;
private logger: Logger;
private favoriteFeaturesStore: IFavoriteFeaturesStore;
private favoriteProjectsStore: IFavoriteProjectsStore;
2022-11-29 16:06:08 +01:00
constructor(
{
favoriteFeaturesStore,
favoriteProjectsStore,
}: Pick<
IUnleashStores,
'favoriteFeaturesStore' | 'favoriteProjectsStore'
>,
2022-11-29 16:06:08 +01:00
config: IUnleashConfig,
) {
this.config = config;
this.logger = config.getLogger('services/favorites-service.ts');
this.favoriteFeaturesStore = favoriteFeaturesStore;
this.favoriteProjectsStore = favoriteProjectsStore;
2022-11-29 16:06:08 +01:00
}
async addFavoriteFeature(
favorite: IFavoriteFeatureKey,
): Promise<IFavoriteFeature> {
return this.favoriteFeaturesStore.addFavoriteFeature(favorite);
}
async removeFavoriteFeature(favorite: IFavoriteFeatureKey): Promise<void> {
return this.favoriteFeaturesStore.delete(favorite);
}
async addFavoriteProject(
favorite: IFavoriteProjectKey,
): Promise<IFavoriteProject> {
return this.favoriteProjectsStore.addFavoriteProject(favorite);
}
async removeFavoriteProject(favorite: IFavoriteProjectKey): Promise<void> {
return this.favoriteProjectsStore.delete(favorite);
}
async isFavoriteProject(
projectId: string,
userId?: number,
): Promise<boolean> {
if (userId) {
return this.favoriteProjectsStore.exists({
project: projectId,
userId,
});
}
return Promise.resolve(false);
}
2022-11-29 16:06:08 +01:00
}