Skip to content

Open-source status page, incident response and uptime monitoring

fiix: stpg subscribers (#1432)

Maximilian Kaske75d140843686
diff --git a/apps/status-page/src/components/nav/header.tsx b/apps/status-page/src/components/nav/header.tsx
index a1cee6393..d31d0004e 100644
--- a/apps/status-page/src/components/nav/header.tsx
+++ b/apps/status-page/src/components/nav/header.tsx
@@ -20,10 +20,12 @@ import { usePathnamePrefix } from "@/hooks/use-pathname-prefix";
import { useTRPC } from "@/lib/trpc/client";
import { cn } from "@/lib/utils";
import { useMutation, useQuery } from "@tanstack/react-query";
+import { isTRPCClientError } from "@trpc/client";
import { Menu, MessageCircleMore } from "lucide-react";
import NextLink from "next/link";
import { useParams, usePathname } from "next/navigation";
import { useState } from "react";
+import { toast } from "sonner";
function useNav() {
const pathname = usePathname();
@@ -62,8 +64,20 @@ export function Header(props: React.ComponentProps<"header">) {
const subscribeMutation = useMutation(
trpc.statusPage.subscribe.mutationOptions({
onSuccess: (id) => {
+ console.log("subscribeMutation onSuccess", id);
if (!id) return;
- sendPageSubscriptionMutation.mutate({ id });
+ sendPageSubscriptionMutation.mutate(
+ { id },
+ {
+ onError: (error) => {
+ if (isTRPCClientError(error)) {
+ toast.error(error.message);
+ } else {
+ toast.error("Failed to subscribe");
+ }
+ },
+ },
+ );
},
}),
);
diff --git a/packages/api/src/router/email/index.ts b/packages/api/src/router/email/index.ts
index dfbb42ff2..d81dd6a98 100644
--- a/packages/api/src/router/email/index.ts
+++ b/packages/api/src/router/email/index.ts
@@ -4,11 +4,17 @@ import { and, eq, isNotNull } from "@openstatus/db";
import {
invitation,
pageSubscriber,
+ selectWorkspaceSchema,
statusReportUpdate,
} from "@openstatus/db/src/schema";
import { EmailClient } from "@openstatus/emails";
+import { TRPCError } from "@trpc/server";
import { env } from "../../env";
-import { createTRPCRouter, protectedProcedure } from "../../trpc";
+import {
+ createTRPCRouter,
+ protectedProcedure,
+ publicProcedure,
+} from "../../trpc";
const emailClient = new EmailClient({ apiKey: env.RESEND_API_KEY });
@@ -93,29 +99,50 @@ export const emailRouter = createTRPCRouter({
}
}),
- sendPageSubscription: protectedProcedure
+ sendPageSubscription: publicProcedure
.input(z.object({ id: z.number() }))
.mutation(async (opts) => {
- const limits = opts.ctx.workspace.limits;
-
- if (limits["status-subscribers"]) {
- const _pageSubscriber =
- await opts.ctx.db.query.pageSubscriber.findFirst({
- where: eq(pageSubscriber.id, opts.input.id),
+ const _pageSubscriber = await opts.ctx.db.query.pageSubscriber.findFirst({
+ where: eq(pageSubscriber.id, opts.input.id),
+ with: {
+ page: {
with: {
- page: true,
+ workspace: true,
},
- });
+ },
+ },
+ });
- if (!_pageSubscriber || !_pageSubscriber.token) return;
+ if (!_pageSubscriber || !_pageSubscriber.token) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Page subscriber not found",
+ });
+ }
+
+ const workspace = selectWorkspaceSchema.safeParse(
+ _pageSubscriber.page.workspace,
+ );
- await emailClient.sendPageSubscription({
- to: _pageSubscriber.email,
- token: _pageSubscriber.token,
- page: _pageSubscriber.page.title,
- // TODO: or use custom domain
- domain: _pageSubscriber.page.slug,
+ if (!workspace.success) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Workspace not found",
});
}
+ if (!workspace.data.limits["status-subscribers"]) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: "Upgrade to use status subscribers",
+ });
+ }
+
+ await emailClient.sendPageSubscription({
+ to: _pageSubscriber.email,
+ token: _pageSubscriber.token,
+ page: _pageSubscriber.page.title,
+ // TODO: or use custom domain
+ domain: _pageSubscriber.page.slug,
+ });
}),
});
diff --git a/packages/api/src/router/statusPage.ts b/packages/api/src/router/statusPage.ts
index 1058eaf9b..a1092b269 100644
--- a/packages/api/src/router/statusPage.ts
+++ b/packages/api/src/router/statusPage.ts
@@ -1,6 +1,6 @@
import { z } from "zod";
-import { and, eq, inArray, sql } from "@openstatus/db";
+import { and, eq, inArray, isNotNull, sql } from "@openstatus/db";
import {
maintenance,
monitorsToPages,
@@ -8,6 +8,7 @@ import {
pageSubscriber,
selectPublicMonitorSchema,
selectPublicPageSchemaWithRelation,
+ selectWorkspaceSchema,
statusReport,
} from "@openstatus/db/src/schema";
@@ -533,15 +534,35 @@ export const statusPageRouter = createTRPCRouter({
},
});
- if (!_page) return null;
+ if (!_page) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Page not found",
+ });
+ }
+
+ const workspace = selectWorkspaceSchema.safeParse(_page.workspace);
- if (_page.workspace.plan === "free") return null;
+ if (!workspace.success) {
+ throw new TRPCError({
+ code: "BAD_REQUEST",
+ message: "Workspace data is invalid",
+ });
+ }
+
+ if (!workspace.data.limits["status-subscribers"]) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: "Upgrade to use status subscribers",
+ });
+ }
const _alreadySubscribed =
await opts.ctx.db.query.pageSubscriber.findFirst({
where: and(
eq(pageSubscriber.pageId, _page.id),
eq(pageSubscriber.email, opts.input.email),
+ isNotNull(pageSubscriber.acceptedAt),
),
});