Skip to content

Open-source status page, incident response and uptime monitoring

Send subscriber update (#517)

Thibault Le Ouay775eb8f9e93c
diff --git a/apps/server/src/v1/page.ts b/apps/server/src/v1/page.ts
index 734848896..486c9f4b9 100644
--- a/apps/server/src/v1/page.ts
+++ b/apps/server/src/v1/page.ts
@@ -3,7 +3,8 @@ import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { and, eq } from "@openstatus/db";
import { db } from "@openstatus/db/src/db";
import { page, pageSubscriber } from "@openstatus/db/src/schema";
-import { sendEmail, SubscribeEmail } from "@openstatus/emails";
+import { SubscribeEmail } from "@openstatus/emails";
+import { sendEmail } from "@openstatus/emails/emails/send";
import type { Variables } from ".";
import { ErrorSchema } from "./shared";
diff --git a/apps/server/src/v1/statusReport.ts b/apps/server/src/v1/statusReport.ts
index 00ef20ef4..eda299e76 100644
--- a/apps/server/src/v1/statusReport.ts
+++ b/apps/server/src/v1/statusReport.ts
@@ -1,11 +1,17 @@
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
-import { and, db, eq } from "@openstatus/db";
+import { and, db, eq, isNotNull } from "@openstatus/db";
import {
+ page,
+ pagesToStatusReports,
+ pageSubscriber,
statusReport,
statusReportStatus,
statusReportUpdate,
+ workspacePlans,
} from "@openstatus/db/src/schema";
+import { sendEmail, sendEmailHtml } from "@openstatus/emails/emails/send";
+import { allPlans } from "@openstatus/plans";
import type { Variables } from "./index";
import { ErrorSchema } from "./shared";
@@ -336,7 +342,43 @@ statusReportApi.openapi(postRouteUpdate, async (c) => {
})
.returning()
.get();
-
+ // send email
+ const workspacePlan = c.get("workspacePlan");
+ if (workspacePlan !== allPlans.free) {
+ const allPages = await db
+ .select()
+ .from(pagesToStatusReports)
+ .where(eq(pagesToStatusReports.statusReportId, statusReportId))
+ .all();
+ for (const currentPage of allPages) {
+ const subscribers = await db
+ .select()
+ .from(pageSubscriber)
+ .where(
+ and(
+ eq(pageSubscriber.pageId, currentPage.pageId),
+ isNotNull(pageSubscriber.acceptedAt),
+ ),
+ )
+ .all();
+ const pageInfo = await db
+ .select()
+ .from(page)
+ .where(eq(page.id, currentPage.pageId))
+ .get();
+ if (!pageInfo) continue;
+ const subscribersEmails = subscribers.map(
+ (subscriber) => subscriber.email,
+ );
+ await sendEmailHtml({
+ to: subscribersEmails,
+ subject: `New status update for ${pageInfo.title}`,
+ html: `<p>Hi,</p><p>${pageInfo.title} just posted an update on their status page:</p><p>New Status : ${statusReportUpdate.status}</p><p>${statusReportUpdate.message}</p></p><p></p><p>Powered by OpenStatus</p><p></p><p></p><p></p><p></p><p></p>
+ `,
+ from: "Notification OpenStatus <[email protected]>",
+ });
+ }
+ }
const data = statusUpdateSchema.parse(_statusReportUpdate);
return c.jsonT({
diff --git a/apps/server/src/v1/statusReportUpdate.ts b/apps/server/src/v1/statusReportUpdate.ts
index 00187275d..a3a102561 100644
--- a/apps/server/src/v1/statusReportUpdate.ts
+++ b/apps/server/src/v1/statusReportUpdate.ts
@@ -1,11 +1,16 @@
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
-import { and, db, eq } from "@openstatus/db";
+import { and, db, eq, isNotNull } from "@openstatus/db";
import {
+ page,
+ pagesToStatusReports,
+ pageSubscriber,
statusReport,
statusReportStatus,
statusReportUpdate,
} from "@openstatus/db/src/schema";
+import { sendEmailHtml } from "@openstatus/emails";
+import { allPlans } from "@openstatus/plans";
import type { Variables } from ".";
import { ErrorSchema } from "./shared";
@@ -172,7 +177,44 @@ statusReportUpdateApi.openapi(createStatusUpdate, async (c) => {
})
.returning()
.get();
-
+ // send email
+ const workspacePlan = c.get("workspacePlan");
+
+ if (workspacePlan !== allPlans.free) {
+ const allPages = await db
+ .select()
+ .from(pagesToStatusReports)
+ .where(eq(pagesToStatusReports.statusReportId, input.status_report_id))
+ .all();
+ for (const currentPage of allPages) {
+ const subscribers = await db
+ .select()
+ .from(pageSubscriber)
+ .where(
+ and(
+ eq(pageSubscriber.pageId, currentPage.pageId),
+ isNotNull(pageSubscriber.acceptedAt),
+ ),
+ )
+ .all();
+ const pageInfo = await db
+ .select()
+ .from(page)
+ .where(eq(page.id, currentPage.pageId))
+ .get();
+ if (!pageInfo) continue;
+ const subscribersEmails = subscribers.map(
+ (subscriber) => subscriber.email,
+ );
+ await sendEmailHtml({
+ to: subscribersEmails,
+ subject: `New status update for ${pageInfo.title}`,
+ html: `<p>Hi,</p><p>${pageInfo.title} just posted an update on their status page:</p><p>New Status : ${statusReportUpdate.status}</p><p>${statusReportUpdate.message}</p></p><p></p><p>Powered by OpenStatus</p><p></p><p></p><p></p><p></p><p></p>
+ `,
+ from: "Notification OpenStatus <[email protected]>",
+ });
+ }
+ }
const data = statusUpdateSchema.parse(res);
return c.jsonT(data);
});
diff --git a/apps/web/next.config.js b/apps/web/next.config.js
index e4f2f94a5..5f2c1a15e 100644
--- a/apps/web/next.config.js
+++ b/apps/web/next.config.js
@@ -4,10 +4,21 @@ const { withContentlayer } = require("next-contentlayer");
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
- transpilePackages: ["@openstatus/ui", "@openstatus/api"],
+ transpilePackages: [
+ "@openstatus/ui",
+ "@openstatus/api",
+ "@react-email/components",
+ "@react-email/render",
+ "@react-email/html",
+ ],
+
experimental: {
serverActions: true,
- serverComponentsExternalPackages: ["libsql"],
+ serverComponentsExternalPackages: [
+ "libsql",
+ "@react-email/components",
+ "@react-email/render",
+ ],
logging: {
level: "verbose",
fullUrl: true,
diff --git a/packages/api/.env.test b/packages/api/.env.test
new file mode 100644
index 000000000..43e7bc953
--- /dev/null
+++ b/packages/api/.env.test
@@ -0,0 +1 @@
+RESEND_API_KEY='test'
diff --git a/packages/api/src/router/statusReport.ts b/packages/api/src/router/statusReport.ts
index e7872312e..e9f37366d 100644
--- a/packages/api/src/router/statusReport.ts
+++ b/packages/api/src/router/statusReport.ts
@@ -1,18 +1,22 @@
import { z } from "zod";
-import { and, eq, inArray } from "@openstatus/db";
+import { and, eq, inArray, isNotNull } from "@openstatus/db";
import {
insertStatusReportSchema,
insertStatusReportUpdateSchema,
monitorsToStatusReport,
+ page,
pagesToStatusReports,
+ pageSubscriber,
selectMonitorSchema,
selectStatusReportSchema,
selectStatusReportUpdateSchema,
statusReport,
statusReportStatusSchema,
statusReportUpdate,
+ workspace,
} from "@openstatus/db/src/schema";
+import { sendEmailHtml } from "@openstatus/emails/emails/send";
import { createTRPCRouter, protectedProcedure } from "../trpc";
@@ -78,11 +82,61 @@ export const statusReportRouter = createTRPCRouter({
.get();
const { id, ...statusReportUpdateInput } = opts.input;
- return await opts.ctx.db
+
+ // Send email
+
+ const updatedValue = await opts.ctx.db
.insert(statusReportUpdate)
.values(statusReportUpdateInput)
.returning()
.get();
+
+ const currentWorkspace = await opts.ctx.db
+ .select()
+ .from(workspace)
+ .where(eq(workspace.id, opts.ctx.workspace.id))
+ .get();
+ if (currentWorkspace?.plan !== "pro") {
+ const allPages = await opts.ctx.db
+ .select()
+ .from(pagesToStatusReports)
+ .where(
+ eq(
+ pagesToStatusReports.statusReportId,
+ updatedValue.statusReportId,
+ ),
+ )
+ .all();
+ for (const currentPage of allPages) {
+ const subscribers = await opts.ctx.db
+ .select()
+ .from(pageSubscriber)
+ .where(
+ and(
+ eq(pageSubscriber.pageId, currentPage.pageId),
+ isNotNull(pageSubscriber.acceptedAt),
+ ),
+ )
+ .all();
+ const pageInfo = await opts.ctx.db
+ .select()
+ .from(page)
+ .where(eq(page.id, currentPage.pageId))
+ .get();
+ if (!pageInfo) continue;
+ const subscribersEmails = subscribers.map(
+ (subscriber) => subscriber.email,
+ );
+ await sendEmailHtml({
+ to: subscribersEmails,
+ subject: `New status update for ${pageInfo.title}`,
+ html: `<p>Hi,</p><p>${pageInfo.title} just posted an update on their status page:</p><p>New Status : ${statusReportUpdate.status}</p><p>${statusReportUpdate.message}</p></p><p></p><p>Powered by OpenStatus</p><p></p><p></p><p></p><p></p><p></p>
+ `,
+ from: "Notification OpenStatus <[email protected]>",
+ });
+ }
+ }
+ return updatedValue;
}),
updateStatusReport: protectedProcedure
diff --git a/packages/emails/emails/send.ts b/packages/emails/emails/send.ts
new file mode 100644
index 000000000..39e949eee
--- /dev/null
+++ b/packages/emails/emails/send.ts
@@ -0,0 +1,38 @@
+import { Resend } from "resend";
+
+import { env } from "../env";
+
+export const resend = new Resend(env.RESEND_API_KEY);
+
+export interface Emails {
+ react: JSX.Element;
+ subject: string;
+ to: string[];
+ from: string;
+}
+
+export type EmailHtml = {
+ html: string;
+ subject: string;
+ to: string[];
+ from: string;
+};
+export const sendEmail = async (email: Emails) => {
+ await resend.emails.send(email);
+};
+
+export const sendEmailHtml = async (email: EmailHtml) => {
+ await fetch("https://api.resend.com/emails", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${env.RESEND_API_KEY}`,
+ },
+ body: JSON.stringify({
+ to: email.to,
+ from: email.from,
+ subject: email.subject,
+ html: email.html,
+ }),
+ });
+};
diff --git a/packages/emails/index.ts b/packages/emails/index.ts
index be42e91f9..2318537fd 100644
--- a/packages/emails/index.ts
+++ b/packages/emails/index.ts
@@ -1,11 +1,8 @@
-import { Resend } from "resend";
-
import { Alert, EmailDataSchema } from "./emails/alert";
import SubscribeEmail from "./emails/subscribe";
import { validateEmailNotDisposable } from "./emails/utils/utils";
import WaitingList from "./emails/waiting-list";
import WelcomeEmail from "./emails/welcome";
-import { env } from "./env";
export {
WelcomeEmail,
@@ -16,15 +13,4 @@ export {
SubscribeEmail,
};
-export const resend = new Resend(env.RESEND_API_KEY);
-
-export interface Emails {
- react: JSX.Element;
- subject: string;
- to: string[];
- from: string;
-}
-
-export const sendEmail = async (email: Emails) => {
- await resend.emails.send(email);
-};
+export { sendEmail, sendEmailHtml } from "./emails/send";