feat(reports): financial hasData existence flag (service + route)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-02 10:13:42 +02:00
parent 8b7099c4c1
commit 58203ca8ea
2 changed files with 24 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ import {
getRecentPayments,
getRefundLog,
getExpenseLedger,
financialHasData,
} from '@/lib/services/reports/financial.service';
/**
@@ -65,6 +66,7 @@ export const GET = withAuth(
recentPayments,
refundLog,
expenseLedger,
hasData,
] = await Promise.all([
getFinancialKpis(ctx.portId, range),
getRevenueByMonth(ctx.portId, range),
@@ -76,6 +78,7 @@ export const GET = withAuth(
getRecentPayments(ctx.portId, range),
getRefundLog(ctx.portId, range),
getExpenseLedger(ctx.portId, range),
financialHasData(ctx.portId),
]);
return NextResponse.json({
@@ -90,6 +93,7 @@ export const GET = withAuth(
recentPayments,
refundLog,
expenseLedger,
hasData,
range: { from: range.from.toISOString(), to: range.to.toISOString() },
},
});

View File

@@ -630,3 +630,23 @@ export async function getExpenseLedger(
}
return out;
}
/**
* Window-independent existence check: does this port have any payment OR
* expense? Drives the Financial report-level empty state.
*/
export async function financialHasData(portId: string): Promise<boolean> {
const [pay, exp] = await Promise.all([
db
.select({ one: sql<number>`1` })
.from(payments)
.where(eq(payments.portId, portId))
.limit(1),
db
.select({ one: sql<number>`1` })
.from(expenses)
.where(eq(expenses.portId, portId))
.limit(1),
]);
return pay.length > 0 || exp.length > 0;
}