Skip to content

Commit 4cfbf85

Browse files
committed
Pass dependencies to Command.process
1 parent 322a483 commit 4cfbf85

File tree

5 files changed

+66
-23
lines changed

5 files changed

+66
-23
lines changed

src/authentication/verify-email/landing-page.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export const landing =
6363
// by the user clicking the verification link.
6464
const resultantEvent = await verifyEmail.process({
6565
events: resourceEvents.right.events,
66+
deps,
6667
command: {
6768
emailAddress: decoded.right.emailAddress,
6869
memberNumber: decoded.right.memberNumber,

src/commands/apply-command-to-resource.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import {
99
} from '../types/failure-with-status';
1010
import {StatusCodes} from 'http-status-codes';
1111

12-
type Deps = Pick<Dependencies, 'commitEvent' | 'getResourceEvents'>;
13-
1412
export const applyToResource =
15-
<T>(deps: Deps, command: Command<T>) =>
13+
<T>(deps: Dependencies, command: Command<T>) =>
1614
(
1715
input: T,
1816
actor: Actor
@@ -25,7 +23,9 @@ export const applyToResource =
2523
return pipe(
2624
resource,
2725
deps.getResourceEvents,
28-
TE.bind('event', ({events}) => command.process({command: inputAndActor, events})),
26+
TE.bind('event', ({events}) =>
27+
command.process({command: inputAndActor, events, deps})
28+
),
2929
TE.chain(({event, version}) => O.isSome(event) ? deps.commitEvent(resource, version)(event.value) : TE.right({
3030
status: StatusCodes.CREATED as StatusCodes.CREATED,
3131
message: 'Success'

src/commands/command.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@ import * as TE from 'fp-ts/TaskEither';
55
import {Actor} from '../types/actor';
66
import {Resource} from '../types/resource';
77
import {FailureWithStatus} from '../types/failure-with-status';
8+
import {Dependencies} from '../dependencies';
89

910
export type WithActor<T> = T & {actor: Actor};
11+
export type CommandProcessInput<T> = {
12+
command: WithActor<T>;
13+
events: ReadonlyArray<DomainEvent>;
14+
deps?: Dependencies;
15+
};
16+
1017
type CommandResult = TE.TaskEither<
1118
FailureWithStatus,
1219
O.Option<DomainEvent>
1320
>;
1421

1522
export type Command<T> = {
1623
resource: (command: T) => Resource;
17-
process: (input: {
18-
command: WithActor<T>;
19-
events: ReadonlyArray<DomainEvent>;
20-
}) => CommandResult;
24+
process: (input: CommandProcessInput<T>) => CommandResult;
2125
decode: Type<T, T, unknown>['decode'];
2226
isAuthorized: (input: {
2327
actor: Actor;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as O from 'fp-ts/Option';
2+
import * as TE from 'fp-ts/TaskEither';
3+
import * as t from 'io-ts';
4+
import {Command} from '../../src/commands';
5+
import {applyToResource} from '../../src/commands/apply-command-to-resource';
6+
import {arbitraryActor, getTaskEitherRightOrFail} from '../helpers';
7+
import {happyPathAdapters} from '../init-dependencies/happy-path-adapters.helper';
8+
9+
describe('apply-command-to-resource', () => {
10+
it('passes dependencies to command.process', async () => {
11+
const actor = arbitraryActor();
12+
const process = jest.fn(() => TE.right(O.none));
13+
const command: Command<{id: string}> = {
14+
resource: ({id}) => ({type: 'Area', id}),
15+
process,
16+
decode: t.strict({id: t.string}).decode,
17+
isAuthorized: () => true,
18+
};
19+
20+
await getTaskEitherRightOrFail(
21+
applyToResource(happyPathAdapters, command)({id: 'area-1'}, actor)
22+
);
23+
24+
expect(process).toHaveBeenCalledWith({
25+
command: {id: 'area-1', actor},
26+
events: [],
27+
deps: happyPathAdapters,
28+
});
29+
});
30+
});

tests/read-models/test-framework.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ export type TestFramework = {
5050
) => Promise<ReadonlyArray<EventOfType<T>>>;
5151
commands: ToFrameworkCommands<typeof commands>;
5252
sharedReadModel: Dependencies['sharedReadModel'];
53-
depsForApplyToResource: {
54-
commitEvent: Dependencies['commitEvent'];
55-
getResourceEvents: Dependencies['getResourceEvents'];
56-
};
53+
depsForApplyToResource: Dependencies;
5754
eventStoreDb: libsqlClient.Client;
5855
googleDB: libsqlClient.Client;
5956
getTroubleTicketData: Dependencies['getTroubleTicketData'];
@@ -86,18 +83,32 @@ export const initTestFramework = async (): Promise<TestFramework> => {
8683
pipe(getAllEvents(eventDB)(), T.map(getRightOrFail))();
8784
const frameworkGetAllEventsByType = <EN extends EventName>(eventType: EN) =>
8885
pipe(getAllEventsByType(eventDB)(eventType), T.map(getRightOrFail))();
86+
const depsForApplyToResource: Dependencies = {
87+
commitEvent: frameworkCommitEvent,
88+
getAllEvents: getAllEvents(eventDB),
89+
getAllEventsByType: getAllEventsByType(eventDB),
90+
getResourceEvents: getResourceEvents(eventDB),
91+
sharedReadModel,
92+
logger,
93+
rateLimitSendingOfEmails: TE.right,
94+
sendEmail: () => TE.right('success'),
95+
lastQuizSync: lastSync(googleDB),
96+
getSheetData: getSheetData(googleDB),
97+
getSheetDataByMemberNumber: getSheetDataByMemberNumber(googleDB),
98+
getTroubleTicketData: getTroubleTicketData(
99+
googleDB,
100+
O.some(TROUBLE_TICKET_SHEET_ID)
101+
),
102+
};
89103

90104
const frameworkify =
91105
<T>(command: Command<T>) =>
92106
async (commandPayload: T & {actor?: Actor}) => {
93107
await pipe(
94-
applyToResource(
95-
{
96-
commitEvent: frameworkCommitEvent,
97-
getResourceEvents: getResourceEvents(eventDB),
98-
},
99-
command
100-
)(commandPayload, commandPayload.actor ?? arbitraryActor())
108+
applyToResource(depsForApplyToResource, command)(
109+
commandPayload,
110+
commandPayload.actor ?? arbitraryActor()
111+
)
101112
)();
102113
};
103114

@@ -113,10 +124,7 @@ export const initTestFramework = async (): Promise<TestFramework> => {
113124
eventStoreDb: eventDB,
114125
googleDB,
115126
sharedReadModel,
116-
depsForApplyToResource: {
117-
commitEvent: frameworkCommitEvent,
118-
getResourceEvents: getResourceEvents(eventDB),
119-
},
127+
depsForApplyToResource,
120128
close: () => {
121129
eventDB.close();
122130
googleDB.close();

0 commit comments

Comments
 (0)