n8n/packages/editor-ui/src/stores/cloudPlan.store.ts
OlegIvaniv 8f0ff460b1
fix(editor): Fix Luxon date parsing of ExecutionsUsage component (#6333)
* fix(editor): Fix luxon date parsing of ExecutionsUsage component

* Fix wrong indent
2023-05-29 13:29:07 +02:00

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,
};
});