fix(editor): Fix cloud plan data loading on instance (#7841)

Moving cloud hooks and store initialization logic after users are
authenticated. This will ensure user local account is available when
their cloud plan data is being fetched.
This PR also adds the following error handling improvements:
- Added error handling to the same initialization logic
- Fixed empty `catch` clauses inside the cloud store which caused it to
silently fail and complicated debugging of this bug
This commit is contained in:
Milorad FIlipović
2023-11-29 10:51:15 +01:00
committed by GitHub
parent 90bb6ba417
commit 8b99384367
7 changed files with 136 additions and 13 deletions

View File

@@ -165,11 +165,15 @@ describe('UI store', () => {
const fetchUserCloudAccountSpy = vi
.spyOn(cloudPlanApi, 'getCloudUserInfo')
.mockResolvedValue(getUserCloudInfo(true));
const getCurrentUsageSpy = vi
.spyOn(cloudPlanApi, 'getCurrentUsage')
.mockResolvedValue({ executions: 1000, activeWorkflows: 100 });
setupOwnerAndCloudDeployment();
await cloudPlanStore.checkForCloudPlanData();
await cloudPlanStore.fetchUserCloudAccount();
expect(fetchCloudSpy).toHaveBeenCalled();
expect(fetchUserCloudAccountSpy).toHaveBeenCalled();
expect(getCurrentUsageSpy).toHaveBeenCalled();
expect(uiStore.bannerStack).toContain('TRIAL');
// There should be no email confirmation banner for trialing users
expect(uiStore.bannerStack).not.toContain('EMAIL_CONFIRMATION');
@@ -183,10 +187,15 @@ describe('UI store', () => {
.spyOn(cloudPlanApi, 'getCloudUserInfo')
.mockResolvedValue(getUserCloudInfo(true));
setupOwnerAndCloudDeployment();
const getCurrentUsageSpy = vi
.spyOn(cloudPlanApi, 'getCurrentUsage')
.mockResolvedValue({ executions: 1000, activeWorkflows: 100 });
setupOwnerAndCloudDeployment();
await cloudPlanStore.checkForCloudPlanData();
await cloudPlanStore.fetchUserCloudAccount();
expect(fetchCloudSpy).toHaveBeenCalled();
expect(fetchUserCloudAccountSpy).toHaveBeenCalled();
expect(getCurrentUsageSpy).toHaveBeenCalled();
expect(uiStore.bannerStack).toContain('TRIAL_OVER');
// There should be no email confirmation banner for trialing users
expect(uiStore.bannerStack).not.toContain('EMAIL_CONFIRMATION');

View File

@@ -10,7 +10,7 @@ function getUserPlanData(trialExpirationDate: Date, isTrial = true): Cloud.PlanD
isActive: true,
displayName: 'Trial',
metadata: {
group: isTrial ? 'trial' : 'pro',
group: isTrial ? 'trial' : 'opt-in',
slug: 'trial-1',
trial: {
gracePeriod: 3,

View File

@@ -55,7 +55,7 @@ export const useCloudPlanStore = defineStore(STORES.CLOUD_PLAN, () => {
const hasCloudPlan = computed(() => {
const cloudUserId = settingsStore.settings.n8nMetadata?.userId;
return usersStore.currentUser?.isOwner && settingsStore.isCloudDeployment && cloudUserId;
return usersStore.isInstanceOwner && settingsStore.isCloudDeployment && cloudUserId;
});
const getUserCloudAccount = async () => {
@@ -68,7 +68,7 @@ export const useCloudPlanStore = defineStore(STORES.CLOUD_PLAN, () => {
}
}
} catch (error) {
throw new Error(error);
throw new Error(error.message);
}
};
@@ -143,13 +143,17 @@ export const useCloudPlanStore = defineStore(STORES.CLOUD_PLAN, () => {
if (!userIsTrialing.value) return;
await getInstanceCurrentUsage();
startPollingInstanceUsageData();
} catch {}
} catch (e) {
throw new Error(e.message);
}
};
const fetchUserCloudAccount = async () => {
try {
await getUserCloudAccount();
} catch {}
} catch (e) {
throw new Error(e.message);
}
};
const redirectToDashboard = async () => {
@@ -163,8 +167,17 @@ export const useCloudPlanStore = defineStore(STORES.CLOUD_PLAN, () => {
return;
}
await checkForCloudPlanData();
await fetchUserCloudAccount();
try {
await checkForCloudPlanData();
} catch (error) {
console.warn('Error checking for cloud plan data:', error);
}
try {
await fetchUserCloudAccount();
} catch (error) {
console.warn('Error fetching user cloud account:', error);
}
state.initialized = true;
};