fix: Enhance email processing to support campaign types and improve unsubscribe logic

This commit is contained in:
Dries Augustyns
2026-04-02 14:09:01 +02:00
parent fb5aa8796a
commit 9e2400c6de
4 changed files with 35 additions and 6 deletions
+4 -1
View File
@@ -59,6 +59,7 @@ export async function createEmailWorker() {
contact: true,
project: true,
template: {select: {type: true}},
campaign: {select: {type: true}},
},
});
@@ -112,7 +113,9 @@ export async function createEmailWorker() {
contact: email.contact,
project: email.project,
includeUnsubscribe:
email.sourceType !== EmailSourceType.TRANSACTIONAL && email.template?.type !== 'HEADLESS',
email.sourceType !== EmailSourceType.TRANSACTIONAL &&
email.template?.type !== 'HEADLESS' &&
email.campaign?.type !== 'HEADLESS',
});
// Use fromName from database if available, otherwise fall back to project name
+5 -4
View File
@@ -293,9 +293,8 @@ export class EmailService {
include: {
contact: true,
project: true,
template: {
select: {type: true},
},
template: {select: {type: true}},
campaign: {select: {type: true}},
},
});
@@ -363,7 +362,9 @@ export class EmailService {
contact: email.contact,
project: email.project,
includeUnsubscribe:
email.sourceType !== EmailSourceType.TRANSACTIONAL && email.template?.type !== 'HEADLESS',
email.sourceType !== EmailSourceType.TRANSACTIONAL &&
email.template?.type !== 'HEADLESS' &&
email.campaign?.type !== 'HEADLESS',
});
// Use explicit fromName if provided, otherwise fall back to project name
@@ -1,5 +1,5 @@
import {beforeEach, describe, expect, it, vi, type Mock} from 'vitest';
import {EmailSourceType, EmailStatus} from '@plunk/db';
import {EmailSourceType, EmailStatus, TemplateType} from '@plunk/db';
import {ActionSchemas} from '@plunk/shared';
import {EmailService} from '../EmailService';
import {sendRawEmail} from '../SESService';
@@ -216,6 +216,29 @@ describe('EmailService', () => {
expect(email.error).toMatch(/unsubscribed/i);
});
it('should keep CAMPAIGN sourceType when campaign type is HEADLESS (no template)', async () => {
const contact = await factories.createContact({projectId, subscribed: true});
// Campaign typed HEADLESS directly — no template involved (inline body)
const campaign = await factories.createCampaign({
projectId,
type: TemplateType.HEADLESS,
body: 'Content with <a href="https://example.com/unsubscribe">unsubscribe</a>',
});
const email = await EmailService.sendCampaignEmail({
projectId,
contactId: contact.id,
campaignId: campaign.id,
subject: 'Newsletter',
body: campaign.body,
from: 'news@example.com',
});
expect(email.sourceType).toBe(EmailSourceType.CAMPAIGN);
expect(email.status).toBe(EmailStatus.PENDING);
});
it('should keep CAMPAIGN sourceType when campaign uses headless template', async () => {
const contact = await factories.createContact({projectId, subscribed: true});