Open source CapCut alternative.
TypeScript
feat(site): the outreach row says what it is storing
David Lec8400f38a877
diff --git a/site/src/app/api/marketing/outreach/route.ts b/site/src/app/api/marketing/outreach/route.tsindex 9e477f0c..19584041 100644--- a/site/src/app/api/marketing/outreach/route.ts+++ b/site/src/app/api/marketing/outreach/route.ts@@ -1,6 +1,7 @@import { NextResponse } from "next/server";import { z } from "zod";+import { formatBytes } from "@/lib/bytes";import { creditMicrosToString } from "@/lib/credits/amounts";import { notFoundResponse, withSuperUser } from "@/lib/donkey-api-auth";import {@@ -67,8 +68,21 @@ type OutreachRow = {user: { createdAt: Date; email: string; id: string; name: string };};-// Micros are BigInt and dates are Date; the client wants neither.-function serialize(row: OutreachRow) {+// Cloud media held by each of these accounts. Storage is read live: an upload+// is the thing being checked, and it moves between scans. Absent row means+// nothing stored.+async function storageByUser(userIds: string[]): Promise<Map<string, bigint>> {+ if (userIds.length === 0) return new Map();+ const rows = await prisma.cutStorageUsage.findMany({+ select: { bytes: true, userId: true },+ where: { userId: { in: userIds } },+ });+ return new Map(rows.map((row) => [row.userId, row.bytes]));+}++// Micros and byte counts are BigInt and dates are Date; the client wants+// neither.+function serialize(row: OutreachRow, storage: Map<string, bigint>) {return {balance: creditMicrosToString(row.balanceMicros),email: row.user.email,@@ -83,12 +97,18 @@ function serialize(row: OutreachRow) {signedUpAt: row.user.createdAt.toISOString(),spent: creditMicrosToString(row.spentMicros),status: row.status,+ storageBytes: (storage.get(row.user.id) ?? BigInt(0)).toString(),userId: row.user.id,};}-// The list is whatever the nightly outreach-scan job wrote, so this reads one-// table and touches no credit data.+// One row and the storage that goes with it.+async function serializeOne(row: OutreachRow) {+ return serialize(row, await storageByUser([row.user.id]));+}++// The list is whatever the nightly outreach-scan job wrote, plus each+// account's cloud bytes; no credit data is touched here.export const GET = withSuperUser(async (request) => {const params = new URL(request.url).searchParams;const parsed = listQuerySchema.safeParse({@@ -136,7 +156,11 @@ export const GET = withSuperUser(async (request) => {counts[group.status as keyof typeof counts] = group._count;}}- return NextResponse.json({ counts, rows: rows.map(serialize) });+ const storage = await storageByUser(rows.map((row) => row.user.id));+ return NextResponse.json({+ counts,+ rows: rows.map((row) => serialize(row, storage)),+ });}// Each list is about a different moment: when they last used the product,@@ -156,7 +180,8 @@ export const GET = withSuperUser(async (request) => {where: { campaign: CREDIT_SPENDERS_CAMPAIGN, status },});- return NextResponse.json({ rows: rows.map(serialize) });+ const storage = await storageByUser(rows.map((row) => row.user.id));+ return NextResponse.json({ rows: rows.map((row) => serialize(row, storage)) });});export const POST = withSuperUser(async (request) => {@@ -186,6 +211,7 @@ export const POST = withSuperUser(async (request) => {const actorUserId = request.donkey.userId;if (parsed.data.action === "send") {+ const storage = await storageByUser([outreach.user.id]);try {await sendOutreachEmail({attempt: outreach.sentCount + 1,@@ -201,6 +227,7 @@ export const POST = withSuperUser(async (request) => {firstName: outreach.user.name.trim().split(/\s+/)[0] || outreach.user.name,name: outreach.user.name,spent: creditMicrosToString(outreach.spentMicros),+ storage: formatBytes(Number(storage.get(outreach.user.id) ?? BigInt(0))),},});} catch (error) {@@ -223,7 +250,7 @@ export const POST = withSuperUser(async (request) => {select: rowSelect,where: { id: outreach.id },});- return NextResponse.json({ row: serialize(row) });+ return NextResponse.json({ row: serialize(row, storage) });}const data =@@ -238,5 +265,5 @@ export const POST = withSuperUser(async (request) => {select: rowSelect,where: { id: outreach.id },});- return NextResponse.json({ row: serialize(row) });+ return NextResponse.json({ row: await serializeOne(row) });});diff --git a/site/src/app/su/outreach/page.tsx b/site/src/app/su/outreach/page.tsxindex a984cdff..5d4ceaa6 100644--- a/site/src/app/su/outreach/page.tsx+++ b/site/src/app/su/outreach/page.tsx@@ -22,6 +22,7 @@ import { Input } from "@/components/ui/input";import { Label } from "@/components/ui/label";import { Switch } from "@/components/ui/switch";import { Textarea } from "@/components/ui/textarea";+import { formatBytes } from "@/lib/bytes";import { cn } from "@/lib/utils";import type { OutreachStatus } from "@/lib/marketing/campaigns";import { OUTREACH_PLACEHOLDERS } from "@/lib/marketing/placeholders";@@ -72,10 +73,12 @@ function ago(iso: string | null): string {return `${Math.floor(mins / (60 * 24))}d ago`;}-// The list carries the numbers the scan wrote; the badges say what they mean-// without a second read.+// The numbers the scan wrote plus the cloud bytes read live; the badges say+// what they mean without a second read. Storage shows only when there is any,+// so a row with the badge is someone with media parked in the cloud.function RowBadges({ row, group }: { row: OutreachRow; group?: string }) {const broke = Number(row.balance) <= 0;+ const stored = Number(row.storageBytes);return (<div className="mt-1 flex flex-wrap items-center gap-1.5">{group ? <Badge>{group}</Badge> : null}@@ -83,6 +86,9 @@ function RowBadges({ row, group }: { row: OutreachRow; group?: string }) {<Badge variant={broke ? "destructive" : "outline"}>{broke ? `$0 left · ${ago(row.ranOutAt)}` : `$${row.balance} left`}</Badge>+ {stored > 0 ? (+ <Badge variant="secondary">{formatBytes(stored)} stored</Badge>+ ) : null}<Badge variant="outline">active {ago(row.lastActiveAt)}</Badge><Badge variant="outline">joined {ago(row.signedUpAt)}</Badge>{row.sentCount > 0 ? (diff --git a/site/src/lib/marketing/placeholders.test.ts b/site/src/lib/marketing/placeholders.test.tsindex 9c0d159f..00409950 100644--- a/site/src/lib/marketing/placeholders.test.ts+++ b/site/src/lib/marketing/placeholders.test.ts@@ -1,6 +1,10 @@import { describe, expect, test } from "bun:test";-import { fillOutreachText, UnknownPlaceholderError } from "./placeholders";+import {+ fillOutreachText,+ OUTREACH_PLACEHOLDERS,+ UnknownPlaceholderError,+} from "./placeholders";const vars = {balance: "0",@@ -8,6 +12,7 @@ const vars = {firstName: "Ada",name: "Ada Lovelace",spent: "2.4",+ storage: "120 MB",};function refusedPlaceholder(text: string): boolean {@@ -37,5 +42,16 @@ describe("fillOutreachText", () => {test("a misspelled placeholder throws instead of mailing the braces", () => {expect(refusedPlaceholder("Hey {{frstName}}")).toBe(true);});++ // The button row offers every name in the list, so every one of them has to+ // resolve to a value; a name the send path forgets to supply would mail the+ // word "undefined" to a real person.+ test("every offered placeholder fills with the recipient's value", () => {+ for (const name of OUTREACH_PLACEHOLDERS) {+ const filled = fillOutreachText(`{{${name}}}`, vars);+ expect(filled).not.toBe("");+ expect(filled).not.toContain("undefined");+ }+ });});diff --git a/site/src/lib/marketing/placeholders.ts b/site/src/lib/marketing/placeholders.tsindex 36c90636..9c4b7dc9 100644--- a/site/src/lib/marketing/placeholders.ts+++ b/site/src/lib/marketing/placeholders.ts@@ -13,6 +13,8 @@ export type OutreachVars = {spent: string;/** USD left on the account. */balance: string;+ /** Cloud media the account is holding, already in reading units ("120 MB"). */+ storage: string;};export const OUTREACH_PLACEHOLDERS = [@@ -21,6 +23,7 @@ export const OUTREACH_PLACEHOLDERS = ["email","spent","balance",+ "storage",] as const satisfies readonly (keyof OutreachVars)[];const PLACEHOLDER = /\{\{\s*([a-zA-Z]+)\s*\}\}/g;diff --git a/site/src/queries/outreach.ts b/site/src/queries/outreach.tsindex ea850af3..821e66f3 100644--- a/site/src/queries/outreach.ts+++ b/site/src/queries/outreach.ts@@ -21,6 +21,9 @@ export type OutreachRow = {// USD strings, as the credits API returns them everywhere else.spent: string;balance: string;+ // Cloud media the account is holding, in bytes. A BigInt on the server, so+ // it travels as a decimal string.+ storageBytes: string;lastActiveAt: string | null;ranOutAt: string | null;signedUpAt: string;