Skip to content

Commit

Permalink
fix(editor): Separate cloud endpoint calls (#7312)
Browse files Browse the repository at this point in the history
This PR untangles calls to cloud endpoints so failure in one of them
doesn't stop others to go through.
  • Loading branch information
MiloradFilipovic authored Oct 2, 2023
1 parent 1691223 commit 04dfcd7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
7 changes: 4 additions & 3 deletions packages/editor-ui/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
await this.initSettings();
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions packages/editor-ui/src/stores/__tests__/ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down
38 changes: 27 additions & 11 deletions packages/editor-ui/src/stores/cloudPlan.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -132,6 +141,12 @@ export const useCloudPlanStore = defineStore(STORES.CLOUD_PLAN, () => {
} catch {}
};

const fetchUserCloudAccount = async () => {
try {
await getUserCloudAccount();
} catch {}
};

return {
state,
getOwnerCurrentPlan,
Expand All @@ -145,5 +160,6 @@ export const useCloudPlanStore = defineStore(STORES.CLOUD_PLAN, () => {
allExecutionsUsed,
reset,
checkForCloudPlanData,
fetchUserCloudAccount,
};
});

0 comments on commit 04dfcd7

Please sign in to comment.