Compare commits

...

3 Commits

Author SHA1 Message Date
joeac 3e14e8acbe adds OTP emails to SentEmails table 2026-01-24 10:09:47 +00:00
joeac 9c7ed56823 adds console warn when email exceeds max daily load 2026-01-24 10:09:31 +00:00
joeac 9c9b20a38a Stop sending emails at max, not after max 2026-01-24 10:08:45 +00:00
2 changed files with 12 additions and 1 deletions
+8 -1
View File
@@ -28,7 +28,10 @@ async function sendOtp({ email, name }: OtpParams) {
SentEmails, SentEmails,
gte(SentEmails.sentAt, Date.now() - 1000 * 60 * 60 * 24), gte(SentEmails.sentAt, Date.now() - 1000 * 60 * 60 * 24),
); );
if (emailsSentLast24Hours > MAX_DAILY_EMAILS) { if (emailsSentLast24Hours >= MAX_DAILY_EMAILS) {
console.warn(
`${name} <${email}> requested an OTP, but ${emailsSentLast24Hours} have already been sent, whereas the max daily load is ${MAX_DAILY_EMAILS}.`,
);
throw new Error( throw new Error(
`${emailsSentLast24Hours} emails have been sent in the last 24 hours, but the max daily load is ${MAX_DAILY_EMAILS}.`, `${emailsSentLast24Hours} emails have been sent in the last 24 hours, but the max daily load is ${MAX_DAILY_EMAILS}.`,
); );
@@ -47,6 +50,10 @@ to do anything.`,
`Sent OTP (${otpPretty}) to ${email}. Message ID: ${info.messageId}`, `Sent OTP (${otpPretty}) to ${email}. Message ID: ${info.messageId}`,
); );
await db
.insert(SentEmails)
.values({ messageId: info.messageId, sentAt: Date.now() });
await db.insert(Otp).values({ await db.insert(Otp).values({
userId: email, userId: email,
value: otp, value: otp,
+4
View File
@@ -61,6 +61,9 @@ async function sendmail({
gte(SentEmails.sentAt, Date.now() - 1000 * 60 * 60 * 24), gte(SentEmails.sentAt, Date.now() - 1000 * 60 * 60 * 24),
); );
if (emailsSentLast24Hours > MAX_DAILY_EMAILS) { if (emailsSentLast24Hours > MAX_DAILY_EMAILS) {
console.warn(
`${name} <${email}> tried to send an email, but ${emailsSentLast24Hours} have already been sent, whereas the max daily load is ${MAX_DAILY_EMAILS}.`,
);
throw new Error( throw new Error(
`${emailsSentLast24Hours} emails have been sent in the last 24 hours, but the max daily load is ${MAX_DAILY_EMAILS}.`, `${emailsSentLast24Hours} emails have been sent in the last 24 hours, but the max daily load is ${MAX_DAILY_EMAILS}.`,
); );
@@ -72,6 +75,7 @@ async function sendmail({
subject: `joeac.net: ${name} left a message`, subject: `joeac.net: ${name} left a message`,
text: `${name} <${email}> sent you a message:\n\n\n${message}`, text: `${name} <${email}> sent you a message:\n\n\n${message}`,
}); });
await db await db
.insert(SentEmails) .insert(SentEmails)
.values({ messageId: info.messageId, sentAt: Date.now() }); .values({ messageId: info.messageId, sentAt: Date.now() });