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