diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index 105e038907..18b44dadf5 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -132,29 +132,30 @@ const determineScopedConfig = async function (db, { type, user, group }) { } ) ) - const configs = response.rows.map(row => { + + function determineScore(row) { const config = row.doc // Config is specific to a user and a group if (config._id.includes(generateConfigID({ type, user, group }))) { - config.score = 4 + return 4 } else if (config._id.includes(generateConfigID({ type, user }))) { // Config is specific to a user only - config.score = 3 + return 3 } else if (config._id.includes(generateConfigID({ type, group }))) { // Config is specific to a group only - config.score = 2 + return 2 } else if (config._id.includes(generateConfigID({ type }))) { // Config is specific to a type only - config.score = 1 + return 1 } - return config - }) + return 0 + } // Find the config with the most granular scope based on context - const scopedConfig = configs.sort((a, b) => b.score - a.score)[0] + const scopedConfig = response.rows.sort((a, b) => determineScore(a) - determineScore(b))[0] - return scopedConfig + return scopedConfig.doc } exports.generateConfigID = generateConfigID diff --git a/packages/builder/src/builderStore/index.js b/packages/builder/src/builderStore/index.js index 6fecda84c0..bd849c834f 100644 --- a/packages/builder/src/builderStore/index.js +++ b/packages/builder/src/builderStore/index.js @@ -2,6 +2,7 @@ import { getFrontendStore } from "./store/frontend" import { getAutomationStore } from "./store/automation" import { getHostingStore } from "./store/hosting" import { getThemeStore } from "./store/theme" +import { getAdminStore } from "./store/admin" import { derived, writable } from "svelte/store" import analytics from "analytics" import { FrontendTypes, LAYOUT_NAMES } from "../constants" @@ -11,6 +12,7 @@ export const store = getFrontendStore() export const automationStore = getAutomationStore() export const themeStore = getThemeStore() export const hostingStore = getHostingStore() +export const adminPanelStore = getAdminStore() export const currentAsset = derived(store, $store => { const type = $store.currentFrontEndType diff --git a/packages/builder/src/builderStore/store/admin.js b/packages/builder/src/builderStore/store/admin.js new file mode 100644 index 0000000000..69fe81c91a --- /dev/null +++ b/packages/builder/src/builderStore/store/admin.js @@ -0,0 +1,47 @@ +import { writable } from "svelte/store" +import api, { get } from "../api" + +const INITIAL_ADMIN_STATE = { + oauth: [] +} + +export const getAdminStore = () => { + const store = writable({ ...INITIAL_ADMIN_STATE }) + store.actions = { + fetch: async () => { + const responses = await Promise.all([ + api.get("/api/hosting/"), + api.get("/api/hosting/urls"), + ]) + const [info, urls] = await Promise.all(responses.map(resp => resp.json())) + store.update(state => { + state.hostingInfo = info + state.appUrl = urls.app + return state + }) + return info + }, + save: async hostingInfo => { + const response = await api.post("/api/hosting", hostingInfo) + const revision = (await response.json()).rev + store.update(state => { + state.hostingInfo = { + ...hostingInfo, + _rev: revision, + } + return state + }) + }, + fetchDeployedApps: async () => { + let deployments = await (await get("/api/hosting/apps")).json() + store.update(state => { + state.deployedApps = deployments + state.deployedAppNames = Object.values(deployments).map(app => app.name) + state.deployedAppUrls = Object.values(deployments).map(app => app.url) + return state + }) + return deployments + }, + } + return store +} diff --git a/packages/builder/src/pages/builder/_layout.svelte b/packages/builder/src/pages/builder/_layout.svelte index c01de46334..d762a7beac 100644 --- a/packages/builder/src/pages/builder/_layout.svelte +++ b/packages/builder/src/pages/builder/_layout.svelte @@ -16,11 +16,14 @@ {#if $auth.user}