@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 182 lines 6.45 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as PinpointSMSVoiceV2 from "@alchemy.run/aws/PinpointSMSVoiceV2";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "optout-handler.ts");
11
12 /**
13 * Destination number the fixture opts out/in — the US simulator success
14 * number, so no real end user is ever affected.
15 */
16 export const TEST_DESTINATION = "+14254147755";
17
18 export class SmsVoiceOptOutTestFunction extends Lambda.Function<Lambda.Function>()(
19 "SmsVoiceOptOutTestFunction",
20 ) {}
21
22 export default SmsVoiceOptOutTestFunction.make(
23 {
24 main,
25 functionUrl: true,
26 timeout: Duration.seconds(30),
27 },
28 Effect.gen(function* () {
29 const optOuts = yield* PinpointSMSVoiceV2.OptOutList("BindingsOptOuts", {
30 tags: { fixture: "smsvoice-bindings" },
31 });
32
33 const putOptedOut = yield* PinpointSMSVoiceV2.PutOptedOutNumber(optOuts);
34 const describeOptedOut = yield* PinpointSMSVoiceV2.DescribeOptedOutNumbers(optOuts);
35 const deleteOptedOut = yield* PinpointSMSVoiceV2.DeleteOptedOutNumber(optOuts);
36 const carrierLookup = yield* PinpointSMSVoiceV2.CarrierLookup();
37 const putFeedback = yield* PinpointSMSVoiceV2.PutMessageFeedback();
38
39 return {
40 fetch: Effect.gen(function* () {
41 const request = yield* HttpServerRequest;
42 const pathname = new URL(request.originalUrl).pathname;
43
44 // Cheap readiness route — no AWS call.
45 if (request.method === "GET" && pathname === "/ping") {
46 return yield* HttpServerResponse.json({ ok: true });
47 }
48
49 if (request.method === "POST" && pathname === "/opt-out") {
50 const result = yield* putOptedOut({
51 OptedOutNumber: TEST_DESTINATION,
52 });
53 return yield* HttpServerResponse.json({
54 optedOutNumber: result.OptedOutNumber,
55 endUserOptedOut: result.EndUserOptedOut,
56 });
57 }
58
59 if (request.method === "POST" && pathname === "/opt-out-check") {
60 const result = yield* describeOptedOut({
61 OptedOutNumbers: [TEST_DESTINATION],
62 }).pipe(
63 Effect.map((r) => ({
64 count: (r.OptedOutNumbers ?? []).length,
65 numbers: (r.OptedOutNumbers ?? []).map((n) => n.OptedOutNumber),
66 })),
67 // Filtering for a number that isn't in the list raises the
68 // typed not-found tag — semantically "0 opted out".
69 Effect.catchTag("ResourceNotFoundException", () =>
70 Effect.succeed({
71 count: 0,
72 numbers: [] as (string | undefined)[],
73 }),
74 ),
75 );
76 return yield* HttpServerResponse.json(result);
77 }
78
79 if (request.method === "POST" && pathname === "/opt-out-delete") {
80 const result = yield* deleteOptedOut({
81 OptedOutNumber: TEST_DESTINATION,
82 }).pipe(
83 Effect.map((r) => ({
84 ok: true as const,
85 deleted: r.OptedOutNumber,
86 })),
87 Effect.catchTag(
88 [
89 "AccessDeniedException",
90 "ResourceNotFoundException",
91 "ValidationException",
92 "ThrottlingException",
93 "InternalServerException",
94 ],
95 (e) =>
96 Effect.succeed({
97 ok: false as const,
98 tag: e._tag,
99 message: e.message,
100 }),
101 ),
102 );
103 return yield* HttpServerResponse.json(result);
104 }
105
106 if (request.method === "POST" && pathname === "/carrier-lookup") {
107 // The lookup may reject the simulator number as unsupported —
108 // surface the typed tag so the test can assert IAM allowed the
109 // call (an AccessDeniedException would prove a broken grant).
110 const result = yield* carrierLookup({
111 PhoneNumber: TEST_DESTINATION,
112 }).pipe(
113 Effect.map((info) => ({
114 ok: true as const,
115 e164PhoneNumber: info.E164PhoneNumber,
116 phoneNumberType: info.PhoneNumberType,
117 })),
118 Effect.catchTag(
119 [
120 "AccessDeniedException",
121 "ValidationException",
122 "ServiceQuotaExceededException",
123 "ThrottlingException",
124 "InternalServerException",
125 ],
126 (e) =>
127 Effect.succeed({
128 ok: false as const,
129 tag: e._tag,
130 message: e.message,
131 }),
132 ),
133 );
134 return yield* HttpServerResponse.json(result);
135 }
136
137 if (request.method === "POST" && pathname === "/feedback-probe") {
138 // No real MessageId exists ungated — assert the typed
139 // ResourceNotFoundException surfaces, proving the grant and the
140 // request wiring (a broken grant would AccessDeniedException).
141 const result = yield* putFeedback({
142 MessageId: "00000000-0000-0000-0000-000000000000",
143 MessageFeedbackStatus: "RECEIVED",
144 }).pipe(
145 Effect.map((r) => ({ ok: true as const, messageId: r.MessageId })),
146 Effect.catchTag(
147 [
148 "AccessDeniedException",
149 "ResourceNotFoundException",
150 "ValidationException",
151 "ThrottlingException",
152 "InternalServerException",
153 ],
154 (e) =>
155 Effect.succeed({
156 ok: false as const,
157 tag: e._tag,
158 message: e.message,
159 }),
160 ),
161 );
162 return yield* HttpServerResponse.json(result);
163 }
164
165 return yield* HttpServerResponse.json(
166 { error: "Not found", method: request.method, pathname },
167 { status: 404 },
168 );
169 }).pipe(Effect.orDie),
170 };
171 }).pipe(
172 Effect.provide(
173 Layer.mergeAll(
174 PinpointSMSVoiceV2.PutOptedOutNumberHttp,
175 PinpointSMSVoiceV2.DescribeOptedOutNumbersHttp,
176 PinpointSMSVoiceV2.DeleteOptedOutNumberHttp,
177 PinpointSMSVoiceV2.CarrierLookupHttp,
178 PinpointSMSVoiceV2.PutMessageFeedbackHttp,
179 ),
180 ),
181 ),
182 );