Open History to see the last commit
1import { CommandId, NonNegativeInt, ProjectId, ThreadId } from "@t3tools/contracts";
2import * as Context from "effect/Context";
3import * as DateTime from "effect/DateTime";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as Schema from "effect/Schema";
8
9import { OrchestrationCommandReceiptRepositoryLive } from "../persistence/Layers/OrchestrationCommandReceipts.ts";
10import * as OrchestrationCommandReceipts from "../persistence/Services/OrchestrationCommandReceipts.ts";
11
12/**
13 * ERRORS
14 */
15export class CommandReceiptStoreWriteError extends Schema.TaggedError<CommandReceiptStoreWriteError>()(
16 "CommandReceiptStoreWriteError",
17 {
18 commandId: CommandId,
19 cause: Schema.optional(Schema.Defect()),
20 },
21) {
22 override get message(): string {
23 return `Failed to write orchestration V2 command receipt ${this.commandId}.`;
24 }
25}
26
27export class CommandReceiptStoreReadError extends Schema.TaggedError<CommandReceiptStoreReadError>()(
28 "CommandReceiptStoreReadError",
29 {
30 commandId: CommandId,
31 cause: Schema.optional(Schema.Defect()),
32 },
33) {
34 override get message(): string {
35 return `Failed to read orchestration V2 command receipt ${this.commandId}.`;
36 }
37}
38
39export const CommandReceiptStoreV2Error = Schema.Union([
40 CommandReceiptStoreWriteError,
41 CommandReceiptStoreReadError,
42]);
43export type CommandReceiptStoreV2Error = typeof CommandReceiptStoreV2Error.Type;
44
45/**
46 * SERVICE DEFINITION
47 */
48export const CommandReceiptV2Status = Schema.Literals(["accepted", "rejected"]);
49export type CommandReceiptV2Status = typeof CommandReceiptV2Status.Type;
50
51export const CommandReceiptV2 = Schema.Struct({
52 commandId: CommandId,
53 threadId: ThreadId,
54 commandType: Schema.String,
55 acceptedAt: Schema.DateTimeUtc,
56 resultSequence: NonNegativeInt,
57 status: CommandReceiptV2Status,
58 error: Schema.NullOr(Schema.String),
59});
60export type CommandReceiptV2 = typeof CommandReceiptV2.Type;
61
62/** Receipt for a project command; shares the receipt table with thread commands. */
63export const ProjectCommandReceiptV2 = Schema.Struct({
64 commandId: CommandId,
65 projectId: ProjectId,
66 commandType: Schema.String,
67 acceptedAt: Schema.DateTimeUtc,
68 resultSequence: NonNegativeInt,
69 status: CommandReceiptV2Status,
70 error: Schema.NullOr(Schema.String),
71});
72export type ProjectCommandReceiptV2 = typeof ProjectCommandReceiptV2.Type;
73
74type AnyCommandReceiptV2 = CommandReceiptV2 | ProjectCommandReceiptV2;
75
76export interface CommandReceiptStoreV2Shape {
77 readonly insertIfAbsent: (
78 receipt: AnyCommandReceiptV2,
79 ) => Effect.Effect<boolean, CommandReceiptStoreV2Error>;
80 readonly upsert: (
81 receipt: AnyCommandReceiptV2,
82 ) => Effect.Effect<void, CommandReceiptStoreV2Error>;
83 /** A thread command's receipt; none when the command id belongs to a project command. */
84 readonly getByCommandId: (
85 commandId: CommandId,
86 ) => Effect.Effect<Option.Option<CommandReceiptV2>, CommandReceiptStoreV2Error>;
87 /** A project command's receipt; none when the command id belongs to a thread command. */
88 readonly getProjectByCommandId: (
89 commandId: CommandId,
90 ) => Effect.Effect<Option.Option<ProjectCommandReceiptV2>, CommandReceiptStoreV2Error>;
91}
92
93export class CommandReceiptStoreV2 extends Context.Service<
94 CommandReceiptStoreV2,
95 CommandReceiptStoreV2Shape
96>()("t3/orchestration-v2/CommandReceiptStore/CommandReceiptStoreV2") {}
97
98/**
99 * IMPLEMENTATIONS
100 */
101const decodeReceipt = Schema.decodeUnknownEffect(
102 CommandReceiptV2.mapFields((fields) => ({
103 ...fields,
104 acceptedAt: Schema.DateTimeUtcFromString,
105 })),
106);
107const decodeProjectReceipt = Schema.decodeUnknownEffect(
108 ProjectCommandReceiptV2.mapFields((fields) => ({
109 ...fields,
110 acceptedAt: Schema.DateTimeUtcFromString,
111 })),
112);
113
114function fromApplicationReceipt(receipt: OrchestrationCommandReceipts.OrchestrationCommandReceipt) {
115 return decodeReceipt({
116 commandId: receipt.commandId,
117 threadId: receipt.aggregateId,
118 commandType: receipt.commandType,
119 acceptedAt: receipt.acceptedAt,
120 resultSequence: receipt.resultSequence,
121 status: receipt.status,
122 error: receipt.error,
123 });
124}
125
126function toApplicationReceipt(
127 receipt: AnyCommandReceiptV2,
128): OrchestrationCommandReceipts.OrchestrationCommandReceipt {
129 return {
130 commandId: receipt.commandId,
131 ...("projectId" in receipt
132 ? { aggregateKind: "project" as const, aggregateId: receipt.projectId }
133 : { aggregateKind: "thread" as const, aggregateId: receipt.threadId }),
134 commandType: receipt.commandType,
135 acceptedAt: DateTime.formatIso(receipt.acceptedAt),
136 resultSequence: receipt.resultSequence,
137 status: receipt.status,
138 error: receipt.error,
139 };
140}
141
142const baseLayer: Layer.Layer<
143 CommandReceiptStoreV2,
144 never,
145 OrchestrationCommandReceipts.OrchestrationCommandReceiptRepository
146> = Layer.effect(
147 CommandReceiptStoreV2,
148 Effect.gen(function* () {
149 const receipts = yield* OrchestrationCommandReceipts.OrchestrationCommandReceiptRepository;
150
151 return CommandReceiptStoreV2.of({
152 insertIfAbsent: (receipt) =>
153 receipts.insertIfAbsent(toApplicationReceipt(receipt)).pipe(
154 Effect.mapError(
155 (cause) =>
156 new CommandReceiptStoreWriteError({
157 commandId: receipt.commandId,
158 cause,
159 }),
160 ),
161 ),
162 upsert: (receipt) =>
163 receipts.upsert(toApplicationReceipt(receipt)).pipe(
164 Effect.mapError(
165 (cause) =>
166 new CommandReceiptStoreWriteError({
167 commandId: receipt.commandId,
168 cause,
169 }),
170 ),
171 ),
172 getByCommandId: (commandId) =>
173 receipts.getByCommandId({ commandId }).pipe(
174 Effect.flatMap(
175 Option.match({
176 onNone: () => Effect.succeed(Option.none()),
177 onSome: (receipt) =>
178 receipt.aggregateKind !== "thread"
179 ? Effect.succeed(Option.none())
180 : fromApplicationReceipt(receipt).pipe(Effect.map(Option.some)),
181 }),
182 ),
183 Effect.mapError(
184 (cause) =>
185 new CommandReceiptStoreReadError({
186 commandId,
187 cause,
188 }),
189 ),
190 ),
191 getProjectByCommandId: (commandId) =>
192 receipts.getByCommandId({ commandId }).pipe(
193 Effect.flatMap((receipt) =>
194 Option.isNone(receipt) || receipt.value.aggregateKind !== "project"
195 ? Effect.succeed(Option.none())
196 : decodeProjectReceipt({
197 commandId: receipt.value.commandId,
198 projectId: receipt.value.aggregateId,
199 commandType: receipt.value.commandType,
200 acceptedAt: receipt.value.acceptedAt,
201 resultSequence: receipt.value.resultSequence,
202 status: receipt.value.status,
203 error: receipt.value.error,
204 }).pipe(Effect.map(Option.some)),
205 ),
206 Effect.mapError((cause) => new CommandReceiptStoreReadError({ commandId, cause })),
207 ),
208 } satisfies CommandReceiptStoreV2Shape);
209 }),
210);
211
212export const layer = baseLayer.pipe(Layer.provide(OrchestrationCommandReceiptRepositoryLive));
213
214export const layerFromApplicationReceipts = baseLayer;