Open History to see the last commit
1import { CommandId, NonNegativeInt, 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 {
11 OrchestrationCommandReceiptRepository,
12 type OrchestrationCommandReceipt,
13} from "../persistence/Services/OrchestrationCommandReceipts.ts";
14
15/**
16 * ERRORS
17 */
18export class CommandReceiptStoreWriteError extends Schema.TaggedErrorClass<CommandReceiptStoreWriteError>()(
19 "CommandReceiptStoreWriteError",
20 {
21 commandId: CommandId,
22 cause: Schema.optional(Schema.Defect()),
23 },
24) {
25 override get message(): string {
26 return `Failed to write orchestration V2 command receipt ${this.commandId}.`;
27 }
28}
29
30export class CommandReceiptStoreReadError extends Schema.TaggedErrorClass<CommandReceiptStoreReadError>()(
31 "CommandReceiptStoreReadError",
32 {
33 commandId: CommandId,
34 cause: Schema.optional(Schema.Defect()),
35 },
36) {
37 override get message(): string {
38 return `Failed to read orchestration V2 command receipt ${this.commandId}.`;
39 }
40}
41
42export const CommandReceiptStoreV2Error = Schema.Union([
43 CommandReceiptStoreWriteError,
44 CommandReceiptStoreReadError,
45]);
46export type CommandReceiptStoreV2Error = typeof CommandReceiptStoreV2Error.Type;
47
48/**
49 * SERVICE DEFINITION
50 */
51export const CommandReceiptV2Status = Schema.Literals(["accepted", "rejected"]);
52export type CommandReceiptV2Status = typeof CommandReceiptV2Status.Type;
53
54export const CommandReceiptV2 = Schema.Struct({
55 commandId: CommandId,
56 threadId: ThreadId,
57 commandType: Schema.String,
58 acceptedAt: Schema.DateTimeUtc,
59 resultSequence: NonNegativeInt,
60 status: CommandReceiptV2Status,
61 error: Schema.NullOr(Schema.String),
62});
63export type CommandReceiptV2 = typeof CommandReceiptV2.Type;
64
65export interface CommandReceiptStoreV2Shape {
66 readonly insertIfAbsent: (
67 receipt: CommandReceiptV2,
68 ) => Effect.Effect<boolean, CommandReceiptStoreV2Error>;
69 readonly upsert: (receipt: CommandReceiptV2) => Effect.Effect<void, CommandReceiptStoreV2Error>;
70 readonly getByCommandId: (
71 commandId: CommandId,
72 ) => Effect.Effect<Option.Option<CommandReceiptV2>, CommandReceiptStoreV2Error>;
73}
74
75export class CommandReceiptStoreV2 extends Context.Service<
76 CommandReceiptStoreV2,
77 CommandReceiptStoreV2Shape
78>()("t3/orchestration-v2/CommandReceiptStore/CommandReceiptStoreV2") {}
79
80/**
81 * IMPLEMENTATIONS
82 */
83const decodeReceipt = Schema.decodeUnknownEffect(
84 CommandReceiptV2.mapFields((fields) => ({
85 ...fields,
86 acceptedAt: Schema.DateTimeUtcFromString,
87 })),
88);
89
90function fromApplicationReceipt(receipt: OrchestrationCommandReceipt) {
91 return decodeReceipt({
92 commandId: receipt.commandId,
93 threadId: receipt.aggregateId,
94 commandType: receipt.commandType,
95 acceptedAt: receipt.acceptedAt,
96 resultSequence: receipt.resultSequence,
97 status: receipt.status,
98 error: receipt.error,
99 });
100}
101
102function toApplicationReceipt(receipt: CommandReceiptV2): OrchestrationCommandReceipt {
103 return {
104 commandId: receipt.commandId,
105 aggregateKind: "thread",
106 aggregateId: receipt.threadId,
107 commandType: receipt.commandType,
108 acceptedAt: DateTime.formatIso(receipt.acceptedAt),
109 resultSequence: receipt.resultSequence,
110 status: receipt.status,
111 error: receipt.error,
112 };
113}
114
115const baseLayer: Layer.Layer<CommandReceiptStoreV2, never, OrchestrationCommandReceiptRepository> =
116 Layer.effect(
117 CommandReceiptStoreV2,
118 Effect.gen(function* () {
119 const receipts = yield* OrchestrationCommandReceiptRepository;
120
121 return CommandReceiptStoreV2.of({
122 insertIfAbsent: (receipt) =>
123 receipts.insertIfAbsent(toApplicationReceipt(receipt)).pipe(
124 Effect.mapError(
125 (cause) =>
126 new CommandReceiptStoreWriteError({
127 commandId: receipt.commandId,
128 cause,
129 }),
130 ),
131 ),
132 upsert: (receipt) =>
133 receipts.upsert(toApplicationReceipt(receipt)).pipe(
134 Effect.mapError(
135 (cause) =>
136 new CommandReceiptStoreWriteError({
137 commandId: receipt.commandId,
138 cause,
139 }),
140 ),
141 ),
142 getByCommandId: (commandId) =>
143 receipts.getByCommandId({ commandId }).pipe(
144 Effect.flatMap(
145 Option.match({
146 onNone: () => Effect.succeed(Option.none()),
147 onSome: (receipt) =>
148 receipt.aggregateKind !== "thread"
149 ? Effect.succeed(Option.none())
150 : fromApplicationReceipt(receipt).pipe(Effect.map(Option.some)),
151 }),
152 ),
153 Effect.mapError(
154 (cause) =>
155 new CommandReceiptStoreReadError({
156 commandId,
157 cause,
158 }),
159 ),
160 ),
161 } satisfies CommandReceiptStoreV2Shape);
162 }),
163 );
164
165export const layer = baseLayer.pipe(Layer.provide(OrchestrationCommandReceiptRepositoryLive));
166
167export const layerFromApplicationReceipts = baseLayer;