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
| 1 | import * as sesv2 from "@distilled.cloud/aws/sesv2"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { createInternalTags, createTagsList, diffTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { AWSEnvironment } from "../Environment.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | |
| 13 | export interface EmailTemplateProps { |
| 14 | /** |
| 15 | * The name of the template. If omitted, a deterministic physical name is |
| 16 | * generated from the app, stage, and logical ID. Changing the name |
| 17 | * replaces the template. |
| 18 | */ |
| 19 | templateName?: string; |
| 20 | /** |
| 21 | * The subject line, with optional `{{variable}}` personalization tags. |
| 22 | */ |
| 23 | subject?: string; |
| 24 | /** |
| 25 | * The plain-text body, with optional `{{variable}}` personalization tags. |
| 26 | */ |
| 27 | text?: string; |
| 28 | /** |
| 29 | * The HTML body, with optional `{{variable}}` personalization tags. |
| 30 | */ |
| 31 | html?: string; |
| 32 | /** |
| 33 | * Tags to apply to the template. Merged with internal Alchemy tags. |
| 34 | */ |
| 35 | tags?: Record<string, string>; |
| 36 | } |
| 37 | |
| 38 | export interface EmailTemplate extends Resource< |
| 39 | "AWS.SES.EmailTemplate", |
| 40 | EmailTemplateProps, |
| 41 | { |
| 42 | templateName: string; |
| 43 | templateArn: string; |
| 44 | }, |
| 45 | never, |
| 46 | Providers |
| 47 | > {} |
| 48 | |
| 49 | /** |
| 50 | * An Amazon SES v2 email template — reusable subject/text/HTML content with |
| 51 | * `{{variable}}` personalization tags, rendered server-side when you send |
| 52 | * templated email. |
| 53 | * ### Creating Templates |
| 54 | * **Example:** Welcome Email Template |
| 55 | * ```typescript |
| 56 | * import * as SES from "@alchemy.run/aws/SES"; |
| 57 | * |
| 58 | * const template = yield* SES.EmailTemplate("Welcome", { |
| 59 | * subject: "Welcome, {{name}}!", |
| 60 | * text: "Hi {{name}}, thanks for signing up.", |
| 61 | * html: "<h1>Hi {{name}}</h1><p>Thanks for signing up.</p>", |
| 62 | * }); |
| 63 | * ``` |
| 64 | * |
| 65 | * ### Sending Templated Email |
| 66 | * **Example:** Send with Template Data |
| 67 | * ```typescript |
| 68 | * const sendEmail = yield* SES.SendEmail(identity); |
| 69 | * |
| 70 | * const result = yield* sendEmail({ |
| 71 | * Destination: { ToAddresses: ["customer@example.com"] }, |
| 72 | * Content: { |
| 73 | * Template: { |
| 74 | * TemplateName: "my-welcome-template", |
| 75 | * TemplateData: JSON.stringify({ name: "Ada" }), |
| 76 | * }, |
| 77 | * }, |
| 78 | * }); |
| 79 | * ``` |
| 80 | * |
| 81 | * @resource |
| 82 | */ |
| 83 | export const EmailTemplate = Resource<EmailTemplate>("AWS.SES.EmailTemplate"); |
| 84 | |
| 85 | const toTagRecord = ( |
| 86 | tags: ReadonlyArray<{ Key: string; Value: string }> | undefined, |
| 87 | ): Record<string, string> => Object.fromEntries((tags ?? []).map((tag) => [tag.Key, tag.Value])); |
| 88 | |
| 89 | const templateArnOf = (region: string, accountId: string, name: string) => |
| 90 | `arn:aws:ses:${region}:${accountId}:template/${name}`; |
| 91 | |
| 92 | const toContent = (props: EmailTemplateProps): sesv2.EmailTemplateContent => ({ |
| 93 | Subject: props.subject, |
| 94 | Text: props.text, |
| 95 | Html: props.html, |
| 96 | }); |
| 97 | |
| 98 | export const EmailTemplateProvider = () => |
| 99 | Provider.effect( |
| 100 | EmailTemplate, |
| 101 | Effect.gen(function* () { |
| 102 | const createName = Effect.fn(function* ( |
| 103 | id: string, |
| 104 | props: Pick<EmailTemplateProps, "templateName">, |
| 105 | ) { |
| 106 | return props.templateName ?? (yield* createPhysicalName({ id, maxLength: 64 })); |
| 107 | }); |
| 108 | |
| 109 | const getTemplate = Effect.fn(function* (name: string) { |
| 110 | return yield* sesv2 |
| 111 | .getEmailTemplate({ TemplateName: name }) |
| 112 | .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined)); |
| 113 | }); |
| 114 | |
| 115 | return EmailTemplate.Provider.of({ |
| 116 | stables: ["templateName", "templateArn"], |
| 117 | |
| 118 | list: () => |
| 119 | Effect.gen(function* () { |
| 120 | const { accountId, region } = yield* AWSEnvironment.current; |
| 121 | const pages = yield* sesv2.listEmailTemplates.pages({}).pipe(Stream.runCollect); |
| 122 | return Array.from(pages) |
| 123 | .flatMap((page) => page.TemplatesMetadata ?? []) |
| 124 | .filter( |
| 125 | (meta): meta is typeof meta & { TemplateName: string } => meta.TemplateName != null, |
| 126 | ) |
| 127 | .map((meta) => ({ |
| 128 | templateName: meta.TemplateName, |
| 129 | templateArn: templateArnOf(region, accountId, meta.TemplateName), |
| 130 | })); |
| 131 | }), |
| 132 | |
| 133 | read: Effect.fn(function* ({ id, olds, output }) { |
| 134 | const { accountId, region } = yield* AWSEnvironment.current; |
| 135 | const name = output?.templateName ?? (yield* createName(id, olds ?? {})); |
| 136 | const found = yield* getTemplate(name); |
| 137 | if (!found) return undefined; |
| 138 | const attrs = { |
| 139 | templateName: name, |
| 140 | templateArn: templateArnOf(region, accountId, name), |
| 141 | }; |
| 142 | const tags = toTagRecord(found.Tags); |
| 143 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 144 | }), |
| 145 | |
| 146 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 147 | if (!isResolved(news)) return undefined; |
| 148 | const oldName = yield* createName(id, olds ?? {}); |
| 149 | const newName = yield* createName(id, news ?? {}); |
| 150 | if (oldName !== newName) { |
| 151 | return { action: "replace" } as const; |
| 152 | } |
| 153 | }), |
| 154 | |
| 155 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 156 | const { accountId, region } = yield* AWSEnvironment.current; |
| 157 | const name = output?.templateName ?? (yield* createName(id, news)); |
| 158 | const templateArn = templateArnOf(region, accountId, name); |
| 159 | const internalTags = yield* createInternalTags(id); |
| 160 | const desiredTags = { ...news.tags, ...internalTags }; |
| 161 | const desiredContent = toContent(news); |
| 162 | |
| 163 | // 1. OBSERVE |
| 164 | let observed = yield* getTemplate(name); |
| 165 | |
| 166 | if (observed === undefined) { |
| 167 | // 2. ENSURE — AlreadyExists is a race → converge via update. |
| 168 | yield* sesv2 |
| 169 | .createEmailTemplate({ |
| 170 | TemplateName: name, |
| 171 | TemplateContent: desiredContent, |
| 172 | Tags: createTagsList(desiredTags), |
| 173 | }) |
| 174 | .pipe( |
| 175 | Effect.catchTag("AlreadyExistsException", () => |
| 176 | sesv2.updateEmailTemplate({ |
| 177 | TemplateName: name, |
| 178 | TemplateContent: desiredContent, |
| 179 | }), |
| 180 | ), |
| 181 | ); |
| 182 | observed = yield* sesv2.getEmailTemplate({ TemplateName: name }); |
| 183 | } else if ( |
| 184 | observed.TemplateContent.Subject !== desiredContent.Subject || |
| 185 | observed.TemplateContent.Text !== desiredContent.Text || |
| 186 | observed.TemplateContent.Html !== desiredContent.Html |
| 187 | ) { |
| 188 | // 3. SYNC content — only when the observed content drifted. |
| 189 | yield* sesv2.updateEmailTemplate({ |
| 190 | TemplateName: name, |
| 191 | TemplateContent: desiredContent, |
| 192 | }); |
| 193 | } |
| 194 | |
| 195 | // 3b. SYNC TAGS — diff against OBSERVED cloud tags. |
| 196 | const observedTags = toTagRecord(observed.Tags); |
| 197 | const { upsert, removed } = diffTags(observedTags, desiredTags); |
| 198 | if (upsert.length > 0) { |
| 199 | yield* sesv2.tagResource({ |
| 200 | ResourceArn: templateArn, |
| 201 | Tags: upsert, |
| 202 | }); |
| 203 | } |
| 204 | if (removed.length > 0) { |
| 205 | yield* sesv2.untagResource({ |
| 206 | ResourceArn: templateArn, |
| 207 | TagKeys: removed, |
| 208 | }); |
| 209 | } |
| 210 | |
| 211 | yield* session.note(templateArn); |
| 212 | return { templateName: name, templateArn }; |
| 213 | }), |
| 214 | |
| 215 | delete: Effect.fn(function* ({ output }) { |
| 216 | yield* sesv2 |
| 217 | .deleteEmailTemplate({ TemplateName: output.templateName }) |
| 218 | .pipe(Effect.catchTag("NotFoundException", () => Effect.void)); |
| 219 | }), |
| 220 | }); |
| 221 | }), |
| 222 | ); |