Skip to content
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

fix(core): Display the last activated plan name when multiple are activated #12835

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions packages/cli/src/__tests__/license.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const MOCK_ACTIVATION_KEY = 'activation-key';
const MOCK_FEATURE_FLAG = 'feat:sharing';
const MOCK_MAIN_PLAN_ID = '1b765dc4-d39d-4ffe-9885-c56dd67c4b26';

function makeDateWithHourOffset(offsetInHours: number): Date {
const date = new Date();
date.setHours(date.getHours() + offsetInHours);
return date;
}

const licenseConfig: GlobalConfig['license'] = {
serverUrl: MOCK_SERVER_URL,
autoRenewalEnabled: true,
Expand Down Expand Up @@ -134,7 +140,7 @@ describe('License', () => {
expect(LicenseManager.prototype.getManagementJwt).toHaveBeenCalled();
});

test('getMainPlan() returns the right entitlement', async () => {
test('getMainPlan() returns the latest main entitlement', async () => {
// mock entitlements response
License.prototype.getCurrentEntitlements = jest.fn().mockReturnValue([
{
Expand All @@ -143,8 +149,21 @@ describe('License', () => {
productMetadata: {},
features: {},
featureOverrides: {},
validFrom: new Date(),
validTo: new Date(),
validFrom: makeDateWithHourOffset(-3),
validTo: makeDateWithHourOffset(1),
},
{
id: '95b9c852-1349-478d-9ad1-b3f55510e488',
productId: '670650f2-72d8-4397-898c-c249906e2cc2',
productMetadata: {
terms: {
isMainPlan: true,
},
},
features: {},
featureOverrides: {},
validFrom: makeDateWithHourOffset(-2),
validTo: makeDateWithHourOffset(1),
},
{
id: MOCK_MAIN_PLAN_ID,
Expand All @@ -156,8 +175,8 @@ describe('License', () => {
},
features: {},
featureOverrides: {},
validFrom: new Date(),
validTo: new Date(),
validFrom: makeDateWithHourOffset(-1), // this is the LATEST / newest plan
validTo: makeDateWithHourOffset(1),
},
]);
jest.fn(license.getMainPlan).mockReset();
Expand All @@ -175,17 +194,17 @@ describe('License', () => {
productMetadata: {}, // has no `productMetadata.terms.isMainPlan`!
features: {},
featureOverrides: {},
validFrom: new Date(),
validTo: new Date(),
validFrom: makeDateWithHourOffset(-1),
validTo: makeDateWithHourOffset(1),
},
{
id: 'c1aae471-c24e-4874-ad88-b97107de486c',
productId: '670650f2-72d8-4397-898c-c249906e2cc2',
productMetadata: {}, // has no `productMetadata.terms.isMainPlan`!
features: {},
featureOverrides: {},
validFrom: new Date(),
validTo: new Date(),
validFrom: makeDateWithHourOffset(-1),
validTo: makeDateWithHourOffset(1),
},
]);
jest.fn(license.getMainPlan).mockReset();
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export class License {
}

/**
* Helper function to get the main plan for a license
* Helper function to get the latest main plan for a license
*/
getMainPlan(): TEntitlement | undefined {
if (!this.manager) {
Expand All @@ -342,6 +342,8 @@ export class License {
return undefined;
}

entitlements.sort((a, b) => b.validFrom.getTime() - a.validFrom.getTime());

return entitlements.find(
(entitlement) => (entitlement.productMetadata?.terms as { isMainPlan?: boolean })?.isMainPlan,
);
Expand Down
Loading