The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix workspace delete cron
Steven Tey9851d141b75b
diff --git a/apps/web/app/api/cron/workspaces/delete/route.ts b/apps/web/app/api/cron/workspaces/delete/route.tsindex 25c17bd5c1..6567ea90c3 100644--- a/apps/web/app/api/cron/workspaces/delete/route.ts+++ b/apps/web/app/api/cron/workspaces/delete/route.ts@@ -43,7 +43,7 @@ export async function POST(req: Request) {});if (links.length > 0) {- await Promise.all([+ const res = await Promise.all([prisma.link.deleteMany({where: {id: {@@ -57,6 +57,8 @@ export async function POST(req: Request) {links,}),]);++ console.log(res);}const remainingLinks = await prisma.link.count({@@ -71,7 +73,9 @@ export async function POST(req: Request) {delay: 2,});- return new Response("Workspace links deletion queued.");+ return new Response(+ `Deleted ${links.length} links, ${remainingLinks} remaining. Starting next batch...`,+ );}// Delete the custom domains@@ -98,7 +102,9 @@ export async function POST(req: Request) {where: { id: workspace.id },});- return new Response("Workspace deleted.");+ return new Response(+ `Deleted ${links.length} links, no more links remaining. Workspace deleted.`,+ );} catch (error) {return handleAndReturnErrorResponse(error);}diff --git a/apps/web/lib/api/workspaces.ts b/apps/web/lib/api/workspaces.tsindex c2cc61a48e..920cd56f9c 100644--- a/apps/web/lib/api/workspaces.ts+++ b/apps/web/lib/api/workspaces.ts@@ -12,7 +12,6 @@ import { waitUntil } from "@vercel/functions";import { qstash } from "../cron";import { cancelSubscription } from "../stripe/cancel-subscription";import { markDomainAsDeleted } from "./domains";-import { linkCache } from "./links/cache";export async function deleteWorkspace(workspace: Pick<WorkspaceProps, "id" | "slug" | "logo" | "stripeId">,@@ -34,59 +33,36 @@ export async function deleteWorkspace(defaultWorkspace: null,},}),+ ]);- // Remove the API keys- prisma.restrictedToken.deleteMany({- where: {- projectId: workspace.id,- },- }),+ waitUntil(+ Promise.all([+ // Remove the API keys+ prisma.restrictedToken.deleteMany({+ where: {+ projectId: workspace.id,+ },+ }),- // Cancel the workspace's Stripe subscription- workspace.stripeId && cancelSubscription(workspace.stripeId),+ // Cancel the workspace's Stripe subscription+ workspace.stripeId && cancelSubscription(workspace.stripeId),- // Delete workspace logo if it's a custom logo stored in R2- workspace.logo &&- workspace.logo.startsWith(`${R2_URL}/logos/${workspace.id}`) &&- storage.delete(workspace.logo.replace(`${R2_URL}/`, "")),- ]);+ // Delete workspace logo if it's a custom logo stored in R2+ workspace.logo &&+ workspace.logo.startsWith(`${R2_URL}/logos/${workspace.id}`) &&+ storage.delete(workspace.logo.replace(`${R2_URL}/`, "")),- waitUntil(- queueWorkspaceDeletion({- workspaceId: workspace.id,- }),+ // queue the workspace for deletion+ queueWorkspaceDeletion({+ workspaceId: workspace.id,+ }),+ ]),);}export async function deleteWorkspaceAdmin(workspace: Pick<WorkspaceProps, "id" | "slug" | "logo" | "stripeId">,) {- const [customDomains, defaultDomainLinks] = await Promise.all([- prisma.domain.findMany({- where: {- projectId: workspace.id,- },- select: {- slug: true,- },- }),- prisma.link.findMany({- where: {- projectId: workspace.id,- domain: {- in: DUB_DOMAINS_ARRAY,- },- },- }),- ]);-- const updateLinkRedisResponse = await linkCache.mset(- defaultDomainLinks.map((link) => ({- ...link,- projectId: LEGAL_WORKSPACE_ID,- })),- );-// update all default domain links to the legal workspaceconst updateLinkPrismaResponse = await prisma.link.updateMany({where: {@@ -101,6 +77,15 @@ export async function deleteWorkspaceAdmin(},});+ const customDomains = await prisma.domain.findMany({+ where: {+ projectId: workspace.id,+ },+ select: {+ slug: true,+ },+ });+// delete all domains, links, and uploaded images associated with the workspaceconst deleteDomainsLinksResponse = await Promise.allSettled(customDomains.map(({ slug }) =>@@ -127,7 +112,6 @@ export async function deleteWorkspaceAdmin(]);return {- updateLinkRedisResponse,updateLinkPrismaResponse,deleteDomainsLinksResponse,deleteWorkspaceResponse,