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.

update implementation

Steven Tey3af6c95ecab9
diff --git a/apps/web/app/api/analytics/[endpoint]/export/route.ts b/apps/web/app/api/analytics/[endpoint]/export/route.ts
index 1106801916..4ac645202b 100644
--- a/apps/web/app/api/analytics/[endpoint]/export/route.ts
+++ b/apps/web/app/api/analytics/[endpoint]/export/route.ts
@@ -62,59 +62,60 @@ export const GET = withAuth(
...parsedParams,
});
- const topLinks = await Promise.all(
- data.map(async ({ linkId }) => {
- const link = await prisma.link.findUnique({
- where: {
- projectId: workspace.id,
- id: linkId,
+ const [links, domains] = await Promise.all([
+ prisma.link.findMany({
+ where: {
+ projectId: workspace.id,
+ id: {
+ in: data.map(({ link }) => link),
},
- select: {
- id: true,
- domain: true,
- key: true,
- clicks: true,
- url: true,
+ },
+ select: {
+ id: true,
+ domain: true,
+ key: true,
+ url: true,
+ },
+ }),
+ prisma.domain.findMany({
+ where: {
+ projectId: workspace.id,
+ id: {
+ in: data.map(({ link }) => link),
},
- });
+ },
+ select: {
+ id: true,
+ slug: true,
+ target: true,
+ },
+ }),
+ ]);
- if (link && link.key !== "_root")
- return {
- linkId: link.id,
- shortLink: linkConstructor({
- domain: link.domain,
- key: link.key,
- }),
- domain: link.domain,
- key: link.key,
- url: link.url,
- clicks: link.clicks,
- };
- else {
- const domainLink = await prisma.domain.findUnique({
- where: {
- projectId: workspace.id,
- id: linkId,
- },
- select: {
- id: true,
- slug: true,
- clicks: true,
- },
- });
+ const allLinks = [
+ ...links.map((link) => ({
+ linkId: link.id,
+ shortLink: linkConstructor({
+ domain: link.domain,
+ key: link.key,
+ pretty: true,
+ }),
+ url: link.url,
+ })),
+ ...domains.map((domain) => ({
+ linkId: domain.id,
+ shortLink: linkConstructor({
+ domain: domain.slug,
+ pretty: true,
+ }),
+ url: domain.target || "",
+ })),
+ ];
- if (domainLink)
- return {
- linkId: domainLink.id,
- shortLink: domainLink.slug,
- domain: domainLink.slug,
- key: "_root",
- url: `https://${domainLink.slug}`,
- clicks: domainLink.clicks,
- };
- }
- }),
- );
+ const topLinks = data.map((d) => ({
+ ...allLinks.find((l) => l.linkId === d.link),
+ clicks: d.clicks,
+ }));
if (!topLinks) {
return new Response(undefined, {