Skip to content

Commit

Permalink
Add favorite events (#2572)
Browse files Browse the repository at this point in the history
This PR adds 4 new events

1. FAVORITE_FEATURE_ADDED
2.  FAVORITE_FEATURE_REMOVED
3. FAVORITE_PROJECT_ADDED
4. FAVORITE_PROJECT_REMOVED
  • Loading branch information
sjaanus committed Nov 30, 2022
1 parent 88bdef6 commit 65851ba
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 41 deletions.
16 changes: 8 additions & 8 deletions src/lib/routes/admin-api/favorites.ts
Expand Up @@ -88,9 +88,9 @@ export default class FavoritesController extends Controller {
): Promise<void> {
const { featureName } = req.params;
const { user } = req;
await this.favoritesService.addFavoriteFeature({
await this.favoritesService.favoriteFeature({
feature: featureName,
userId: user.id,
user,
});
res.status(200).end();
}
Expand All @@ -101,9 +101,9 @@ export default class FavoritesController extends Controller {
): Promise<void> {
const { featureName } = req.params;
const { user } = req;
await this.favoritesService.removeFavoriteFeature({
await this.favoritesService.unfavoriteFeature({
feature: featureName,
userId: user.id,
user,
});
res.status(200).end();
}
Expand All @@ -114,9 +114,9 @@ export default class FavoritesController extends Controller {
): Promise<void> {
const { projectId } = req.params;
const { user } = req;
await this.favoritesService.addFavoriteProject({
await this.favoritesService.favoriteProject({
project: projectId,
userId: user.id,
user,
});
res.status(200).end();
}
Expand All @@ -127,9 +127,9 @@ export default class FavoritesController extends Controller {
): Promise<void> {
const { projectId } = req.params;
const { user } = req;
await this.favoritesService.removeFavoriteProject({
await this.favoritesService.unfavoriteProject({
project: projectId,
userId: user.id,
user: user,
});
res.status(200).end();
}
Expand Down
126 changes: 96 additions & 30 deletions src/lib/services/favorites-service.ts
@@ -1,15 +1,31 @@
import { IUnleashConfig } from '../types/option';
import { IUnleashStores } from '../types/stores';
import { Logger } from '../logger';
import {
IFavoriteFeatureKey,
IFavoriteFeaturesStore,
} from '../types/stores/favorite-features';
IEventStore,
IFavoriteProjectsStore,
IUnleashStores,
} from '../types/stores';
import { Logger } from '../logger';
import { IFavoriteFeaturesStore } from '../types/stores/favorite-features';
import { IFavoriteFeature, IFavoriteProject } from '../types/favorites';
import {
IFavoriteProjectKey,
IFavoriteProjectsStore,
} from '../types/stores/favorite-projects';
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;
}

export class FavoritesService {
private config: IUnleashConfig;
Expand All @@ -20,51 +36,101 @@ export class FavoritesService {

private favoriteProjectsStore: IFavoriteProjectsStore;

private eventStore: IEventStore;

constructor(
{
favoriteFeaturesStore,
favoriteProjectsStore,
eventStore,
}: Pick<
IUnleashStores,
'favoriteFeaturesStore' | 'favoriteProjectsStore'
'favoriteFeaturesStore' | 'favoriteProjectsStore' | 'eventStore'
>,
config: IUnleashConfig,
) {
this.config = config;
this.logger = config.getLogger('services/favorites-service.ts');
this.favoriteFeaturesStore = favoriteFeaturesStore;
this.favoriteProjectsStore = favoriteProjectsStore;
this.eventStore = eventStore;
}

async addFavoriteFeature(
favorite: IFavoriteFeatureKey,
): Promise<IFavoriteFeature> {
return this.favoriteFeaturesStore.addFavoriteFeature(favorite);
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;
}

async removeFavoriteFeature(favorite: IFavoriteFeatureKey): Promise<void> {
return this.favoriteFeaturesStore.delete(favorite);
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;
}

async addFavoriteProject(
favorite: IFavoriteProjectKey,
): Promise<IFavoriteProject> {
return this.favoriteProjectsStore.addFavoriteProject(favorite);
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;
}

async removeFavoriteProject(favorite: IFavoriteProjectKey): Promise<void> {
return this.favoriteProjectsStore.delete(favorite);
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;
}

async isFavoriteProject(
projectId: string,
userId?: number,
): Promise<boolean> {
if (userId) {
return this.favoriteProjectsStore.exists({
project: projectId,
userId,
});
async isFavoriteProject(favorite: IFavoriteProjectKey): Promise<boolean> {
if (favorite.userId) {
return this.favoriteProjectsStore.exists(favorite);
}
return Promise.resolve(false);
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/services/project-health-service.ts
Expand Up @@ -77,10 +77,10 @@ export default class ProjectHealthService {
projectId,
);

const favorite = await this.favoritesService.isFavoriteProject(
projectId,
const favorite = await this.favoritesService.isFavoriteProject({
project: projectId,
userId,
);
});
return {
name: project.name,
description: project.description,
Expand Down
5 changes: 5 additions & 0 deletions src/lib/types/events.ts
Expand Up @@ -99,6 +99,11 @@ export const API_TOKEN_CREATED = 'api-token-created';
export const API_TOKEN_UPDATED = 'api-token-updated';
export const API_TOKEN_DELETED = 'api-token-deleted';

export const FEATURE_FAVORITED = 'feature-favorited';
export const FEATURE_UNFAVORITED = 'feature-unfavorited';
export const PROJECT_FAVORITED = 'project-favorited';
export const PROJECT_UNFAVORITED = 'project-unfavorited';

export interface IBaseEvent {
type: string;
createdBy: string;
Expand Down

0 comments on commit 65851ba

Please sign in to comment.