Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add user preference change to event log #8652

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
type IEventStore,
type IUnleashConfig,
type IUnleashStores,
type IUser,
type IUserStore,
TEST_AUDIT_USER,
} from '../../types';
import type { UserSubscriptionsService } from './user-subscriptions-service';
Expand All @@ -13,32 +14,39 @@ import type { IUserSubscriptionsReadModel } from './user-subscriptions-read-mode

let stores: IUnleashStores;
let db: ITestDb;
let userStore: IUserStore;
let userSubscriptionService: UserSubscriptionsService;
let userSubscriptionsReadModel: IUserSubscriptionsReadModel;
let eventsStore: IEventStore;
let config: IUnleashConfig;
let user: IUser;

beforeAll(async () => {
db = await dbInit('user_subscriptions', getLogger);
stores = db.stores;
config = createTestConfig({});

userStore = stores.userStore;
userSubscriptionService = createUserSubscriptionsService(config)(
db.rawDatabase,
);
userSubscriptionsReadModel = db.stores.userSubscriptionsReadModel;
eventsStore = db.stores.eventStore;
});

user = await stores.userStore.insert({
email: '[email protected]',
name: 'Sample Name',
});
beforeEach(async () => {
await userStore.deleteAll();
});

afterAll(async () => {
await db.destroy();
});

test('Subscribe and unsubscribe', async () => {
const user = await userStore.insert({
email: '[email protected]',
name: 'Sample Name',
});

const subscribers = await userSubscriptionsReadModel.getSubscribedUsers(
'productivity-report',
);
Expand All @@ -47,7 +55,7 @@ test('Subscribe and unsubscribe', async () => {
]);

const userSubscriptions =
await userSubscriptionsReadModel.getUserSubscriptions(user.id);
await userSubscriptionService.getUserSubscriptions(user.id);
expect(userSubscriptions).toMatchObject(['productivity-report']);

await userSubscriptionService.unsubscribe(
Expand All @@ -62,6 +70,49 @@ test('Subscribe and unsubscribe', async () => {
expect(noSubscribers).toMatchObject([]);

const noUserSubscriptions =
await userSubscriptionsReadModel.getUserSubscriptions(user.id);
await userSubscriptionService.getUserSubscriptions(user.id);
expect(noUserSubscriptions).toMatchObject([]);
});

test('Event log for subscription actions', async () => {
const user = await userStore.insert({
email: '[email protected]',
name: 'Sample Name',
});

await userSubscriptionService.unsubscribe(
user.id,
'productivity-report',
TEST_AUDIT_USER,
);

const unsubscribeEvent = (await eventsStore.getAll())[0];

expect(unsubscribeEvent).toEqual(
expect.objectContaining({
type: 'user-preference-updated',
data: {
subscription: 'productivity-report',
action: 'unsubscribed',
},
}),
);

await userSubscriptionService.subscribe(
user.id,
'productivity-report',
TEST_AUDIT_USER,
);

const subscribeEvent = (await eventsStore.getAll())[0];

expect(subscribeEvent).toEqual(
expect.objectContaining({
type: 'user-preference-updated',
data: {
subscription: 'productivity-report',
action: 'subscribed',
},
}),
);
});
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 {
UserPreferenceUpdatedEvent,
type IUnleashConfig,
type IUnleashStores,
} from '../../types';
import type { Logger } from '../../logger';
import type { IAuditUser } from '../../types/user';
import type {
Expand Down Expand Up @@ -49,13 +53,13 @@ export class UserSubscriptionsService {
};

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 UserPreferenceUpdatedEvent({
userId,
data: { subscription, action: 'subscribed' },
auditUser,
}),
);
}

async unsubscribe(
Expand All @@ -69,12 +73,12 @@ export class UserSubscriptionsService {
};

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 UserPreferenceUpdatedEvent({
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 UserPreferenceUpdatedEvent extends BaseEvent {
readonly userId;
readonly data: any;

constructor(eventData: {
userId: number;
data: any;
Tymek marked this conversation as resolved.
Show resolved Hide resolved
auditUser: IAuditUser;
}) {
super(USER_PREFERENCE_UPDATED, eventData.auditUser);
this.userId = eventData.userId;
this.data = eventData.data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Production updates last month: {{productionUpdates}}
Go to your Insights to learn more: {{unleashUrl}}/insights

This email was sent to {{userEmail}}. You’ve received this as you are a user of Unleash.
If you wish to unsubscribe, go to you profile settings <a href="{{unleashUrl}}/profile">here</a>.
If you wish to unsubscribe, go to you profile settings on {{unleashUrl}}/profile
Loading