Skip to content

Commit fcc09de

Browse files
committed
fix(dashboard): resolve pinned license plan version
1 parent c29524e commit fcc09de

4 files changed

Lines changed: 48 additions & 5 deletions

File tree

vite/src/hooks/queries/usePlanLicensesQuery.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@ import { useQuery, useQueryClient } from "@tanstack/react-query";
33
import { useQueryKeyFactory } from "@/hooks/common/useQueryKeyFactory";
44
import { useAxiosInstance } from "@/services/useAxiosInstance";
55

6-
const EMPTY_PLAN_LICENSES: PlanLicense[] = [];
6+
export type VersionedPlanLicense = PlanLicense & { version: number };
7+
8+
const EMPTY_PLAN_LICENSES: VersionedPlanLicense[] = [];
79

810
const toPlanLicenses = ({
911
parentPlanId,
1012
licenses,
1113
}: {
1214
parentPlanId: string;
1315
licenses: NonNullable<ApiPlanV1["licenses"]>;
14-
}): PlanLicense[] =>
16+
}): VersionedPlanLicense[] =>
1517
licenses.map((link) => ({
1618
id: `${parentPlanId}:${link.license_plan_id}`,
1719
parent_plan_id: parentPlanId,
1820
license_plan_id: link.license_plan_id,
21+
version: link.version,
1922
included: link.included,
2023
prepaid_only: link.prepaid_only,
2124
customize: link.customize ?? null,
@@ -45,7 +48,7 @@ export const usePlanLicensesQuery = (parentPlanId?: string) => {
4548
});
4649
};
4750

48-
const { data, isLoading, error, refetch } = useQuery<PlanLicense[]>({
51+
const { data, isLoading, error, refetch } = useQuery<VersionedPlanLicense[]>({
4952
queryKey: buildKey(["plan_licenses", parentPlanId ?? null]),
5053
queryFn: () => fetchPlanLicenses(parentPlanId as string),
5154
enabled: Boolean(parentPlanId),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { ProductV2 } from "@autumn/shared";
2+
3+
export const resolvePlanLicenseProduct = ({
4+
products,
5+
planId,
6+
version,
7+
}: {
8+
products: ProductV2[];
9+
planId: string;
10+
version: number;
11+
}) =>
12+
products.find(
13+
(product) => product.id === planId && product.version === version,
14+
);

vite/src/views/products/plan/components/plan-licenses/useResolvedPlanLicenses.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
pendingPlanLicense,
1313
usePendingLicenseLinks,
1414
} from "./PendingLicenseLinksContext";
15+
import { resolvePlanLicenseProduct } from "./resolvePlanLicenseProduct";
1516

1617
export interface ResolvedPlanLicense {
1718
planLicense: PlanLicense;
@@ -33,7 +34,7 @@ export function useResolvedPlanLicenses(): ResolvedPlanLicense[] {
3334
const { planLicenses: fallbackPlanLicenses } = usePlanLicensesQuery(
3435
isLicense || pageCatalogLicenses ? undefined : product?.id,
3536
);
36-
const { products } = useProductsQuery();
37+
const { products } = useProductsQuery({ allVersions: true });
3738
const { pendingLicenseIds } = usePendingLicenseLinks();
3839
const initialPatches = useInitialLicensePatches();
3940

@@ -44,10 +45,20 @@ export function useResolvedPlanLicenses(): ResolvedPlanLicense[] {
4445
const licenseById = new Map(
4546
products.map((license) => [license.id, license]),
4647
);
48+
for (const license of products) {
49+
const latest = licenseById.get(license.id);
50+
if (!latest || license.version > latest.version) {
51+
licenseById.set(license.id, license);
52+
}
53+
}
4754
const persistedLicenses = pageCatalogLicenses
4855
? pageCatalogLicenses.map((entry) => ({ ...entry, isPendingLink: false }))
4956
: fallbackPlanLicenses.flatMap((planLicense) => {
50-
const license = licenseById.get(planLicense.license_plan_id);
57+
const license = resolvePlanLicenseProduct({
58+
products,
59+
planId: planLicense.license_plan_id,
60+
version: planLicense.version,
61+
});
5162
return license
5263
? [{ planLicense, license, isPendingLink: false }]
5364
: [];
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect, test } from "bun:test";
2+
import type { ProductV2 } from "@autumn/shared";
3+
import { resolvePlanLicenseProduct } from "@/views/products/plan/components/plan-licenses/resolvePlanLicenseProduct";
4+
5+
test("resolves the license version pinned by the parent plan", () => {
6+
const products = [
7+
{ id: "team_seat", version: 1, name: "$54" },
8+
{ id: "team_seat", version: 5, name: "$72" },
9+
] as ProductV2[];
10+
11+
expect(
12+
resolvePlanLicenseProduct({ products, planId: "team_seat", version: 1 })
13+
?.name,
14+
).toBe("$54");
15+
});

0 commit comments

Comments
 (0)