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 /api/bounties/stats

Kiran Ka074e3a30eaa
diff --git a/apps/web/app/(ee)/api/bounties/stats/route.ts b/apps/web/app/(ee)/api/bounties/stats/route.ts
new file mode 100644
index 0000000000..70dd69fd36
--- /dev/null
+++ b/apps/web/app/(ee)/api/bounties/stats/route.ts
@@ -0,0 +1,91 @@
+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
+import { withWorkspace } from "@/lib/auth";
+import { bountyStatsSchema } from "@/lib/zod/schemas/bounties";
+import { prisma } from "@dub/prisma";
+import { NextResponse } from "next/server";
+import { z } from "zod";
+
+// TODO (kiran):
+// Simplify this route
+
+// GET /api/bounties/stats - get the stats for all bounties in a program
+export const GET = withWorkspace(async ({ workspace }) => {
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const [bounties, enrollmentsCount, pendingSubmissionsCounts] =
+ await Promise.all([
+ prisma.bounty.findMany({
+ where: {
+ programId,
+ },
+ include: {
+ groups: {
+ include: {
+ partnerGroup: {
+ include: {
+ _count: {
+ select: {
+ partners: {
+ where: {
+ status: "approved",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ _count: {
+ select: {
+ submissions: true,
+ },
+ },
+ },
+ }),
+
+ prisma.programEnrollment.count({
+ where: {
+ programId,
+ status: "approved",
+ },
+ }),
+
+ prisma.bountySubmission.groupBy({
+ by: ["bountyId"],
+ where: {
+ bounty: {
+ programId,
+ },
+ status: "pending",
+ },
+ _count: true,
+ }),
+ ]);
+
+ const pendingSubmissionsMap = pendingSubmissionsCounts.reduce(
+ (acc, item) => {
+ acc[item.bountyId] = item._count;
+ return acc;
+ },
+ {} as Record<string, number>,
+ );
+
+ const result = bounties.map((bounty) => {
+ const totalPartners = bounty.groups.reduce(
+ (acc, group) => acc + group.partnerGroup._count.partners,
+ 0,
+ );
+
+ return {
+ id: bounty.id,
+ submissions: bounty._count.submissions,
+ pendingSubmissions: pendingSubmissionsMap[bounty.id] || 0,
+ partners: bounty.groups.length > 0 ? totalPartners : enrollmentsCount,
+ };
+ });
+
+ console.log(result);
+
+ return NextResponse.json(z.array(bountyStatsSchema).parse(result));
+});
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-card.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-card.tsx
index d84f6d48ac..34c60bff1e 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-card.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-card.tsx
@@ -1,22 +1,27 @@
import { mutatePrefix } from "@/lib/swr/mutate";
+import useBountiesStats from "@/lib/swr/use-bounties-stats";
import useWorkspace from "@/lib/swr/use-workspace";
import { BountyExtendedProps } from "@/lib/types";
import { useConfirmModal } from "@/ui/modals/confirm-modal";
import { ProgramOverviewCard } from "@/ui/partners/overview/program-overview-card";
import { Button, MenuItem, Popover, useRouterStuff } from "@dub/ui";
import { CalendarDays, Dots, PenWriting, Trash, Users } from "@dub/ui/icons";
-import { formatDate } from "@dub/utils";
+import { formatDate, pluralize } from "@dub/utils";
import { Command } from "cmdk";
import Link from "next/link";
-import { useState } from "react";
+import { useMemo, useState } from "react";
import { toast } from "sonner";
export function BountyCard({ bounty }: { bounty: BountyExtendedProps }) {
const { id: workspaceId } = useWorkspace();
const { queryParams } = useRouterStuff();
-
+ const { bountiesStats, isLoading: isLoadingStats } = useBountiesStats();
const [isMenuOpen, setIsMenuOpen] = useState(false);
+ const stats = useMemo(() => {
+ return bountiesStats?.find(({ id }) => id === bounty.id);
+ }, [bountiesStats, bounty.id]);
+
const { confirmModal: deleteModal, setShowConfirmModal: setShowDeleteModal } =
useConfirmModal({
title: "Delete bounty",
@@ -50,6 +55,7 @@ export function BountyCard({ bounty }: { bounty: BountyExtendedProps }) {
return (
<ProgramOverviewCard className="relative cursor-pointer border-neutral-200 p-5 transition-all hover:border-neutral-300 hover:shadow-lg">
{deleteModal}
+
<Link
href={`/program/bounties/${bounty.id}`}
className="flex flex-col gap-5"
@@ -68,6 +74,10 @@ export function BountyCard({ bounty }: { bounty: BountyExtendedProps }) {
})}
className="size-full object-contain"
/>
+
+ {stats?.pendingSubmissions && stats.pendingSubmissions > 0 ? (
+ <PendingSubmissionsBadge count={stats.pendingSubmissions} />
+ ) : null}
</div>
</div>
@@ -91,12 +101,21 @@ export function BountyCard({ bounty }: { bounty: BountyExtendedProps }) {
<div className="text-content-subtle flex items-center gap-2 text-sm font-medium">
<Users className="size-3.5" />
- <div>
- <span className="text-content-default">N</span> of{" "}
- <span className="text-content-default">
- {bounty.partnersCount}
- </span>{" "}
- partners completed
+ <div className="h-5">
+ {isLoadingStats || bountiesStats === undefined ? (
+ <div className="h-5 w-48 animate-pulse rounded bg-neutral-300" />
+ ) : stats ? (
+ <>
+ <span className="text-content-default">
+ {stats.submissions}
+ </span>{" "}
+ of{" "}
+ <span className="text-content-default">{stats.partners}</span>{" "}
+ partners completed
+ </>
+ ) : (
+ <span>Failed to load stats</span>
+ )}
</div>
</div>
</div>
@@ -146,6 +165,16 @@ export function BountyCard({ bounty }: { bounty: BountyExtendedProps }) {
);
}
+const PendingSubmissionsBadge = ({ count }: { count: number }) => {
+ return (
+ <div className="absolute left-2 top-2 z-10">
+ <div className="flex h-5 items-center gap-1 rounded-md bg-blue-100 px-2 py-1 text-xs font-semibold text-blue-600">
+ {count} {pluralize("submission", count)} for review
+ </div>
+ </div>
+ );
+};
+
export const BountyCardSkeleton = () => {
return (
<ProgramOverviewCard className="cursor-pointer p-5">
diff --git a/apps/web/lib/swr/use-bounties-stats.ts b/apps/web/lib/swr/use-bounties-stats.ts
new file mode 100644
index 0000000000..6e21d2a8a5
--- /dev/null
+++ b/apps/web/lib/swr/use-bounties-stats.ts
@@ -0,0 +1,23 @@
+import { fetcher } from "@dub/utils";
+import useSWR from "swr";
+import { BountyStats } from "../types";
+import useWorkspace from "./use-workspace";
+
+export default function useBountiesStats() {
+ const { id: workspaceId } = useWorkspace();
+
+ const {
+ data: bountiesStats,
+ error,
+ isLoading,
+ } = useSWR<BountyStats[]>(
+ workspaceId ? `/api/bounties/stats?workspaceId=${workspaceId}` : undefined,
+ fetcher,
+ );
+
+ return {
+ bountiesStats,
+ isLoading,
+ error,
+ };
+}
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 74fdb3bd0b..8623c54012 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -26,6 +26,7 @@ import { WEBHOOK_TRIGGER_DESCRIPTIONS } from "./webhook/constants";
import {
BountySchema,
BountySchemaExtended,
+ bountyStatsSchema,
BountySubmissionSchema,
SUBMISSION_REQUIREMENTS,
} from "./zod/schemas/bounties";
@@ -531,6 +532,8 @@ export type BountySubmissionProps = z.infer<typeof BountySubmissionSchema>;
export type BountySubmissionRequirement =
(typeof SUBMISSION_REQUIREMENTS)[number];
+export type BountyStats = z.infer<typeof bountyStatsSchema>;
+
export type WorkflowCondition = z.infer<typeof workflowConditionSchema>;
export type WorkflowConditionAttribute =
diff --git a/apps/web/lib/zod/schemas/bounties.ts b/apps/web/lib/zod/schemas/bounties.ts
index ee81b70f82..bfe1398fa7 100644
--- a/apps/web/lib/zod/schemas/bounties.ts
+++ b/apps/web/lib/zod/schemas/bounties.ts
@@ -90,3 +90,10 @@ export const getBountiesQuerySchema = z
includeExpandedFields: booleanQuerySchema.optional(),
})
.merge(getPaginationQuerySchema({ pageSize: BOUNTIES_MAX_PAGE_SIZE }));
+
+export const bountyStatsSchema = z.object({
+ id: z.string(),
+ submissions: z.number(),
+ pendingSubmissions: z.number(),
+ partners: z.number(),
+});