Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Button } from "@usesend/ui/src/button";
import Spinner from "@usesend/ui/src/spinner";
import { toast } from "@usesend/ui/src/toaster";
import { formatDistanceToNow } from "date-fns";
import { useState } from "react";

import { api } from "~/trpc/react";

Expand All @@ -17,6 +18,15 @@ const VERDICT: Record<
reject: { label: "Guard: reject", variant: "destructive" },
};

type Filter = "approve" | "unsure" | "reject" | "none";

const FILTERS: Array<[Filter, string]> = [
["approve", "Guard says let in"],
["unsure", "Unsure"],
["reject", "Guard says reject"],
["none", "Not checked yet"],
];

/** Pending applications with the Guard's advice, and a button for each decision. */
export function PendingApplications({
onDecided,
Expand All @@ -25,7 +35,8 @@ export function PendingApplications({
onDecided?: (userId: number) => void;
}) {
const utils = api.useUtils();
const pending = api.admin.pendingApplications.useQuery();
const [filter, setFilter] = useState<Filter>("approve");
const pending = api.admin.pendingApplications.useQuery({ verdict: filter });
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

const letIn = api.admin.updateUserWaitlist.useMutation({
onSuccess: (user) => {
Expand Down Expand Up @@ -80,22 +91,49 @@ export function PendingApplications({
);
}

const applications = pending.data ?? [];
const applications = pending.data?.items ?? [];
const counts = pending.data?.counts;
const total = counts
? counts.approve + counts.unsure + counts.reject + counts.none
: 0;
const inFilter = counts?.[filter] ?? 0;

return (
<div className="space-y-3">
<div className="flex items-baseline justify-between gap-2">
<h2 className="text-lg font-semibold">Pending applications</h2>
<p className="text-sm text-muted-foreground">{total} waiting</p>
</div>

<div className="flex flex-wrap gap-2" role="tablist">
{FILTERS.map(([key, label]) => (
<Button
key={key}
type="button"
size="sm"
role="tab"
aria-selected={filter === key}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
variant={filter === key ? "default" : "outline"}
onClick={() => setFilter(key)}
>
{label}
<span className="ml-1.5 tabular-nums opacity-70">
{counts?.[key] ?? 0}
</span>
</Button>
))}
</div>

{inFilter > applications.length ? (
<p className="text-sm text-muted-foreground">
{applications.length >= 200
? "Oldest 200 shown"
: `${applications.length} waiting`}
Newest {applications.length} of {inFilter} shown. Decide some and the
next ones appear.
</p>
</div>
) : null}

{applications.length === 0 ? (
<div className="rounded-lg border border-dashed p-6 text-sm text-muted-foreground">
Nobody is waiting.
{filter === "none" ? "Everything has been checked." : "Nothing here."}
</div>
) : null}

Expand Down
49 changes: 35 additions & 14 deletions apps/web/src/server/api/routers/admin-waitlist.trpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { mockDb, mockSendMail } = vi.hoisted(() => {
waitlistApplication: {
findMany: vi.fn(),
findUnique: vi.fn(),
groupBy: vi.fn(),
updateMany: vi.fn(),
},
};
Expand Down Expand Up @@ -198,23 +199,43 @@ describe("rejecting someone", () => {
});

describe("pendingApplications", () => {
it("shows the Guard's suggested approvals first", async () => {
mockDb.waitlistApplication.findMany.mockResolvedValue([
{ id: "a", guardVerdict: null },
{ id: "b", guardVerdict: "reject" },
{ id: "c", guardVerdict: "approve" },
{ id: "d", guardVerdict: "unsure" },
]);
const list = await caller.pendingApplications();
expect(list.map((application) => application.id)).toEqual([
"c",
"d",
"b",
"a",
it("shows one kind of advice at a time, newest first, with counts for all", async () => {
mockDb.waitlistApplication.groupBy.mockResolvedValue([
{ guardVerdict: "approve", _count: { _all: 3 } },
{ guardVerdict: null, _count: { _all: 800 } },
{ guardVerdict: "reject", _count: { _all: 2 } },
]);
mockDb.waitlistApplication.findMany.mockResolvedValue([{ id: "c" }]);

const result = await caller.pendingApplications({ verdict: "none" });

expect(result.counts).toEqual({
approve: 3,
unsure: 0,
reject: 2,
none: 800,
});
expect(result.items).toEqual([{ id: "c" }]);
expect(mockDb.waitlistApplication.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: {
status: "PENDING",
user: { isWaitlisted: true },
guardVerdict: null,
},
orderBy: [{ createdAt: "desc" }, { id: "desc" }],
take: 100,
}),
);
});

it("opens on the Guard's suggested approvals", async () => {
mockDb.waitlistApplication.groupBy.mockResolvedValue([]);
mockDb.waitlistApplication.findMany.mockResolvedValue([]);
await caller.pendingApplications();
expect(mockDb.waitlistApplication.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: { status: "PENDING", user: { isWaitlisted: true } },
where: expect.objectContaining({ guardVerdict: "approve" }),
}),
);
});
Expand Down
79 changes: 54 additions & 25 deletions apps/web/src/server/api/routers/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import {
} from "~/server/service/guard-service";
import { TRPCError } from "@trpc/server";

/** Applications shown per filter on the admin waitlist page, newest first. */
const PENDING_PAGE = 100;

const waitlistUserSelection = {
id: true,
email: true,
Expand Down Expand Up @@ -175,31 +178,57 @@ export const adminRouter = createTRPCRouter({
}),

/** Pending applications with the Guard's advice: suggested approvals first. */
pendingApplications: adminProcedure.query(async () => {
const applications = await db.waitlistApplication.findMany({
where: { status: "PENDING", user: { isWaitlisted: true } },
orderBy: { createdAt: "asc" },
take: 200,
select: {
id: true,
userId: true,
email: true,
website: true,
emailTypes: true,
emailVolume: true,
listSource: true,
description: true,
guardVerdict: true,
guardNote: true,
createdAt: true,
},
});
const order = (verdict: string | null) =>
["approve", "unsure", "reject"].indexOf(verdict ?? "") + 1 || 4;
return applications.sort(
(a, b) => order(a.guardVerdict) - order(b.guardVerdict),
);
}),
pendingApplications: adminProcedure
.input(
z
.object({
/** The Guard's advice to show; "none" is not checked yet. */
verdict: z
.enum(["approve", "unsure", "reject", "none"])
.default("approve"),
})
.default({}),
)
.query(async ({ input }) => {
const pending = {
status: "PENDING" as const,
user: { isWaitlisted: true },
};
const [groups, applications] = await Promise.all([
db.waitlistApplication.groupBy({
by: ["guardVerdict"],
where: pending,
_count: { _all: true },
}),
db.waitlistApplication.findMany({
where: {
...pending,
guardVerdict: input.verdict === "none" ? null : input.verdict,
},
orderBy: [{ createdAt: "desc" }, { id: "desc" }],
take: PENDING_PAGE,
select: {
id: true,
userId: true,
email: true,
website: true,
emailTypes: true,
emailVolume: true,
listSource: true,
description: true,
guardVerdict: true,
guardNote: true,
createdAt: true,
},
}),
]);
const counts = { approve: 0, unsure: 0, reject: 0, none: 0 };
for (const group of groups) {
const key = (group.guardVerdict ?? "none") as keyof typeof counts;
if (key in counts) counts[key] += group._count._all;
}
return { counts, items: applications, pageSize: PENDING_PAGE };
}),

findUserByEmail: adminProcedure
.input(
Expand Down
Loading