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

commandInvariants.ts

167 lines5.4 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​
20function findThreadById(
21 readModel: OrchestrationReadModel,
22 threadId: ThreadId,
23): OrchestrationThread | undefined {
24 return readModel.threads.find((thread) => thread.id === threadId);
26​
27function 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.filterOrFail(
122 (thread) => thread.archivedAt !== null,
123 () =>
124 invariantError(
125 input.command.type,
126 `Thread '${input.threadId}' is not archived for command '${input.command.type}'.`,
127 ),
128 ),
129 );
131​
132export function requireThreadNotArchived(input: {
133 readonly readModel: OrchestrationReadModel;
134 readonly command: OrchestrationCommand;
135 readonly threadId: ThreadId;
136}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
137 return requireThread(input).pipe(
138 Effect.filterOrFail(
139 (thread) => thread.archivedAt === null,
140 () =>
141 invariantError(
142 input.command.type,
143 `Thread '${input.threadId}' is already archived and cannot handle command '${input.command.type}'.`,
144 ),
145 ),
146 );
148​
149export function requireThreadAbsent(input: {
150 readonly readModel: OrchestrationReadModel;
151 readonly command: OrchestrationCommand;
152 readonly threadId: ThreadId;
153}): Effect.Effect<void, OrchestrationCommandInvariantError> {
154 // Thread deletion is a soft delete and a draft keeps its client-minted id
155 // across retries, so only a live row blocks creation. Projectors reset the
156 // thread's rows when the id is created again.
157 const existing = findThreadById(input.readModel, input.threadId);
158 if (existing === undefined || existing.deletedAt !== null) {
159 return Effect.void;
160 }
161 return Effect.fail(
162 invariantError(
163 input.command.type,
164 `Thread '${input.threadId}' already exists and cannot be created twice.`,
165 ),
166 );