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): create slice complete task (#211) #305
Merged
kacper-cyra
merged 19 commits into
main
from
issue-211-_Write_LearningMaterialsTasks_Create_slice_CompleteTask_TaskWasCompleted
Sep 8, 2021
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2f69917
command
kacper-cyra d915cfd
Merge branch 'main' into issue-211-_Write_LearningMaterialsTasks_Crea…
kacper-cyra 49f3fee
complete task function
kacper-cyra f712302
controller
kacper-cyra 90c14bb
command handler
kacper-cyra 22ccd6c
module
kacper-cyra a7f1641
module test
kacper-cyra 7fac1ef
function name
kacper-cyra 0e12fc0
Merge branch 'main' into issue-211-_Write_LearningMaterialsTasks_Crea…
kacper-cyra fd24678
module test
kacper-cyra fd43980
lint:fix
kacper-cyra c6e60ff
Schema change & prettier
kacper-cyra 01593fd
Merge branch 'main' into issue-211-_Write_LearningMaterialsTasks_Crea…
kacper-cyra 7a5c15f
fix
kacper-cyra fdaeef1
move from find to reduce
kacper-cyra ca83677
code simplifying
kacper-cyra 7cd7be4
adjusting to comments
kacper-cyra 81757ac
courseProgressStateUpdated change
kacper-cyra 46bd835
Merge branch 'main' into issue-211-_Write_LearningMaterialsTasks_Crea…
kacper-cyra 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
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
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
5 changes: 5 additions & 0 deletions
5
packages/api/src/module/shared/commands/complete-task.application-command.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,5 @@ | ||
| import { AbstractApplicationCommand } from '@/module/application-command-events'; | ||
|
|
||
| import { CompleteTask } from './complete-task.domain-command'; | ||
|
|
||
| export class CompleteTaskApplicationCommand extends AbstractApplicationCommand<CompleteTask> {} |
4 changes: 4 additions & 0 deletions
4
packages/api/src/module/shared/commands/complete-task.domain-command.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,4 @@ | ||
| export type CompleteTask = { | ||
| type: 'CompleteTask'; | ||
| data: { learningMaterialsId: string; taskId: string }; | ||
| }; |
30 changes: 30 additions & 0 deletions
30
...pi/src/module/write/learning-materials-tasks/application/complete-task.command-handler.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,30 @@ | ||
| import { Inject } from '@nestjs/common'; | ||
| import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; | ||
|
|
||
| import { CompleteTaskApplicationCommand } from '@/commands/complete-task.application-command'; | ||
| import { TaskWasCompleted } from '@/module/events/task-was-completed.domain-event'; | ||
| import { APPLICATION_SERVICE, ApplicationService } from '@/write/shared/application/application-service'; | ||
| import { EventStreamName } from '@/write/shared/application/event-stream-name.value-object'; | ||
|
|
||
| import { completeTask } from '../domain/complete-task'; | ||
|
|
||
| @CommandHandler(CompleteTaskApplicationCommand) | ||
| export class CompleteTaskCommandHandler implements ICommandHandler<CompleteTaskApplicationCommand> { | ||
| constructor( | ||
| @Inject(APPLICATION_SERVICE) | ||
| private readonly applicationService: ApplicationService, | ||
| ) {} | ||
|
|
||
| async execute(command: CompleteTaskApplicationCommand): Promise<void> { | ||
| const eventStream = EventStreamName.from('LearningMaterialsTasks', command.data.learningMaterialsId); | ||
|
|
||
| await this.applicationService.execute<TaskWasCompleted>( | ||
| eventStream, | ||
| { | ||
| causationId: command.id, | ||
| correlationId: command.metadata.correlationId, | ||
| }, | ||
| (pastEvents) => completeTask(pastEvents, command), | ||
| ); | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
packages/api/src/module/write/learning-materials-tasks/domain/complete-task.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,64 @@ | ||
| import { CompleteTask } from '@/module/commands/complete-task.domain-command'; | ||
| import { TaskWasCompleted } from '@/module/events/task-was-completed.domain-event'; | ||
|
|
||
| import { completeTask } from './complete-task'; | ||
|
|
||
| describe('complete task', () => { | ||
| const command: CompleteTask = { | ||
| type: 'CompleteTask', | ||
| data: { learningMaterialsId: 'sbAPITNMsl2wW6j2cg1H2A', taskId: 'L9EXtwmBNBXgo_qh0uzbq' }, | ||
| }; | ||
|
|
||
| it('should return task was completed', () => { | ||
| // Given | ||
| const pastEvents: TaskWasCompleted[] = []; | ||
|
|
||
| // When | ||
| const events = completeTask(pastEvents, command); | ||
|
|
||
| // Then | ||
| expect(events).toStrictEqual([ | ||
| { | ||
| type: 'TaskWasCompleted', | ||
| data: { learningMaterialsId: command.data.learningMaterialsId, taskId: command.data.taskId }, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should return task was completed if other tasks are completed', () => { | ||
| // Given | ||
| const pastEvents: TaskWasCompleted[] = [ | ||
| { | ||
| type: 'TaskWasCompleted', | ||
| data: { learningMaterialsId: command.data.learningMaterialsId, taskId: 'BIt23CR3dLKkHn_a2IM4V' }, | ||
| }, | ||
| ]; | ||
|
|
||
| // When | ||
| const events = completeTask(pastEvents, command); | ||
|
|
||
| // Then | ||
| expect(events).toStrictEqual([ | ||
| { | ||
| type: 'TaskWasCompleted', | ||
| data: { learningMaterialsId: command.data.learningMaterialsId, taskId: command.data.taskId }, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should throw exception if task was already completed', () => { | ||
| // Given | ||
| const pastEvents: TaskWasCompleted[] = [ | ||
| { | ||
| type: 'TaskWasCompleted', | ||
| data: { learningMaterialsId: command.data.learningMaterialsId, taskId: command.data.taskId }, | ||
| }, | ||
| ]; | ||
|
|
||
| // When | ||
| const events = () => completeTask(pastEvents, command); | ||
|
|
||
| // Then | ||
| expect(events).toThrowError('Task was already completed'); | ||
| }); | ||
| }); | ||
37 changes: 37 additions & 0 deletions
37
packages/api/src/module/write/learning-materials-tasks/domain/complete-task.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,37 @@ | ||
| import { TaskWasCompleted } from '@/events/task-was-completed.domain-event'; | ||
| import { CompleteTask } from '@/module/commands/complete-task.domain-command'; | ||
|
|
||
| export function completeTask( | ||
| pastEvents: TaskWasCompleted[], | ||
| { data: { learningMaterialsId, taskId } }: CompleteTask, | ||
| ): TaskWasCompleted[] { | ||
| const state = pastEvents | ||
| .filter(({ data }) => data.taskId === taskId) | ||
| .reduce<{ completed: boolean }>( | ||
| (acc, event) => { | ||
| switch (event.type) { | ||
| case 'TaskWasCompleted': { | ||
| return { completed: true }; | ||
| } | ||
| default: { | ||
| return acc; | ||
| } | ||
| } | ||
| }, | ||
| { completed: false }, | ||
| ); | ||
|
|
||
| if (state.completed) { | ||
| throw new Error('Task was already completed'); | ||
| } | ||
|
|
||
| const newEvent: TaskWasCompleted = { | ||
| type: 'TaskWasCompleted', | ||
| data: { | ||
| taskId, | ||
| learningMaterialsId, | ||
| }, | ||
| }; | ||
|
|
||
| return [newEvent]; | ||
| } |
5 changes: 5 additions & 0 deletions
5
...ges/api/src/module/write/learning-materials-tasks/learning-materials-tasks.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,5 @@ | ||
| import { initWriteTestModule } from '@/shared/test-utils'; | ||
|
|
||
| export async function learningMaterialsTasksTestModule() { | ||
| return initWriteTestModule(); | ||
| } |
Oops, something went wrong.
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.