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.

Refactor links usage cron job

Kiran K0fdf4effbe9e
diff --git a/apps/web/app/api/cron/links/usage/route.ts b/apps/web/app/api/cron/links/usage/route.ts
index c329e98eb8..14f628f05b 100644
--- a/apps/web/app/api/cron/links/usage/route.ts
+++ b/apps/web/app/api/cron/links/usage/route.ts
@@ -2,84 +2,36 @@ import { sendLimitEmail, verifySignature } from "@/lib/cron";
import { log } from "@dub/utils";
import prisma from "@/lib/prisma";
import { WorkspaceProps } from "@/lib/types";
+import { Project } from "@prisma/client";
// Cron to update the links usage of each workspace.
// Runs once every 5 mins (*/5 * * * *)
+const take = 30;
+
export async function GET(req: Request) {
const validSignature = await verifySignature(req);
+
if (!validSignature) {
return new Response("Unauthorized", { status: 401 });
}
- try {
- const workspaces = await prisma.project.findMany({
- select: {
- id: true,
- name: true,
- slug: true,
- plan: true,
- linksUsage: true,
- linksLimit: true,
- },
- take: 30,
- });
-
- for (const workspace of workspaces) {
- const percentage = Math.round(
- (workspace.linksUsage / workspace.linksLimit) * 100,
- );
-
- // Skip if the workspace is not close to the limit
- if (![80, 100].includes(percentage)) {
- continue;
- }
+ const workspaces = await prisma.project.findMany({
+ select: {
+ id: true,
+ name: true,
+ slug: true,
+ plan: true,
+ linksUsage: true,
+ linksLimit: true,
+ },
+ take,
+ });
- const sentNotification = await prisma.sentEmail.count({
- where: {
- projectId: workspace.id,
- type:
- percentage === 80
- ? "firstLinksLimitEmail"
- : "secondLinksLimitEmail",
- },
- });
-
- // Skip if the email has already been sent
- if (sentNotification) {
- continue;
- }
-
- const users = await prisma.user.findMany({
- where: {
- projects: {
- some: {
- projectId: workspace.id,
- },
- },
- },
- select: {
- email: true,
- },
- });
-
- const emails = users.map(({ email }) => email) as string[];
-
- await sendLimitEmail({
- emails,
- workspace: workspace 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",
- });
- }
+ try {
+ await Promise.allSettled(
+ workspaces.map((workspace) => sendUsageEmail(workspace)),
+ );
return new Response("OK");
} catch (error) {
@@ -91,3 +43,65 @@ export async function GET(req: Request) {
return new Response(error.message, { status: 500 });
}
}
+
+// Check if the workspace is close to the limit and send an email to the users
+const sendUsageEmail = async (
+ workspace: Pick<
+ Project,
+ "id" | "name" | "slug" | "plan" | "linksUsage" | "linksLimit"
+ >,
+) => {
+ const percentage = Math.round(
+ (workspace.linksUsage / workspace.linksLimit) * 100,
+ );
+
+ // Skip if the workspace is not close to the limit
+ if (![80, 100].includes(percentage)) {
+ return;
+ }
+
+ const sentNotification = await prisma.sentEmail.count({
+ where: {
+ projectId: workspace.id,
+ type:
+ percentage === 80 ? "firstLinksLimitEmail" : "secondLinksLimitEmail",
+ },
+ });
+
+ // Skip if the email has already been sent
+ if (sentNotification) {
+ return;
+ }
+
+ 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.allSettled([
+ sendLimitEmail({
+ emails,
+ workspace: workspace 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",
+ }),
+ ]);
+};