diff --git a/packages/editor-ui/src/stores/__tests__/ui.test.ts b/packages/editor-ui/src/stores/__tests__/ui.test.ts index ace9ab3bca..a96f7180b5 100644 --- a/packages/editor-ui/src/stores/__tests__/ui.test.ts +++ b/packages/editor-ui/src/stores/__tests__/ui.test.ts @@ -1,5 +1,9 @@ import { createPinia, setActivePinia } from 'pinia'; -import { generateUpgradeLinkUrl, useUIStore } from '@/stores/ui.store'; +import { + generateCloudDashboardAutoLoginLink, + generateUpgradeLink, + useUIStore, +} from '@/stores/ui.store'; import { useSettingsStore } from '@/stores/settings.store'; import { useUsersStore } from '@/stores/users.store'; import { merge } from 'lodash-es'; @@ -98,7 +102,7 @@ describe('UI store', () => { 'https://n8n.io/pricing?utm_campaign=utm-test-campaign&source=test_source', ], ])( - '"generateUpgradeLinkUrl" should generate the correct URL for "%s" deployment and "%s" license environment and user role "%s"', + '"generateUpgradeLink" should generate the correct URL for "%s" deployment and "%s" license environment and user role "%s"', async (type, environment, role, expectation) => { setUser(role as IRole); @@ -115,7 +119,7 @@ describe('UI store', () => { }), ); - const updateLinkUrl = await generateUpgradeLinkUrl('test_source', 'utm-test-campaign', type); + const updateLinkUrl = await generateUpgradeLink('test_source', 'utm-test-campaign', type); expect(updateLinkUrl).toBe(expectation); }, diff --git a/packages/editor-ui/src/stores/ui.store.ts b/packages/editor-ui/src/stores/ui.store.ts index 09247f0d18..50913c4a38 100644 --- a/packages/editor-ui/src/stores/ui.store.ts +++ b/packages/editor-ui/src/stores/ui.store.ts @@ -577,15 +577,7 @@ export const useUIStore = defineStore(STORES.UI, () => { workflowsLeft, }); - let upgradeLink = N8N_PRICING_PAGE_URL; - - if (deploymentType === 'cloud' && hasPermission(['instanceOwner'])) { - upgradeLink = await generateCloudDashboardAutoLoginLink({ - source, - utm_campaign, - redirectionPath: '/account/change-plan', - }); - } + const upgradeLink = await generateUpgradeLink(source, utm_campaign, deploymentType); if (mode === 'open') { window.open(upgradeLink, '_blank'); @@ -773,29 +765,43 @@ export const listenForModalChanges = (opts: { }); }; +export const generateUpgradeLink = async ( + source: string, + utm_campaign: string, + deploymentType: string, +) => { + let upgradeLink = N8N_PRICING_PAGE_URL; + if (deploymentType === 'cloud' && hasPermission(['instanceOwner'])) { + upgradeLink = await generateCloudDashboardAutoLoginLink({ + redirectionPath: '/account/change-plan', + }); + } + + const url = new URL(upgradeLink); + + if (utm_campaign) { + url.searchParams.set('utm_campaign', utm_campaign); + } + + if (source) { + url.searchParams.set('source', source); + } + + return url.toString(); +}; + const generateCloudDashboardAutoLoginLink = async (data: { - source?: string; - utm_campaign?: string; redirectionPath: string; }) => { - let linkUrl = ''; - const searchParams = new URLSearchParams(); const cloudPlanStore = useCloudPlanStore(); const adminPanelHost = new URL(window.location.href).host.split('.').slice(1).join('.'); const { code } = await cloudPlanStore.getAutoLoginCode(); - linkUrl = `https://${adminPanelHost}/login`; + const linkUrl = `https://${adminPanelHost}/login`; searchParams.set('code', code); searchParams.set('returnPath', data.redirectionPath); - if (data?.utm_campaign) { - searchParams.set('utm_campaign', data.utm_campaign); - } - - if (data?.source) { - searchParams.set('source', data.source); - } return `${linkUrl}?${searchParams.toString()}`; };