Open History to see the last commit
1import {
2 ClientOrchestrationCommand,
3 OrchestrationDispatchCommandError,
4 OrchestrationGetSnapshotError,
5 type OrchestrationReadModel,
6} from "@t3tools/contracts";
7import { Effect } from "effect";
8import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http";
9
10import { ServerAuth } from "../auth/Services/ServerAuth.ts";
11import { normalizeDispatchCommand } from "./Normalizer.ts";
12import { OrchestrationEngineService } from "./Services/OrchestrationEngine.ts";
13import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts";
14
15const respondToOrchestrationHttpError = (
16 error: OrchestrationDispatchCommandError | OrchestrationGetSnapshotError,
17) =>
18 Effect.gen(function* () {
19 if (error._tag === "OrchestrationGetSnapshotError") {
20 yield* Effect.logError("orchestration http route failed", {
21 message: error.message,
22 cause: error.cause,
23 });
24 return HttpServerResponse.jsonUnsafe({ error: error.message }, { status: 500 });
25 }
26
27 return HttpServerResponse.jsonUnsafe({ error: error.message }, { status: 400 });
28 });
29
30const authenticateOwnerSession = Effect.gen(function* () {
31 const request = yield* HttpServerRequest.HttpServerRequest;
32 const serverAuth = yield* ServerAuth;
33 const session = yield* serverAuth.authenticateHttpRequest(request);
34 if (session.role !== "owner") {
35 return yield* new OrchestrationDispatchCommandError({
36 message: "Only owner sessions can manage projects.",
37 });
38 }
39 return session;
40});
41
42export const orchestrationSnapshotRouteLayer = HttpRouter.add(
43 "GET",
44 "/api/orchestration/snapshot",
45 Effect.gen(function* () {
46 yield* authenticateOwnerSession;
47 const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
48 const snapshot = yield* projectionSnapshotQuery.getSnapshot().pipe(
49 Effect.mapError(
50 (cause) =>
51 new OrchestrationGetSnapshotError({
52 message: "Failed to load orchestration snapshot.",
53 cause,
54 }),
55 ),
56 );
57 return HttpServerResponse.jsonUnsafe(snapshot satisfies OrchestrationReadModel, {
58 status: 200,
59 });
60 }).pipe(
61 Effect.catchTag("OrchestrationDispatchCommandError", respondToOrchestrationHttpError),
62 Effect.catchTag("OrchestrationGetSnapshotError", respondToOrchestrationHttpError),
63 ),
64);
65
66export const orchestrationDispatchRouteLayer = HttpRouter.add(
67 "POST",
68 "/api/orchestration/dispatch",
69 Effect.gen(function* () {
70 yield* authenticateOwnerSession;
71 const orchestrationEngine = yield* OrchestrationEngineService;
72 const command = yield* HttpServerRequest.schemaBodyJson(ClientOrchestrationCommand).pipe(
73 Effect.mapError(
74 (cause) =>
75 new OrchestrationDispatchCommandError({
76 message: "Invalid orchestration command payload.",
77 cause,
78 }),
79 ),
80 );
81 const normalizedCommand = yield* normalizeDispatchCommand(command);
82 const result = yield* orchestrationEngine.dispatch(normalizedCommand).pipe(
83 Effect.mapError(
84 (cause) =>
85 new OrchestrationDispatchCommandError({
86 message: "Failed to dispatch orchestration command.",
87 cause,
88 }),
89 ),
90 );
91 return HttpServerResponse.jsonUnsafe(result, { status: 200 });
92 }).pipe(Effect.catchTag("OrchestrationDispatchCommandError", respondToOrchestrationHttpError)),
93);