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 ProjectService from "./ProjectService.ts";
18import { projectMutationOperation } from "./ProjectMutation.ts";
19
20const failProjectMutation = Effect.fn("environment.projects.failMutation")(function* (
21 cause: ProjectService.ProjectServiceError | ServerRuntimeStartup.ServerRuntimeStartupError,
22) {
23 if (
24 cause._tag === "ProjectNotFoundError" ||
25 cause._tag === "ProjectConflictError" ||
26 cause._tag === "ProjectNotEmptyError"
27 ) {
28 return yield* failEnvironmentInvalidRequest("invalid_command");
29 }
30 return yield* failEnvironmentInternal("project_mutation_failed", cause);
31});
32
33export const layer = HttpApiBuilder.group(
34 EnvironmentHttpApi,
35 "projects",
36 Effect.fnUntraced(function* (handlers) {
37 const projects = yield* ProjectService.ProjectService;
38 const startup = yield* ServerRuntimeStartup.ServerRuntimeStartup;
39
40 return handlers
41 .handle(
42 "snapshot",
43 Effect.fn("environment.projects.snapshot")(function* (args) {
44 yield* annotateEnvironmentRequest(args.endpoint.name);
45 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
46 return yield* projects.snapshot.pipe(
47 traceLocalHandlerWork,
48 Effect.catch((cause) => failEnvironmentInternal("project_snapshot_failed", cause)),
49 );
50 }),
51 )
52 .handle(
53 "mutate",
54 Effect.fn("environment.projects.mutate")(function* (args) {
55 yield* annotateEnvironmentRequest(args.endpoint.name);
56 for (const scope of requiredScopesForProjectMutation(args.payload)) {
57 yield* requireEnvironmentScope(scope);
58 }
59 const operation = projectMutationOperation(projects, args.payload);
60 return yield* startup
61 .enqueueCommand(operation)
62 .pipe(traceLocalHandlerWork, Effect.catch(failProjectMutation));
63 }),
64 );
65 }),
66);