This repository was archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
feat(api): [automation | WhenRegistrationCompletedThenRegisterCurrentEditionCourseUser] implement slice #406
Merged
MateuszNaKodach
merged 6 commits into
main
from
issue-329-_Automation_|_WhenRegistrationCompletedThenRegisterCurrentEditionCourseUser_Implement_slice
Nov 4, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
826461a
Initial
KrystianKjjk fcac223
Add automation module
KrystianKjjk 8c57b75
add test to added automation module
KrystianKjjk 72799e2
clean unused variables
KrystianKjjk 622e290
Correct ApplicationCommand class
KrystianKjjk 9a4d831
correct test naming
KrystianKjjk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...ster-current-edition-course-user/user-registration-was-completed.event-handler.service.ts
This file contains hidden or 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,52 @@ | ||
| import { Injectable, OnApplicationBootstrap, OnModuleDestroy } from '@nestjs/common'; | ||
| import { CommandBus } from '@nestjs/cqrs'; | ||
|
|
||
| import { ApplicationEvent } from '@/module/application-command-events'; | ||
| import { | ||
| RegisterCourseUserApplicationCommand, | ||
| RegisterCourseUserCommand, | ||
| } from '@/module/commands/register-course-user'; | ||
| import { UserRegistrationWasCompleted } from '@/module/events/user-registration-was-completed.domain-event'; | ||
| import { env } from '@/shared/env'; | ||
| import { ApplicationCommandFactory } from '@/write/shared/application/application-command.factory'; | ||
| import { EventsSubscription } from '@/write/shared/application/events-subscription/events-subscription'; | ||
| import { EventsSubscriptionsRegistry } from '@/write/shared/application/events-subscription/events-subscriptions-registry'; | ||
|
|
||
| @Injectable() | ||
| export class UserRegistrationWasCompletedEventHandler implements OnApplicationBootstrap, OnModuleDestroy { | ||
| private eventsSubscription: EventsSubscription; | ||
|
|
||
| constructor( | ||
| private readonly commandBus: CommandBus, | ||
| private readonly commandFactory: ApplicationCommandFactory, | ||
| private readonly eventsSubscriptionsFactory: EventsSubscriptionsRegistry, | ||
| ) {} | ||
|
|
||
| async onApplicationBootstrap() { | ||
| this.eventsSubscription = this.eventsSubscriptionsFactory | ||
| .subscription('WhenUserRegistrationWasCompletedThenRegisterCourseUser_Automation_v1') | ||
| .onEvent<UserRegistrationWasCompleted>('UserRegistrationWasCompleted', (event) => | ||
| this.onUserRegistrationWasCompleted(event), | ||
| ) | ||
| .build(); | ||
| await this.eventsSubscription.start(); | ||
| } | ||
|
|
||
| async onModuleDestroy() { | ||
| await this.eventsSubscription.stop(); | ||
| } | ||
|
|
||
| async onUserRegistrationWasCompleted(event: ApplicationEvent<UserRegistrationWasCompleted>) { | ||
| const command = this.commandFactory.applicationCommand((idGenerator) => ({ | ||
| class: RegisterCourseUserApplicationCommand, | ||
| ...RegisterCourseUserCommand({ | ||
| userId: event.data.userId, | ||
| courseUserId: idGenerator.generate(), | ||
| courseId: env.CURRENT_COURSE_ID, | ||
| }), | ||
| metadata: { correlationId: event.metadata.correlationId, causationId: event.id }, | ||
| })); | ||
|
|
||
| await this.commandBus.execute(command); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...hen-registration-completed-then-register-current-edition-course-user-automation.module.ts
This file contains hidden or 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,11 @@ | ||
| import { Module } from '@nestjs/common'; | ||
|
|
||
| import { SharedModule } from '@/write/shared/shared.module'; | ||
|
|
||
| import { UserRegistrationWasCompletedEventHandler } from './user-registration-was-completed.event-handler.service'; | ||
|
|
||
| @Module({ | ||
| imports: [SharedModule], | ||
| providers: [UserRegistrationWasCompletedEventHandler], | ||
| }) | ||
| export class WhenRegistrationCompletedThenRegisterCurrentUserAutomationModule {} |
42 changes: 42 additions & 0 deletions
42
...course-user/when-registration-completed-then-register-current-edition-course-user.spec.ts
This file contains hidden or 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,42 @@ | ||
| import { AsyncReturnType } from 'type-fest'; | ||
|
|
||
| import { RegisterCourseUser, RegisterCourseUserCommand } from '@/module/commands/register-course-user'; | ||
| import { userRegistrationWasCompletedEvent } from '@/module/events/user-registration-was-completed.domain-event'; | ||
| import { EventStreamName } from '@/write/shared/application/event-stream-name.value-object'; | ||
|
|
||
| import { whenUserRegistrationWasCompletedThenRegisterCurrentEditionCourseUserAutomationTestModule } from './when-registration-completed-then-register-current-edition-course-user.test-module'; | ||
|
|
||
| describe('RegisterCourseUser when registrationWasCompleted', () => { | ||
| let moduleUnderTest: AsyncReturnType< | ||
| typeof whenUserRegistrationWasCompletedThenRegisterCurrentEditionCourseUserAutomationTestModule | ||
| >; | ||
|
|
||
| beforeEach(async () => { | ||
| moduleUnderTest = await whenUserRegistrationWasCompletedThenRegisterCurrentEditionCourseUserAutomationTestModule(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await moduleUnderTest.close(); | ||
| }); | ||
|
|
||
| it('publishes registerCourseUser command when userRegistrationWasCompletedEvent occurred', async () => { | ||
| // Given | ||
|
|
||
| const courseId = process.env.CURRENT_COURSE_ID ?? ''; | ||
|
|
||
| const userId = 'ca63d023-4cbd-40ca-9f53-f19dbb19b0ab'; | ||
| const fullName = 'test user'; | ||
| const emailAddress = 'testUser@test.pl'; | ||
| const hashedPassword = '41c2c1fc8f6cdc15.d5ee8246071726582172f83d569287951a0d727c94dfc35e291fe17abec789c2'; | ||
|
|
||
| const event = userRegistrationWasCompletedEvent({ userId, fullName, emailAddress, hashedPassword }); | ||
|
|
||
| // When | ||
| await moduleUnderTest.eventOccurred(EventStreamName.from('UserRegistration', userId), event); | ||
|
|
||
| // Then | ||
| await moduleUnderTest.expectCommandExecutedLastly<RegisterCourseUser>({ | ||
| ...RegisterCourseUserCommand({ courseId, userId, courseUserId: moduleUnderTest.lastGeneratedId() }), | ||
| }); | ||
| }); | ||
| }); |
6 changes: 6 additions & 0 deletions
6
...user/when-registration-completed-then-register-current-edition-course-user.test-module.ts
This file contains hidden or 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,6 @@ | ||
| import { WhenRegistrationCompletedThenRegisterCurrentUserAutomationModule } from '@/automation/when-registration-completed-then-register-current-edition-course-user/when-registration-completed-then-register-current-edition-course-user-automation.module'; | ||
| import { initAutomationTestModule } from '@/shared/test-utils'; | ||
|
|
||
| export async function whenUserRegistrationWasCompletedThenRegisterCurrentEditionCourseUserAutomationTestModule() { | ||
| return initAutomationTestModule([WhenRegistrationCompletedThenRegisterCurrentUserAutomationModule]); | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.