-
-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: composition root for user subscriptions (#8649)
Co-Authored-By: Tymoteusz Czech <[email protected]>
- Loading branch information
Showing
5 changed files
with
111 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/lib/features/user-subscriptions/createUserSubscriptionsService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { Db, IUnleashConfig } from '../../server-impl'; | ||
import UserSubscriptionService from './user-subscriptions-service'; | ||
import { UserUnsubscribeStore } from './user-unsubscribe-store'; | ||
import { | ||
createEventsService, | ||
createFakeEventsService, | ||
} from '../events/createEventsService'; | ||
import { FakeUserUnsubscribeStore } from './fake-user-unsubscribe-store'; | ||
|
||
export const createUserSubscriptionsService = | ||
(config: IUnleashConfig) => | ||
(db: Db): UserSubscriptionService => { | ||
const userUnsubscribeStore = new UserUnsubscribeStore(db); | ||
const eventService = createEventsService(db, config); | ||
|
||
const userSubscriptionsService = new UserSubscriptionService( | ||
{ userUnsubscribeStore }, | ||
config, | ||
eventService, | ||
); | ||
|
||
return userSubscriptionsService; | ||
}; | ||
|
||
export const createFakeUserSubscriptionsService = (config: IUnleashConfig) => { | ||
const userUnsubscribeStore = new FakeUserUnsubscribeStore(); | ||
const eventService = createFakeEventsService(config); | ||
|
||
const userSubscriptionsService = new UserSubscriptionService( | ||
{ userUnsubscribeStore }, | ||
config, | ||
eventService, | ||
); | ||
|
||
return userSubscriptionsService; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/lib/features/user-subscriptions/user-subscriptions-service.e2e.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
type IUnleashConfig, | ||
type IUnleashStores, | ||
type IUser, | ||
TEST_AUDIT_USER, | ||
} from '../../types'; | ||
import type UserSubscriptionService from './user-subscriptions-service'; | ||
import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init'; | ||
import { createTestConfig } from '../../../test/config/test-config'; | ||
import getLogger from '../../../test/fixtures/no-logger'; | ||
import { createUserSubscriptionsService } from './createUserSubscriptionsService'; | ||
import type { IUserSubscriptionsReadModel } from './user-subscriptions-read-model-type'; | ||
|
||
let stores: IUnleashStores; | ||
let db: ITestDb; | ||
let userSubscriptionService: UserSubscriptionService; | ||
let userSubscriptionsReadModel: IUserSubscriptionsReadModel; | ||
let config: IUnleashConfig; | ||
let user: IUser; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('user_subscriptions', getLogger); | ||
stores = db.stores; | ||
config = createTestConfig({}); | ||
|
||
userSubscriptionService = createUserSubscriptionsService(config)( | ||
db.rawDatabase, | ||
); | ||
userSubscriptionsReadModel = db.stores.userSubscriptionsReadModel; | ||
|
||
user = await stores.userStore.insert({ | ||
email: '[email protected]', | ||
name: 'Sample Name', | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await db.destroy(); | ||
}); | ||
|
||
test('Subscribe and unsubscribe', async () => { | ||
const subscribers = await userSubscriptionsReadModel.getSubscribedUsers( | ||
'productivity-report', | ||
); | ||
expect(subscribers).toMatchObject([ | ||
{ email: '[email protected]', name: 'Sample Name' }, | ||
]); | ||
|
||
const userSubscriptions = | ||
await userSubscriptionsReadModel.getUserSubscriptions(user.id); | ||
expect(userSubscriptions).toMatchObject(['productivity-report']); | ||
|
||
await userSubscriptionService.unsubscribe( | ||
user.id, | ||
'productivity-report', | ||
TEST_AUDIT_USER, | ||
); | ||
|
||
const noSubscribers = await userSubscriptionsReadModel.getSubscribedUsers( | ||
'productivity-report', | ||
); | ||
expect(noSubscribers).toMatchObject([]); | ||
|
||
const noUserSubscriptions = | ||
await userSubscriptionsReadModel.getUserSubscriptions(user.id); | ||
expect(noUserSubscriptions).toMatchObject([]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters