mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-09 11:57:28 -08:00
d3f01270c7
to test in staging use version `PR-6930-ado-990-trial-banner-does-not-disappear-after-sign-out` <img width="875" alt="image" src="https://github.com/n8n-io/n8n/assets/16496553/dfffe60f-bec3-4c48-bd9c-5990c68afa52">
134 lines
3.6 KiB
TypeScript
134 lines
3.6 KiB
TypeScript
import { computed, reactive } from 'vue';
|
|
import { defineStore } from 'pinia';
|
|
import type { CloudPlanState } from '@/Interface';
|
|
import { useRootStore } from '@/stores/n8nRoot.store';
|
|
import { useSettingsStore } from '@/stores/settings.store';
|
|
import { useUsersStore } from '@/stores/users.store';
|
|
import { getCurrentPlan, getCurrentUsage } from '@/api/cloudPlans';
|
|
import { DateTime } from 'luxon';
|
|
import { CLOUD_TRIAL_CHECK_INTERVAL } from '@/constants';
|
|
|
|
const DEFAULT_STATE: CloudPlanState = {
|
|
data: null,
|
|
usage: null,
|
|
loadingPlan: false,
|
|
};
|
|
|
|
export const useCloudPlanStore = defineStore('cloudPlan', () => {
|
|
const rootStore = useRootStore();
|
|
const settingsStore = useSettingsStore();
|
|
const usersStore = useUsersStore();
|
|
|
|
const state = reactive<CloudPlanState>(DEFAULT_STATE);
|
|
|
|
const setData = (data: CloudPlanState['data']) => {
|
|
state.data = data;
|
|
};
|
|
|
|
const setUsage = (data: CloudPlanState['usage']) => {
|
|
state.usage = data;
|
|
};
|
|
|
|
const reset = () => {
|
|
state.data = null;
|
|
state.usage = null;
|
|
};
|
|
|
|
const userIsTrialing = computed(() => state.data?.metadata?.group === 'trial');
|
|
|
|
const currentPlanData = computed(() => state.data);
|
|
|
|
const currentUsageData = computed(() => state.usage);
|
|
|
|
const trialExpired = computed(
|
|
() =>
|
|
state.data?.metadata?.group === 'trial' &&
|
|
DateTime.now().toMillis() >= DateTime.fromISO(state.data?.expirationDate).toMillis(),
|
|
);
|
|
|
|
const allExecutionsUsed = computed(() => {
|
|
if (!state.usage?.executions || !state.data?.monthlyExecutionsLimit) return false;
|
|
return state.usage?.executions >= state.data?.monthlyExecutionsLimit;
|
|
});
|
|
|
|
const getOwnerCurrentPlan = async () => {
|
|
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');
|
|
state.loadingPlan = true;
|
|
let plan;
|
|
try {
|
|
plan = await getCurrentPlan(rootStore.getRestApiContext);
|
|
state.data = plan;
|
|
state.loadingPlan = false;
|
|
} catch (error) {
|
|
state.loadingPlan = false;
|
|
throw new Error(error);
|
|
}
|
|
|
|
return plan;
|
|
};
|
|
|
|
const getInstanceCurrentUsage = async () => {
|
|
const usage = await getCurrentUsage({ baseUrl: rootStore.getBaseUrl, sessionId: '' });
|
|
state.usage = usage;
|
|
return usage;
|
|
};
|
|
|
|
const usageLeft = computed(() => {
|
|
if (!state.data || !state.usage) return { workflowsLeft: -1, executionsLeft: -1 };
|
|
|
|
return {
|
|
workflowsLeft: state.data.activeWorkflowsLimit - state.usage.activeWorkflows,
|
|
executionsLeft: state.data.monthlyExecutionsLimit - state.usage.executions,
|
|
};
|
|
});
|
|
|
|
const trialDaysLeft = computed(() => {
|
|
if (!state.data?.expirationDate) return -1;
|
|
|
|
const differenceInMs = new Date().valueOf() - new Date(state.data.expirationDate).valueOf();
|
|
|
|
const differenceInDays = Math.floor(differenceInMs / (1000 * 60 * 60 * 24));
|
|
|
|
return Math.ceil(differenceInDays);
|
|
});
|
|
|
|
const startPollingInstanceUsageData = () => {
|
|
const interval = setInterval(async () => {
|
|
try {
|
|
await getInstanceCurrentUsage();
|
|
if (trialExpired.value || allExecutionsUsed.value) {
|
|
clearTimeout(interval);
|
|
return;
|
|
}
|
|
} catch {}
|
|
}, CLOUD_TRIAL_CHECK_INTERVAL);
|
|
};
|
|
|
|
const checkForCloudPlanData = async (): Promise<void> => {
|
|
try {
|
|
await getOwnerCurrentPlan();
|
|
if (!userIsTrialing.value) return;
|
|
await getInstanceCurrentUsage();
|
|
startPollingInstanceUsageData();
|
|
} catch {}
|
|
};
|
|
|
|
return {
|
|
state,
|
|
getOwnerCurrentPlan,
|
|
getInstanceCurrentUsage,
|
|
usageLeft,
|
|
trialDaysLeft,
|
|
userIsTrialing,
|
|
currentPlanData,
|
|
currentUsageData,
|
|
trialExpired,
|
|
allExecutionsUsed,
|
|
reset,
|
|
checkForCloudPlanData,
|
|
};
|
|
});
|