feat: offer badges, unified dashboard, income forecast, Pipedrive-style follow-up
- Client detail: category + tier badges on active offers - Dashboard: remove top KPI cards + recent activity; fold current KPIs into bottom MetricCard style; add 12-month income forecast chart - Forecast query: branch by offer_type (retainer = monthly fee, una_tantum = spread over duration), filter archived projects - Payments: set the month a payment was collected (paid_at) from PaymentsTab - Redesign FollowUpWidget in clean Pipedrive style (brand green, no orange) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { db } from "@/db";
|
||||
import { clients, projects, payments, deliverables, time_entries } from "@/db/schema";
|
||||
import { eq, and, isNotNull, isNull, desc, sum, count } from "drizzle-orm";
|
||||
import { clients, projects, payments } from "@/db/schema";
|
||||
import { eq, sum, count } from "drizzle-orm";
|
||||
|
||||
export type KpiStats = {
|
||||
clientiAttivi: number;
|
||||
@@ -9,16 +9,8 @@ export type KpiStats = {
|
||||
progettiInCorso: number; // projects not archived
|
||||
};
|
||||
|
||||
export type ActivityItem = {
|
||||
type: "nuovo_cliente" | "nuovo_progetto" | "deliverable_approvato" | "timer_stoppato";
|
||||
label: string;
|
||||
detail: string;
|
||||
timestamp: Date;
|
||||
};
|
||||
|
||||
export type DashboardStats = {
|
||||
kpi: KpiStats;
|
||||
activity: ActivityItem[];
|
||||
};
|
||||
|
||||
export async function getDashboardStats(): Promise<DashboardStats> {
|
||||
@@ -59,80 +51,5 @@ export async function getDashboardStats(): Promise<DashboardStats> {
|
||||
progettiInCorso: progettiRow?.count ?? 0,
|
||||
};
|
||||
|
||||
// ── Activity feed ─────────────────────────────────────────────────────────────
|
||||
// Fetch recent events from multiple tables, merge and sort
|
||||
const [recentClients, recentProjects, recentApprovals, recentTimers] = await Promise.all([
|
||||
db
|
||||
.select({ id: clients.id, name: clients.name, created_at: clients.created_at })
|
||||
.from(clients)
|
||||
.orderBy(desc(clients.created_at))
|
||||
.limit(5),
|
||||
|
||||
db
|
||||
.select({ id: projects.id, name: projects.name, created_at: projects.created_at })
|
||||
.from(projects)
|
||||
.orderBy(desc(projects.created_at))
|
||||
.limit(5),
|
||||
|
||||
db
|
||||
.select({ id: deliverables.id, title: deliverables.title, approved_at: deliverables.approved_at })
|
||||
.from(deliverables)
|
||||
.where(isNotNull(deliverables.approved_at))
|
||||
.orderBy(desc(deliverables.approved_at))
|
||||
.limit(5),
|
||||
|
||||
db
|
||||
.select({
|
||||
id: time_entries.id,
|
||||
ended_at: time_entries.ended_at,
|
||||
duration_seconds: time_entries.duration_seconds,
|
||||
})
|
||||
.from(time_entries)
|
||||
.where(
|
||||
and(
|
||||
isNotNull(time_entries.ended_at),
|
||||
isNotNull(time_entries.duration_seconds)
|
||||
)
|
||||
)
|
||||
.orderBy(desc(time_entries.ended_at))
|
||||
.limit(5),
|
||||
]);
|
||||
|
||||
const activity: ActivityItem[] = [
|
||||
...recentClients.map((c) => ({
|
||||
type: "nuovo_cliente" as const,
|
||||
label: "Nuovo cliente",
|
||||
detail: c.name,
|
||||
timestamp: c.created_at,
|
||||
})),
|
||||
...recentProjects.map((p) => ({
|
||||
type: "nuovo_progetto" as const,
|
||||
label: "Nuovo progetto",
|
||||
detail: p.name,
|
||||
timestamp: p.created_at,
|
||||
})),
|
||||
...recentApprovals
|
||||
.filter((d): d is typeof d & { approved_at: Date } => d.approved_at !== null)
|
||||
.map((d) => ({
|
||||
type: "deliverable_approvato" as const,
|
||||
label: "Deliverable approvato",
|
||||
detail: d.title,
|
||||
timestamp: d.approved_at,
|
||||
})),
|
||||
...recentTimers
|
||||
.filter(
|
||||
(t): t is typeof t & { ended_at: Date; duration_seconds: number } =>
|
||||
t.ended_at !== null && t.duration_seconds !== null
|
||||
)
|
||||
.map((t) => ({
|
||||
type: "timer_stoppato" as const,
|
||||
label: "Timer stoppato",
|
||||
detail: `${Math.round(t.duration_seconds / 60)} min`,
|
||||
timestamp: t.ended_at,
|
||||
})),
|
||||
]
|
||||
.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime())
|
||||
.slice(0, 10);
|
||||
|
||||
return { kpi, activity };
|
||||
return { kpi };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user