The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
address CR feedback
Steven Teyf6ad45b346ae
diff --git a/apps/web/app/(ee)/api/cron/usage/utils.ts b/apps/web/app/(ee)/api/cron/usage/utils.tsindex 335fcf0542..7be35daf02 100644--- a/apps/web/app/(ee)/api/cron/usage/utils.ts+++ b/apps/web/app/(ee)/api/cron/usage/utils.ts@@ -73,15 +73,8 @@ export const updateUsage = async () => {const { usage, usageLimit, plan, planPeriod, billingCycleEndsAt } =workspace;- /*- We only reset clicks usage if it's not over usageLimit by:- - 4x for free plan (4K clicks)- - 2x for all other plans- */- let resetUsage =- plan === "free" ? usage <= usageLimit * 4 : usage <= usageLimit * 2;-// if yearly plan, we skip resetting usage if the billing cycle end date is not this year+month+ let resetUsage = true;if (planPeriod === "yearly" &&billingCycleEndsAt &&@@ -94,12 +87,20 @@ export const updateUsage = async () => {}if (resetUsage) {+ /*+ We only reset events usage if it's not over usageLimit by:+ - 4x for free plan (4K events)+ - 2x for all other plans+ */+ const resetEventsUsage =+ plan === "free" ? usage <= usageLimit * 4 : usage <= usageLimit * 2;+await prisma.project.update({where: {id: workspace.id,},data: {- usage: 0,+ usage: resetEventsUsage ? 0 : undefined,linksUsage: 0,payoutsUsage: 0,aiUsage: 0,@@ -117,6 +118,7 @@ export const updateUsage = async () => {},},});+ console.log(`Reset usage for workspace "${workspace.slug}"`);}/* Only send the 30-day summary email if:diff --git a/apps/web/app/(ee)/api/stripe/webhook/utils/update-workspace-plan.ts b/apps/web/app/(ee)/api/stripe/webhook/utils/update-workspace-plan.tsindex 08c4162864..a3c90f3dc8 100644--- a/apps/web/app/(ee)/api/stripe/webhook/utils/update-workspace-plan.ts+++ b/apps/web/app/(ee)/api/stripe/webhook/utils/update-workspace-plan.ts@@ -21,8 +21,7 @@ import { webhookCache } from "@/lib/webhook/cache";import { sendBatchEmail } from "@dub/email";import AdvancedPlanDowngradeNotice from "@dub/email/templates/advanced-plan-downgrade-notice";import UpgradeEmail from "@dub/email/templates/upgrade-email";-import { getPlanAndTierFromPriceId } from "@dub/utils";-import { NEW_BUSINESS_PRICE_IDS } from "@dub/utils/src";+import { getPlanAndTierFromPriceId, NEW_BUSINESS_PRICE_IDS } from "@dub/utils";import type Stripe from "stripe";import { getPlanPeriodFromStripeSubscription } from "./get-plan-period-from-stripe-subscription";import { getWorkspaceLimitsFromStripeSubscription } from "./get-workspace-limits-from-stripe-subscription";diff --git a/apps/web/scripts/migrations/update-yearly-workspace-usage.ts b/apps/web/scripts/migrations/update-yearly-workspace-usage.tsindex e9d7462780..4e9d8ec330 100644--- a/apps/web/scripts/migrations/update-yearly-workspace-usage.ts+++ b/apps/web/scripts/migrations/update-yearly-workspace-usage.ts@@ -1,10 +1,9 @@import { getAnalytics } from "@/lib/analytics/get-analytics";import { prisma } from "@/lib/prisma";+import { chunk } from "@dub/utils";import { subYears } from "date-fns";import "dotenv-flow/config";-const BATCH_SIZE = 50;-// TODO: will need to also double check that billingCycleEndsAt is actually reliableasync function main() {while (true) {@@ -17,7 +16,7 @@ async function main() {not: null,},},- take: BATCH_SIZE,+ take: 500,});if (workspaces.length === 0) {@@ -25,74 +24,79 @@ async function main() {break;}- await Promise.allSettled(- workspaces.map(async (workspace) => {- const billingStart = subYears(workspace.billingCycleEndsAt!, 1);+ const workspaceChunks = chunk(workspaces, 10);- const [clicksResponse, linksUsage, payoutsUsage] = await Promise.all([- getAnalytics({- workspaceId: workspace.id,- event: "clicks",- start: billingStart,- end: new Date(),- dataAvailableFrom: workspace.createdAt,- }),- prisma.link.count({- where: {- projectId: workspace.id,- createdAt: {- gte: billingStart,- },- },- }),- prisma.invoice.aggregate({- where: {+ for (const workspaceChunk of workspaceChunks) {+ await Promise.allSettled(+ workspaceChunk.map(async (workspace) => {+ const billingStart = subYears(workspace.billingCycleEndsAt!, 1);++ const [clicksResponse, linksUsage, payoutsUsage] = await Promise.all([+ getAnalytics({workspaceId: workspace.id,- type: "partnerPayout",- status: "completed",- paidAt: {- gte: billingStart,+ event: "clicks",+ groupBy: "count",+ start: billingStart,+ end: new Date(),+ dataAvailableFrom: workspace.createdAt,+ }),+ prisma.link.count({+ where: {+ projectId: workspace.id,+ createdAt: {+ gte: billingStart,+ },},- },- _sum: {- amount: true,- },- }),- ]);+ }),+ prisma.invoice.aggregate({+ where: {+ workspaceId: workspace.id,+ type: "partnerPayout",+ status: "completed",+ paidAt: {+ gte: billingStart,+ },+ },+ _sum: {+ amount: true,+ },+ }),+ ]);- const usage =- typeof clicksResponse === "object" && clicksResponse !== null- ? (clicksResponse as { clicks?: number }).clicks ?? 0- : typeof clicksResponse === "number"- ? clicksResponse- : 0;+ const usage =+ typeof clicksResponse === "object" && clicksResponse !== null+ ? (clicksResponse as { clicks?: number }).clicks ?? 0+ : typeof clicksResponse === "number"+ ? clicksResponse+ : 0;- const newUsageLimit = workspace.usageLimit * 12;- const newLinksLimit = workspace.linksLimit * 12;- const newPayoutsLimit = workspace.payoutsLimit * 12;- const newAiLimit = workspace.aiLimit * 12;- const newPayoutsUsage = payoutsUsage._sum.amount ?? 0;+ const newUsageLimit = workspace.usageLimit * 12;+ const newLinksLimit = workspace.linksLimit * 12;+ const newPayoutsLimit = workspace.payoutsLimit * 12;+ const newAiLimit = workspace.aiLimit * 12;+ const newPayoutsUsage = payoutsUsage._sum.amount ?? 0;- await prisma.project.update({- where: {- id: workspace.id,- },- data: {- usage,- linksUsage,- payoutsUsage: newPayoutsUsage,- usageLimit: newUsageLimit,- linksLimit: newLinksLimit,- payoutsLimit: newPayoutsLimit,- aiLimit: newAiLimit,- },- });+ await prisma.project.update({+ where: {+ id: workspace.id,+ },+ data: {+ usage,+ linksUsage,+ payoutsUsage: newPayoutsUsage,+ usageLimit: newUsageLimit,+ linksLimit: newLinksLimit,+ payoutsLimit: newPayoutsLimit,+ aiLimit: newAiLimit,+ },+ });- console.log(- `Updated ${workspace.slug}: clicks ${usage}/${newUsageLimit}, links ${linksUsage}/${newLinksLimit}, payouts ${newPayoutsUsage}/${newPayoutsLimit}, ai ${workspace.aiUsage}/${newAiLimit}`,- );- }),- );+ console.log(+ `Updated ${workspace.slug}: clicks ${usage}/${newUsageLimit}, links ${linksUsage}/${newLinksLimit}, payouts ${newPayoutsUsage}/${newPayoutsLimit}, ai ${workspace.aiUsage}/${newAiLimit}`,+ );+ }),+ );+ }}}