Skip to content

Commit

Permalink
feat: add user preference change to event log
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymek committed Nov 5, 2024
1 parent 1897f8a commit c402d39
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/lib/features/user-subscriptions/user-subscriptions-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { IUnleashConfig, IUnleashStores } from '../../types';
import {
UserPreferenceEvent,
type IUnleashConfig,
type IUnleashStores,
} from '../../types';
import type { Logger } from '../../logger';
import type { IAuditUser } from '../../types/user';
import type {
Expand Down Expand Up @@ -35,13 +39,13 @@ export default class UserSubscriptionService {
};

await this.userUnsubscribeStore.delete(entry);
// TODO: log an event
// await this.eventService.storeEvent(
// new UserSubscriptionEvent({
// data: { ...entry, action: 'subscribed' },
// auditUser,
// }),
// );
await this.eventService.storeEvent(
new UserPreferenceEvent({
userId,
data: { subscription, action: 'subscribed' },
auditUser,
}),
);
}

async unsubscribe(
Expand All @@ -55,12 +59,12 @@ export default class UserSubscriptionService {
};

await this.userUnsubscribeStore.insert(entry);
// TODO: log an event
// await this.eventService.storeEvent(
// new UserSubscriptionEvent({
// data: { ...entry, action: 'unsubscribed' },
// auditUser,
// }),
// );
await this.eventService.storeEvent(
new UserPreferenceEvent({
userId,
data: { subscription, action: 'unsubscribed' },
auditUser,
}),
);
}
}
18 changes: 18 additions & 0 deletions src/lib/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ export const ACTIONS_CREATED = 'actions-created' as const;
export const ACTIONS_UPDATED = 'actions-updated' as const;
export const ACTIONS_DELETED = 'actions-deleted' as const;

export const USER_PREFERENCE_UPDATED = 'user-preference-updated' as const;

export const IEventTypes = [
APPLICATION_CREATED,
FEATURE_CREATED,
Expand Down Expand Up @@ -351,6 +353,7 @@ export const IEventTypes = [
ACTIONS_CREATED,
ACTIONS_UPDATED,
ACTIONS_DELETED,
USER_PREFERENCE_UPDATED,
] as const;
export type IEventType = (typeof IEventTypes)[number];

Expand Down Expand Up @@ -2024,3 +2027,18 @@ function mapUserToData(user: IUserEventData): any {
rootRole: user.rootRole,
};
}

export class UserPreferenceEvent extends BaseEvent {
readonly userId;
readonly data: any;

constructor(eventData: {
userId: number;
data: any;
auditUser: IAuditUser;
}) {
super(USER_PREFERENCE_UPDATED, eventData.auditUser);
this.userId = eventData.userId;
this.data = eventData.data;
}
}

0 comments on commit c402d39

Please sign in to comment.