feat: Add flatrate billing model for editorial accounts
This commit is contained in:
1 parent
699580a6a0
commit
0b1ffdfdfc
39 files changed
+4900
-57
No files matched your search
@@ -155,6 +155,7 @@ export type FinalizeTransactionInput = {
|
||||
address: BillingReportAddress;
|
||||
repeatDownloads: BillingReportRepeatDownload[];
|
||||
netTotalCents: number;
|
||||
flatrateCentsSnapshot: number | null;
|
||||
pdfBytes: Buffer;
|
||||
pdfSha256: string;
|
||||
documentId: string;
|
||||
@@ -171,6 +172,7 @@ export type FinalizeTransactionResult = {
|
||||
export type ReportRepository = {
|
||||
// Snapshot building (pre-transaction)
|
||||
getCurrentAddress(editorialId: string): Promise<BillingReportAddress | null>;
|
||||
getEditorialFlatrate(editorialId: string): Promise<{ flatrateActive: boolean; flatratePriceCents: number | null; flatrateAssetLimit: number | null; } | null>;
|
||||
getSelectedUsages(editorialId: string, usageIds: string[]): Promise<SelectedUsage[]>;
|
||||
getRepeatDownloads(editorialId: string, period: BillingPeriod): Promise<BillingReportRepeatDownload[]>;
|
||||
allocateReportNumber(): Promise<number>;
|
||||
@@ -235,6 +237,7 @@ export function createReportService({
|
||||
selectedPositions: selectedUsages.map(toReportPosition),
|
||||
repeatDownloads,
|
||||
netTotalCents,
|
||||
flatrateCentsSnapshot: input.flatrateCentsSnapshot,
|
||||
reportNumber: 0,
|
||||
invoiceNumber: "VORSCHAU",
|
||||
period: input.period,
|
||||
@@ -273,6 +276,7 @@ export function createReportService({
|
||||
selectedPositions: selectedUsages.map(toReportPosition),
|
||||
repeatDownloads,
|
||||
netTotalCents,
|
||||
flatrateCentsSnapshot: input.flatrateCentsSnapshot,
|
||||
reportNumber,
|
||||
invoiceNumber,
|
||||
period: input.period,
|
||||
@@ -298,6 +302,7 @@ export function createReportService({
|
||||
address,
|
||||
repeatDownloads,
|
||||
netTotalCents,
|
||||
flatrateCentsSnapshot: input.flatrateCentsSnapshot,
|
||||
pdfBytes: rendered.bytes,
|
||||
pdfSha256: rendered.sha256,
|
||||
documentId,
|
||||
@@ -394,6 +399,7 @@ async function buildSnapshot(
|
||||
address: BillingReportAddress;
|
||||
repeatDownloads: BillingReportRepeatDownload[];
|
||||
netTotalCents: number;
|
||||
flatrateCentsSnapshot: number | null;
|
||||
}> {
|
||||
if (input.usageIds.length === 0) {
|
||||
throw new ReportValidationError("NO_USAGES_SELECTED");
|
||||
@@ -426,11 +432,20 @@ async function buildSnapshot(
|
||||
throw new ReportValidationError("ADDRESS_NOT_FOUND");
|
||||
}
|
||||
|
||||
const editorialFlatrate = await repo.getEditorialFlatrate(input.editorialId);
|
||||
let flatrateCentsSnapshot: number | null = null;
|
||||
if (editorialFlatrate && editorialFlatrate.flatrateActive && editorialFlatrate.flatratePriceCents !== null) {
|
||||
flatrateCentsSnapshot = editorialFlatrate.flatratePriceCents;
|
||||
}
|
||||
|
||||
const repeatDownloads = await repo.getRepeatDownloads(input.editorialId, input.period);
|
||||
|
||||
const netTotalCents = selectedUsages.reduce((sum, u) => sum + u.priceCents, 0);
|
||||
let netTotalCents = selectedUsages.reduce((sum, u) => sum + u.priceCents, 0);
|
||||
if (flatrateCentsSnapshot !== null) {
|
||||
netTotalCents += flatrateCentsSnapshot;
|
||||
}
|
||||
|
||||
return { usages: selectedUsages, address, repeatDownloads, netTotalCents };
|
||||
return { usages: selectedUsages, address, repeatDownloads, netTotalCents, flatrateCentsSnapshot };
|
||||
}
|
||||
|
||||
function toReportPosition(usage: SelectedUsage): BillingReportPosition {
|
||||
@@ -484,6 +499,19 @@ export function createReportRepository(
|
||||
return version;
|
||||
},
|
||||
|
||||
async getEditorialFlatrate(editorialId) {
|
||||
const [editorial] = await database
|
||||
.select({
|
||||
flatrateActive: editorialAccounts.flatrateActive,
|
||||
flatratePriceCents: editorialAccounts.flatratePriceCents,
|
||||
flatrateAssetLimit: editorialAccounts.flatrateAssetLimit,
|
||||
})
|
||||
.from(editorialAccounts)
|
||||
.where(eq(editorialAccounts.id, editorialId))
|
||||
.limit(1);
|
||||
return editorial || null;
|
||||
},
|
||||
|
||||
async getSelectedUsages(editorialId, usageIds) {
|
||||
const rows = await database
|
||||
.select({
|
||||
@@ -581,6 +609,7 @@ export function createReportRepository(
|
||||
billingAddressSnapshot: input.address,
|
||||
invoiceNumber: input.invoiceNumber,
|
||||
netTotalCents: input.netTotalCents,
|
||||
flatrateCentsSnapshot: input.flatrateCentsSnapshot,
|
||||
status: "final",
|
||||
pdf: input.pdfBytes,
|
||||
pdfSha256: input.pdfSha256,
|
||||
|
||||
@@ -72,4 +72,6 @@ export type BillingReportSnapshot = {
|
||||
createdAt: Date;
|
||||
/** Legal notice clarifying that the report is not a VAT invoice. */
|
||||
legalSeparationNotice: string;
|
||||
/** Flatrate amount snapshot, if applicable. */
|
||||
flatrateCentsSnapshot?: number | null;
|
||||
};
|
||||
Reference in new issue
Block a user