From 04dfcd73bee2c1ea0d47fd7102383719827d53d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milorad=20FIlipovi=C4=87?= Date: Mon, 2 Oct 2023 14:25:03 +0200 Subject: [PATCH] fix(editor): Separate cloud endpoint calls (#7312) This PR untangles calls to cloud endpoints so failure in one of them doesn't stop others to go through. --- packages/editor-ui/src/App.vue | 7 ++-- .../editor-ui/src/stores/__tests__/ui.test.ts | 9 +++-- .../editor-ui/src/stores/cloudPlan.store.ts | 38 +++++++++++++------ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/packages/editor-ui/src/App.vue b/packages/editor-ui/src/App.vue index aab8297a9a..925a50b2de 100644 --- a/packages/editor-ui/src/App.vue +++ b/packages/editor-ui/src/App.vue @@ -144,8 +144,9 @@ export default defineComponent({ console.log(HIRING_BANNER); } }, - async checkForCloudPlanData() { - return this.cloudPlanStore.checkForCloudPlanData(); + async checkForCloudData() { + await this.cloudPlanStore.checkForCloudPlanData(); + await this.cloudPlanStore.fetchUserCloudAccount(); }, async initialize(): Promise { await this.initSettings(); @@ -237,7 +238,7 @@ export default defineComponent({ await this.authenticate(); await this.redirectIfNecessary(); void this.checkForNewVersions(); - await this.checkForCloudPlanData(); + await this.checkForCloudData(); void this.postAuthenticate(); this.loading = false; diff --git a/packages/editor-ui/src/stores/__tests__/ui.test.ts b/packages/editor-ui/src/stores/__tests__/ui.test.ts index 898f02000b..91365a36d9 100644 --- a/packages/editor-ui/src/stores/__tests__/ui.test.ts +++ b/packages/editor-ui/src/stores/__tests__/ui.test.ts @@ -123,7 +123,8 @@ describe('UI store', () => { .spyOn(cloudPlanApi, 'getCloudUserInfo') .mockResolvedValue(getUserCloudInfo(true)); setupOwnerAndCloudDeployment(); - await cloudPlanStore.getOwnerCurrentPlan(); + await cloudPlanStore.checkForCloudPlanData(); + await cloudPlanStore.fetchUserCloudAccount(); expect(fetchCloudSpy).toHaveBeenCalled(); expect(fetchUserCloudAccountSpy).toHaveBeenCalled(); expect(uiStore.bannerStack).toContain('TRIAL'); @@ -137,7 +138,8 @@ describe('UI store', () => { .spyOn(cloudPlanApi, 'getCloudUserInfo') .mockResolvedValue(getUserCloudInfo(true)); setupOwnerAndCloudDeployment(); - await cloudPlanStore.getOwnerCurrentPlan(); + await cloudPlanStore.checkForCloudPlanData(); + await cloudPlanStore.fetchUserCloudAccount(); expect(fetchCloudSpy).toHaveBeenCalled(); expect(fetchUserCloudAccountSpy).toHaveBeenCalled(); expect(uiStore.bannerStack).toContain('TRIAL_OVER'); @@ -151,7 +153,8 @@ describe('UI store', () => { .spyOn(cloudPlanApi, 'getCloudUserInfo') .mockResolvedValue(getUserCloudInfo(false)); setupOwnerAndCloudDeployment(); - await cloudPlanStore.getOwnerCurrentPlan(); + await cloudPlanStore.checkForCloudPlanData(); + await cloudPlanStore.fetchUserCloudAccount(); expect(fetchCloudSpy).toHaveBeenCalled(); expect(fetchUserCloudAccountSpy).toHaveBeenCalled(); expect(uiStore.bannerStack).toContain('TRIAL_OVER'); diff --git a/packages/editor-ui/src/stores/cloudPlan.store.ts b/packages/editor-ui/src/stores/cloudPlan.store.ts index f736aa8bba..f6cdecdb39 100644 --- a/packages/editor-ui/src/stores/cloudPlan.store.ts +++ b/packages/editor-ui/src/stores/cloudPlan.store.ts @@ -52,11 +52,27 @@ export const useCloudPlanStore = defineStore(STORES.CLOUD_PLAN, () => { return state.usage?.executions >= state.data?.monthlyExecutionsLimit; }); - const getOwnerCurrentPlan = async () => { + const hasCloudPlan = computed(() => { const cloudUserId = settingsStore.settings.n8nMetadata?.userId; - const hasCloudPlan = - usersStore.currentUser?.isOwner && settingsStore.isCloudDeployment && cloudUserId; - if (!hasCloudPlan) throw new Error('User does not have a cloud plan'); + return usersStore.currentUser?.isOwner && settingsStore.isCloudDeployment && cloudUserId; + }); + + const getUserCloudAccount = async () => { + if (!hasCloudPlan.value) throw new Error('User does not have a cloud plan'); + try { + if (useUsersStore().isInstanceOwner) { + await usersStore.fetchUserCloudAccount(); + if (!usersStore.currentUserCloudInfo?.confirmed) { + useUIStore().pushBannerToStack('EMAIL_CONFIRMATION'); + } + } + } catch (error) { + throw new Error(error); + } + }; + + const getOwnerCurrentPlan = async () => { + if (!hasCloudPlan.value) throw new Error('User does not have a cloud plan'); state.loadingPlan = true; let plan; try { @@ -71,13 +87,6 @@ export const useCloudPlanStore = defineStore(STORES.CLOUD_PLAN, () => { useUIStore().pushBannerToStack('TRIAL'); } } - - if (useUsersStore().isInstanceOwner) { - await usersStore.fetchUserCloudAccount(); - if (!usersStore.currentUserCloudInfo?.confirmed) { - useUIStore().pushBannerToStack('EMAIL_CONFIRMATION'); - } - } } catch (error) { state.loadingPlan = false; throw new Error(error); @@ -132,6 +141,12 @@ export const useCloudPlanStore = defineStore(STORES.CLOUD_PLAN, () => { } catch {} }; + const fetchUserCloudAccount = async () => { + try { + await getUserCloudAccount(); + } catch {} + }; + return { state, getOwnerCurrentPlan, @@ -145,5 +160,6 @@ export const useCloudPlanStore = defineStore(STORES.CLOUD_PLAN, () => { allExecutionsUsed, reset, checkForCloudPlanData, + fetchUserCloudAccount, }; });