From 6d3739fdeab37f4d15bd6ab4a42deeab80fd0d6c Mon Sep 17 00:00:00 2001 From: Marcelo Lazaroni Date: Thu, 16 Oct 2025 11:39:49 +0100 Subject: [PATCH] Deserialisation fixes --- src/app/eventStore.ts | 30 ++++++++++----- .../membership/command/submitApplication.ts | 38 ++++++++++--------- src/index.ts | 2 +- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/app/eventStore.ts b/src/app/eventStore.ts index 39601c6..58ff823 100644 --- a/src/app/eventStore.ts +++ b/src/app/eventStore.ts @@ -165,12 +165,18 @@ class PostgresEventStore implements EventStore { aggregateId: Id, ): Promise { 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; @@ -184,9 +190,15 @@ class PostgresEventStore implements EventStore { private async insert>(edata: EventData) { 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); @@ -203,7 +215,6 @@ class PostgresEventStore implements EventStore { serialized.aggregate_version, // @ts-ignore serialized.payload, - '{}', // @ts-ignore serialized.recorded_on, edata.event.values.type, @@ -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) );`, ); diff --git a/src/domain/cookingClub/membership/command/submitApplication.ts b/src/domain/cookingClub/membership/command/submitApplication.ts index 6aad3d7..03260cf 100644 --- a/src/domain/cookingClub/membership/command/submitApplication.ts +++ b/src/domain/cookingClub/membership/command/submitApplication.ts @@ -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; const decoder = d.object({ @@ -20,25 +21,26 @@ const decoder = d.object({ const handler: CommandHandler = ({ command, store, -}): Future => { - store.emit({ - aggregate: ApplicationSubmitted.aggregate, - event: new ApplicationSubmitted({ - type: ApplicationSubmitted.type, - aggregateId: Id.random(), - firstName: command.firstName, - lastName: command.lastName, - favouriteCousine: command.favouriteCousine, - yearsOfProfessionalExperience: command.yearsOfProfessionalExperience, - numberOfCookingBooksRead: command.numberOfCookingBooksRead, - }), - }); - - return Future.resolve( - json({ - content: { message: 'success' }, +}): Future => + Future.attemptP(() => + store.emit({ + aggregate: ApplicationSubmitted.aggregate, + event: new ApplicationSubmitted({ + type: ApplicationSubmitted.type, + aggregateId: Id.random(), + firstName: command.firstName, + lastName: command.lastName, + favouriteCousine: command.favouriteCousine, + yearsOfProfessionalExperience: command.yearsOfProfessionalExperience, + numberOfCookingBooksRead: command.numberOfCookingBooksRead, + }), }), + ).bimap( + (_) => internalServerError, + (_) => + json({ + content: { message: 'success' }, + }), ); -}; const controller: CommandController = { decoder, handler }; diff --git a/src/index.ts b/src/index.ts index 8c0a86f..bf479db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -117,4 +117,4 @@ async function main() { }); } -await main(); +main();