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

commandInvariants.ts

159 lines4.7 KB

1import type {
2 OrchestrationCommand,
3 OrchestrationProject,
4 OrchestrationReadModel,
5 OrchestrationThread,
6 ProjectId,
7 ThreadId,
8} from "@t3tools/contracts";
9import { Effect } from "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 requireThread(input: {
75 readonly readModel: OrchestrationReadModel;
76 readonly command: OrchestrationCommand;
77 readonly threadId: ThreadId;
78}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
79 const thread = findThreadById(input.readModel, input.threadId);
80 if (thread) {
81 return Effect.succeed(thread);
82 }
83 return Effect.fail(
84 invariantError(
85 input.command.type,
86 `Thread '${input.threadId}' does not exist for command '${input.command.type}'.`,
87 ),
88 );
90​
91export function requireThreadArchived(input: {
92 readonly readModel: OrchestrationReadModel;
93 readonly command: OrchestrationCommand;
94 readonly threadId: ThreadId;
95}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
96 return requireThread(input).pipe(
97 Effect.flatMap((thread) =>
98 thread.archivedAt !== null
99 ? Effect.succeed(thread)
100 : Effect.fail(
101 invariantError(
102 input.command.type,
103 `Thread '${input.threadId}' is not archived for command '${input.command.type}'.`,
104 ),
105 ),
106 ),
107 );
109​
110export function requireThreadNotArchived(input: {
111 readonly readModel: OrchestrationReadModel;
112 readonly command: OrchestrationCommand;
113 readonly threadId: ThreadId;
114}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
115 return requireThread(input).pipe(
116 Effect.flatMap((thread) =>
117 thread.archivedAt === null
118 ? Effect.succeed(thread)
119 : Effect.fail(
120 invariantError(
121 input.command.type,
122 `Thread '${input.threadId}' is already archived and cannot handle command '${input.command.type}'.`,
123 ),
124 ),
125 ),
126 );
128​
129export function requireThreadAbsent(input: {
130 readonly readModel: OrchestrationReadModel;
131 readonly command: OrchestrationCommand;
132 readonly threadId: ThreadId;
133}): Effect.Effect<void, OrchestrationCommandInvariantError> {
134 if (!findThreadById(input.readModel, input.threadId)) {
135 return Effect.void;
136 }
137 return Effect.fail(
138 invariantError(
139 input.command.type,
140 `Thread '${input.threadId}' already exists and cannot be created twice.`,
141 ),
142 );
144​
145export function requireNonNegativeInteger(input: {
146 readonly commandType: OrchestrationCommand["type"];
147 readonly field: string;
148 readonly value: number;
149}): Effect.Effect<void, OrchestrationCommandInvariantError> {
150 if (Number.isInteger(input.value) && input.value >= 0) {
151 return Effect.void;
152 }
153 return Effect.fail(
154 invariantError(
155 input.commandType,
156 `${input.field} must be an integer greater than or equal to 0.`,
157 ),
158 );