Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OrchestrationCommandReceipts.ts

68 lines2.3 KB

1/**
2 * OrchestrationCommandReceiptRepository - Repository interface for command receipts.
3 *
4 * Owns persistence operations for deduplication and status tracking of
5 * orchestration command handling.
6 *
7 * @module OrchestrationCommandReceiptRepository
8 */
9import { CommandId, IsoDateTime, NonNegativeInt, ProjectId, ThreadId } from "@t3tools/contracts";
10import * as Option from "effect/Option";
11import * as Schema from "effect/Schema";
12import * as Context from "effect/Context";
13import type * as Effect from "effect/Effect";
14​
15import type { OrchestrationCommandReceiptRepositoryError } from "../Errors.ts";
16​
17export const OrchestrationCommandReceipt = Schema.Struct({
18 commandId: CommandId,
19 aggregateKind: Schema.Literals(["project", "thread"]),
20 aggregateId: Schema.Union([ProjectId, ThreadId]),
21 commandType: Schema.String,
22 acceptedAt: IsoDateTime,
23 resultSequence: NonNegativeInt,
24 status: Schema.Literals(["accepted", "rejected"]),
25 error: Schema.NullOr(Schema.String),
26});
27export type OrchestrationCommandReceipt = typeof OrchestrationCommandReceipt.Type;
28​
29export const GetByCommandIdInput = Schema.Struct({
30 commandId: CommandId,
31});
32export type GetByCommandIdInput = typeof GetByCommandIdInput.Type;
33​
34/**
35 * OrchestrationCommandReceiptRepositoryShape - Service API for command receipts.
36 */
37export interface OrchestrationCommandReceiptRepositoryShape {
38 readonly insertIfAbsent: (
39 receipt: OrchestrationCommandReceipt,
40 ) => Effect.Effect<boolean, OrchestrationCommandReceiptRepositoryError>;
41​
42 /**
43 * Insert or replace a command receipt row.
44 *
45 * Upserts by `commandId` for idempotent command-result tracking.
46 */
47 readonly upsert: (
48 receipt: OrchestrationCommandReceipt,
49 ) => Effect.Effect<void, OrchestrationCommandReceiptRepositoryError>;
50​
51 /**
52 * Read a command receipt by command id.
53 */
54 readonly getByCommandId: (
55 input: GetByCommandIdInput,
56 ) => Effect.Effect<
57 Option.Option<OrchestrationCommandReceipt>,
58 OrchestrationCommandReceiptRepositoryError
59 >;
61​
62/**
63 * OrchestrationCommandReceiptRepository - Service tag for command receipt persistence.
64 */
65export class OrchestrationCommandReceiptRepository extends Context.Service<
66 OrchestrationCommandReceiptRepository,
67 OrchestrationCommandReceiptRepositoryShape
68>()("t3/persistence/Services/OrchestrationCommandReceipts/OrchestrationCommandReceiptRepository") {}