Feat: Added ability to overwrite sender mail and name for templates and campaigns

This commit is contained in:
Dries Augustyns
2024-09-24 17:19:55 +02:00
parent 213081174d
commit f1d4d5073c
11 changed files with 259 additions and 29 deletions
+11 -2
View File
@@ -41,6 +41,9 @@ export class Tasks {
let subject = "";
let body = "";
let email = "";
let name = "";
if (action) {
const { template, notevents } = action;
@@ -52,6 +55,9 @@ export class Tasks {
}
}
email = project.verified && project.email ? template.email ?? project.email : "no-reply@useplunk.dev";
name = template.from ?? project.from ?? project.name;
({ subject, body } = EmailService.format({
subject: template.subject,
body: template.body,
@@ -62,6 +68,9 @@ export class Tasks {
},
}));
} else if (campaign) {
email = project.verified && project.email ? campaign.email ?? project.email : "no-reply@useplunk.dev";
name = campaign.from ?? project.from ?? project.name;
({ subject, body } = EmailService.format({
subject: campaign.subject,
body: campaign.body,
@@ -75,8 +84,8 @@ export class Tasks {
const { messageId } = await EmailService.send({
from: {
name: project.from ?? project.name,
email: project.verified && project.email ? project.email : "no-reply@useplunk.dev",
name,
email,
},
to: [contact.email],
content: {
+24 -3
View File
@@ -3,7 +3,7 @@ import { CampaignSchemas, UtilitySchemas } from "@plunk/shared";
import dayjs from "dayjs";
import type { Request, Response } from "express";
import { prisma } from "../../database/prisma";
import { HttpException, NotFound } from "../../exceptions";
import { HttpException, NotAllowed, NotFound } from "../../exceptions";
import { type IJwt, type ISecret, isAuthenticated, isValidSecretKey } from "../../middleware/auth";
import { CampaignService } from "../../services/CampaignService";
import { ContactService } from "../../services/ContactService";
@@ -160,6 +160,7 @@ export class Campaigns {
subject: campaign.subject,
body: campaign.body,
style: campaign.style,
email: campaign.email,
},
});
@@ -180,7 +181,15 @@ export class Campaigns {
throw new NotFound("project");
}
let { subject, body, recipients, style } = CampaignSchemas.create.parse(req.body);
let { subject, body, recipients, style, email, from } = CampaignSchemas.create.parse(req.body);
if (email && !project.verified) {
throw new NotAllowed("You need to attach a domain to your project to customize the sender address");
}
if (email && email.split("@")[1] !== project.email?.split("@")[1]) {
throw new NotAllowed("The sender address must be the same domain as the project's email address");
}
if (recipients.length === 1 && recipients[0] === "all") {
const projectContacts = await prisma.contact.findMany({
@@ -197,6 +206,8 @@ export class Campaigns {
subject,
body,
style,
from: from === "" ? null : from,
email: email === "" ? null : email,
},
});
@@ -253,7 +264,15 @@ export class Campaigns {
throw new NotFound("project");
}
let { id, subject, body, recipients, style } = CampaignSchemas.update.parse(req.body);
let { id, subject, body, recipients, style, email, from } = CampaignSchemas.update.parse(req.body);
if (email && !project.verified) {
throw new NotAllowed("You need to attach a domain to your project to customize the sender address");
}
if (email && email.split("@")[1] !== project.email?.split("@")[1]) {
throw new NotAllowed("The sender address must be the same domain as the project's email address");
}
if (recipients.length === 1 && recipients[0] === "all") {
const projectContacts = await prisma.contact.findMany({
@@ -276,6 +295,8 @@ export class Campaigns {
subject,
body,
style,
from: from === "" ? null : from,
email: email === "" ? null : email,
},
include: {
recipients: { select: { id: true } },
+29 -3
View File
@@ -60,6 +60,7 @@ export class Templates {
body: template.body,
type: template.type,
style: template.style,
email: template.email,
},
});
@@ -101,7 +102,15 @@ export class Templates {
throw new NotFound("project");
}
const { subject, body, type, style } = TemplateSchemas.create.parse(req.body);
const { subject, body, type, style, email, from } = TemplateSchemas.create.parse(req.body);
if (email && !project.verified) {
throw new NotAllowed("You need to attach a domain to your project to customize the sender address");
}
if (email && email.split("@")[1] !== project.email?.split("@")[1]) {
throw new NotAllowed("The sender address must be the same domain as the project's email address");
}
const template = await prisma.template.create({
data: {
@@ -110,6 +119,8 @@ export class Templates {
body,
type,
style,
from: from === "" ? null : from,
email: email === "" ? null : email,
},
});
@@ -151,7 +162,7 @@ export class Templates {
throw new NotFound("project");
}
const { id, subject, body, type, style } = TemplateSchemas.update.parse(req.body);
const { id, subject, body, type, style, email, from } = TemplateSchemas.update.parse(req.body);
let template = await TemplateService.id(id);
@@ -159,9 +170,24 @@ export class Templates {
throw new NotFound("template");
}
if (email && !project.verified) {
throw new NotAllowed("You need to attach a domain to your project to customize the sender address");
}
if (email && email.split("@")[1] !== project.email?.split("@")[1]) {
throw new NotAllowed("The sender address must be the same domain as the project's email address");
}
template = await prisma.template.update({
where: { id },
data: { subject, body, type, style },
data: {
subject,
body,
type,
style,
from: from === "" ? null : from,
email: email === "" ? null : email,
},
include: {
actions: true,
},
+2 -2
View File
@@ -120,8 +120,8 @@ export class ActionService {
const { messageId } = await EmailService.send({
from: {
name: project.from ?? project.name,
email: project.verified && project.email ? project.email : "no-reply@useplunk.dev",
name: action.template.from ?? project.from ?? project.name,
email: project.verified && project.email ? action.template.email ?? project.email : "no-reply@useplunk.dev",
},
to: [contact.email],
content: {