feat: add billing period selection

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Leon TonhäuserandClaude committed 2026-08-10 17:31:42 +02:00
1 parent ba673ad732
commit d37affc80f
15 files changed
+1625

No files matched your search

@@ -0,0 +1,121 @@
import "server-only";
import { eq } from "drizzle-orm";
import { db } from "@/db/client";
import { editorialAccounts } from "@/db/schema";
import { queries } from "@/modules/billing/queries";
import { monthPeriod, quarterPeriod, type BillingPeriod } from "@/modules/billing/period";
import type { BillingPosition, RepeatDownloadPosition } from "@/modules/billing/queries";
export type BillingSelectionData = {
editorial: { id: string; name: string };
period: BillingPeriod;
periodLabel: string;
isQuarter: boolean;
year: number;
month: number | null;
quarter: number | null;
positions: BillingPosition[];
repeatDownloads: RepeatDownloadPosition[];
};
export async function loadBillingSelection(
editorialId: string,
searchParams: Record<string, string | string[] | undefined>,
): Promise<BillingSelectionData | null> {
const [editorial] = await db
.select({ id: editorialAccounts.id, name: editorialAccounts.name })
.from(editorialAccounts)
.where(eq(editorialAccounts.id, editorialId))
.limit(1);
if (!editorial) return null;
const { period, isQuarter, year, month, quarter, periodLabel } = resolvePeriod(searchParams);
const [positions, repeatDownloads] = await Promise.all([
queries.listBillablePositions(editorialId, period),
queries.listNonBillableDownloads(editorialId, period),
]);
return {
editorial,
period,
periodLabel,
isQuarter,
year,
month,
quarter,
positions,
repeatDownloads,
};
}
export function resolvePeriod(searchParams: Record<string, string | string[] | undefined>): {
period: BillingPeriod;
isQuarter: boolean;
year: number;
month: number | null;
quarter: number | null;
periodLabel: string;
} {
const now = new Date();
const defaultYear = now.getUTCFullYear();
const defaultMonth = now.getUTCMonth() + 1;
const year = parseNumberParam(searchParams.year, defaultYear);
const quarterParam = firstParam(searchParams.quarter);
if (quarterParam !== undefined) {
const quarter = parseNumberParam(searchParams.quarter, Math.floor((defaultMonth - 1) / 3) + 1);
return {
period: quarterPeriod(year, quarter),
isQuarter: true,
year,
month: null,
quarter,
periodLabel: `Q${quarter} ${year}`,
};
}
const month = parseNumberParam(searchParams.month, defaultMonth);
return {
period: monthPeriod(year, month),
isQuarter: false,
year,
month,
quarter: null,
periodLabel: monthLabel(year, month),
};
}
function monthLabel(year: number, month: number): string {
const names = [
"Januar",
"Februar",
"März",
"April",
"Mai",
"Juni",
"Juli",
"August",
"September",
"Oktober",
"November",
"Dezember",
];
return `${names[month - 1]} ${year}`;
}
function parseNumberParam(
value: string | string[] | undefined,
fallback: number,
): number {
const parsed = Number.parseInt(firstParam(value) ?? "", 10);
return Number.isFinite(parsed) ? parsed : fallback;
}
function firstParam(value: string | string[] | undefined): string | undefined {
if (Array.isArray(value)) return value[0];
return value;
}