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

commandInvariants.ts

121 lines3.5 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 requireThreadAbsent(input: {
92 readonly readModel: OrchestrationReadModel;
93 readonly command: OrchestrationCommand;
94 readonly threadId: ThreadId;
95}): Effect.Effect<void, OrchestrationCommandInvariantError> {
96 if (!findThreadById(input.readModel, input.threadId)) {
97 return Effect.void;
98 }
99 return Effect.fail(
100 invariantError(
101 input.command.type,
102 `Thread '${input.threadId}' already exists and cannot be created twice.`,
103 ),
104 );
106​
107export function requireNonNegativeInteger(input: {
108 readonly commandType: OrchestrationCommand["type"];
109 readonly field: string;
110 readonly value: number;
111}): Effect.Effect<void, OrchestrationCommandInvariantError> {
112 if (Number.isInteger(input.value) && input.value >= 0) {
113 return Effect.void;
114 }
115 return Effect.fail(
116 invariantError(
117 input.commandType,
118 `${input.field} must be an integer greater than or equal to 0.`,
119 ),
120 );