-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathroute.ts
More file actions
157 lines (139 loc) · 4.04 KB
/
Copy pathroute.ts
File metadata and controls
157 lines (139 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {
AccountNotFoundError,
getSocialProfile,
} from "@/lib/api/scrape-creators/get-social-profile";
import { qstash } from "@/lib/cron";
import { withCron } from "@/lib/cron/with-cron";
import { prisma } from "@dub/prisma";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { subDays } from "date-fns";
import * as z from "zod/v4";
import { logAndRespond } from "../utils";
export const dynamic = "force-dynamic";
const BATCH_SIZE = 50;
const schema = z.object({
startingAfter: z.string().optional(),
});
/**
* This route is used to update stats for verified Instagram, TikTok, and Twitter partners using the ScrapeCreators API
* Runs once a day at 06:00 AM UTC (cron expression: 0 6 * * *)
* POST /api/cron/partner-platforms
*/
export const POST = withCron(async ({ rawBody }) => {
if (!process.env.SCRAPECREATORS_API_KEY) {
throw new Error("SCRAPECREATORS_API_KEY is not defined");
}
let { startingAfter } = schema.parse(
rawBody ? JSON.parse(rawBody) : { startingAfter: undefined },
);
const verifiedProfiles = await prisma.partnerPlatform.findMany({
where: {
type: {
in: ["instagram", "tiktok", "twitter"],
},
verifiedAt: {
not: null,
},
// only check platforms that haven't been checked in the last 7 days
OR: [
{
lastCheckedAt: {
lt: subDays(new Date(), 7),
},
},
{
lastCheckedAt: null,
},
],
// only check partners approved/trusted in the network
partner: {
networkStatus: {
in: ["approved", "trusted"],
},
},
},
take: BATCH_SIZE,
...(startingAfter && {
cursor: {
id: startingAfter,
},
skip: 1,
}),
orderBy: {
id: "asc",
},
});
if (verifiedProfiles.length === 0) {
return logAndRespond(
"No more verified social profiles found. Finished updating social platform stats.",
);
}
await Promise.allSettled(
verifiedProfiles.map(async (verifiedProfile) => {
if (!verifiedProfile.identifier || !verifiedProfile.type) {
return;
}
try {
const socialProfile = await getSocialProfile({
platform: verifiedProfile.type,
handle: verifiedProfile.identifier,
});
const newStats = {
subscribers: socialProfile.subscribers,
posts: socialProfile.posts,
avatarUrl: socialProfile.avatarUrl,
};
await prisma.partnerPlatform.update({
where: {
id: verifiedProfile.id,
},
data: {
...newStats,
lastCheckedAt: new Date(),
},
});
console.log(
`Updated ${verifiedProfile.type} stats for @${verifiedProfile.identifier}`,
newStats,
);
} catch (error) {
// If account doesn't exist, unverify the platform
if (error instanceof AccountNotFoundError) {
await prisma.partnerPlatform.update({
where: {
id: verifiedProfile.id,
},
data: {
verifiedAt: null,
lastCheckedAt: new Date(),
},
});
console.log(
`Account @${verifiedProfile.identifier} on ${verifiedProfile.type} no longer exists. Unverified platform.`,
);
return;
}
console.error(
`Error updating ${verifiedProfile.type} stats for @${verifiedProfile.identifier}:`,
error,
);
}
}),
);
if (verifiedProfiles.length === BATCH_SIZE) {
startingAfter = verifiedProfiles[verifiedProfiles.length - 1].id;
await qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/partner-platforms`,
method: "POST",
body: {
startingAfter,
},
});
return logAndRespond(
`Processed ${BATCH_SIZE} profiles. Scheduled next batch (startingAfter: ${startingAfter}).`,
);
}
return logAndRespond(
"Finished updating social platform stats for all verified profiles.",
);
});