fix: Update sentCount on campaign sent for correct overview stats

This commit is contained in:
Dries Augustyns
2026-01-03 11:18:44 +01:00
parent 5d44b1606d
commit ee00eb3481
4 changed files with 67 additions and 2 deletions
+1
View File
@@ -204,6 +204,7 @@ export async function createEmailWorker() {
where: {id: email.campaignId},
data: {
status: CampaignStatus.SENT,
sentCount,
},
});
+1
View File
@@ -564,6 +564,7 @@ export class CampaignService {
await prisma.campaign.update({
where: {id: campaignId},
data: {
sentCount: sentEmails,
deliveredCount: deliveredEmails,
openedCount: openedEmails,
clickedCount: clickedEmails,
+36
View File
@@ -503,6 +503,42 @@ export class EmailService {
data: updateData,
});
// Update campaign stats if applicable
if (email.campaignId) {
const campaignUpdate: Prisma.CampaignUpdateInput = {};
switch (eventType) {
case 'delivered':
campaignUpdate.deliveredCount = {increment: 1};
break;
case 'opened':
// Only increment unique opens to match getStats logic
if (!email.openedAt) {
campaignUpdate.openedCount = {increment: 1};
}
break;
case 'clicked':
// Only increment unique clicks to match getStats logic
if (!email.clickedAt) {
campaignUpdate.clickedCount = {increment: 1};
}
break;
case 'bounced':
campaignUpdate.bouncedCount = {increment: 1};
break;
}
if (Object.keys(campaignUpdate).length > 0) {
await prisma.campaign.update({
where: {id: email.campaignId},
data: campaignUpdate,
});
}
}
// Track event
await prisma.event.create({
data: {