@@ -4,28 +4,24 @@ import { linkCache } from "@/lib/api/links/cache";
44import { verifyQstashSignature } from "@/lib/cron/verify-qstash" ;
55import { recordLink } from "@/lib/tinybird" ;
66import { prisma } from "@dub/prisma" ;
7+ import { Link } from "@dub/prisma/client" ;
8+ import { linkConstructorSimple } from "@dub/utils" ;
79import { z } from "zod" ;
810
911export const dynamic = "force-dynamic" ;
1012
1113const schema = z . object ( {
1214 newDomain : z . string ( ) ,
1315 oldDomain : z . string ( ) ,
14- workspaceId : z . string ( ) ,
15- page : z . number ( ) ,
1616} ) ;
1717
18- const pageSize = 100 ;
19-
2018// POST /api/cron/domains/update
2119export async function POST ( req : Request ) {
2220 try {
2321 const rawBody = await req . text ( ) ;
2422 await verifyQstashSignature ( { req, rawBody } ) ;
2523
26- const { newDomain, oldDomain, workspaceId, page } = schema . parse (
27- JSON . parse ( rawBody ) ,
28- ) ;
24+ const { newDomain, oldDomain } = schema . parse ( JSON . parse ( rawBody ) ) ;
2925
3026 const newDomainRecord = await prisma . domain . findUnique ( {
3127 where : {
@@ -37,46 +33,85 @@ export async function POST(req: Request) {
3733 return new Response ( `Domain ${ newDomain } not found. Skipping update...` ) ;
3834 }
3935
40- const links = await prisma . link . findMany ( {
36+ const linksToUpdate = await prisma . link . findMany ( {
4137 where : {
38+ domain : oldDomain ,
39+ } ,
40+ take : 100 ,
41+ } ) ;
42+
43+ if ( linksToUpdate . length === 0 ) {
44+ return new Response ( "No more links to update. Exiting..." ) ;
45+ }
46+
47+ const linkIdsToUpdate = linksToUpdate . map ( ( link ) => link . id ) ;
48+
49+ await prisma . link . updateMany ( {
50+ where : {
51+ id : {
52+ in : linkIdsToUpdate ,
53+ } ,
54+ } ,
55+ data : {
4256 domain : newDomain ,
4357 } ,
58+ } ) ;
59+
60+ const updatedLinks = await prisma . link . findMany ( {
61+ where : {
62+ id : {
63+ in : linkIdsToUpdate ,
64+ } ,
65+ } ,
4466 include : {
4567 tags : {
4668 select : {
4769 tag : true ,
4870 } ,
4971 } ,
5072 } ,
51- skip : ( page - 1 ) * pageSize ,
52- take : pageSize ,
5373 } ) ;
5474
55- if ( links . length === 0 ) {
56- return new Response ( "No more links to update. Exiting..." ) ;
57- }
58-
59- await Promise . all ( [
60- // rename redis keys
61- linkCache . rename ( {
62- links,
63- oldDomain,
64- } ) ,
65-
66- // update links in Tinybird
67- recordLink ( links ) ,
75+ await Promise . allSettled ( [
76+ // update the `shortLink` field for each of the short links
77+ updateShortLinks ( updatedLinks ) ,
78+ // record new link values in Tinybird (dub_links_metadata)
79+ recordLink ( updatedLinks ) ,
80+ // expire the redis cache for the old links
81+ linkCache . expireMany ( linksToUpdate ) ,
6882 ] ) ;
6983
7084 await queueDomainUpdate ( {
71- workspaceId,
72- oldDomain,
7385 newDomain,
74- page : page + 1 ,
75- delay : 2 ,
86+ oldDomain ,
87+ delay : 1 ,
7688 } ) ;
7789
7890 return new Response ( "Domain's links updated." ) ;
7991 } catch ( error ) {
8092 return handleAndReturnErrorResponse ( error ) ;
8193 }
8294}
95+
96+ // Update the shortLink column for a list of links
97+ const updateShortLinks = async (
98+ links : Pick < Link , "id" | "domain" | "key" > [ ] ,
99+ ) => {
100+ if ( ! links || links . length === 0 ) {
101+ return new Response ( "No links found." ) ;
102+ }
103+
104+ for ( const link of links ) {
105+ await prisma . link . update ( {
106+ where : {
107+ id : link . id ,
108+ } ,
109+ data : {
110+ shortLink : linkConstructorSimple ( {
111+ domain : link . domain ,
112+ key : link . key ,
113+ } ) ,
114+ } ,
115+ } ) ;
116+ }
117+ } ;
0 commit comments