Open History to see the last commit
1import {
2 AuthOrchestrationReadScope,
3 EnvironmentHttpApi,
4 requiredScopesForProjectMutation,
5} from "@t3tools/contracts";
6import * as Effect from "effect/Effect";
7import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
8
9import {
10 annotateEnvironmentRequest,
11 failEnvironmentInternal,
12 failEnvironmentInvalidRequest,
13 requireEnvironmentScope,
14} from "../auth/http.ts";
15import { traceLocalHandlerWork } from "../cloud/traceRelayRequest.ts";
16import * as ServerRuntimeStartup from "../serverRuntimeStartup.ts";
17import * as ProjectCloneTracker from "./ProjectCloneTracker.ts";
18import * as ProjectService from "./ProjectService.ts";
19import { mutateProjectWithClone } from "./ProjectMutation.ts";
20
21const failProjectMutation = Effect.fn("environment.projects.failMutation")(function* (
22 cause: ProjectService.ProjectServiceError | ServerRuntimeStartup.ServerRuntimeStartupError,
23) {
24 if (
25 cause._tag === "ProjectNotFoundError" ||
26 cause._tag === "ProjectConflictError" ||
27 cause._tag === "ProjectNotEmptyError"
28 ) {
29 return yield* failEnvironmentInvalidRequest("invalid_command");
30 }
31 return yield* failEnvironmentInternal("project_mutation_failed", cause);
32});
33
34export const layer = HttpApiBuilder.group(
35 EnvironmentHttpApi,
36 "projects",
37 Effect.fnUntraced(function* (handlers) {
38 const projects = yield* ProjectService.ProjectService;
39 const startup = yield* ServerRuntimeStartup.ServerRuntimeStartup;
40 const projectClones = yield* ProjectCloneTracker.ProjectCloneTracker;
41
42 return handlers
43 .handle(
44 "snapshot",
45 Effect.fn("environment.projects.snapshot")(function* (args) {
46 yield* annotateEnvironmentRequest(args.endpoint.name);
47 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
48 return yield* projects.snapshot.pipe(
49 traceLocalHandlerWork,
50 Effect.catch((cause) => failEnvironmentInternal("project_snapshot_failed", cause)),
51 );
52 }),
53 )
54 .handle(
55 "mutate",
56 Effect.fn("environment.projects.mutate")(function* (args) {
57 yield* annotateEnvironmentRequest(args.endpoint.name);
58 for (const scope of requiredScopesForProjectMutation(args.payload)) {
59 yield* requireEnvironmentScope(scope);
60 }
61 const operation = mutateProjectWithClone(projects, projectClones, args.payload);
62 return yield* startup
63 .enqueueCommand(operation)
64 .pipe(traceLocalHandlerWork, Effect.catch(failProjectMutation));
65 }),
66 );
67 }),
68);