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 plan usage + update script

Steven Tey9efe3d852d89
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsx
index 3e06bde1a5..e67b0b82f7 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsx
@@ -213,9 +213,7 @@ export default function PlanUsage() {
<UsageCategory
title="Partner payouts"
icon={CreditCard}
- usage={
- payoutsUsage !== undefined ? payoutsUsage / 100 : undefined
- }
+ usage={payoutsUsage}
usageLimit={payoutsLimit}
unit="$"
href={
@@ -421,9 +419,10 @@ function UsageCategory(data: {
<div className="flex items-center gap-1.5 text-sm font-medium text-neutral-800">
{usage || usage === 0 ? (
<p>
- {unit && <span>{unit}</span>}
{typeof usage === "number"
- ? nFormatter(usage, { full: true })
+ ? `${unit ?? ""}${nFormatter(usage / (unit === "$" ? 100 : 1), {
+ full: true,
+ })}`
: usage}
</p>
) : (
@@ -435,7 +434,12 @@ function UsageCategory(data: {
<p className="text-neutral-500">
{usageLimit && usageLimit >= INFINITY_NUMBER
? "∞"
- : nFormatter(usageLimit, { full: true })}
+ : `${unit ?? ""}${nFormatter(
+ usageLimit / (unit === "$" ? 100 : 1),
+ {
+ full: true,
+ },
+ )}`}
</p>
</>
)}
diff --git a/apps/web/scripts/update-payouts-limits.ts b/apps/web/scripts/update-payouts-limits.ts
index 2f02538c40..d29f6ec5da 100644
--- a/apps/web/scripts/update-payouts-limits.ts
+++ b/apps/web/scripts/update-payouts-limits.ts
@@ -1,19 +1,81 @@
import { prisma } from "@dub/prisma";
+import { getFirstAndLastDay } from "@dub/utils";
import "dotenv-flow/config";
async function main() {
- const workspaces = await prisma.project.updateMany({
+ // const workspaces = await prisma.project.updateMany({
+ // where: {
+ // plan: {
+ // startsWith: "business",
+ // },
+ // },
+ // data: {
+ // payoutsLimit: 2_500_00,
+ // },
+ // });
+
+ // console.table(workspaces);
+
+ // workspace with payouts in the last 30 days
+ const workspaceWithPayouts = await prisma.invoice.groupBy({
+ by: ["workspaceId"],
where: {
- plan: {
- startsWith: "business",
+ createdAt: {
+ gte: new Date(new Date().setDate(new Date().getDate() - 30)),
},
},
- data: {
- payoutsLimit: 2_500_00,
+ _sum: {
+ amount: true,
},
});
- console.table(workspaces);
+ console.table(workspaceWithPayouts);
+
+ const res = await Promise.all(
+ workspaceWithPayouts.map(async (w) => {
+ if (!w.workspaceId) {
+ return;
+ }
+
+ const workspace = await prisma.project.findUnique({
+ where: { id: w.workspaceId },
+ });
+
+ if (!workspace) {
+ return;
+ }
+
+ const { firstDay } = getFirstAndLastDay(workspace.billingCycleStart);
+
+ const invoices = await prisma.invoice.aggregate({
+ where: {
+ workspaceId: workspace.id,
+ createdAt: {
+ gte: firstDay,
+ },
+ },
+ _sum: {
+ amount: true,
+ },
+ });
+
+ await prisma.project.update({
+ where: { id: workspace.id },
+ data: {
+ payoutsUsage: invoices._sum.amount ?? 0,
+ },
+ });
+
+ return {
+ workspaceId: workspace.id,
+ workspaceName: workspace.name,
+ payoutsUsage: invoices._sum.amount,
+ payoutsLimit: workspace.payoutsLimit,
+ };
+ }),
+ );
+
+ console.table(res);
}
main();