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';
|
2022-11-30 12:41:53 +01:00
|
|
|
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;
|
|
|
|
|
2022-11-30 12:41:53 +01:00
|
|
|
private favoriteProjectsStore: IFavoriteProjectsStore;
|
|
|
|
|
2022-11-29 16:06:08 +01:00
|
|
|
constructor(
|
|
|
|
{
|
|
|
|
favoriteFeaturesStore,
|
2022-11-30 12:41:53 +01:00
|
|
|
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;
|
2022-11-30 12:41:53 +01:00
|
|
|
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);
|
|
|
|
}
|
2022-11-30 12:41:53 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|