block sending emails on limits (#216)
This commit is contained in:
@@ -10,6 +10,8 @@ import { DEFAULT_QUEUE_OPTIONS } from "../queue/queue-constants";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { logger } from "../logger/log";
|
||||
import { createWorkerHandler, TeamJob } from "../queue/bullmq-context";
|
||||
import { LimitService } from "./limit-service";
|
||||
// Notifications about limits are handled inside LimitService.
|
||||
|
||||
type QueueEmailJob = TeamJob<{
|
||||
emailId: string;
|
||||
@@ -366,6 +368,29 @@ async function executeEmail(job: QueueEmailJob) {
|
||||
}
|
||||
|
||||
try {
|
||||
// Check limits right before sending (cloud-only)
|
||||
const limitCheck = await LimitService.checkEmailLimit(email.teamId);
|
||||
logger.info({ limitCheck }, `[EmailQueueService]: Limit check`);
|
||||
if (limitCheck.isLimitReached) {
|
||||
await db.emailEvent.create({
|
||||
data: {
|
||||
emailId: email.id,
|
||||
status: "FAILED",
|
||||
data: {
|
||||
error: "Email sending limit reached",
|
||||
reason: limitCheck.reason,
|
||||
limit: limitCheck.limit,
|
||||
},
|
||||
teamId: email.teamId,
|
||||
},
|
||||
});
|
||||
await db.email.update({
|
||||
where: { id: email.id },
|
||||
data: { latestStatus: "FAILED" },
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const messageId = await sendRawEmail({
|
||||
to: email.to,
|
||||
from: email.from,
|
||||
|
Reference in New Issue
Block a user