1import * as recaptchaenterprise from "@distilled.cloud/gcp/recaptchaenterprise_v1";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import {
5 CreateAssessment,
6 type CreateAssessmentRequest,
7} from "./CreateAssessment.ts";
8import { lastSegment } from "./internal.ts";
9import type { Key } from "./Key.ts";
10import { bindGcpHost } from "../Host.ts";
11
12/**
13 * HTTP implementation of {@link CreateAssessment}.
14 *
15 * @layer
16 * @provides GCP.RecaptchaEnterprise.CreateAssessment
17 */
18export const CreateAssessmentHttp = Layer.effect(
19 CreateAssessment,
20 Effect.gen(function* () {
21 const createProjectsAssessments =
22 yield* recaptchaenterprise.createProjectsAssessments;
23 return Effect.fn(function* <K extends Key>(key: K) {
24 yield* bindGcpHost({
25 tag: "GCP.RecaptchaEnterprise.CreateAssessment",
26 resource: key,
27 // reCAPTCHA keys have no resource-level IAM.
28 iam: [{ role: "roles/recaptchaenterprise.agent" }],
29 });
30 const name = yield* key.name;
31 return Effect.fn(
32 `GCP.RecaptchaEnterprise.CreateAssessment(${key.LogicalId})`,
33 )(function* (request?: CreateAssessmentRequest) {
34 const keyName = yield* name;
35 const parts = keyName.split("/").filter((part) => part.length > 0);
36 const projectAt = parts.lastIndexOf("projects");
37 const project =
38 projectAt >= 0 && parts[projectAt + 1] ? parts[projectAt + 1]! : "";
39 const siteKey = lastSegment(keyName);
40 return yield* createProjectsAssessments({
41 parent: `projects/${project}`,
42 body: {
43 ...request?.body,
44 event: {
45 ...request?.body?.event,
46 siteKey: request?.body?.event?.siteKey ?? siteKey,
47 },
48 },
49 });
50 });
51 });
52 }),
53);