2022-11-29 16:06:08 +01:00
|
|
|
import { IUnleashConfig } from '../types/option';
|
|
|
|
import {
|
2022-11-30 13:26:17 +01:00
|
|
|
IEventStore,
|
|
|
|
IFavoriteProjectsStore,
|
|
|
|
IUnleashStores,
|
|
|
|
} from '../types/stores';
|
|
|
|
import { Logger } from '../logger';
|
|
|
|
import { IFavoriteFeaturesStore } from '../types/stores/favorite-features';
|
2022-11-30 12:41:53 +01:00
|
|
|
import { IFavoriteFeature, IFavoriteProject } from '../types/favorites';
|
|
|
|
import {
|
2022-11-30 13:26:17 +01:00
|
|
|
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;
|
|
|
|
}
|
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-30 13:26:17 +01:00
|
|
|
private eventStore: IEventStore;
|
|
|
|
|
2022-11-29 16:06:08 +01:00
|
|
|
constructor(
|
|
|
|
{
|
|
|
|
favoriteFeaturesStore,
|
2022-11-30 12:41:53 +01:00
|
|
|
favoriteProjectsStore,
|
2022-11-30 13:26:17 +01:00
|
|
|
eventStore,
|
2022-11-30 12:41:53 +01:00
|
|
|
}: Pick<
|
|
|
|
IUnleashStores,
|
2022-11-30 13:26:17 +01:00
|
|
|
'favoriteFeaturesStore' | 'favoriteProjectsStore' | 'eventStore'
|
2022-11-30 12:41:53 +01:00
|
|
|
>,
|
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-30 13:26:17 +01:00
|
|
|
this.eventStore = eventStore;
|
2022-11-29 16:06:08 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 13:26:17 +01:00
|
|
|
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;
|
2022-11-29 16:06:08 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 13:26:17 +01:00
|
|
|
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;
|
2022-11-29 16:06:08 +01:00
|
|
|
}
|
2022-11-30 12:41:53 +01:00
|
|
|
|
2022-11-30 13:26:17 +01:00
|
|
|
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;
|
2022-11-30 12:41:53 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 13:26:17 +01:00
|
|
|
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;
|
2022-11-30 12:41:53 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 13:26:17 +01:00
|
|
|
async isFavoriteProject(favorite: IFavoriteProjectKey): Promise<boolean> {
|
|
|
|
if (favorite.userId) {
|
|
|
|
return this.favoriteProjectsStore.exists(favorite);
|
2022-11-30 12:41:53 +01:00
|
|
|
}
|
|
|
|
return Promise.resolve(false);
|
|
|
|
}
|
2022-11-29 16:06:08 +01:00
|
|
|
}
|