Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {Events} from './controllers/Events.js';
import {Oauth} from './controllers/Oauth/index.js';
import {Projects} from './controllers/Projects.js';
import {Segments} from './controllers/Segments.js';
import {Suppressions} from './controllers/Suppressions.js';
import {Templates} from './controllers/Templates.js';
import {Unsubscribe} from './controllers/Unsubscribe.js';
import {Uploads} from './controllers/Uploads.js';
Expand Down Expand Up @@ -164,6 +165,7 @@ const server = new (class extends Server {
new Projects(),
new Segments(),
new Templates(),
new Suppressions(),
new Unsubscribe(),
new Uploads(),
new Webhooks(),
Expand Down
117 changes: 117 additions & 0 deletions apps/api/src/controllers/Suppressions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import {Controller, Get, Middleware, Post, Put} from '@overnightjs/core';
import {EmailStatus} from '@plunk/db';
import type {Request, Response} from 'express';
import {z} from 'zod';

import {prisma} from '../database/prisma.js';
import {HttpException} from '../exceptions/index.js';
import {requireAuth, requireEmailVerified} from '../middleware/auth.js';
import {RecipientSuppressionService, suppressionRuleSchema} from '../services/RecipientSuppressionService.js';
import {CatchAsync} from '../utils/asyncHandler.js';

@Controller('suppressions')
export class Suppressions {
@Get('')
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async list(_req: Request, res: Response) {
return res.json(
await prisma.recipientSuppressionRule.findMany({
where: {projectId: res.locals.auth.projectId},
orderBy: [{createdAt: 'asc'}, {id: 'asc'}],
}),
);
}

@Post('')
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async create(req: Request, res: Response) {
const parsed = suppressionRuleSchema.safeParse(req.body);
if (!parsed.success) throw new HttpException(400, parsed.error.issues[0]?.message || 'Invalid rule');
const data = parsed.data;
const projectId = res.locals.auth.projectId;
// Lock only this project to make the rule cap safe under concurrent creates.
const rule = await prisma.$transaction(async tx => {
await tx.$queryRaw`SELECT id FROM projects WHERE id = ${projectId} FOR UPDATE`;
if ((await tx.recipientSuppressionRule.count({where: {projectId}})) >= 100)
throw new HttpException(400, 'Maximum 100 suppression rules per project');
return tx.recipientSuppressionRule.create({data: {...data, projectId}});
});
return res.status(201).json(rule);
}

@Put(':id')
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async update(req: Request, res: Response) {
const parsed = suppressionRuleSchema.safeParse(req.body);
if (!parsed.success) throw new HttpException(400, parsed.error.issues[0]?.message || 'Invalid rule');
const data = parsed.data;
const result = await prisma.recipientSuppressionRule.updateMany({
where: {id: String(req.params.id), projectId: res.locals.auth.projectId},
data,
});
if (!result.count) throw new HttpException(404, 'Suppression rule not found');
return res.json({success: true});
}

@Post('preview')
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async preview(req: Request, res: Response) {
const {address} = z.object({address: z.string().email().max(254)}).parse(req.body);
const match = await RecipientSuppressionService.match(res.locals.auth.projectId, address);
return res.json({suppressed: !!match, match: match || null});
}

@Get('emails')
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async emails(req: Request, res: Response) {
const {cursor, limit} = z
.object({cursor: z.string().optional(), limit: z.coerce.number().int().min(1).max(100).default(20)})
.parse(req.query);
const position = cursor
? await prisma.email.findFirst({
where: {id: cursor, projectId: res.locals.auth.projectId, status: EmailStatus.SUPPRESSED},
select: {id: true, createdAt: true},
})
: null;
if (cursor && !position) throw new HttpException(400, 'Invalid capture cursor');
const emails = await prisma.email.findMany({
where: {
projectId: res.locals.auth.projectId,
status: EmailStatus.SUPPRESSED,
...(position
? {OR: [{createdAt: {lt: position.createdAt}}, {createdAt: position.createdAt, id: {lt: position.id}}]}
: {}),
},
orderBy: [{createdAt: 'desc'}, {id: 'desc'}],
take: limit + 1,
select: {
id: true,
createdAt: true,
recipientAddress: true,
subject: true,
renderedSubject: true,
suppression: true,
status: true,
},
});
const hasMore = emails.length > limit;
if (hasMore) emails.pop();
return res.json({emails, nextCursor: hasMore ? emails[emails.length - 1]?.id : null});
}

@Get('emails/:id')
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async email(req: Request, res: Response) {
const email = await prisma.email.findFirst({
where: {id: String(req.params.id), projectId: res.locals.auth.projectId, status: EmailStatus.SUPPRESSED},
});
if (!email) throw new HttpException(404, 'Captured email not found');
return res.json(email);
}
}
2 changes: 1 addition & 1 deletion apps/api/src/jobs/email-body-cleanup-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function processCleanup(job: Job<EmailBodyCleanupJobData>): Promise<{clear
// and is served by the partial index emails_createdAt_unpurged_idx.
const cleared = await prisma.$executeRaw`
UPDATE "emails"
SET "body" = ''
SET "body" = '', "renderedBody" = NULL
WHERE "id" IN (
SELECT "id" FROM "emails"
WHERE "createdAt" < ${cutoffDate}
Expand Down
37 changes: 28 additions & 9 deletions apps/api/src/jobs/email-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
EMAIL_WORKER_MAX_CONCURRENCY,
} from '../app/constants.js';
import {prisma} from '../database/prisma.js';
import {BillingLimitService} from '../services/BillingLimitService.js';
import {CampaignService} from '../services/CampaignService.js';
import {
bodyHasListManagementLink,
Expand All @@ -35,6 +36,7 @@ import {EmailService} from '../services/EmailService.js';
import {EventService} from '../services/EventService.js';
import {MeterService} from '../services/MeterService.js';
import {emailQueue} from '../services/QueueService.js';
import {RecipientSuppressionService} from '../services/RecipientSuppressionService.js';
import {SecurityService} from '../services/SecurityService.js';
import {getSendingQuota, sendRawEmail} from '../services/SESService.js';
import {ThrottleService} from '../services/ThrottleService.js';
Expand Down Expand Up @@ -192,10 +194,11 @@ export async function processEmailJob(emailId: string): Promise<void> {

try {
// Update status to sending
await prisma.email.update({
where: {id: emailId},
const claimed = await prisma.email.updateMany({
where: {id: emailId, status: EmailStatus.PENDING},
data: {status: EmailStatus.SENDING},
});
if (!claimed.count) return;

// Format template variables in subject and body
const contactData = (email.contact.data as Record<string, unknown>) || {};
Expand Down Expand Up @@ -242,7 +245,25 @@ export async function processEmailJob(emailId: string): Promise<void> {
: undefined;

// Check for custom recipient override in headers
const recipientEmail = customHeaders?.['X-Plunk-Recipient-Override'] || email.contact.email;
const recipientEmail =
email.recipientAddress || customHeaders?.['X-Plunk-Recipient-Override'] || email.contact.email;

const suppression = email.suppression || (await RecipientSuppressionService.match(email.projectId, recipientEmail));
await prisma.email.update({
where: {id: emailId},
data: {
recipientAddress: recipientEmail,
renderedSubject: formattedEmail.subject,
renderedBody: compiledHtml,
...(suppression ? {status: EmailStatus.SUPPRESSED, suppression} : {}),
},
});
if (suppression) {
await ThrottleService.releaseForEmail(emailId);
await BillingLimitService.invalidateCache(email.projectId);
if (email.campaignId) await CampaignService.finalizeIfDone(email.campaignId);
return;
}

// Remove internal headers before sending
const publicHeaders = customHeaders ? {...customHeaders} : undefined;
Expand Down Expand Up @@ -391,9 +412,9 @@ export async function processEmailJob(emailId: string): Promise<void> {
} catch (error) {
signale.error(`[EMAIL-PROCESSOR] Failed to send email ${emailId}:`, error);

// Mark as failed
await prisma.email.update({
where: {id: emailId},
// Never overwrite a committed suppression decision on retry.
await prisma.email.updateMany({
where: {id: emailId, status: {not: EmailStatus.SUPPRESSED}},
data: {
status: EmailStatus.FAILED,
error: error instanceof Error ? error.message : 'Unknown error',
Expand All @@ -412,9 +433,7 @@ export async function createEmailWorker() {
// Fetch the rate limit (from env, AWS, or default)
const rateLimit = await getEmailRateLimit();
const concurrency = deriveWorkerConcurrency(rateLimit);
signale.info(
`[EMAIL-PROCESSOR] Worker concurrency: ${concurrency} (rate limit: ${rateLimit}/s)`,
);
signale.info(`[EMAIL-PROCESSOR] Worker concurrency: ${concurrency} (rate limit: ${rateLimit}/s)`);
const worker = new Worker<SendEmailJobData>(
emailQueue.name,
async (job: Job<SendEmailJobData>) => {
Expand Down
84 changes: 74 additions & 10 deletions apps/api/src/services/ActivityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ export class ActivityService {
const fetchLimit = effectiveLimit + 1;

// Default date range to last 30 days if not specified.
// IMPORTANT: When cursor is provided (pagination), we should NOT apply the gte constraint
// to allow users to paginate back beyond the initial date range.
// Pagination may extend beyond the default date floor, but an explicitly selected
// start date must remain in force on every page.
//
// Contact-scoped feeds are exempt from the default 30-day floor: a single contact's
// history is naturally bounded (not the project-wide firehose the floor guards against),
Expand All @@ -120,9 +120,8 @@ export class ActivityService {
const defaultStartDate = new Date(now.getTime() - this.DEFAULT_DAYS_BACK * 24 * 60 * 60 * 1000);
const initialStartDate = startDate ?? (contactId ? undefined : defaultStartDate);
const dateFilter: Prisma.DateTimeFilter = {
// Only apply start date filter on initial load (no cursor)
// This allows pagination to go back indefinitely
...(cursor || !initialStartDate ? {} : {gte: initialStartDate}),
// Keep explicit ranges; only the implicit default floor is initial-page-only.
...(startDate ? {gte: startDate} : cursor || !initialStartDate ? {} : {gte: initialStartDate}),
...(endDate ? {lte: endDate} : {}),
};

Expand All @@ -137,17 +136,18 @@ export class ActivityService {

// Fetch activities from different sources in parallel
// Each source fetches up to fetchLimit items
const [events, emails, workflows] = await Promise.all([
const [captures, events, emails, workflows] = await Promise.all([
this.fetchSuppressedActivities(projectId, fetchLimit, dateFilter, cursorTimestamp, cursorId, contactId, types),
this.fetchEvents(projectId, fetchLimit, dateFilter, cursorTimestamp, cursorId, contactId, types),
this.fetchEmailActivities(projectId, fetchLimit, dateFilter, cursorTimestamp, cursorId, contactId, types),
this.fetchWorkflowActivities(projectId, fetchLimit, dateFilter, cursorTimestamp, cursorId, contactId, types),
]);

// Merge all activities
const allActivities = [...events, ...emails, ...workflows];
const allActivities = [...events, ...emails, ...workflows, ...captures];

// Sort by timestamp descending (most recent first)
allActivities.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime());
allActivities.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime() || b.id.localeCompare(a.id));

// Take only the requested limit + 1 (to check if there are more)
const paginatedActivities = allActivities.slice(0, effectiveLimit + 1);
Expand Down Expand Up @@ -176,14 +176,14 @@ export class ActivityService {
public static async getEmailBody(projectId: string, emailId: string): Promise<{body: string}> {
const email = await prisma.email.findFirst({
where: {id: emailId, projectId},
select: {body: true},
select: {body: true, renderedBody: true, status: true},
});

if (!email) {
throw new HttpException(404, 'Email not found');
}

return email;
return {body: email.status === 'SUPPRESSED' ? (email.renderedBody ?? '') : email.body};
}

/**
Expand Down Expand Up @@ -226,6 +226,7 @@ export class ActivityService {
// Aggregate email stats
prisma.email.aggregate({
where: {
status: {not: 'SUPPRESSED'},
projectId,
createdAt: dateFilter,
},
Expand Down Expand Up @@ -564,6 +565,69 @@ export class ActivityService {
/**
* Fetch email activities (sent, delivered, opened, clicked, bounced)
*/
/** Captures have no delivery timestamp: emit exactly one independent activity. */
private static async fetchSuppressedActivities(
projectId: string,
limit: number,
dateFilter: Prisma.DateTimeFilter,
cursorTimestamp?: Date,
cursorId?: string,
contactId?: string,
types?: ActivityType[],
): Promise<Activity[]> {
if (types && !types.includes(ActivityType.EMAIL_SUPPRESSED)) return [];
const emails = await prisma.email.findMany({
where: {
projectId,
status: 'SUPPRESSED',
...(contactId ? {contactId} : {}),
createdAt: dateFilter,
...(cursorTimestamp
? {
OR: [
{createdAt: {...dateFilter, lt: cursorTimestamp}},
{createdAt: cursorTimestamp, id: {lt: cursorId || ''}},
],
}
: {}),
},
orderBy: [{createdAt: 'desc'}, {id: 'desc'}],
take: limit,
select: {
id: true,
createdAt: true,
contactId: true,
recipientAddress: true,
subject: true,
renderedSubject: true,
from: true,
fromName: true,
replyTo: true,
toName: true,
sourceType: true,
suppression: true,
contact: {select: {email: true}},
},
});
return emails.map(email => ({
id: `${email.id}_suppressed`,
type: ActivityType.EMAIL_SUPPRESSED,
timestamp: email.createdAt,
contactId: email.contactId,
contactEmail: email.recipientAddress || email.contact.email,
metadata: {
emailId: email.id,
subject: email.renderedSubject ?? email.subject,
from: email.from,
fromName: email.fromName,
replyTo: email.replyTo,
toName: email.toName,
sourceType: email.sourceType,
suppression: email.suppression,
},
}));
}

private static async fetchEmailActivities(
projectId: string,
limit: number,
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/services/AnalyticsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ function emailScope(query: AnalyticsQuery, stream: AnalyticsStream = query.strea
AND e."createdAt" >= ${utc(query.from)} AND e."createdAt" < ${utc(query.to)}
AND ${streamFilter}
AND e."simulated" = false
AND e."status" <> 'SUPPRESSED'
`;
}

Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/services/BillingLimitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class BillingLimitService {
where: {
projectId,
// Never metered, so never counted -- see WHAT COUNTS AS USAGE above.
status: {notIn: [EmailStatus.FAILED, EmailStatus.CANCELLED]},
status: {notIn: [EmailStatus.FAILED, EmailStatus.CANCELLED, EmailStatus.SUPPRESSED]},
createdAt: {
gte: start,
lt: end,
Expand Down Expand Up @@ -109,7 +109,7 @@ export class BillingLimitService {
projectId,
sourceType,
// Never metered, so never counted -- see WHAT COUNTS AS USAGE above.
status: {notIn: [EmailStatus.FAILED, EmailStatus.CANCELLED]},
status: {notIn: [EmailStatus.FAILED, EmailStatus.CANCELLED, EmailStatus.SUPPRESSED]},
createdAt: {
gte: start,
lt: end,
Expand Down
Loading