Files
cfw-autumn/server/src/internal/balances/utils/allocatedInvoice/createAllocatedInvoice.ts
2026-03-01 14:14:32 +00:00

42 lines
1021 B
TypeScript

import {
type FullCusEntWithFullCusProduct,
type FullCustomer,
InternalError,
} from "@autumn/shared";
import type { AutumnContext } from "@/honoUtils/HonoEnv";
import type { DeductionUpdate } from "../types/deductionUpdate";
import { computeAllocatedInvoicePlan } from "./compute/computeAllocatedInvoicePlan";
import { setupAllocatedInvoiceContext } from "./setupAllocatedInvoiceContext";
export const createAllocatedInvoice = async ({
ctx,
customerEntitlement,
fullCustomer,
update,
}: {
ctx: AutumnContext;
customerEntitlement: FullCusEntWithFullCusProduct;
fullCustomer: FullCustomer;
update: DeductionUpdate;
}) => {
const billingContext = await setupAllocatedInvoiceContext({
ctx,
customerEntitlement,
fullCustomer,
update,
});
if (!billingContext) {
throw new InternalError({
message: "setupAllocatedInvoiceContext: no billing context found",
});
}
const plan = computeAllocatedInvoicePlan({
ctx,
billingContext,
});
console.log("Plan:", JSON.stringify(plan, null, 2));
};