From c402d390937bef87569481b7cc97eeb9562c5e89 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Tue, 5 Nov 2024 12:02:02 +0100 Subject: [PATCH] feat: add user preference change to event log --- .../user-subscriptions-service.ts | 34 +++++++++++-------- src/lib/types/events.ts | 18 ++++++++++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/lib/features/user-subscriptions/user-subscriptions-service.ts b/src/lib/features/user-subscriptions/user-subscriptions-service.ts index 3e197da2984e..4fbacb166904 100644 --- a/src/lib/features/user-subscriptions/user-subscriptions-service.ts +++ b/src/lib/features/user-subscriptions/user-subscriptions-service.ts @@ -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 { @@ -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( @@ -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, + }), + ); } } diff --git a/src/lib/types/events.ts b/src/lib/types/events.ts index d71c28df5f91..ba0d66419bf7 100644 --- a/src/lib/types/events.ts +++ b/src/lib/types/events.ts @@ -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, @@ -351,6 +353,7 @@ export const IEventTypes = [ ACTIONS_CREATED, ACTIONS_UPDATED, ACTIONS_DELETED, + USER_PREFERENCE_UPDATED, ] as const; export type IEventType = (typeof IEventTypes)[number]; @@ -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; + } +}