Open History to see the last commit
1import {
2 AuthOrchestrationOperateScope,
3 AuthOrchestrationReadScope,
4 EnvironmentHttpApi,
5} from "@t3tools/contracts";
6import * as Effect from "effect/Effect";
7import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
8
9import {
10 annotateEnvironmentRequest,
11 failEnvironmentInternal,
12 requireEnvironmentScope,
13} from "../auth/http.ts";
14import * as ServerRuntimeStartup from "../serverRuntimeStartup.ts";
15import { ProjectService } from "./ProjectService.ts";
16
17export const projectHttpApiLayer = HttpApiBuilder.group(
18 EnvironmentHttpApi,
19 "projects",
20 Effect.fnUntraced(function* (handlers) {
21 const projects = yield* ProjectService;
22 const startup = yield* ServerRuntimeStartup.ServerRuntimeStartup;
23
24 return handlers
25 .handle(
26 "snapshot",
27 Effect.fn("environment.projects.snapshot")(function* (args) {
28 yield* annotateEnvironmentRequest(args.endpoint.name);
29 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
30 return yield* projects.snapshot.pipe(
31 Effect.catch((cause) => failEnvironmentInternal("project_snapshot_failed", cause)),
32 );
33 }),
34 )
35 .handle(
36 "mutate",
37 Effect.fn("environment.projects.mutate")(function* (args) {
38 yield* annotateEnvironmentRequest(args.endpoint.name);
39 yield* requireEnvironmentScope(AuthOrchestrationOperateScope);
40 const mutation = args.payload;
41 const operation =
42 mutation.type === "project.create"
43 ? projects.create({
44 commandId: mutation.commandId,
45 projectId: mutation.projectId,
46 title: mutation.title,
47 workspaceRoot: mutation.workspaceRoot,
48 ...(mutation.defaultModelSelection === undefined
49 ? {}
50 : { defaultModelSelection: mutation.defaultModelSelection }),
51 ...(mutation.scripts === undefined ? {} : { scripts: mutation.scripts }),
52 })
53 : mutation.type === "project.update"
54 ? projects.update({
55 commandId: mutation.commandId,
56 projectId: mutation.projectId,
57 ...(mutation.title === undefined ? {} : { title: mutation.title }),
58 ...(mutation.workspaceRoot === undefined
59 ? {}
60 : { workspaceRoot: mutation.workspaceRoot }),
61 ...(mutation.defaultModelSelection === undefined
62 ? {}
63 : { defaultModelSelection: mutation.defaultModelSelection }),
64 ...(mutation.scripts === undefined ? {} : { scripts: mutation.scripts }),
65 })
66 : projects.delete({
67 commandId: mutation.commandId,
68 projectId: mutation.projectId,
69 });
70 return yield* startup
71 .enqueueCommand(operation)
72 .pipe(
73 Effect.catch((cause) => failEnvironmentInternal("project_mutation_failed", cause)),
74 );
75 }),
76 );
77 }),
78);