-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathcustomerLicenseToStripeItemSpecs.ts
More file actions
55 lines (51 loc) · 1.94 KB
/
Copy pathcustomerLicenseToStripeItemSpecs.ts
File metadata and controls
55 lines (51 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type {
BillingContext,
FullCustomerLicense,
StripeItemSpec,
} from "@autumn/shared";
import { customerLicenseToUnusedPrepaidRows } from "@/internal/billing/v2/utils/lineItems/customerLicenseToUnusedPrepaidRows";
import { resolveLicenseBillingRowsThroughDefinition } from "@/internal/billing/v2/utils/lineItems/resolveLicenseBillingRowsThroughDefinition";
import { licenseBillingRowToStripeItemSpec } from "../stripeItemSpec/licenseBillingRowToStripeItemSpec";
/**
* Desired Stripe items for one customer license: the context's seat rows
* keyed to THIS row's id (persisted snapshots for continuing rows,
* transitioned copies for planted successors) plus the unassigned buffer.
* Seat + buffer specs sharing a price merge in the callers' accumulators.
*/
export const customerLicenseToStripeItemSpecs = ({
billingContext,
customerLicense,
}: {
billingContext: BillingContext;
customerLicense: FullCustomerLicense;
}): StripeItemSpec[] => {
const planLicense = customerLicense.planLicense;
if (!planLicense) return [];
const seatRows = (
billingContext.customerLicenseBillingContext?.licenseBillingPriceRows ?? []
).filter((row) => row.source.customerLicenseId === customerLicense.id);
const projectedPlanLicenseIds =
billingContext.customerLicenseBillingContext?.projectedPlanLicenseIds ?? [];
// Desired state prices seats through the pool's (possibly repointed)
// definition — the read-time twin of the seat repoint executor.
const licenseBillingRows = [
...resolveLicenseBillingRowsThroughDefinition({
licenseBillingRows: seatRows,
planLicense,
projectedPlanLicenseIds,
}),
];
const billableAssignedQuantity = licenseBillingRows.reduce(
(total, row) => total + row.quantity,
0,
);
licenseBillingRows.push(
...customerLicenseToUnusedPrepaidRows({
customerLicense,
billableAssignedQuantity,
}),
);
return licenseBillingRows.map((licenseBillingRow) =>
licenseBillingRowToStripeItemSpec({ licenseBillingRow }),
);
};