mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-10 04:17:28 -08:00
8f0ff460b1
* fix(editor): Fix luxon date parsing of ExecutionsUsage component * Fix wrong indent
84 lines
2.3 KiB
TypeScript
84 lines
2.3 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';
|
|
|
|
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 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.getRestCloudApiContext, `${cloudUserId}`);
|
|
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;
|
|
};
|
|
|
|
return {
|
|
state,
|
|
getOwnerCurrentPlan,
|
|
getInstanceCurrentUsage,
|
|
userIsTrialing,
|
|
currentPlanData,
|
|
currentUsageData,
|
|
trialExpired,
|
|
allExecutionsUsed,
|
|
};
|
|
});
|