Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/app/eventStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,18 @@ class PostgresEventStore implements EventStore {
aggregateId: Id<T>,
): Promise<Json[]> {
const sql = `
SELECT id, event_id, aggregate_id, causation_id, correlation_id,
aggregate_version, json_payload, json_metadata, recorded_on, event_name
SELECT
event_id,
aggregate_id,
aggregate_version,
correlation_id,
causation_id,
recorded_on,
payload,
event_name
FROM ${this.eventStoreTable}
WHERE aggregate_id = $1
ORDER BY aggregate_version ASC`;

try {
const result = await this.transaction.query(sql, [aggregateId.value]);
return result.rows;
Expand All @@ -184,9 +190,15 @@ class PostgresEventStore implements EventStore {
private async insert<E extends Event<any>>(edata: EventData<E>) {
const sql = `
INSERT INTO ${this.eventStoreTable} (
event_id, aggregate_id, causation_id, correlation_id,
aggregate_version, json_payload, json_metadata, recorded_on, event_name
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`;
event_id,
aggregate_id,
aggregate_version,
correlation_id,
causation_id,
recorded_on,
payload,
event_name
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`;

const serialized = this.schemas.encode(edata);

Expand All @@ -203,7 +215,6 @@ class PostgresEventStore implements EventStore {
serialized.aggregate_version,
// @ts-ignore
serialized.payload,
'{}',
// @ts-ignore
serialized.recorded_on,
edata.event.values.type,
Expand Down Expand Up @@ -256,12 +267,11 @@ async function initialize({
event_id TEXT NOT NULL UNIQUE,
aggregate_id TEXT NOT NULL,
aggregate_version BIGINT NOT NULL,
causation_id TEXT NOT NULL,
correlation_id TEXT NOT NULL,
causation_id TEXT NOT NULL,
recorded_on TIMESTAMPTZ NOT NULL,
payload TEXT NOT NULL,
event_name TEXT NOT NULL,
json_payload TEXT NOT NULL,
json_metadata TEXT NOT NULL,
PRIMARY KEY (id)
);`,
);
Expand Down
38 changes: 20 additions & 18 deletions src/domain/cookingClub/membership/command/submitApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Response, json } from '@/lib/router';
import { Id } from '@/lib/eventSourcing/event';
import { ApplicationSubmitted } from '@/domain/cookingClub/membership/events/membership/applicationSubmitted';
import { Membership } from '@/domain/cookingClub/membership/aggregate/membership';
import { internalServerError } from '@/app/responses';

type Command = d.Infer<typeof decoder>;
const decoder = d.object({
Expand All @@ -20,25 +21,26 @@ const decoder = d.object({
const handler: CommandHandler<Command> = ({
command,
store,
}): Future<Response, Response> => {
store.emit({
aggregate: ApplicationSubmitted.aggregate,
event: new ApplicationSubmitted({
type: ApplicationSubmitted.type,
aggregateId: Id.random<Membership>(),
firstName: command.firstName,
lastName: command.lastName,
favouriteCousine: command.favouriteCousine,
yearsOfProfessionalExperience: command.yearsOfProfessionalExperience,
numberOfCookingBooksRead: command.numberOfCookingBooksRead,
}),
});

return Future.resolve(
json({
content: { message: 'success' },
}): Future<Response, Response> =>
Future.attemptP(() =>
store.emit({
aggregate: ApplicationSubmitted.aggregate,
event: new ApplicationSubmitted({
type: ApplicationSubmitted.type,
aggregateId: Id.random<Membership>(),
firstName: command.firstName,
lastName: command.lastName,
favouriteCousine: command.favouriteCousine,
yearsOfProfessionalExperience: command.yearsOfProfessionalExperience,
numberOfCookingBooksRead: command.numberOfCookingBooksRead,
}),
}),
).bimap(
(_) => internalServerError,
(_) =>
json({
content: { message: 'success' },
}),
);
};

const controller: CommandController<Command> = { decoder, handler };
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ async function main() {
});
}

await main();
main();