-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
270 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import { database } from "./database"; | ||
import { email } from "./email"; | ||
import { allSecrets } from "./secret"; | ||
|
||
export const bus = new sst.aws.Bus("Bus"); | ||
|
||
bus.subscribe({ | ||
handler: "./packages/functions/src/event/event.handler", | ||
link: [database, ...allSecrets], | ||
link: [database, email, ...allSecrets], | ||
permissions: [ | ||
{ | ||
actions: ["ses:SendEmail"], | ||
resources: ["*"], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
if ($app.stage === "production") { | ||
const zone = cloudflare.getZoneOutput({ | ||
name: "trm.sh", | ||
}); | ||
new cloudflare.PageRule("ShortRedirect", { | ||
zoneId: zone.id, | ||
target: "trm.sh/*", | ||
actions: { | ||
forwardingUrl: { | ||
url: "https://terminal.shop/$1", | ||
statusCode: 301, | ||
}, | ||
}, | ||
priority: 1, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { SESv2Client, SendEmailCommand } from "@aws-sdk/client-sesv2"; | ||
import { Resource } from "sst"; | ||
|
||
export module Email { | ||
const ses = new SESv2Client({}); | ||
|
||
export async function send( | ||
from: string, | ||
to: string, | ||
subject: string, | ||
body: string, | ||
) { | ||
from = from + "@" + Resource.Email.sender; | ||
console.log("sending email", subject, from, to); | ||
const params = { | ||
Destination: { | ||
ToAddresses: [to], | ||
}, | ||
Content: { | ||
Simple: { | ||
Body: { | ||
Html: { | ||
Data: body, | ||
}, | ||
}, | ||
Subject: { | ||
Data: subject, | ||
}, | ||
}, | ||
}, | ||
FromEmailAddress: from, | ||
}; | ||
|
||
const command = new SendEmailCommand(params); | ||
await ses.send(command); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Sample } from "@terminal/email/templates/email"; | ||
import { useTransaction } from "@terminal/core/drizzle/transaction"; | ||
import { render as renderTemplate } from "jsx-email"; | ||
import { userTable } from "../user/user.sql"; | ||
import { orderTable } from "../order/order.sql"; | ||
import { and, count, eq, lte, sql } from "drizzle-orm"; | ||
import { Email } from "./index"; | ||
export module Template { | ||
const templates = { | ||
Sample, | ||
}; | ||
|
||
export function render<Name extends keyof typeof templates>( | ||
name: Name, | ||
data: Parameters<(typeof templates)[Name]>[0], | ||
) { | ||
return renderTemplate(templates[name](data)); | ||
} | ||
|
||
export async function sendOrderConfirmation(orderID: string) { | ||
const order = await useTransaction((tx) => | ||
tx | ||
.select({ | ||
email: userTable.email, | ||
name: userTable.name, | ||
index: sql<string>`${tx | ||
.select({ index: count() }) | ||
.from(orderTable) | ||
.where( | ||
and( | ||
eq(orderTable.userID, userTable.id), | ||
lte(orderTable.id, orderID), | ||
), | ||
)}`, | ||
}) | ||
.from(orderTable) | ||
.innerJoin(userTable, eq(userTable.id, orderTable.userID)) | ||
.where(eq(orderTable.id, orderID)) | ||
.then((rows) => rows[0]), | ||
); | ||
if (!order) throw new Error(`Order not found: ${orderID}`); | ||
const template = await render("Sample", { | ||
email: order.email!, | ||
name: order.name!, | ||
}); | ||
await Email.send( | ||
"order", | ||
order.email!, | ||
`Terminal Order #${order.index} Confirmation`, | ||
template, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
node_modules | ||
|
||
# env | ||
.env | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
.pnpm-debug.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "@terminal/email", | ||
"version": "0.0.0", | ||
"private": true, | ||
"description": "A simple starter for jsx-email", | ||
"scripts": { | ||
"build": "email build ./templates", | ||
"create": "email create", | ||
"dev": "email preview ./templates" | ||
}, | ||
"dependencies": { | ||
"jsx-email": "^1.10.11" | ||
}, | ||
"devDependencies": { | ||
"react": "^18.2.0", | ||
"@types/react": "^18.2.0", | ||
"typescript": "^5.2.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { | ||
Body, | ||
Button, | ||
Container, | ||
Head, | ||
Hr, | ||
Html, | ||
Preview, | ||
Section, | ||
Text, | ||
} from "jsx-email"; | ||
|
||
export type TemplateProps = { | ||
email: string; | ||
name: string; | ||
}; | ||
|
||
const main = { | ||
backgroundColor: "#f6f9fc", | ||
fontFamily: | ||
'-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Ubuntu,sans-serif', | ||
}; | ||
|
||
const container = { | ||
backgroundColor: "#ffffff", | ||
margin: "0 auto", | ||
marginBottom: "64px", | ||
padding: "20px 0 48px", | ||
}; | ||
|
||
const box = { | ||
padding: "0 48px", | ||
}; | ||
|
||
const hr = { | ||
borderColor: "#e6ebf1", | ||
margin: "20px 0", | ||
}; | ||
|
||
const paragraph = { | ||
color: "#777", | ||
fontSize: "16px", | ||
lineHeight: "24px", | ||
textAlign: "left" as const, | ||
}; | ||
|
||
const anchor = { | ||
color: "#777", | ||
}; | ||
|
||
const button = { | ||
backgroundColor: "#777", | ||
borderRadius: "5px", | ||
color: "#fff", | ||
display: "block", | ||
fontSize: "16px", | ||
fontWeight: "bold", | ||
textAlign: "center" as const, | ||
textDecoration: "none", | ||
width: "100%", | ||
padding: "10px", | ||
}; | ||
|
||
export const defaultProps = {} as TemplateProps; | ||
|
||
export const templateName = "email"; | ||
|
||
export const Sample = (_props: TemplateProps) => ( | ||
<Html> | ||
<Head /> | ||
<Preview>Order Confirmed</Preview> | ||
<Body style={main}> | ||
<Container style={container}> | ||
<Section style={box}> | ||
<Text style={paragraph}>Order Confirmed</Text> | ||
<Button style={button}>This button does nothing</Button> | ||
<Hr style={hr} /> | ||
<Text style={paragraph}> | ||
Look we're still working on this email template okay? | ||
</Text> | ||
</Section> | ||
</Container> | ||
</Body> | ||
</Html> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"declarationMap": true, | ||
"esModuleInterop": true, | ||
"jsx": "react-jsx", | ||
"lib": ["ES2023"], | ||
"module": "ESNext", | ||
"moduleResolution": "node", | ||
"noEmitOnError": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"preserveSymlinks": true, | ||
"preserveWatchOutput": true, | ||
"resolveJsonModule": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"strictNullChecks": true, | ||
"target": "ESNext" | ||
}, | ||
"exclude": ["**/dist", "**/node_modules"], | ||
"include": ["templates"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters