Skip to content

Commit

Permalink
Keep track of when people where recorded as owners #59
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Haarhoff committed Sep 5, 2024
1 parent 3a48c71 commit bcc2fc2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/read-models/shared-state/get-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const getMember =
.select({
id: ownersTable.areaId,
name: areasTable.name,
ownershipRecordedAt: ownersTable.ownershipRecordedAt,
})
.from(ownersTable)
.leftJoin(areasTable, eq(areasTable.id, ownersTable.areaId))
Expand Down
1 change: 1 addition & 0 deletions src/read-models/shared-state/return-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type TrainedOn = {
type OwnerOf = {
id: string;
name: string;
ownershipRecordedAt: Date;
};

export type Member = {
Expand Down
6 changes: 5 additions & 1 deletion src/read-models/shared-state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ export const ownersTable = sqliteTable('owners', {
areaId: text('areaId')
.notNull()
.references(() => areasTable.id),
ownershipRecordedAt: blob('ownershipRecordedAt', {mode: 'json'})
.notNull()
.$type<Date>(),
});

const createOwnersTable = sql`
CREATE TABLE IF NOT EXISTS owners (
memberNumber INTEGER,
areaId TEXT
areaId TEXT,
ownershipRecordedAt BLOB
)
`;

Expand Down
6 changes: 5 additions & 1 deletion src/read-models/shared-state/update-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ export const updateState =
break;
case 'OwnerAdded':
db.insert(ownersTable)
.values({memberNumber: event.memberNumber, areaId: event.areaId})
.values({
memberNumber: event.memberNumber,
areaId: event.areaId,
ownershipRecordedAt: event.recordedAt,
})
.run();
break;

Expand Down
10 changes: 9 additions & 1 deletion tests/read-models/shared-state/get-member.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,11 @@ describe('get-via-shared-read-model', () => {
name: faker.company.buzzNoun() as NonEmptyString,
id: faker.string.uuid() as UUID,
};
const recordedAt = faker.date.future();
recordedAt.setMilliseconds(0);
beforeEach(async () => {
await framework.commands.area.create(createArea);
advanceTo(recordedAt);
await framework.commands.area.addOwner({
memberNumber: memberNumber,
areaId: createArea.id,
Expand All @@ -236,7 +239,12 @@ describe('get-via-shared-read-model', () => {
expect(result.ownerOf[0].name).toStrictEqual(createArea.name);
});

it.todo('returns when they became an owner');
it('returns when they became an owner', () => {
const result = runQuery();
expect(result.ownerOf[0].ownershipRecordedAt).toStrictEqual(
recordedAt.toISOString()
);
});
});
});
});

0 comments on commit bcc2fc2

Please sign in to comment.