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

commandInvariants.ts

171 lines5.5 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 { normalizeProjectPathForComparison } from "@t3tools/shared/path";
9import * as Effect from "effect/Effect";
10​
11import { OrchestrationCommandInvariantError } from "./Errors.ts";
12​
13function invariantError(commandType: string, detail: string): OrchestrationCommandInvariantError {
14 return new OrchestrationCommandInvariantError({
15 commandType,
16 detail,
17 });
19​
20export function findThreadById(
21 readModel: OrchestrationReadModel,
22 threadId: ThreadId,
23): OrchestrationThread | undefined {
24 return readModel.threads.find((thread) => thread.id === threadId);
26​
27export function findProjectById(
28 readModel: OrchestrationReadModel,
29 projectId: ProjectId,
30): OrchestrationProject | undefined {
31 return readModel.projects.find((project) => project.id === projectId);
33​
34export function listThreadsByProjectId(
35 readModel: OrchestrationReadModel,
36 projectId: ProjectId,
37): ReadonlyArray<OrchestrationThread> {
38 return readModel.threads.filter((thread) => thread.projectId === projectId);
40​
41export function requireProject(input: {
42 readonly readModel: OrchestrationReadModel;
43 readonly command: OrchestrationCommand;
44 readonly projectId: ProjectId;
45}): Effect.Effect<OrchestrationProject, OrchestrationCommandInvariantError> {
46 const project = findProjectById(input.readModel, input.projectId);
47 if (project) {
48 return Effect.succeed(project);
49 }
50 return Effect.fail(
51 invariantError(
52 input.command.type,
53 `Project '${input.projectId}' does not exist for command '${input.command.type}'.`,
54 ),
55 );
57​
58export function requireProjectAbsent(input: {
59 readonly readModel: OrchestrationReadModel;
60 readonly command: OrchestrationCommand;
61 readonly projectId: ProjectId;
62}): Effect.Effect<void, OrchestrationCommandInvariantError> {
63 if (!findProjectById(input.readModel, input.projectId)) {
64 return Effect.void;
65 }
66 return Effect.fail(
67 invariantError(
68 input.command.type,
69 `Project '${input.projectId}' already exists and cannot be created twice.`,
70 ),
71 );
73​
74export function requireActiveProjectWorkspaceRootAbsent(input: {
75 readonly readModel: OrchestrationReadModel;
76 readonly command: OrchestrationCommand;
77 readonly workspaceRoot: string;
78 readonly exceptProjectId?: ProjectId;
79}): Effect.Effect<void, OrchestrationCommandInvariantError> {
80 const normalizedWorkspaceRoot = normalizeProjectPathForComparison(input.workspaceRoot);
81 const existingProject = input.readModel.projects.find(
82 (project) =>
83 project.deletedAt === null &&
84 normalizeProjectPathForComparison(project.workspaceRoot) === normalizedWorkspaceRoot &&
85 project.id !== input.exceptProjectId,
86 );
87 if (existingProject === undefined) {
88 return Effect.void;
89 }
90 return Effect.fail(
91 invariantError(
92 input.command.type,
93 `Active project '${existingProject.id}' already exists for workspace root '${normalizedWorkspaceRoot}'.`,
94 ),
95 );
97​
98export function requireThread(input: {
99 readonly readModel: OrchestrationReadModel;
100 readonly command: OrchestrationCommand;
101 readonly threadId: ThreadId;
102}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
103 const thread = findThreadById(input.readModel, input.threadId);
104 if (thread) {
105 return Effect.succeed(thread);
106 }
107 return Effect.fail(
108 invariantError(
109 input.command.type,
110 `Thread '${input.threadId}' does not exist for command '${input.command.type}'.`,
111 ),
112 );
114​
115export function requireThreadArchived(input: {
116 readonly readModel: OrchestrationReadModel;
117 readonly command: OrchestrationCommand;
118 readonly threadId: ThreadId;
119}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
120 return requireThread(input).pipe(
121 Effect.flatMap((thread) =>
122 thread.archivedAt !== null
123 ? Effect.succeed(thread)
124 : Effect.fail(
125 invariantError(
126 input.command.type,
127 `Thread '${input.threadId}' is not archived for command '${input.command.type}'.`,
128 ),
129 ),
130 ),
131 );
133​
134export function requireThreadNotArchived(input: {
135 readonly readModel: OrchestrationReadModel;
136 readonly command: OrchestrationCommand;
137 readonly threadId: ThreadId;
138}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
139 return requireThread(input).pipe(
140 Effect.flatMap((thread) =>
141 thread.archivedAt === null
142 ? Effect.succeed(thread)
143 : Effect.fail(
144 invariantError(
145 input.command.type,
146 `Thread '${input.threadId}' is already archived and cannot handle command '${input.command.type}'.`,
147 ),
148 ),
149 ),
150 );
152​
153export function requireThreadAbsent(input: {
154 readonly readModel: OrchestrationReadModel;
155 readonly command: OrchestrationCommand;
156 readonly threadId: ThreadId;
157}): Effect.Effect<void, OrchestrationCommandInvariantError> {
158 // Thread deletion is a soft delete and a draft keeps its client-minted id
159 // across retries, so only a live row blocks creation. Projectors reset the
160 // thread's rows when the id is created again.
161 const existing = findThreadById(input.readModel, input.threadId);
162 if (existing === undefined || existing.deletedAt !== null) {
163 return Effect.void;
164 }
165 return Effect.fail(
166 invariantError(
167 input.command.type,
168 `Thread '${input.threadId}' already exists and cannot be created twice.`,
169 ),
170 );