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.

throwIfNegativeNetworkBalance

Steven Tey47a6b18fd326
diff --git a/apps/web/lib/actions/partners/generate-paypal-oauth-url.ts b/apps/web/lib/actions/partners/generate-paypal-oauth-url.ts
index 3b82061206..e34535d2e0 100644
--- a/apps/web/lib/actions/partners/generate-paypal-oauth-url.ts
+++ b/apps/web/lib/actions/partners/generate-paypal-oauth-url.ts
@@ -6,6 +6,7 @@ import { paypalOAuthProvider } from "@/lib/paypal/oauth";
import { COUNTRIES } from "@dub/utils";
import { PartnerPayoutMethod } from "@prisma/client";
import { authPartnerActionClient } from "../safe-action";
+import { throwIfNegativeNetworkBalance } from "./throw-if-negative-network-balance";
export const generatePaypalOAuthUrl = authPartnerActionClient.action(
async ({ ctx }) => {
@@ -32,6 +33,9 @@ export const generatePaypalOAuthUrl = authPartnerActionClient.action(
);
}
+ // edge case: cannot switch to PayPal if partner has negative network balance
+ await throwIfNegativeNetworkBalance(partner.id);
+
return {
url: await paypalOAuthProvider.generateAuthUrl(user.id),
};
diff --git a/apps/web/lib/actions/partners/set-default-payout-method.ts b/apps/web/lib/actions/partners/set-default-payout-method.ts
index 70618d9702..5e40b4bb46 100644
--- a/apps/web/lib/actions/partners/set-default-payout-method.ts
+++ b/apps/web/lib/actions/partners/set-default-payout-method.ts
@@ -11,8 +11,10 @@ import {
import { getPayoutMethodLabel } from "@/ui/partners/payouts/payout-method-config";
import { sendEmail } from "@dub/email";
import DefaultPayoutMethodChanged from "@dub/email/templates/default-payout-method-changed";
+import { PartnerPayoutMethod } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { authPartnerActionClient } from "../safe-action";
+import { throwIfNegativeNetworkBalance } from "./throw-if-negative-network-balance";
export const setDefaultPayoutMethodAction = authPartnerActionClient
.inputSchema(setDefaultPayoutMethodSchema)
@@ -42,6 +44,10 @@ export const setDefaultPayoutMethodAction = authPartnerActionClient
throw new Error("This is already your default payout method.");
}
+ if (payoutMethod.type === PartnerPayoutMethod.paypal) {
+ await throwIfNegativeNetworkBalance(partner.id);
+ }
+
const { activePayoutMethods } = await recomputePartnerPayoutState(partner);
if (!activePayoutMethods.includes(type)) {
throw new Error(
diff --git a/apps/web/lib/actions/partners/throw-if-negative-network-balance.ts b/apps/web/lib/actions/partners/throw-if-negative-network-balance.ts
new file mode 100644
index 0000000000..e3ce1e6c42
--- /dev/null
+++ b/apps/web/lib/actions/partners/throw-if-negative-network-balance.ts
@@ -0,0 +1,23 @@
+import { prisma } from "@/lib/prisma";
+import { NETWORK_PROGRAM_ID } from "@dub/utils";
+import { PayoutStatus } from "@prisma/client";
+
+export async function throwIfNegativeNetworkBalance(partnerId: string) {
+ // edge case: cannot switch to PayPal if partner has negative network balance
+ const negativeNetworkPayoutsCount = await prisma.payout.count({
+ where: {
+ partnerId,
+ programId: NETWORK_PROGRAM_ID,
+ status: PayoutStatus.processed, // if sent/completed, means that the negative balance was fulfilled
+ amount: {
+ lt: 0,
+ },
+ },
+ });
+
+ if (negativeNetworkPayoutsCount > 0) {
+ throw new Error(
+ "You cannot perform this action since you have a negative network balance.",
+ );
+ }
+}