Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
87 changes: 87 additions & 0 deletions actions/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,93 @@ export async function getOrgStats(orgId: string) {
return { threadCount: threadCount ?? 0, replyCount: replyCount ?? 0 };
}

export async function getDetailedOrgStats(orgId: string) {
const supabase = await createServerClient();

const STATS_DAY_RANGE = 7;
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - (STATS_DAY_RANGE - 1));
sevenDaysAgo.setHours(0, 0, 0, 0);
const since = sevenDaysAgo.toISOString();

const [
{ count: openThreads },
{ count: closedThreads },
{ data: replyRows },
{ data: recentThreads },
{ data: recentReplies },
] = await Promise.all([
supabase
.from('thread')
.select('id', { count: 'exact', head: true })
.eq('organization_id', orgId)
.eq('status', 'open'),
supabase
.from('thread')
.select('id', { count: 'exact', head: true })
.eq('organization_id', orgId)
.eq('status', 'closed'),
supabase
.from('reply')
.select('status, is_perfect')
.eq('organization_id', orgId),
supabase
.from('thread')
.select('created_at')
.eq('organization_id', orgId)
.gte('created_at', since),
supabase
.from('reply')
.select('created_at')
.eq('organization_id', orgId)
.gte('created_at', since),
]);

const replyStatusCounts: Record<string, number> = {};
let perfectCount = 0;
let editedCount = 0;
const replies: { status: string; is_perfect: boolean | null }[] =
replyRows ?? [];
for (const r of replies) {
replyStatusCounts[r.status] = (replyStatusCounts[r.status] ?? 0) + 1;
if (r.is_perfect === true) perfectCount++;
else if (r.is_perfect === false) editedCount++;
}

const dayLabels: string[] = [];
const threadsByDay: number[] = [];
const repliesByDay: number[] = [];
const threads: { created_at: string }[] = recentThreads ?? [];
const recentRepliesList: { created_at: string }[] = recentReplies ?? [];
for (let i = 0; i < STATS_DAY_RANGE; i++) {
const d = new Date(sevenDaysAgo);
d.setDate(d.getDate() + i);
const label = d.toLocaleDateString('en-US', { weekday: 'short' });
const dateStr = d.toISOString().slice(0, 10);
dayLabels.push(label);
threadsByDay.push(
threads.filter((t) => t.created_at.slice(0, 10) === dateStr).length
);
repliesByDay.push(
recentRepliesList.filter((r) => r.created_at.slice(0, 10) === dateStr)
.length
);
}

return {
openThreads: openThreads ?? 0,
closedThreads: closedThreads ?? 0,
totalReplies: replies.length,
replyStatusCounts,
perfectCount,
editedCount,
dayLabels,
threadsByDay,
repliesByDay,
dayRange: STATS_DAY_RANGE,
};
}

export async function getThreads(orgId: string, status: 'open' | 'closed') {
const supabase = await createServerClient();
const { data, error } = await supabase
Expand Down
24 changes: 24 additions & 0 deletions app/org/[slug]/stats/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Metadata } from 'next';
import { notFound } from 'next/navigation';
import { getOrganizationBySlug } from '@/actions/organization';
import { getDetailedOrgStats } from '@/actions/email';

import { StatsPage } from '@/components/organization/StatsPage';

interface Props {
params: Promise<{ slug: string }>;
}

export const metadata: Metadata = {
title: 'Stats',
};

export default async function OrgStatsPage({ params }: Props) {
const { slug } = await params;
const { data: org } = await getOrganizationBySlug(slug);
if (!org?.id) return notFound();

const stats = await getDetailedOrgStats(org.id);

return <StatsPage orgName={org.name} stats={stats} />;
}
Loading