Skip to content

Commit e716d43

Browse files
committed
Update Command process to support TaskEither
1 parent 67b023d commit e716d43

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

src/commands/command.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {Type} from 'io-ts';
22
import {DomainEvent} from '../types/domain-event';
33
import * as O from 'fp-ts/Option';
4+
import * as TE from 'fp-ts/TaskEither';
45
import {Actor} from '../types/actor';
56
import {Resource} from '../types/resource';
7+
import { FailureWithStatus } from '../types/failure-with-status';
8+
import { Dependencies } from '../dependencies';
69

710
export type WithActor<T> = T & {actor: Actor};
811

@@ -11,7 +14,8 @@ export type Command<T> = {
1114
process: (input: {
1215
command: WithActor<T>;
1316
events: ReadonlyArray<DomainEvent>;
14-
}) => O.Option<DomainEvent>;
17+
deps: Dependencies;
18+
}) => TE.TaskEither<FailureWithStatus, O.Option<DomainEvent>>;
1519
decode: Type<T, T, unknown>['decode'];
1620
isAuthorized: (input: {
1721
actor: Actor;

src/commands/exclusion-events/exclude-event-form.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ const renderForm = (viewModel: ViewModel) =>
2121
pipe(
2222
html`
2323
<h1>Exclude an event</h1>
24-
<p>Are you sure you want to exclude (delete) this event? This form should only be used
24+
<p>Are you sure you want to exclude (delete) this event? This form should only be used in limited circumstances.
2525
</p>
2626
${renderEvent(viewModel.event.value)}
2727
<form action="/events/exclude-event" method="post">
2828
<input type="hidden" name="event_id" value="${viewModel.event.value.event_id}"/>
29+
<label for="reason">Whats the reason for deleting this event?</label>
30+
<input type="text" name="reason" id="reason" value="" minlength="3" required/>
2931
<button type="submit">Confirm</button>
3032
</form>
3133
`,

src/commands/exclusion-events/exclude-event.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
import * as t from 'io-ts';
22
import * as tt from 'io-ts-types';
33
import * as O from 'fp-ts/Option';
4+
import * as TE from 'fp-ts/TaskEither';
45
import {Command} from '../command';
56
import {isAdminOrSuperUser} from '../is-admin-or-super-user';
7+
import { pipe } from 'fp-ts/lib/function';
8+
import { FailureWithStatus, failureWithStatus } from '../../types/failure-with-status';
9+
import { StatusCodes } from 'http-status-codes';
610

711
const codec = t.strict({
8-
event_id: tt.UUID
12+
event_id: tt.UUID,
13+
reason: t.string,
914
});
1015

1116
export type ExcludeEvent = t.TypeOf<typeof codec>;
1217

13-
const process: Command<ExcludeEvent>['process'] = () => O.none;
18+
const process: Command<ExcludeEvent>['process'] = input => pipe(
19+
input.command.actor.tag === 'user'
20+
? TE.right<FailureWithStatus, number>(input.command.actor.user.memberNumber)
21+
: TE.left(failureWithStatus('Unknown user', StatusCodes.BAD_REQUEST)()),
22+
TE.chain(memberNumber => input.deps.excludeEvent(
23+
input.command.event_id,
24+
memberNumber,
25+
input.command.reason,
26+
)),
27+
TE.map(() => O.none)
28+
);
1429

1530
const resource = () => ({
1631
type: 'ExcludeEvent',

src/init-dependencies/event-store/exclude-event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const excludeEvent =
4040
]
4141
),
4242
failureWithStatus(
43-
'Failed to execlude event',
43+
'Failed to exclude event',
4444
StatusCodes.INTERNAL_SERVER_ERROR
4545
)
4646
);

0 commit comments

Comments
 (0)