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

commandInvariants.ts

184 lines5.6 KB

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