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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"migrate:deploy": "prisma migrate deploy",
"generate": "prisma generate",
"services:up": "docker compose -f docker-compose.dev.yml up -d",
"services:down": "docker compose -f docker-compose.dev.yml down"
"services:down": "docker compose -f docker-compose.dev.yml down",
"ses-local": "yarn dlx aws-ses-v2-local"
},
"packageManager": "[email protected]"
}
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-async-errors": "^3.1.1",
"handlebars": "^4.7.8",
"helmet": "^7.1.0",
"ioredis": "^5.4.1",
"jsonwebtoken": "^9.0.2",
Expand Down
48 changes: 47 additions & 1 deletion packages/api/src/controllers/Projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { UserService } from "../services/UserService";
import { Keys } from "../services/keys";
import { redis } from "../services/redis";
import { generateToken } from "../util/tokens";
import { EmailService } from "../services/EmailService";

@Controller("projects")
export class Projects {
Expand Down Expand Up @@ -450,6 +451,7 @@ export class Projects {
memberships: {
create: [{ userId, role: "OWNER" }],
},
templatingLanguage: "DEFAULT",
},
});

Expand Down Expand Up @@ -512,10 +514,51 @@ export class Projects {
return res.status(200).json({ success: true, project });
}


@Post("preview/template")
@Middleware([isAuthenticated])
public async previewProjectTemplate(req: Request, res: Response) {
const { id: projectId, baseTemplate, unsubscribeFooter } = ProjectSchemas.update.parse(req.body);

const { userId } = res.locals.auth as IJwt;

let project = await ProjectService.id(projectId);

if (!project) {
throw new NotFound("project");
}

const isAdmin = await MembershipService.isAdmin(projectId, userId);

if (!isAdmin) {
throw new NotAllowed();
}

try {
const html = EmailService.compile({
content: "Hello, world!",
footer: {
unsubscribe: true,
},
contact: {
id: "23",
},
project: {
name: project.name,
baseTemplate: baseTemplate as string,
unsubscribeFooter: unsubscribeFooter as string,
},
});
return res.status(200).json({ success: true, html });
} catch (error) {
return res.status(400).json({ success: false, message: (error as Error).message });
}
}

@Put("update")
@Middleware([isAuthenticated])
public async updateProject(req: Request, res: Response) {
const { id: projectId, name, url } = ProjectSchemas.update.parse(req.body);
const { id: projectId, name, url, baseTemplate, unsubscribeFooter, templatingLanguage } = ProjectSchemas.update.parse(req.body);

const { userId } = res.locals.auth as IJwt;

Expand All @@ -536,6 +579,9 @@ export class Projects {
data: {
name,
url,
baseTemplate,
unsubscribeFooter,
templatingLanguage,
},
});

Expand Down
5 changes: 5 additions & 0 deletions packages/api/src/controllers/Tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { prisma } from "../database/prisma";
import { ContactService } from "../services/ContactService";
import { EmailService } from "../services/EmailService";
import { ProjectService } from "../services/ProjectService";
import { TemplatingLanguage } from "@plunk/shared";

@Controller("tasks")
export class Tasks {
Expand Down Expand Up @@ -66,6 +67,7 @@ export class Tasks {
plunk_email: contact.email,
...JSON.parse(contact.data ?? "{}"),
},
templatingLanguage: project.templatingLanguage as TemplatingLanguage ?? "DEFAULT",
}));
} else if (campaign) {
email = project.verified && project.email ? campaign.email ?? project.email : "[email protected]";
Expand All @@ -79,6 +81,7 @@ export class Tasks {
plunk_email: contact.email,
...JSON.parse(contact.data ?? "{}"),
},
templatingLanguage: project.templatingLanguage as TemplatingLanguage ?? "DEFAULT",
}));
}

Expand All @@ -100,6 +103,8 @@ export class Tasks {
},
project: {
name: project.name,
baseTemplate: project.baseTemplate,
unsubscribeFooter: project.unsubscribeFooter,
},
isHtml: (campaign && campaign.style === "HTML") ?? (!!action && action.template.style === "HTML"),
}),
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/controllers/v1/Campaigns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export class Campaigns {
},
project: {
name: project.name,
baseTemplate: project.baseTemplate,
unsubscribeFooter: project.unsubscribeFooter,
},
}),
},
Expand Down
5 changes: 4 additions & 1 deletion packages/api/src/controllers/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChildControllers, Controller, Middleware, Post } from "@overnightjs/core";
import { EventSchemas } from "@plunk/shared";
import { EventSchemas, TemplatingLanguage } from "@plunk/shared";
import dayjs from "dayjs";
import type { Request, Response } from "express";
import signale from "signale";
Expand Down Expand Up @@ -200,6 +200,7 @@ export class V1 {
plunk_email: contact.email,
...JSON.parse(contact.data ?? "{}"),
},
templatingLanguage: project.templatingLanguage as TemplatingLanguage ?? "DEFAULT",
});

const { messageId } = await EmailService.send({
Expand All @@ -224,6 +225,8 @@ export class V1 {
},
project: {
name: project.name,
baseTemplate: project.baseTemplate,
unsubscribeFooter: project.unsubscribeFooter,
},
}),
},
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/services/ActionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ContactService } from "./ContactService";
import { EmailService } from "./EmailService";
import { Keys } from "./keys";
import { wrapRedis } from "./redis";
import { TemplatingLanguage } from "shared/dist";

export class ActionService {
/**
Expand Down Expand Up @@ -116,6 +117,7 @@ export class ActionService {
plunk_email: contact.email,
...JSON.parse(contact.data ?? "{}"),
},
templatingLanguage: project.templatingLanguage as TemplatingLanguage,
});

const { messageId } = await EmailService.send({
Expand All @@ -136,6 +138,8 @@ export class ActionService {
},
project: {
name: project.name,
baseTemplate: project.baseTemplate,
unsubscribeFooter: project.unsubscribeFooter,
},
isHtml: action.template.style === "HTML",
}),
Expand Down
Loading