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

OrchestrationCommandReceipts.ts

167 lines5.1 KB

1import { CommandId, IsoDateTime, NonNegativeInt, ProjectId, ThreadId } from "@t3tools/contracts";
2import * as SqlClient from "effect/sql/SqlClient";
3import * as SqlSchema from "effect/sql/SqlSchema";
4import * as Context from "effect/Context";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import * as Option from "effect/Option";
8import * as Schema from "effect/Schema";
9​
10import {
11 toPersistenceSqlError,
12 type OrchestrationCommandReceiptRepositoryError,
13} from "./Errors.ts";
14​
15export const OrchestrationCommandReceipt = Schema.Struct({
16 commandId: CommandId,
17 aggregateKind: Schema.Literals(["project", "thread"]),
18 aggregateId: Schema.Union([ProjectId, ThreadId]),
19 commandType: Schema.String,
20 acceptedAt: IsoDateTime,
21 resultSequence: NonNegativeInt,
22 status: Schema.Literals(["accepted", "rejected"]),
23 error: Schema.NullOr(Schema.String),
24});
25export type OrchestrationCommandReceipt = typeof OrchestrationCommandReceipt.Type;
26​
27export const GetByCommandIdInput = Schema.Struct({
28 commandId: CommandId,
29});
30export type GetByCommandIdInput = typeof GetByCommandIdInput.Type;
31​
32export class OrchestrationCommandReceiptRepository extends Context.Service<
33 OrchestrationCommandReceiptRepository,
34 {
35 readonly insertIfAbsent: (
36 receipt: OrchestrationCommandReceipt,
37 ) => Effect.Effect<boolean, OrchestrationCommandReceiptRepositoryError>;
38​
39 readonly upsert: (
40 receipt: OrchestrationCommandReceipt,
41 ) => Effect.Effect<void, OrchestrationCommandReceiptRepositoryError>;
42​
43 readonly getByCommandId: (
44 input: GetByCommandIdInput,
45 ) => Effect.Effect<
46 Option.Option<OrchestrationCommandReceipt>,
47 OrchestrationCommandReceiptRepositoryError
48 >;
49 }
50>()("t3/persistence/OrchestrationCommandReceipts/OrchestrationCommandReceiptRepository") {}
51​
52const makeOrchestrationCommandReceiptRepository = Effect.gen(function* () {
53 const sql = yield* SqlClient.SqlClient;
54​
55 const upsertReceiptRow = SqlSchema.void({
56 Request: OrchestrationCommandReceipt,
57 execute: (receipt) =>
58 sql`
59 INSERT INTO orchestration_command_receipts (
60 command_id,
61 aggregate_kind,
62 aggregate_id,
63 command_type,
64 accepted_at,
65 result_sequence,
66 status,
67 error
68 )
69 VALUES (
70 ${receipt.commandId},
71 ${receipt.aggregateKind},
72 ${receipt.aggregateId},
73 ${receipt.commandType},
74 ${receipt.acceptedAt},
75 ${receipt.resultSequence},
76 ${receipt.status},
77 ${receipt.error}
78 )
79 ON CONFLICT (command_id)
80 DO UPDATE SET
81 aggregate_kind = excluded.aggregate_kind,
82 aggregate_id = excluded.aggregate_id,
83 command_type = excluded.command_type,
84 accepted_at = excluded.accepted_at,
85 result_sequence = excluded.result_sequence,
86 status = excluded.status,
87 error = excluded.error
88 `,
89 });
90​
91 const findReceiptByCommandId = SqlSchema.findOneOption({
92 Request: GetByCommandIdInput,
93 Result: OrchestrationCommandReceipt,
94 execute: ({ commandId }) =>
95 sql`
96 SELECT
97 command_id AS "commandId",
98 aggregate_kind AS "aggregateKind",
99 aggregate_id AS "aggregateId",
100 command_type AS "commandType",
101 accepted_at AS "acceptedAt",
102 result_sequence AS "resultSequence",
103 status,
104 error
105 FROM orchestration_command_receipts
106 WHERE command_id = ${commandId}
107 `,
108 });
109​
110 const upsert: OrchestrationCommandReceiptRepository["Service"]["upsert"] = (receipt) =>
111 upsertReceiptRow(receipt).pipe(
112 Effect.mapError(toPersistenceSqlError("OrchestrationCommandReceiptRepository.upsert:query")),
113 );
114​
115 const insertIfAbsent: OrchestrationCommandReceiptRepository["Service"]["insertIfAbsent"] = (
116 receipt,
117 ) =>
118 sql<{ readonly command_id: string }>`
119 INSERT INTO orchestration_command_receipts (
120 command_id,
121 aggregate_kind,
122 aggregate_id,
123 command_type,
124 accepted_at,
125 result_sequence,
126 status,
127 error
128 )
129 VALUES (
130 ${receipt.commandId},
131 ${receipt.aggregateKind},
132 ${receipt.aggregateId},
133 ${receipt.commandType},
134 ${receipt.acceptedAt},
135 ${receipt.resultSequence},
136 ${receipt.status},
137 ${receipt.error}
138 )
139 ON CONFLICT(command_id) DO NOTHING
140 RETURNING command_id
141 `.pipe(
142 Effect.map((rows) => rows.length === 1),
143 Effect.mapError(
144 toPersistenceSqlError("OrchestrationCommandReceiptRepository.insertIfAbsent:query"),
145 ),
146 );
147​
148 const getByCommandId: OrchestrationCommandReceiptRepository["Service"]["getByCommandId"] = (
149 input,
150 ) =>
151 findReceiptByCommandId(input).pipe(
152 Effect.mapError(
153 toPersistenceSqlError("OrchestrationCommandReceiptRepository.getByCommandId:query"),
154 ),
155 );
156​
157 return {
158 insertIfAbsent,
159 upsert,
160 getByCommandId,
161 } satisfies OrchestrationCommandReceiptRepository["Service"];
162});
163​
164export const layer = Layer.effect(
165 OrchestrationCommandReceiptRepository,
166 makeOrchestrationCommandReceiptRepository,
167);