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.

Create persist-customer-avatars.ts

Steven Tey2b7cee7d5a5b
diff --git a/apps/web/scripts/persist-customer-avatars.ts b/apps/web/scripts/persist-customer-avatars.ts
new file mode 100644
index 0000000000..db2fd365e3
--- /dev/null
+++ b/apps/web/scripts/persist-customer-avatars.ts
@@ -0,0 +1,59 @@
+import { prisma } from "@dub/prisma";
+import { Prisma } from "@dub/prisma/client";
+import { nanoid } from "@dub/utils";
+import "dotenv-flow/config";
+import { storage } from "../lib/storage";
+
+const R2_URL = "https://dubassets.com";
+
+async function main() {
+ const where: Prisma.CustomerWhereInput = {
+ avatar: {
+ startsWith: "https://lh3.googleusercontent.com",
+ },
+ };
+
+ const customers = await prisma.customer.findMany({
+ where,
+ orderBy: {
+ createdAt: "desc",
+ },
+ take: 20,
+ });
+
+ await Promise.all(
+ customers.map(async (customer) => {
+ if (!customer.avatar) {
+ return;
+ }
+ const finalCustomerAvatar = `${R2_URL}/customers/${customer.id}/avatar_${nanoid(7)}`;
+
+ await storage.upload(
+ finalCustomerAvatar.replace(`${R2_URL}/`, ""),
+ customer.avatar,
+ {
+ width: 128,
+ height: 128,
+ },
+ );
+
+ const updatedCustomer = await prisma.customer.update({
+ where: { id: customer.id },
+ data: {
+ avatar: finalCustomerAvatar,
+ },
+ });
+
+ console.log(
+ `Updated customer ${customer.id} to ${updatedCustomer.avatar}`,
+ );
+ }),
+ );
+
+ const remainingCustomers = await prisma.customer.count({
+ where,
+ });
+
+ console.log(`Remaining customers: ${remainingCustomers}`);
+}
+main();