fix: Do not unsubscribe existing contacts

This commit is contained in:
Dries Augustyns
2026-02-20 16:58:17 +01:00
parent 64bd094b47
commit 2c4d95e604
2 changed files with 8 additions and 4 deletions
+6 -3
View File
@@ -72,12 +72,13 @@ export class Actions {
// Create or update contact with persistent data only // Create or update contact with persistent data only
// ContactService.upsert will filter out non-persistent fields // ContactService.upsert will filter out non-persistent fields
// Event tracking should subscribe contacts by default // Event tracking should subscribe new contacts by default (subscribed=true in ContactService)
// but preserve existing subscription state for existing contacts
const contact = await ContactService.upsert( const contact = await ContactService.upsert(
auth.projectId, auth.projectId,
email, email,
data as Record<string, unknown> | undefined, data as Record<string, unknown> | undefined,
subscribed ?? true, subscribed,
); );
// Track the event with ALL data (persistent + non-persistent) // Track the event with ALL data (persistent + non-persistent)
@@ -268,7 +269,9 @@ export class Actions {
// Create or update contact with metadata // Create or update contact with metadata
// Transactional emails should not subscribe contacts by default // Transactional emails should not subscribe contacts by default
const contact = await ContactService.upsert(auth.projectId, recipient.email, recipientData, subscribed ?? false); // New contacts default to unsubscribed unless explicitly opted in
// Existing contacts preserve their subscription state unless explicitly changed
const contact = await ContactService.upsert(auth.projectId, recipient.email, recipientData, subscribed, false);
// Get merged data including non-persistent fields for template rendering // Get merged data including non-persistent fields for template rendering
const mergedData = ContactService.getMergedData(contact, data as Record<string, unknown> | undefined); const mergedData = ContactService.getMergedData(contact, data as Record<string, unknown> | undefined);
+2 -1
View File
@@ -199,6 +199,7 @@ export class ContactService {
email: string, email: string,
data?: Record<string, unknown>, data?: Record<string, unknown>,
subscribed?: boolean, subscribed?: boolean,
defaultSubscribed: boolean = true,
): Promise<Contact> { ): Promise<Contact> {
// Find existing contact // Find existing contact
const existing = await prisma.contact.findFirst({ const existing = await prisma.contact.findFirst({
@@ -292,7 +293,7 @@ export class ContactService {
projectId, projectId,
email, email,
data: Object.keys(mergedData).length > 0 ? toPrismaJson(mergedData) : Prisma.JsonNull, data: Object.keys(mergedData).length > 0 ? toPrismaJson(mergedData) : Prisma.JsonNull,
subscribed: subscribed ?? true, subscribed: subscribed ?? defaultSubscribed,
}, },
}); });
} }