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.

syncUserPlanToPlain

Steven Teya85063144366
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.ts
index 145d986d36..bd00b4718a 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
@@ -1,6 +1,6 @@
import { deleteWorkspaceFolders } from "@/lib/api/folders/delete-workspace-folders";
import { tokenCache } from "@/lib/auth/token-cache";
-import { syncCustomerPlanToPlain } from "@/lib/plain/sync-customer-plan";
+import { syncUserPlanToPlain } from "@/lib/plain/sync-user-plan";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
import { WorkspaceProps } from "@/lib/types";
import { webhookCache } from "@/lib/webhook/cache";
@@ -168,11 +168,7 @@ export async function updateWorkspacePlan({
updatedWorkspace.value.users.length
) {
const workspaceOwner = updatedWorkspace.value.users[0].user;
- waitUntil(
- syncCustomerPlanToPlain({
- customer: workspaceOwner,
- }),
- );
+ waitUntil(syncUserPlanToPlain(workspaceOwner));
}
} else if (workspace.paymentFailedAt) {
await prisma.project.update({
diff --git a/apps/web/app/api/callback/plain/workspace/route.ts b/apps/web/app/api/callback/plain/workspace/route.ts
index 128321f9d0..a48ed9fcab 100644
--- a/apps/web/app/api/callback/plain/workspace/route.ts
+++ b/apps/web/app/api/callback/plain/workspace/route.ts
@@ -1,5 +1,5 @@
import { prefixWorkspaceId } from "@/lib/api/workspaces/workspace-id";
-import { syncCustomerPlanToPlain } from "@/lib/plain/sync-customer-plan";
+import { syncUserPlanToPlain } from "@/lib/plain/sync-user-plan";
import { upsertPlainCustomer } from "@/lib/plain/upsert-plain-customer";
import { prisma } from "@dub/prisma";
import { capitalize, formatDate } from "@dub/utils";
@@ -52,13 +52,9 @@ export async function POST(req: NextRequest) {
name: user.name,
email: user.email,
});
- }
- waitUntil(
- syncCustomerPlanToPlain({
- customer,
- }),
- );
+ waitUntil(syncUserPlanToPlain(user));
+ }
const topWorkspace = await prisma.project.findFirst({
where: {
diff --git a/apps/web/lib/plain/sync-customer-plan.ts b/apps/web/lib/plain/sync-user-plan.ts
similarity index 74%
rename from apps/web/lib/plain/sync-customer-plan.ts
rename to apps/web/lib/plain/sync-user-plan.ts
index d8d205ffd7..e09a3a283d 100644
--- a/apps/web/lib/plain/sync-customer-plan.ts
+++ b/apps/web/lib/plain/sync-user-plan.ts
@@ -3,31 +3,25 @@ import { isGenericEmail } from "../is-generic-email";
import { plain, PlainUser } from "./client";
import { upsertPlainCustomer } from "./upsert-plain-customer";
-export const syncCustomerPlanToPlain = async ({
- customer,
-}: {
- customer: PlainUser;
-}) => {
- if (!customer.email) {
- console.log(
- `Customer email is null for user ${customer.id}, skipping sync...`,
- );
+export const syncUserPlanToPlain = async (user: PlainUser) => {
+ if (!user.email) {
+ console.log(`User ${user.id} has no email, skipping sync...`);
return;
}
await upsertPlainCustomer({
- id: customer.id,
- name: customer.name,
- email: customer.email,
+ id: user.id,
+ name: user.name,
+ email: user.email,
});
const plainCustomer = await plain.getCustomerByExternalId({
- externalId: customer.id,
+ externalId: user.id,
});
let companyDomainName: string | undefined;
- if (!isGenericEmail(customer.email)) {
- companyDomainName = customer.email.split("@")[1];
+ if (!isGenericEmail(user.email)) {
+ companyDomainName = user.email.split("@")[1];
}
const topWorkspace = await prisma.project.findFirst({
@@ -42,14 +36,13 @@ export const syncCustomerPlanToPlain = async ({
},
}
: {
- userId: customer.id,
+ userId: user.id,
},
},
},
orderBy: {
usageLimit: "desc",
},
- take: 1,
});
if (!topWorkspace) {
@@ -84,7 +77,7 @@ export const syncCustomerPlanToPlain = async ({
]);
console.log(
- `Synced customer ${customer.email}'s plan in Plain to ${topWorkspace.plan}`,
+ `Synced user ${user.id}'s plan in Plain to ${topWorkspace.plan}`,
);
}
diff --git a/apps/web/scripts/migrations/backfill-plain-customers.ts b/apps/web/scripts/migrations/backfill-plain-customers.ts
index 7ea06b8d40..fdcde718f2 100644
--- a/apps/web/scripts/migrations/backfill-plain-customers.ts
+++ b/apps/web/scripts/migrations/backfill-plain-customers.ts
@@ -2,7 +2,7 @@ import { GENERIC_EMAIL_DOMAINS } from "@/lib/is-generic-email";
import { prisma } from "@dub/prisma";
import { chunk } from "@dub/utils";
import "dotenv-flow/config";
-import { syncCustomerPlanToPlain } from "../../lib/plain/sync-customer-plan";
+import { syncUserPlanToPlain } from "../../lib/plain/sync-user-plan";
async function main() {
const users = await prisma.user.findMany({
@@ -37,13 +37,7 @@ async function main() {
const chunks = chunk(users, 50);
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
- await Promise.allSettled(
- chunk.map((user) =>
- syncCustomerPlanToPlain({
- customer: user,
- }),
- ),
- );
+ await Promise.allSettled(chunk.map((user) => syncUserPlanToPlain(user)));
console.log(
`Backfilled ${chunk.length} users in batch ${i + 1} of ${chunks.length} (out of ${users.length} total users)`,
);
diff --git a/apps/web/ui/partners/payout-row-menu.tsx b/apps/web/ui/partners/payout-row-menu.tsx
index 9b52f4ba9c..2f847b9fe6 100644
--- a/apps/web/ui/partners/payout-row-menu.tsx
+++ b/apps/web/ui/partners/payout-row-menu.tsx
@@ -1,9 +1,10 @@
import { retryFailedPaypalPayoutsAction } from "@/lib/actions/partners/retry-failed-paypal-payouts";
+import usePartnerProfile from "@/lib/swr/use-partner-profile";
import { PartnerPayoutResponse } from "@/lib/types";
import { useConfirmModal } from "@/ui/modals/confirm-modal";
import { Button, Icon, Popover } from "@dub/ui";
import { Dots, Refresh2 } from "@dub/ui/icons";
-import { cn } from "@dub/utils";
+import { cn, PAYPAL_SUPPORTED_COUNTRIES } from "@dub/utils";
import { Row } from "@tanstack/react-table";
import { Command } from "cmdk";
import { useAction } from "next-safe-action/hooks";
@@ -11,6 +12,7 @@ import { useState } from "react";
import { toast } from "sonner";
export function PayoutRowMenu({ row }: { row: Row<PartnerPayoutResponse> }) {
+ const { partner } = usePartnerProfile();
const [isOpen, setIsOpen] = useState(false);
const { executeAsync: executeRetryPayout, isPending: isRetryPayoutPending } =
@@ -39,7 +41,10 @@ export function PayoutRowMenu({ row }: { row: Row<PartnerPayoutResponse> }) {
confirmText: "Retry payout",
});
- const canRetry = row.original.status === "failed";
+ const canRetry =
+ row.original.status === "failed" &&
+ partner?.country &&
+ PAYPAL_SUPPORTED_COUNTRIES.includes(partner.country);
if (!canRetry) {
return null;