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

commandInvariants.ts

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