diff --git a/server/src/shared/infra/db/postgres/migrations/002_fix_endpoint_metrics_latency_column.ts b/server/src/shared/infra/db/postgres/migrations/002_fix_endpoint_metrics_latency_column.ts new file mode 100644 index 0000000..60f1272 --- /dev/null +++ b/server/src/shared/infra/db/postgres/migrations/002_fix_endpoint_metrics_latency_column.ts @@ -0,0 +1,32 @@ +import { Kysely, sql } from "kysely"; + +export async function up(db: Kysely): Promise { + const hasTotalLatency = await sql<{ exists: boolean }>` + SELECT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_name = 'endpoint_metrics' AND column_name = 'total_latency' + ) as exists + `.execute(db); + + if (hasTotalLatency.rows[0]?.exists) return; + + const hasAvgLatency = await sql<{ exists: boolean }>` + SELECT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_name = 'endpoint_metrics' AND column_name = 'avg_latency' + ) as exists + `.execute(db); + + if (hasAvgLatency.rows[0]?.exists) { + await db.schema.alterTable("endpoint_metrics").renameColumn("avg_latency", "total_latency").execute(); + } else { + await db.schema + .alterTable("endpoint_metrics") + .addColumn("total_latency", "double precision", (col) => col.defaultTo(0.0)) + .execute(); + } +} + +export async function down(db: Kysely): Promise { + await db.schema.alterTable("endpoint_metrics").renameColumn("total_latency", "avg_latency").execute(); +} diff --git a/server/src/shared/infra/messaging/eventProducer.ts b/server/src/shared/infra/messaging/eventProducer.ts index 4e1da2e..c0af5b2 100644 --- a/server/src/shared/infra/messaging/eventProducer.ts +++ b/server/src/shared/infra/messaging/eventProducer.ts @@ -5,13 +5,7 @@ import { IRetryStrategy } from "../../contracts/infra/resilience/IRetryStrategy. import { CircuitBreakerStatsType } from "../../typings/circuitBreaker.typings"; import { ResourceNotInitializedError } from "../../typings/error.typings"; import { ProducerShuttingDownError } from "../../typings/eventError.typings"; -import { - EventProducerMetricsType, - EventType, - PublishingEventDataType, - PublishingMessageType, - PublishOptions, -} from "../../typings/messaging.typings"; +import { EventProducerMetricsType, PublishingEventDataType, PublishOptions } from "../../typings/messaging.typings"; export class EventProducer { private channelManager: IConfirmChannelManager; @@ -44,12 +38,6 @@ export class EventProducer { } private async publish(obj: PublishingEventDataType): Promise { - const message: PublishingMessageType = { - type: EventType.API_HITS, - data: obj, - publishedAt: new Date().toISOString(), - }; - const publishOptions: PublishOptions = { persistent: true, contentType: "application/json", @@ -63,7 +51,7 @@ export class EventProducer { } try { const currentChannel = await this.channelManager.getChannel(); - const messageBuffer = Buffer.from(JSON.stringify(message)); + const messageBuffer = Buffer.from(JSON.stringify(obj)); return new Promise((resolve, reject) => { const written = currentChannel.publish("", this.queueName, messageBuffer, publishOptions, (err) => { diff --git a/server/src/shared/middleware/zodValidators.middleware.ts b/server/src/shared/middleware/zodValidators.middleware.ts index f6d7101..4a078b4 100644 --- a/server/src/shared/middleware/zodValidators.middleware.ts +++ b/server/src/shared/middleware/zodValidators.middleware.ts @@ -24,7 +24,10 @@ export const validateBody = (schema: z.ZodType) => async (req: Request, res export const validateQuery = (schema: z.ZodType) => async (req: Request, res: Response, next: NextFunction) => { try { - req.query = await schema.parseAsync(req.query); + const parsed = await schema.parseAsync(req.query); + // Express 5 exposes req.query as a getter-only accessor, so it can't be + // reassigned outright — mutate the existing object in place instead. + Object.assign(req.query, parsed); next(); } catch (error: any) { const formattedErrors = diff --git a/server/src/shared/typings/messaging.typings.ts b/server/src/shared/typings/messaging.typings.ts index 4e4b4ad..c55c820 100644 --- a/server/src/shared/typings/messaging.typings.ts +++ b/server/src/shared/typings/messaging.typings.ts @@ -35,12 +35,6 @@ export interface EventDataType { userAgent?: string; } -export interface PublishingMessageType { - type: EventType; - data: PublishingEventDataType; - publishedAt: string; -} - export interface PublishOptions { persistent: boolean; // Whether the message should be marked as persistent contentType: string; // Content type of the message