-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ implement cronjob and db logic for sending emails (#25)
* feat: ✨ implement cronjob and db logic for sending emails * fix: 🐛 email database schema * fix: 🐛 update mimetext for Mailbox class --------- Co-authored-by: Alexander Liu <[email protected]>
- Loading branch information
1 parent
9166365
commit 78ee809
Showing
7 changed files
with
56 additions
and
44 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,30 @@ | ||
import { asc, eq } from "drizzle-orm"; | ||
import { drizzle } from "drizzle-orm/postgres-js"; | ||
import { google } from "googleapis"; | ||
import postgres from "postgres"; | ||
|
||
import { email } from "../src/lib/db/schema"; | ||
|
||
async function main() { | ||
const { DATABASE_URL, GOOGLE_OAUTH_CLIENT_ID, GOOGLE_OAUTH_CLIENT_SECRET, MAILER_REFRESH_TOKEN } = | ||
process.env; | ||
const db = drizzle(postgres(DATABASE_URL, { max: 1, ssl: { rejectUnauthorized: false } }), { | ||
schema: { email }, | ||
}); | ||
const auth = new google.auth.OAuth2(GOOGLE_OAUTH_CLIENT_ID, GOOGLE_OAUTH_CLIENT_SECRET); | ||
auth.setCredentials({ refresh_token: MAILER_REFRESH_TOKEN }); | ||
const gmail = google.gmail({ version: "v1", auth }); | ||
const messages = await db | ||
.select({ id: email.id, raw: email.raw }) | ||
.from(email) | ||
.orderBy(asc(email.submittedAt)) | ||
.limit(15); | ||
for (const { id, raw } of messages) { | ||
await gmail.users.messages.send({ userId: "me", requestBody: { raw } }); | ||
await db.delete(email).where(eq(email.id, id)); | ||
await new Promise((_) => setTimeout(_, 1000)); | ||
} | ||
process.exit(0); | ||
} | ||
|
||
main().then(); |
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 was deleted.
Oops, something went wrong.
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,11 +1,11 @@ | ||
import { error, json } from "@sveltejs/kit"; | ||
import { createMimeMessage } from "mimetext"; | ||
import { createMimeMessage, Mailbox } from "mimetext"; | ||
|
||
import type { RequestHandler } from "./$types"; | ||
|
||
import { gmail } from "$lib/server/gmail"; | ||
import { email } from "$lib/db/schema"; | ||
import { drizzle } from "$lib/server/drizzle"; | ||
import { auth } from "$lib/server/lucia"; | ||
import { sleep } from "$lib/util/sleep"; | ||
|
||
type AttachmentBase = { | ||
filename: string; | ||
|
@@ -43,7 +43,7 @@ export const POST: RequestHandler = async (event) => { | |
({ attachments, htmlBody, plaintextBody, recipient, replyTo, subject }) => { | ||
const msg = createMimeMessage(); | ||
msg.setSender({ name: "ICS Student Council", addr: "[email protected]" }); | ||
msg.setHeader("Reply-To", { addr: replyTo }); | ||
msg.setHeader("Reply-To", new Mailbox(replyTo)); | ||
msg.setRecipient(recipient); | ||
msg.setSubject(subject); | ||
msg.addMessage({ contentType: "text/plain", data: plaintextBody }); | ||
|
@@ -61,9 +61,5 @@ export const POST: RequestHandler = async (event) => { | |
return { raw: msg.asEncoded() }; | ||
}, | ||
); | ||
for (const requestBody of messages) { | ||
await gmail.users.messages.send({ userId: "me", requestBody }); | ||
await sleep(1000); | ||
} | ||
return json({}); | ||
return json(await drizzle.insert(email).values(messages).returning({ id: email.id })); | ||
}; |
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