Skip to content

Commit

Permalink
Bump Resend version + fixed scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Nov 6, 2023
1 parent ef75df9 commit b1689a0
Show file tree
Hide file tree
Showing 9 changed files with 2,272 additions and 263 deletions.
46 changes: 24 additions & 22 deletions apps/web/app/api/cron/usage/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,30 @@ export const updateUsage = async () => {
},
},
}),
getStats({
domain: project.domains.map((domain) => domain.slug).join(","),
endpoint: "top_links",
interval: "30d",
}).then((data) =>
data
.slice(0, 5)
.map(
({
domain,
key,
clicks,
}: {
domain: string;
key: string;
clicks: number;
}) => ({
link: linkConstructor({ domain, key, pretty: true }),
clicks,
}),
),
),
project.usage > 0
? getStats({
domain: project.domains.map((domain) => domain.slug).join(","),
endpoint: "top_links",
interval: "30d",
}).then((data) =>
data
.slice(0, 5)
.map(
({
domain,
key,
clicks,
}: {
domain: string;
key: string;
clicks: number;
}) => ({
link: linkConstructor({ domain, key, pretty: true }),
clicks,
}),
),
)
: [],
]);

const emails = project.users.map((user) => user.user.email) as string[];
Expand Down
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"rehype-pretty-code": "^0.9.5",
"rehype-slug": "^5.1.0",
"remark-gfm": "^3.0.1",
"resend": "^0.17.1",
"resend": "1.1.0",
"shiki": "^0.14.1",
"sonner": "^0.5.0",
"stripe": "^12.12.0",
Expand Down
22 changes: 1 addition & 21 deletions apps/web/scripts/get-top-links-for-project.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
import "dotenv-flow/config";
import prisma from "@/lib/prisma";
import { getStats } from "@/lib/stats";

function linkConstructor({
key,
domain = "dub.sh",
localhost,
pretty,
noDomain,
}: {
key: string;
domain?: string;
localhost?: boolean;
pretty?: boolean;
noDomain?: boolean;
}) {
const link = `${
localhost ? "http://home.localhost:8888" : `https://${domain}`
}${key !== "_root" ? `/${key}` : ""}`;

if (noDomain) return `/${key}`;
return pretty ? link.replace(/^https?:\/\//, "") : link;
}
import { linkConstructor } from "./utils";

async function main() {
const project = await prisma.project.findUnique({
Expand Down
4 changes: 3 additions & 1 deletion apps/web/scripts/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ if (!command) {
process.exit(1);
}

const scriptPath = `./scripts/${command}.ts`;
const scriptPath = `./scripts/${command}.${
command === "send-emails" ? "tsx" : "ts"
}`;

exec(
`tsx ${scriptPath}`,
Expand Down
42 changes: 42 additions & 0 deletions apps/web/scripts/send-emails.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import "dotenv-flow/config";
import prisma from "@/lib/prisma";

async function main() {
const users = await prisma.user.findMany({
where: {
projects: {
some: {
project: {
slug: "steven",
},
},
},
},
select: {
name: true,
email: true,
},
});

const response = await fetch("https://api.resend.com/emails/batch", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.RESEND_API_KEY}`,
},
body: JSON.stringify(
users.map(({ name, email }) => {
return {
from: "Steven from Dub <[email protected]>",
to: email,
subject: "Hello from Dub",
text: `Hi ${name},\n\nThis is a test email from Dub.\n\nBest,\nSteven`,
};
}),
),
}).then((res) => res.json());

console.log(response);
}

main();
31 changes: 31 additions & 0 deletions apps/web/scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Redis } from "@upstash/redis";
import { Resend } from "resend";

// Initiate Redis instance by connecting to REST URL
export const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL || "",
token: process.env.UPSTASH_REDIS_REST_TOKEN || "",
});

export const resend = new Resend(process.env.RESEND_API_KEY);

export function linkConstructor({
key,
domain = "dub.sh",
localhost,
pretty,
noDomain,
}: {
key: string;
domain?: string;
localhost?: boolean;
pretty?: boolean;
noDomain?: boolean;
}) {
const link = `${
localhost ? "http://home.localhost:8888" : `https://${domain}`
}${key !== "_root" ? `/${key}` : ""}`;

if (noDomain) return `/${key}`;
return pretty ? link.replace(/^https?:\/\//, "") : link;
}
26 changes: 14 additions & 12 deletions apps/web/ui/stats/feedback/action.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
"use server";

import { nanoid } from "@dub/utils";
import { resend } from "emails";
import FeedbackEmail from "emails/feedback-email";

export async function submitFeedback(data: FormData) {
const email = data.get("email") as string;
const feedback = data.get("feedback") as string;

return await fetch("https://api.resend.com/emails", {
method: "POST",
return await resend?.emails.send({
from: "[email protected]",
to: ["[email protected]"],
...(email && { reply_to: email }),
subject: "🎉 New Feedback Received!",
react: FeedbackEmail({
email,
feedback,
}),
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + process.env.RESEND_API_KEY,
"X-Entity-Ref-ID": nanoid(),
},
body: JSON.stringify({
from: "[email protected]",
to: "[email protected]",
reply_to: email,
...(email && { reply_to: email }),
subject: "🎉 New Feedback Received!",
text: feedback,
}),
});
}
Loading

0 comments on commit b1689a0

Please sign in to comment.