| ... | @@ -3,7 +3,7 @@ import type { | ... | @@ -3,7 +3,7 @@ import type { |
| 3 | OrchestrationEvent, | 3 | OrchestrationEvent, |
| 4 | OrchestrationReadModel, | 4 | OrchestrationReadModel, |
| 5 | } from "@t3tools/contracts"; | 5 | } from "@t3tools/contracts"; |
| 6 | import { Effect } from "effect"; | 6 | import { DateTime, Effect } from "effect"; |
| 7 | | 7 | |
| 8 | import { OrchestrationCommandInvariantError } from "./Errors.ts"; | 8 | import { OrchestrationCommandInvariantError } from "./Errors.ts"; |
| 9 | import { | 9 | import { |
| ... | @@ -17,17 +17,7 @@ import { | ... | @@ -17,17 +17,7 @@ import { |
| 17 | } from "./commandInvariants.ts"; | 17 | } from "./commandInvariants.ts"; |
| 18 | import { projectEvent } from "./projector.ts"; | 18 | import { projectEvent } from "./projector.ts"; |
| 19 | | 19 | |
| 20 | const nowIso = () => new Date().toISOString(); | 20 | const currentIso = DateTime.now.pipe(Effect.map(DateTime.formatIso)); |
| 21 | const defaultMetadata: Omit<OrchestrationEvent, "sequence" | "type" | "payload"> = { | | |
| 22 | eventId: crypto.randomUUID() as OrchestrationEvent["eventId"], | | |
| 23 | aggregateKind: "thread", | | |
| 24 | aggregateId: "" as OrchestrationEvent["aggregateId"], | | |
| 25 | occurredAt: nowIso(), | | |
| 26 | commandId: null, | | |
| 27 | causationEventId: null, | | |
| 28 | correlationId: null, | | |
| 29 | metadata: {}, | | |
| 30 | }; | | |
| 31 | | 21 | |
| 32 | function withEventBase( | 22 | function withEventBase( |
| 33 | input: Pick<OrchestrationCommand, "commandId"> & { | 23 | input: Pick<OrchestrationCommand, "commandId"> & { |
| ... | @@ -38,12 +28,12 @@ function withEventBase( | ... | @@ -38,12 +28,12 @@ function withEventBase( |
| 38 | }, | 28 | }, |
| 39 | ): Omit<OrchestrationEvent, "sequence" | "type" | "payload"> { | 29 | ): Omit<OrchestrationEvent, "sequence" | "type" | "payload"> { |
| 40 | return { | 30 | return { |
| 41 | ...defaultMetadata, | | |
| 42 | eventId: crypto.randomUUID() as OrchestrationEvent["eventId"], | 31 | eventId: crypto.randomUUID() as OrchestrationEvent["eventId"], |
| 43 | aggregateKind: input.aggregateKind, | 32 | aggregateKind: input.aggregateKind, |
| 44 | aggregateId: input.aggregateId, | 33 | aggregateId: input.aggregateId, |
| 45 | occurredAt: input.occurredAt, | 34 | occurredAt: input.occurredAt, |
| 46 | commandId: input.commandId, | 35 | commandId: input.commandId, |
| | 36 | causationEventId: null, |
| 47 | correlationId: input.commandId, | 37 | correlationId: input.commandId, |
| 48 | metadata: input.metadata ?? {}, | 38 | metadata: input.metadata ?? {}, |
| 49 | }; | 39 | }; |
| ... | @@ -126,7 +116,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" | ... | @@ -126,7 +116,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" |
| 126 | command, | 116 | command, |
| 127 | projectId: command.projectId, | 117 | projectId: command.projectId, |
| 128 | }); | 118 | }); |
| 129 | const occurredAt = nowIso(); | 119 | const occurredAt = yield* currentIso; |
| 130 | return { | 120 | return { |
| 131 | ...withEventBase({ | 121 | ...withEventBase({ |
| 132 | aggregateKind: "project", | 122 | aggregateKind: "project", |
| ... | @@ -183,7 +173,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" | ... | @@ -183,7 +173,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" |
| 183 | }); | 173 | }); |
| 184 | } | 174 | } |
| 185 | | 175 | |
| 186 | const occurredAt = nowIso(); | 176 | const occurredAt = yield* currentIso; |
| 187 | return { | 177 | return { |
| 188 | ...withEventBase({ | 178 | ...withEventBase({ |
| 189 | aggregateKind: "project", | 179 | aggregateKind: "project", |
| ... | @@ -239,7 +229,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" | ... | @@ -239,7 +229,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" |
| 239 | command, | 229 | command, |
| 240 | threadId: command.threadId, | 230 | threadId: command.threadId, |
| 241 | }); | 231 | }); |
| 242 | const occurredAt = nowIso(); | 232 | const occurredAt = yield* currentIso; |
| 243 | return { | 233 | return { |
| 244 | ...withEventBase({ | 234 | ...withEventBase({ |
| 245 | aggregateKind: "thread", | 235 | aggregateKind: "thread", |
| ... | @@ -261,7 +251,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" | ... | @@ -261,7 +251,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" |
| 261 | command, | 251 | command, |
| 262 | threadId: command.threadId, | 252 | threadId: command.threadId, |
| 263 | }); | 253 | }); |
| 264 | const occurredAt = nowIso(); | 254 | const occurredAt = yield* currentIso; |
| 265 | return { | 255 | return { |
| 266 | ...withEventBase({ | 256 | ...withEventBase({ |
| 267 | aggregateKind: "thread", | 257 | aggregateKind: "thread", |
| ... | @@ -284,7 +274,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" | ... | @@ -284,7 +274,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" |
| 284 | command, | 274 | command, |
| 285 | threadId: command.threadId, | 275 | threadId: command.threadId, |
| 286 | }); | 276 | }); |
| 287 | const occurredAt = nowIso(); | 277 | const occurredAt = yield* currentIso; |
| 288 | return { | 278 | return { |
| 289 | ...withEventBase({ | 279 | ...withEventBase({ |
| 290 | aggregateKind: "thread", | 280 | aggregateKind: "thread", |
| ... | @@ -306,7 +296,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" | ... | @@ -306,7 +296,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" |
| 306 | command, | 296 | command, |
| 307 | threadId: command.threadId, | 297 | threadId: command.threadId, |
| 308 | }); | 298 | }); |
| 309 | const occurredAt = nowIso(); | 299 | const occurredAt = yield* currentIso; |
| 310 | return { | 300 | return { |
| 311 | ...withEventBase({ | 301 | ...withEventBase({ |
| 312 | aggregateKind: "thread", | 302 | aggregateKind: "thread", |
| ... | @@ -334,7 +324,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" | ... | @@ -334,7 +324,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" |
| 334 | command, | 324 | command, |
| 335 | threadId: command.threadId, | 325 | threadId: command.threadId, |
| 336 | }); | 326 | }); |
| 337 | const occurredAt = nowIso(); | 327 | const occurredAt = yield* currentIso; |
| 338 | return { | 328 | return { |
| 339 | ...withEventBase({ | 329 | ...withEventBase({ |
| 340 | aggregateKind: "thread", | 330 | aggregateKind: "thread", |
| ... | @@ -357,7 +347,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" | ... | @@ -357,7 +347,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" |
| 357 | command, | 347 | command, |
| 358 | threadId: command.threadId, | 348 | threadId: command.threadId, |
| 359 | }); | 349 | }); |
| 360 | const occurredAt = nowIso(); | 350 | const occurredAt = yield* currentIso; |
| 361 | return { | 351 | return { |
| 362 | ...withEventBase({ | 352 | ...withEventBase({ |
| 363 | aggregateKind: "thread", | 353 | aggregateKind: "thread", |