Skip to content

Commit 38297d2

Browse files
committed
Pass dependencies to command
1 parent e716d43 commit 38297d2

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import {pipe} from 'fp-ts/lib/function';
22
import * as TE from 'fp-ts/TaskEither';
3+
import * as O from 'fp-ts/Option';
34
import {Command} from '.';
4-
import {Dependencies} from '../dependencies';
55
import {Actor} from '../types/actor';
66
import {
77
FailureWithStatus,
8-
failureWithStatus,
98
} from '../types/failure-with-status';
109
import {StatusCodes} from 'http-status-codes';
11-
12-
type Deps = Pick<Dependencies, 'commitEvent' | 'getResourceEvents'>;
10+
import { CommandDependencies } from './command';
1311

1412
export const applyToResource =
15-
<T>(deps: Deps, command: Command<T>) =>
13+
<T>(deps: CommandDependencies, command: Command<T>) =>
1614
(
1715
input: T,
1816
actor: Actor
@@ -26,13 +24,9 @@ export const applyToResource =
2624
resource,
2725
deps.getResourceEvents,
2826
TE.bind('event', ({events}) =>
29-
pipe(
30-
command.process({command: inputAndActor, events}),
31-
TE.fromOption(() =>
32-
failureWithStatus('no new event raised', StatusCodes.OK)()
33-
)
34-
)
27+
command.process({command: inputAndActor, events, deps}),
3528
),
36-
TE.chain(({event, version}) => deps.commitEvent(resource, version)(event))
29+
// If no event is raised then we still treat it as success to the user as they don't care about the difference -> if we want to raise an error then use TE.Left.
30+
TE.chain(({event, version}) => O.isSome(event) ? deps.commitEvent(resource, version)(event.value) : TE.right({status: StatusCodes.CREATED, message: 'Success'}))
3731
);
3832
};

src/commands/area/set-mailing-list.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import {constructEvent} from '../../types';
1+
import {constructEvent, DomainEvent} from '../../types';
22
import * as t from 'io-ts';
33
import * as tt from 'io-ts-types';
44
import * as O from 'fp-ts/Option';
55
import * as E from 'fp-ts/Either';
6+
import * as TE from 'fp-ts/TaskEither';
67
import {pipe} from 'fp-ts/lib/function';
78
import {Command} from '../command';
89
import {isAdminOrSuperUser} from '../is-admin-or-super-user';
@@ -27,6 +28,7 @@ const process: Command<SetMailingList>['process'] = input => {
2728
email,
2829
})),
2930
O.map(constructEvent('AreaEmailUpdated')),
31+
TE.right,
3032
);
3133
};
3234

src/commands/command.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import {Resource} from '../types/resource';
77
import { FailureWithStatus } from '../types/failure-with-status';
88
import { Dependencies } from '../dependencies';
99

10+
export type CommandDependencies = Pick<Dependencies, 'commitEvent' | 'getResourceEvents' | 'excludeEvent'>;
11+
1012
export type WithActor<T> = T & {actor: Actor};
1113

1214
export type Command<T> = {
1315
resource: (command: T) => Resource;
1416
process: (input: {
1517
command: WithActor<T>;
1618
events: ReadonlyArray<DomainEvent>;
17-
deps: Dependencies;
19+
deps: CommandDependencies;
1820
}) => TE.TaskEither<FailureWithStatus, O.Option<DomainEvent>>;
1921
decode: Type<T, T, unknown>['decode'];
2022
isAuthorized: (input: {

tests/read-models/test-framework.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {lastSync} from '../../src/sync-worker/db/last_sync';
2727
import {getSheetData, getSheetDataByMemberNumber} from '../../src/sync-worker/db/get_sheet_data';
2828
import {TrainingSummaryDeps} from '../../src/sync-worker/training-summary/training-summary-deps';
2929
import {NonEmptyString} from 'io-ts-types/lib/NonEmptyString';
30+
import { excludeEvent } from '../../src/init-dependencies/event-store/exclude-event';
3031

3132
const TROUBLE_TICKET_SHEET_ID = 'trouble_ticket_sheet_id';
3233

@@ -87,6 +88,8 @@ export const initTestFramework = async (): Promise<TestFramework> => {
8788
const frameworkGetAllEventsByType = <EN extends EventName>(eventType: EN) =>
8889
pipe(getAllEventsByType(eventDB)(eventType), T.map(getRightOrFail))();
8990

91+
const frameworkExcludeEvent = excludeEvent(eventDB);
92+
9093
const frameworkify =
9194
<T>(command: Command<T>) =>
9295
async (commandPayload: T & {actor?: Actor}) => {
@@ -95,6 +98,7 @@ export const initTestFramework = async (): Promise<TestFramework> => {
9598
{
9699
commitEvent: frameworkCommitEvent,
97100
getResourceEvents: getResourceEvents(eventDB),
101+
excludeEvent: frameworkExcludeEvent
98102
},
99103
command
100104
)(commandPayload, commandPayload.actor ?? arbitraryActor())

0 commit comments

Comments
 (0)