feat: finalize immutable billing reports
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
a384c4c35e
commit
7f1ecd9322
15 files changed
+1697
-3
No files matched your search
@@ -1,13 +1,22 @@
|
||||
import "server-only";
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
|
||||
import { db } from "@/db/client";
|
||||
import { editorialAccounts } from "@/db/schema";
|
||||
import { billingReports, 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 FinalizedReportSummary = {
|
||||
id: string;
|
||||
reportNumber: number;
|
||||
invoiceNumber: string | null;
|
||||
netTotalCents: number;
|
||||
status: string;
|
||||
createdAt: Date;
|
||||
};
|
||||
|
||||
export type BillingSelectionData = {
|
||||
editorial: { id: string; name: string };
|
||||
period: BillingPeriod;
|
||||
@@ -18,6 +27,7 @@ export type BillingSelectionData = {
|
||||
quarter: number | null;
|
||||
positions: BillingPosition[];
|
||||
repeatDownloads: RepeatDownloadPosition[];
|
||||
reports: FinalizedReportSummary[];
|
||||
};
|
||||
|
||||
export async function loadBillingSelection(
|
||||
@@ -33,9 +43,21 @@ export async function loadBillingSelection(
|
||||
|
||||
const { period, isQuarter, year, month, quarter, periodLabel } = resolvePeriod(searchParams);
|
||||
|
||||
const [positions, repeatDownloads] = await Promise.all([
|
||||
const [positions, repeatDownloads, reportRows] = await Promise.all([
|
||||
queries.listBillablePositions(editorialId, period),
|
||||
queries.listNonBillableDownloads(editorialId, period),
|
||||
db
|
||||
.select({
|
||||
id: billingReports.id,
|
||||
reportNumber: billingReports.reportNumber,
|
||||
invoiceNumber: billingReports.invoiceNumber,
|
||||
netTotalCents: billingReports.netTotalCents,
|
||||
status: billingReports.status,
|
||||
createdAt: billingReports.createdAt,
|
||||
})
|
||||
.from(billingReports)
|
||||
.where(eq(billingReports.editorialAccountId, editorialId))
|
||||
.orderBy(desc(billingReports.createdAt)),
|
||||
]);
|
||||
|
||||
return {
|
||||
@@ -48,6 +70,14 @@ export async function loadBillingSelection(
|
||||
quarter,
|
||||
positions,
|
||||
repeatDownloads,
|
||||
reports: reportRows.map((row) => ({
|
||||
id: row.id,
|
||||
reportNumber: row.reportNumber,
|
||||
invoiceNumber: row.invoiceNumber,
|
||||
netTotalCents: row.netTotalCents,
|
||||
status: row.status,
|
||||
createdAt: row.createdAt,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user