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

commandInvariants.ts

158 lines4.7 KB

1import type { ProjectId, ThreadId } from "@t3tools/contracts";
2import type {
3 OrchestrationCommand,
4 OrchestrationProject,
5 OrchestrationReadModel,
6 OrchestrationThread,
7} from "@t3tools/contracts/legacy-orchestration";
8import * as Effect from "effect/Effect";
9​
10import { OrchestrationCommandInvariantError } from "./Errors.ts";
11​
12function invariantError(commandType: string, detail: string): OrchestrationCommandInvariantError {
13 return new OrchestrationCommandInvariantError({
14 commandType,
15 detail,
16 });
18​
19export function findThreadById(
20 readModel: OrchestrationReadModel,
21 threadId: ThreadId,
22): OrchestrationThread | undefined {
23 return readModel.threads.find((thread) => thread.id === threadId);
25​
26export function findProjectById(
27 readModel: OrchestrationReadModel,
28 projectId: ProjectId,
29): OrchestrationProject | undefined {
30 return readModel.projects.find((project) => project.id === projectId);
32​
33export function listThreadsByProjectId(
34 readModel: OrchestrationReadModel,
35 projectId: ProjectId,
36): ReadonlyArray<OrchestrationThread> {
37 return readModel.threads.filter((thread) => thread.projectId === projectId);
39​
40export function requireProject(input: {
41 readonly readModel: OrchestrationReadModel;
42 readonly command: OrchestrationCommand;
43 readonly projectId: ProjectId;
44}): Effect.Effect<OrchestrationProject, OrchestrationCommandInvariantError> {
45 const project = findProjectById(input.readModel, input.projectId);
46 if (project) {
47 return Effect.succeed(project);
48 }
49 return Effect.fail(
50 invariantError(
51 input.command.type,
52 `Project '${input.projectId}' does not exist for command '${input.command.type}'.`,
53 ),
54 );
56​
57export function requireProjectAbsent(input: {
58 readonly readModel: OrchestrationReadModel;
59 readonly command: OrchestrationCommand;
60 readonly projectId: ProjectId;
61}): Effect.Effect<void, OrchestrationCommandInvariantError> {
62 if (!findProjectById(input.readModel, input.projectId)) {
63 return Effect.void;
64 }
65 return Effect.fail(
66 invariantError(
67 input.command.type,
68 `Project '${input.projectId}' already exists and cannot be created twice.`,
69 ),
70 );
72​
73export function requireThread(input: {
74 readonly readModel: OrchestrationReadModel;
75 readonly command: OrchestrationCommand;
76 readonly threadId: ThreadId;
77}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
78 const thread = findThreadById(input.readModel, input.threadId);
79 if (thread) {
80 return Effect.succeed(thread);
81 }
82 return Effect.fail(
83 invariantError(
84 input.command.type,
85 `Thread '${input.threadId}' does not exist for command '${input.command.type}'.`,
86 ),
87 );
89​
90export function requireThreadArchived(input: {
91 readonly readModel: OrchestrationReadModel;
92 readonly command: OrchestrationCommand;
93 readonly threadId: ThreadId;
94}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
95 return requireThread(input).pipe(
96 Effect.flatMap((thread) =>
97 thread.archivedAt !== null
98 ? Effect.succeed(thread)
99 : Effect.fail(
100 invariantError(
101 input.command.type,
102 `Thread '${input.threadId}' is not archived for command '${input.command.type}'.`,
103 ),
104 ),
105 ),
106 );
108​
109export function requireThreadNotArchived(input: {
110 readonly readModel: OrchestrationReadModel;
111 readonly command: OrchestrationCommand;
112 readonly threadId: ThreadId;
113}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
114 return requireThread(input).pipe(
115 Effect.flatMap((thread) =>
116 thread.archivedAt === null
117 ? Effect.succeed(thread)
118 : Effect.fail(
119 invariantError(
120 input.command.type,
121 `Thread '${input.threadId}' is already archived and cannot handle command '${input.command.type}'.`,
122 ),
123 ),
124 ),
125 );
127​
128export function requireThreadAbsent(input: {
129 readonly readModel: OrchestrationReadModel;
130 readonly command: OrchestrationCommand;
131 readonly threadId: ThreadId;
132}): Effect.Effect<void, OrchestrationCommandInvariantError> {
133 if (!findThreadById(input.readModel, input.threadId)) {
134 return Effect.void;
135 }
136 return Effect.fail(
137 invariantError(
138 input.command.type,
139 `Thread '${input.threadId}' already exists and cannot be created twice.`,
140 ),
141 );
143​
144export function requireNonNegativeInteger(input: {
145 readonly commandType: OrchestrationCommand["type"];
146 readonly field: string;
147 readonly value: number;
148}): Effect.Effect<void, OrchestrationCommandInvariantError> {
149 if (Number.isInteger(input.value) && input.value >= 0) {
150 return Effect.void;
151 }
152 return Effect.fail(
153 invariantError(
154 input.commandType,
155 `${input.field} must be an integer greater than or equal to 0.`,
156 ),
157 );