unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
77 lines • 2.62 kB
JavaScript
import { FeatureFavoritedEvent, FeatureUnfavoritedEvent, ProjectFavoritedEvent, ProjectUnfavoritedEvent, } from '../types/index.js';
import { NotFoundError } from '../error/index.js';
export class FavoritesService {
constructor({ favoriteFeaturesStore, favoriteProjectsStore, }, _config, eventService) {
this.favoriteFeaturesStore = favoriteFeaturesStore;
this.favoriteProjectsStore = favoriteProjectsStore;
this.eventService = eventService;
}
async favoriteFeature({ feature, user }, auditUser) {
const data = await this.favoriteFeaturesStore.addFavoriteFeature({
feature: feature,
userId: user.id,
});
if (data === undefined) {
throw new NotFoundError(`Feature with name ${feature} did not exist`);
}
await this.eventService.storeEvent(new FeatureFavoritedEvent({
featureName: feature,
data: {
feature,
},
auditUser,
}));
return data;
}
async unfavoriteFeature({ feature, user }, auditUser) {
const data = await this.favoriteFeaturesStore.delete({
feature: feature,
userId: user.id,
});
await this.eventService.storeEvent(new FeatureUnfavoritedEvent({
featureName: feature,
data: {
feature,
},
auditUser,
}));
return data;
}
async favoriteProject({ project, user }, auditUser) {
const data = await this.favoriteProjectsStore.addFavoriteProject({
project,
userId: user.id,
});
if (data === undefined) {
throw new NotFoundError(`Project with id ${project} was not found`);
}
await this.eventService.storeEvent(new ProjectFavoritedEvent({
data: {
project,
},
project,
auditUser,
}));
return data;
}
async unfavoriteProject({ project, user }, auditUser) {
const _data = await this.favoriteProjectsStore.delete({
project: project,
userId: user.id,
});
await this.eventService.storeEvent(new ProjectUnfavoritedEvent({
data: {
project,
},
project,
auditUser,
}));
}
async isFavoriteProject(favorite) {
if (favorite.userId) {
return this.favoriteProjectsStore.exists(favorite);
}
return Promise.resolve(false);
}
}
//# sourceMappingURL=favorites-service.js.map