Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
collettemathieu committed Sep 20, 2024
1 parent 1f3a4b1 commit be3a086
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 38 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
type PDSPBPChangeTitlePathwayPersistencePort,
type PDSPBPPathwayPresenterPort,
PDSPBP_CHANGE_TITLE_PATHWAY_PERSISTENCE_PORT,
PDSPBP_PATHWAY_PRESENTER_PORT,
} from '@bewoak/pathway-design-server-pathway-business';

import { Inject } from '@nestjs/common';
// biome-ignore lint/style/useImportType: <explanation>
import { CommandHandler, EventPublisher, type ICommandHandler } from '@nestjs/cqrs';

// biome-ignore lint/style/useImportType: <explanation>
import { PDSPACUChangeTitlePathwayUseCase } from '../usecase/change-title-pathway.usecase';
import { PDSPAChangeTitlePathwayCommand } from './change-title-pathway.command';

@CommandHandler(PDSPAChangeTitlePathwayCommand)
export class PDSPAChangeTitlePathwayCommandHandler implements ICommandHandler<PDSPAChangeTitlePathwayCommand> {
constructor(
private readonly pDSPACUChangeTitlePathwayUseCase: PDSPACUChangeTitlePathwayUseCase,
@Inject(PDSPBP_CHANGE_TITLE_PATHWAY_PERSISTENCE_PORT)
private readonly pDSPBPChangeTitlePathwayPersistencePort: PDSPBPChangeTitlePathwayPersistencePort,
@Inject(PDSPBP_PATHWAY_PRESENTER_PORT)
private readonly pDSPBPPathwayPresenterPort: PDSPBPPathwayPresenterPort,
private readonly eventPublisher: EventPublisher
) {}

execute(pDSPAChangeTitlePathwayCommand: PDSPAChangeTitlePathwayCommand) {
return this.pDSPACUChangeTitlePathwayUseCase.execute(
this.pDSPBPChangeTitlePathwayPersistencePort,
this.pDSPBPPathwayPresenterPort,
this.eventPublisher,
{
pathwayId: pDSPAChangeTitlePathwayCommand.pathwayId,
title: pDSPAChangeTitlePathwayCommand.title,
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class PDSPAChangeTitlePathwayCommand {
constructor(
public readonly pathwayId: string,
public readonly title: string
) {}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { PDSPBPPathwayPresenters } from '@bewoak/pathway-design-server-pathway-business';
import { Injectable } from '@nestjs/common';
// biome-ignore lint/style/useImportType: <explanation>
import { CommandBus } from '@nestjs/cqrs';
import type { PDSPAChangeTitlePathwayCommand } from '../command/change-title-pathway.command';

@Injectable()
export class PDSPAChangeTitlePathwayService {
constructor(private readonly commandBus: CommandBus) {}

execute(pDSPAChangeTitlePathwayCommand: PDSPAChangeTitlePathwayCommand) {
return this.commandBus.execute<PDSPAChangeTitlePathwayCommand, PDSPBPPathwayPresenters>(pDSPAChangeTitlePathwayCommand);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Feature: Application - Change the title of a pathway
And It should call the presenter to return the new title of the pathway
And It should emit an event indicating that the title of the pathway has been changed
And I should receive the new title of the pathway
| title
| My New Pathway
| title |
| My New Pathway |
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ import { strict as assert } from 'node:assert';

import {
type PDSPBEPathwayEntity,
type PDSPBPChangeTitlePathwayPersistencePort,
type PDSPBPPathwayPresenterPort,
type PDSPBPPathwayPresenters,
pDSPBFPathwayFactory,
} from '@bewoak/pathway-design-server-pathway-business';
import type { DataTable } from '@cucumber/cucumber';
import type { EventPublisher } from '@nestjs/cqrs';
import { before, binding, given, then, when } from 'cucumber-tsflow';
import sinon from 'sinon';
import { PDSPBUChangeTitlePathwayUseCase } from '../usecase/change-title-pathway.usecase';
import { PDSPACUChangeTitlePathwayUseCase } from '../usecase/change-title-pathway.usecase';

class FakeChangeTitlePathwayPersistence implements PDSPBPInitializePathwayPersistencePort {
save(pDSPBEPathwayEntity: PDSPBEPathwayEntity) {
return Promise.resolve(pDSPBEPathwayEntity);
class FakeChangeTitlePathwayPersistence implements PDSPBPChangeTitlePathwayPersistencePort {
changeTitle(pDSPBEPathwayEntity: PDSPBEPathwayEntity, title: string) {
const pathwayWithTitleChanged = pDSPBFPathwayFactory({
description: pDSPBEPathwayEntity.description,
id: pDSPBEPathwayEntity.id,
researchField: pDSPBEPathwayEntity.researchField,
title,
});
return Promise.resolve(pathwayWithTitleChanged);
}
}

Expand Down Expand Up @@ -42,7 +50,7 @@ class FakeEventPublisher {

@binding()
export default class ControllerSteps {
private PDSPBUchangeTitlePathwayUseCase = new PDSPBUChangeTitlePathwayUseCase();
private pDSPACUchangeTitlePathwayUseCase = new PDSPACUChangeTitlePathwayUseCase();
private pDSPBEPathwayEntity: PDSPBEPathwayEntity | undefined;
private fakeEventPublisher = new FakeEventPublisher();
private fakeChangeTitlePathwayPersistence = new FakeChangeTitlePathwayPersistence();
Expand All @@ -53,7 +61,7 @@ export default class ControllerSteps {

@before()
public before() {
this.persistenceSpy = sinon.spy(this.fakeChangeTitlePathwayPersistence, 'save');
this.persistenceSpy = sinon.spy(this.fakeChangeTitlePathwayPersistence, 'changeTitle');
this.presenterSpy = sinon.spy(this.fakePathwayPresenter, 'present');
}

Expand All @@ -78,25 +86,15 @@ export default class ControllerSteps {
throw new Error('Pathway is not initialized');
}

this.result = await this.PDSPBUchangeTitlePathwayUseCase.execute({
pathway: this.pDSPBEPathwayEntity,
title,
});
}

@then('I should receive the new title of the pathway')
public thenIShouldReceiveTheNewTitleOfPathway(dataTable: DataTable) {
if (this.pDSPBEPathwayEntity === undefined) {
throw new Error('Pathway is not initialized');
}

const firstRow = dataTable.hashes()[0] as {
title: string;
};

assert.strictEqual(this.result?.title, firstRow.title);
assert.strictEqual(this.result?.description, this.pDSPBEPathwayEntity.description);
assert.strictEqual(this.result?.researchField, this.pDSPBEPathwayEntity.researchField);
this.result = await this.pDSPACUchangeTitlePathwayUseCase.execute(
this.fakeChangeTitlePathwayPersistence,
this.fakePathwayPresenter,
this.fakeEventPublisher as EventPublisher,
{
pathway: this.pDSPBEPathwayEntity,
title,
}
);
}

@then('It should call the persistence layer to modify the title of the pathway')
Expand All @@ -113,4 +111,19 @@ export default class ControllerSteps {
public thenItShouldEmitAnEventIndicatingThatTheTitlePathwayHasBeenChanged() {
assert.ok(FakeEventPublisher.isEventPublished);
}

@then('I should receive the new title of the pathway')
public thenIShouldReceiveTheNewTitleOfPathway(dataTable: DataTable) {
if (this.pDSPBEPathwayEntity === undefined) {
throw new Error('Pathway is not initialized');
}

const firstRow = dataTable.hashes()[0] as {
title: string;
};

assert.strictEqual(this.result?.title, firstRow.title);
assert.strictEqual(this.result?.description, this.pDSPBEPathwayEntity.description);
assert.strictEqual(this.result?.researchField, this.pDSPBEPathwayEntity.researchField);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { type PDSPBEPathwayEntity, PDSPBVOTitleValueObjects } from '@bewoak/pathway-design-server-pathway-business';
import type {
PDSPBEPathwayEntity,
PDSPBPChangeTitlePathwayPersistencePort,
PDSPBPPathwayPresenterPort,
} from '@bewoak/pathway-design-server-pathway-business';
import type { EventPublisher } from '@nestjs/cqrs';

export class PDSPBUChangeTitlePathwayUseCase {
execute({
pathway,
title,
}: {
pathway: PDSPBEPathwayEntity;
title: string;
}) {
const newTitle = new PDSPBVOTitleValueObjects(title);
pathway.changeTitle(newTitle);
export class PDSPACUChangeTitlePathwayUseCase {
async execute(
pDSPBPChangeTitlePathwayPersistencePort: PDSPBPChangeTitlePathwayPersistencePort,
pDSPBPPathwayPresenterPort: PDSPBPPathwayPresenterPort,
eventPublisher: EventPublisher,
{
pathway,
title,
}: {
pathway: PDSPBEPathwayEntity;
title: string;
}
) {
const pathwayFromPersistence = await pDSPBPChangeTitlePathwayPersistencePort.changeTitle(pathway, title);

eventPublisher.mergeObjectContext(pathway);
pathway.commit();

return pDSPBPPathwayPresenterPort.present(pathwayFromPersistence);
}
}

0 comments on commit be3a086

Please sign in to comment.