Open History to see the last commit
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 {
11 OrchestrationAggregateKind,
12 OrchestrationCommandReceiptStatus,
13} from "@t3tools/contracts/legacy-orchestration";
14import * as Option from "effect/Option";
15import * as Schema from "effect/Schema";
16import * as Context from "effect/Context";
17import type * as Effect from "effect/Effect";
18
19import type { OrchestrationCommandReceiptRepositoryError } from "../Errors.ts";
20
21export const OrchestrationCommandReceipt = Schema.Struct({
22 commandId: CommandId,
23 aggregateKind: OrchestrationAggregateKind,
24 aggregateId: Schema.Union([ProjectId, ThreadId]),
25 commandType: Schema.String,
26 acceptedAt: IsoDateTime,
27 resultSequence: NonNegativeInt,
28 status: OrchestrationCommandReceiptStatus,
29 error: Schema.NullOr(Schema.String),
30});
31export type OrchestrationCommandReceipt = typeof OrchestrationCommandReceipt.Type;
32
33export const GetByCommandIdInput = Schema.Struct({
34 commandId: CommandId,
35});
36export type GetByCommandIdInput = typeof GetByCommandIdInput.Type;
37
38/**
39 * OrchestrationCommandReceiptRepositoryShape - Service API for command receipts.
40 */
41export interface OrchestrationCommandReceiptRepositoryShape {
42 readonly insertIfAbsent: (
43 receipt: OrchestrationCommandReceipt,
44 ) => Effect.Effect<boolean, OrchestrationCommandReceiptRepositoryError>;
45
46 /**
47 * Insert or replace a command receipt row.
48 *
49 * Upserts by `commandId` for idempotent command-result tracking.
50 */
51 readonly upsert: (
52 receipt: OrchestrationCommandReceipt,
53 ) => Effect.Effect<void, OrchestrationCommandReceiptRepositoryError>;
54
55 /**
56 * Read a command receipt by command id.
57 */
58 readonly getByCommandId: (
59 input: GetByCommandIdInput,
60 ) => Effect.Effect<
61 Option.Option<OrchestrationCommandReceipt>,
62 OrchestrationCommandReceiptRepositoryError
63 >;
64}
65
66/**
67 * OrchestrationCommandReceiptRepository - Service tag for command receipt persistence.
68 */
69export class OrchestrationCommandReceiptRepository extends Context.Service<
70 OrchestrationCommandReceiptRepository,
71 OrchestrationCommandReceiptRepositoryShape
72>()("t3/persistence/Services/OrchestrationCommandReceipts/OrchestrationCommandReceiptRepository") {}