Skip to content

Commit

Permalink
Handle owner agreement signings on the read side #25
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Haarhoff authored and Lan2u committed Jun 25, 2024
1 parent 338a64a commit b13a21f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/commands/members/sign-owner-agreement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import * as tt from 'io-ts-types';
import * as O from 'fp-ts/Option';
import {Command} from '../command';
import {isSelfOrPrivileged} from '../is-self-or-privileged';
import {constructEvent} from '../../types';
import {pipe} from 'fp-ts/lib/function';

const codec = t.strict({
memberNumber: tt.NumberFromString,
signedAt: tt.DateFromISOString,
});

type SignOwnerAgreement = t.TypeOf<typeof codec>;

const process: Command<SignOwnerAgreement>['process'] = input => O.none;
const process: Command<SignOwnerAgreement>['process'] = input =>
pipe(input.command, constructEvent('OwnerAgreementSigned'), O.some);

const resource: Command<SignOwnerAgreement>['resource'] = input => ({
type: 'OwnerAgreementSigning',
Expand Down
17 changes: 12 additions & 5 deletions src/read-models/members/getPotentialOwners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ type Member = {
email: EmailAddress;
name: O.Option<string>;
pronouns: O.Option<string>;
};

type PotentialOwner = Member & {
agreementSigned: O.Option<Date>;
};

type AreaOwners = {
existing: ReadonlyArray<Member>;
potential: ReadonlyArray<PotentialOwner>;
potential: ReadonlyArray<Member>;
};

type Area = {
Expand All @@ -47,6 +44,7 @@ const pertinentEventTypes: Array<EventName> = [
'MemberDetailsUpdated',
'AreaCreated',
'OwnerAdded',
'OwnerAgreementSigned',
];

const handleEvent = (
Expand All @@ -59,6 +57,7 @@ const handleEvent = (
email: event.email,
name: O.none,
pronouns: O.none,
agreementSigned: O.none,
});
}
if (isEventOfType('MemberDetailsUpdated')(event)) {
Expand All @@ -69,6 +68,15 @@ const handleEvent = (
state.members.set(event.memberNumber, {...current, name, pronouns});
}
}
if (isEventOfType('OwnerAgreementSigned')(event)) {
const current = state.members.get(event.memberNumber);
if (current) {
state.members.set(event.memberNumber, {
...current,
agreementSigned: O.some(event.signedAt),
});
}
}
if (isEventOfType('AreaCreated')(event)) {
state.areas.set(event.id, {id: event.id, owners: new Set()});
}
Expand Down Expand Up @@ -106,7 +114,6 @@ export const getPotentialOwners =
pipe(
Array.from(members.values()),
RA.filter(({number}) => !requestedArea.owners.has(number)),
RA.map(member => ({...member, agreementSigned: O.none})),
O.some
)
)
Expand Down
4 changes: 4 additions & 0 deletions src/types/domain-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ export const DomainEvent = t.union([
name: t.union([t.string, t.undefined]),
pronouns: t.union([t.string, t.undefined]),
}),
eventCodec('OwnerAgreementSigned', {
memberNumber: t.number,
signedAt: tt.DateFromISOString,
}),
]);

export type DomainEvent = t.TypeOf<typeof DomainEvent>;
Expand Down
4 changes: 2 additions & 2 deletions tests/read-models/members/get-potential-owners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ describe('getPotentialOwners', () => {
result = await callQuery(createArea.id);
});

it.failing('includes the date they signed', () => {
it('includes the date they signed', () => {
expect(result.potential[0].agreementSigned).toStrictEqual(
O.some(signAgreement)
O.some(signAgreement.signedAt)
);
});
});
Expand Down

0 comments on commit b13a21f

Please sign in to comment.