Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

fix notification email setup

Steven Tey7524c0c04ecb
diff --git a/apps/web/app/api/cron/links/event/route.ts b/apps/web/app/api/cron/links/event/route.ts
index 5b32667445..be9dcb2798 100644
--- a/apps/web/app/api/cron/links/event/route.ts
+++ b/apps/web/app/api/cron/links/event/route.ts
@@ -1,5 +1,5 @@
import { getAnalytics } from "@/lib/analytics";
-import { limiter, qstash, receiver } from "@/lib/cron";
+import { qstash, receiver, sendLimitEmail } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { recordLink } from "@/lib/tinybird";
import { WorkspaceProps } from "@/lib/types";
@@ -10,8 +10,6 @@ import {
getApexDomain,
log,
} from "@dub/utils";
-import { sendEmail } from "emails";
-import LinksLimitAlert from "emails/links-limit";
export async function POST(req: Request) {
const body = await req.json();
@@ -95,45 +93,54 @@ export async function POST(req: Request) {
(workspace.linksUsage / workspace.linksLimit) * 100,
);
+ // send alert if 80% or 100% of links limit is reached
if (percentage === 80 || percentage === 100) {
- const users = await prisma.user.findMany({
+ // check if the alert has already been sent
+ const sentNotification = await prisma.sentEmail.findFirst({
where: {
- projects: {
- some: {
- projectId: workspace.id,
- },
- },
- },
- select: {
- email: true,
+ projectId: workspace.id,
+ type:
+ percentage === 80
+ ? "firstLinksLimitEmail"
+ : "secondLinksLimitEmail",
},
});
- const emails = users.map(({ email }) => email) as string[];
-
- await Promise.allSettled([
- emails.map((email) => {
- limiter.schedule(() =>
- sendEmail({
- subject: `${process.env.NEXT_PUBLIC_APP_NAME} Alert: ${
- workspace.name
- } has used ${percentage.toString()}% of its links limit for the month.`,
- email,
- react: LinksLimitAlert({
- email,
- workspace: workspace as Partial<WorkspaceProps>,
- }),
- }),
- );
- }),
- log({
- message: `*${
- workspace.slug
- }* has used ${percentage.toString()}% of its links limit for the month.`,
- type: workspace.plan === "free" ? "cron" : "alerts",
- mention: workspace.plan !== "free",
- }),
- ]);
+ // if not, send the alert
+ if (!sentNotification) {
+ const users = await prisma.user.findMany({
+ where: {
+ projects: {
+ some: {
+ projectId: workspace.id,
+ },
+ },
+ },
+ select: {
+ email: true,
+ },
+ });
+
+ const emails = users.map(({ email }) => email) as string[];
+
+ await Promise.all([
+ sendLimitEmail({
+ emails,
+ workspace: workspace as unknown as WorkspaceProps,
+ type:
+ percentage === 80
+ ? "firstLinksLimitEmail"
+ : "secondLinksLimitEmail",
+ }),
+ log({
+ message: `*${
+ workspace.slug
+ }* has used ${percentage.toString()}% of its links limit for the month.`,
+ type: workspace.plan === "free" ? "cron" : "alerts",
+ mention: workspace.plan !== "free",
+ }),
+ ]);
+ }
}
}
diff --git a/apps/web/app/api/cron/usage/utils.ts b/apps/web/app/api/cron/usage/utils.ts
index 9f592090bc..246760bd52 100644
--- a/apps/web/app/api/cron/usage/utils.ts
+++ b/apps/web/app/api/cron/usage/utils.ts
@@ -1,5 +1,5 @@
import { getAnalytics } from "@/lib/analytics";
-import { limiter, qstash } from "@/lib/cron";
+import { limiter, qstash, sendLimitEmail } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { WorkspaceProps } from "@/lib/types";
import {
@@ -10,7 +10,6 @@ import {
log,
} from "@dub/utils";
import { sendEmail } from "emails";
-import ClicksExceeded from "emails/clicks-exceeded";
import ClicksSummary from "emails/clicks-summary";
const limit = 250;
@@ -24,16 +23,7 @@ export const updateUsage = async (skip?: number) => {
},
},
},
- select: {
- id: true,
- name: true,
- slug: true,
- usage: true,
- usageLimit: true,
- linksUsage: true,
- linksLimit: true,
- plan: true,
- billingCycleStart: true,
+ include: {
users: {
select: {
user: true,
@@ -45,7 +35,6 @@ export const updateUsage = async (skip?: number) => {
},
},
sentEmails: true,
- createdAt: true,
},
skip: skip || 0,
take: limit,
@@ -88,8 +77,11 @@ export const updateUsage = async (skip?: number) => {
(email) => email.type === "firstUsageLimitEmail",
);
if (!sentFirstUsageLimitEmail) {
- // @ts-ignore
- sendUsageLimitEmail(emails, workspace, "first");
+ sendLimitEmail({
+ emails,
+ workspace: workspace as unknown as WorkspaceProps,
+ type: "firstUsageLimitEmail",
+ });
} else {
const sentSecondUsageLimitEmail = sentEmails.some(
(email) => email.type === "secondUsageLimitEmail",
@@ -101,8 +93,11 @@ export const updateUsage = async (skip?: number) => {
(1000 * 3600 * 24),
);
if (daysSinceFirstEmail >= 3) {
- // @ts-ignore
- sendUsageLimitEmail(emails, workspace, "second");
+ sendLimitEmail({
+ emails,
+ workspace: workspace as unknown as WorkspaceProps,
+ type: "secondUsageLimitEmail",
+ });
}
}
}
@@ -215,7 +210,12 @@ export const updateUsage = async (skip?: number) => {
sentEmails: {
deleteMany: {
type: {
- in: ["firstUsageLimitEmail", "secondUsageLimitEmail"],
+ in: [
+ "firstUsageLimitEmail",
+ "secondUsageLimitEmail",
+ "firstLinksLimitEmail",
+ "secondLinksLimitEmail",
+ ],
},
},
},
@@ -231,35 +231,3 @@ export const updateUsage = async (skip?: number) => {
method: "GET",
});
};
-
-const sendUsageLimitEmail = async (
- emails: string[],
- workspace: WorkspaceProps,
- type: "first" | "second",
-) => {
- return await Promise.allSettled([
- emails.map((email) => {
- limiter.schedule(() =>
- sendEmail({
- subject: `${process.env.NEXT_PUBLIC_APP_NAME} Alert: Clicks Limit Exceeded`,
- email,
- react: ClicksExceeded({
- email,
- workspace,
- type,
- }),
- }),
- );
- }),
- prisma.sentEmail.create({
- data: {
- project: {
- connect: {
- slug: workspace.slug,
- },
- },
- type: `${type}UsageLimitEmail`,
- },
- }),
- ]);
-};
diff --git a/apps/web/emails/clicks-exceeded.tsx b/apps/web/emails/clicks-exceeded.tsx
index a29f0830d7..0af38b2e83 100644
--- a/apps/web/emails/clicks-exceeded.tsx
+++ b/apps/web/emails/clicks-exceeded.tsx
@@ -25,11 +25,11 @@ export default function ClicksExceeded({
usageLimit: 1000,
plan: "business",
},
- type = "first",
+ type = "firstUsageLimitEmail",
}: {
email: string;
workspace: Partial<WorkspaceProps>;
- type: "first" | "second";
+ type: "firstUsageLimitEmail" | "secondUsageLimitEmail";
}) {
const { slug, name, usage, usageLimit, plan } = workspace;
const nextPlan = getNextPlan(plan as string);
@@ -94,7 +94,7 @@ export default function ClicksExceeded({
</Section>
<Text className="text-sm leading-6 text-black">
To respect your inbox,{" "}
- {type === "first"
+ {type === "firstUsageLimitEmail"
? "we will only send you one more email about this in 3 days"
: "this will be the last time we'll email you about this for the current billing cycle"}
. Feel free to ignore this email if you don't plan on upgrading,
diff --git a/apps/web/lib/cron.ts b/apps/web/lib/cron.ts
index f2cc694b81..263ae6782a 100644
--- a/apps/web/lib/cron.ts
+++ b/apps/web/lib/cron.ts
@@ -1,5 +1,10 @@
import { Client, Receiver } from "@upstash/qstash";
import Bottleneck from "bottleneck";
+import { sendEmail } from "emails";
+import ClicksExceeded from "emails/clicks-exceeded";
+import LinksLimitAlert from "emails/links-limit";
+import prisma from "./prisma";
+import { WorkspaceProps } from "./types";
export const limiter = new Bottleneck({
maxConcurrent: 1, // maximum concurrent requests
@@ -32,3 +37,52 @@ export const verifySignature = async (req: Request) => {
}
return true;
};
+
+export const sendLimitEmail = async ({
+ emails,
+ workspace,
+ type,
+}: {
+ emails: string[];
+ workspace: WorkspaceProps;
+ type:
+ | "firstUsageLimitEmail"
+ | "secondUsageLimitEmail"
+ | "firstLinksLimitEmail"
+ | "secondLinksLimitEmail";
+}) => {
+ const percentage = Math.round(
+ (workspace.linksUsage / workspace.linksLimit) * 100,
+ );
+
+ return await Promise.allSettled([
+ emails.map((email) => {
+ limiter.schedule(() =>
+ sendEmail({
+ subject: type.endsWith("UsageLimitEmail")
+ ? `${process.env.NEXT_PUBLIC_APP_NAME} Alert: Clicks Limit Exceeded`
+ : `${process.env.NEXT_PUBLIC_APP_NAME} Alert: ${
+ workspace.name
+ } has used ${percentage.toString()}% of its links limit for the month.`,
+ email,
+ react: type.endsWith("UsageLimitEmail")
+ ? ClicksExceeded({
+ email,
+ workspace,
+ type: type as "firstUsageLimitEmail" | "secondUsageLimitEmail",
+ })
+ : LinksLimitAlert({
+ email,
+ workspace,
+ }),
+ }),
+ );
+ }),
+ prisma.sentEmail.create({
+ data: {
+ projectId: workspace.id,
+ type,
+ },
+ }),
+ ]);
+};