-
Notifications
You must be signed in to change notification settings - Fork 244
feat(licenses): support child plan propagation #2295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
49c0ce5
feat(licenses): 🎸 support child plan propagation
johnyeocx 091e518
test(licenses): 💍 update product item reference mocks
johnyeocx a1ae586
Merge remote-tracking branch 'origin/dev' into license-update-child
johnyeocx 45f283d
fix: integration tests
johnyeocx 291bcb9
fix: integration tests
johnyeocx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule ai
updated
from bca809 to 7c63f3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
server/src/internal/billing/v2/common/errors/handleLicenseAttachTargetErrors.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { | ||
| ErrCode, | ||
| type FullCustomer, | ||
| type FullProduct, | ||
| RecaseError, | ||
| } from "@autumn/shared"; | ||
|
|
||
| /** The customer's plan offering this license, if any: matched via their | ||
| * pools (survives version pins) or the license's catalog parent links | ||
| * (covers links added after the parent was attached). */ | ||
| const findLicenseParentPlanId = ({ | ||
| fullCustomer, | ||
| licenseProduct, | ||
| }: { | ||
| fullCustomer: FullCustomer; | ||
| licenseProduct: FullProduct; | ||
| }): string | null => { | ||
| const parentInternalProductIds = new Set( | ||
| (licenseProduct.parent_plan_licenses ?? []).map( | ||
| (link) => link.parent_internal_product_id, | ||
| ), | ||
| ); | ||
|
|
||
| for (const customerProduct of fullCustomer.customer_products) { | ||
| if (parentInternalProductIds.has(customerProduct.internal_product_id)) { | ||
| return customerProduct.product.id; | ||
| } | ||
| const pool = (customerProduct.customer_licenses ?? []).find( | ||
| (customerLicense) => | ||
| customerLicense.planLicense?.product.id === licenseProduct.id, | ||
| ); | ||
| if (pool) return customerProduct.product.id; | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
| /** License linkage preconditions on the attach target: plans offering | ||
| * licenses live on the customer, and licensed seats go through the parent. */ | ||
| export const handleLicenseAttachTargetErrors = ({ | ||
| fullCustomer, | ||
| attachProduct, | ||
| }: { | ||
| fullCustomer: FullCustomer; | ||
| attachProduct: FullProduct; | ||
| }) => { | ||
| const entity = fullCustomer.entity; | ||
| if (entity && attachProduct.licenses?.length) { | ||
| throw new RecaseError({ | ||
| message: | ||
| `Plan ${attachProduct.id} offers licenses, so it can only be attached ` + | ||
| `to the customer — not to entity ${entity.id ?? entity.internal_id}.`, | ||
| code: ErrCode.InvalidRequest, | ||
| statusCode: 400, | ||
| }); | ||
| } | ||
|
|
||
| const parentPlanId = findLicenseParentPlanId({ | ||
| fullCustomer, | ||
| licenseProduct: attachProduct, | ||
| }); | ||
| if (parentPlanId) { | ||
| throw new RecaseError({ | ||
| message: | ||
| `Plan ${attachProduct.id} is a license under ${parentPlanId}, which ` + | ||
| `this customer is on. Assign seats with licenses.attach instead of ` + | ||
| `attaching it directly.`, | ||
| code: ErrCode.InvalidRequest, | ||
| statusCode: 400, | ||
| }); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
.../internal/billing/v2/compute/customerLicenseTransitions/matchCustomerLicenseSuccessors.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import type { FullCustomerLicense } from "@autumn/shared"; | ||
|
|
||
| export type CustomerLicenseSuccessorMatch = { | ||
| outgoingCustomerLicense: FullCustomerLicense; | ||
| incomingCustomerLicense: FullCustomerLicense; | ||
| }; | ||
|
|
||
| export type UnmatchedOutgoingCustomerLicense = { | ||
| outgoingCustomerLicense: FullCustomerLicense; | ||
| reason: "dropped" | "ambiguous"; | ||
| group?: string; | ||
| }; | ||
|
|
||
| const licensePlanIdOf = (customerLicense: FullCustomerLicense) => | ||
| customerLicense.planLicense?.product.id ?? | ||
| customerLicense.license_internal_product_id; | ||
|
|
||
| /** Empty group never cross-pairs — grouping is the explicit opt-in for | ||
| * "this is the same seat across plans". */ | ||
| const licenseGroupOf = (customerLicense: FullCustomerLicense) => | ||
| customerLicense.planLicense?.product.group || null; | ||
|
|
||
| /** | ||
| * Ranked successor selection for license pools across a parent plan | ||
| * transition: the same license plan id always wins; otherwise pools pair by | ||
| * their license plan's group, but only when the group resolves 1:1 among the | ||
| * pools no id claimed. Several candidates on either side match nothing. | ||
| */ | ||
| export const matchCustomerLicenseSuccessors = ({ | ||
| outgoingCustomerLicenses, | ||
| incomingCustomerLicenses, | ||
| }: { | ||
| outgoingCustomerLicenses: FullCustomerLicense[]; | ||
| incomingCustomerLicenses: FullCustomerLicense[]; | ||
| }): { | ||
| matches: CustomerLicenseSuccessorMatch[]; | ||
| unmatched: UnmatchedOutgoingCustomerLicense[]; | ||
| } => { | ||
| const incomingByLicensePlanId = new Map<string, FullCustomerLicense>(); | ||
| for (const incoming of incomingCustomerLicenses) { | ||
| const licensePlanId = licensePlanIdOf(incoming); | ||
| if (!incomingByLicensePlanId.has(licensePlanId)) { | ||
| incomingByLicensePlanId.set(licensePlanId, incoming); | ||
| } | ||
| } | ||
|
|
||
| const idClaimedIncoming = new Set<FullCustomerLicense>(); | ||
| for (const outgoing of outgoingCustomerLicenses) { | ||
| const claimed = incomingByLicensePlanId.get(licensePlanIdOf(outgoing)); | ||
| if (claimed) idClaimedIncoming.add(claimed); | ||
| } | ||
|
|
||
| const incomingByGroup = new Map<string, FullCustomerLicense[]>(); | ||
| for (const incoming of incomingCustomerLicenses) { | ||
| if (idClaimedIncoming.has(incoming)) continue; | ||
| const group = licenseGroupOf(incoming); | ||
| if (!group) continue; | ||
| const rows = incomingByGroup.get(group); | ||
| if (rows) rows.push(incoming); | ||
| else incomingByGroup.set(group, [incoming]); | ||
| } | ||
|
|
||
| const outgoingGroupCounts = new Map<string, number>(); | ||
| for (const outgoing of outgoingCustomerLicenses) { | ||
| if (incomingByLicensePlanId.has(licensePlanIdOf(outgoing))) continue; | ||
| const group = licenseGroupOf(outgoing); | ||
| if (!group) continue; | ||
| outgoingGroupCounts.set(group, (outgoingGroupCounts.get(group) ?? 0) + 1); | ||
| } | ||
|
|
||
| const matches: CustomerLicenseSuccessorMatch[] = []; | ||
| const unmatched: UnmatchedOutgoingCustomerLicense[] = []; | ||
| for (const outgoing of outgoingCustomerLicenses) { | ||
| const idMatch = incomingByLicensePlanId.get(licensePlanIdOf(outgoing)); | ||
| if (idMatch) { | ||
| matches.push({ | ||
| outgoingCustomerLicense: outgoing, | ||
| incomingCustomerLicense: idMatch, | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| const group = licenseGroupOf(outgoing); | ||
| const candidates = group ? (incomingByGroup.get(group) ?? []) : []; | ||
| if (candidates.length === 0) { | ||
| unmatched.push({ outgoingCustomerLicense: outgoing, reason: "dropped" }); | ||
| continue; | ||
| } | ||
| const ambiguous = | ||
| candidates.length > 1 || (outgoingGroupCounts.get(group ?? "") ?? 0) > 1; | ||
| if (ambiguous) { | ||
| unmatched.push({ | ||
| outgoingCustomerLicense: outgoing, | ||
| reason: "ambiguous", | ||
| group: group ?? undefined, | ||
| }); | ||
| continue; | ||
| } | ||
| matches.push({ | ||
| outgoingCustomerLicense: outgoing, | ||
| incomingCustomerLicense: candidates[0], | ||
| }); | ||
| } | ||
|
|
||
| return { matches, unmatched }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: A dangling customer-license row can now suppress a valid live-license transition. Filter dead-link rows before calling
matchCustomerLicenseSuccessors; filtering after matching lets their fallback id claim the incoming pool, so its match is discarded and the live pool is left unpropagated.Prompt for AI agents