From 2d3f79c7962a0fb152ca282f5fa09406c1af6a28 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 2 Sep 2024 14:16:17 +0545 Subject: [PATCH] checking billing role --- src/lib/sdk/billing.ts | 2 +- src/lib/stores/roles.ts | 12 ++++- .../organization-[organization]/+layout.ts | 12 ++++- .../organization-[organization]/header.svelte | 52 +++++++++++++------ 4 files changed, 57 insertions(+), 21 deletions(-) diff --git a/src/lib/sdk/billing.ts b/src/lib/sdk/billing.ts index bf94cab26..dde3071c0 100644 --- a/src/lib/sdk/billing.ts +++ b/src/lib/sdk/billing.ts @@ -358,7 +358,7 @@ export class Billing { ); } - async getScopes(organizationId: string): Promise { + async getRoles(organizationId: string): Promise { const path = `/organizations/${organizationId}/roles`; const uri = new URL(this.client.config.endpoint + path); return await this.client.call( diff --git a/src/lib/stores/roles.ts b/src/lib/stores/roles.ts index ce1f0ba2d..d5aa20d65 100644 --- a/src/lib/stores/roles.ts +++ b/src/lib/stores/roles.ts @@ -1,8 +1,16 @@ import { page } from "$app/stores"; import { derived } from "svelte/store"; -export const roles = derived(page, ($page) => $page.data.roles); +export const roles = derived(page, function ($page) { + console.log("roles", $page.data.roles); + return $page.data.roles; + }); export const scopes = derived(page, ($page) => $page.data.scopes); export const isDeveloper = derived(roles, ($roles) => $roles.includes("developer")); - +export const isBilling = derived(roles, function ($roles) { + let isBilling= $roles.includes("billing"); + console.log("isBilling", isBilling); + return isBilling; + }); +export const isOwner = derived(roles, ($roles) => $roles.includes("owner")); export const canSeeDatabases = derived(scopes, ($scopes) => $scopes.includes("databases.read")); \ No newline at end of file diff --git a/src/routes/console/organization-[organization]/+layout.ts b/src/routes/console/organization-[organization]/+layout.ts index 17171db8f..8e2d2cce9 100644 --- a/src/routes/console/organization-[organization]/+layout.ts +++ b/src/routes/console/organization-[organization]/+layout.ts @@ -16,7 +16,10 @@ export const load: LayoutLoad = async ({ params, depends }) => { depends(Dependencies.ORGANIZATION); depends(Dependencies.MEMBERS); depends(Dependencies.PAYMENT_METHODS); - + let data: {roles: string[], scopes: string[]} = { + roles: [], + scopes: [] + }; if (isCloud) { await failedInvoice.load(params.organization); @@ -28,6 +31,10 @@ export const load: LayoutLoad = async ({ params, depends }) => { importance: 1 }); } + + const res = await sdk.forConsole.billing.getRoles(params.organization); + data.roles = res.roles; + data.scopes = res.scopes; } try { @@ -41,6 +48,8 @@ export const load: LayoutLoad = async ({ params, depends }) => { organization: await (sdk.forConsole.teams.get( params.organization ) as Promise), + roles: data.roles, + scopes: data.scopes, members: await sdk.forConsole.teams.listMemberships(params.organization) }; } catch (e) { @@ -49,4 +58,5 @@ export const load: LayoutLoad = async ({ params, depends }) => { sdk.forConsole.account.updatePrefs(newPrefs); error(e.code, e.message); } + return data; }; diff --git a/src/routes/console/organization-[organization]/header.svelte b/src/routes/console/organization-[organization]/header.svelte index 01ef09e1d..1ffdfaae3 100644 --- a/src/routes/console/organization-[organization]/header.svelte +++ b/src/routes/console/organization-[organization]/header.svelte @@ -32,6 +32,7 @@ organization, organizationList } from '$lib/stores/organization'; + import { isBilling, isDeveloper } from '$lib/stores/roles'; import { GRACE_PERIOD_OVERRIDE, isCloud } from '$lib/system'; let areMembersLimited: boolean; @@ -46,6 +47,8 @@ function createOrg() { showDropdown = false; + console.log('billing:', isBilling); + console.log('developer:', isDeveloper); if (isCloud) { goto(`${base}/console/create-organization`); } else newOrgModal.set(true); @@ -76,23 +79,38 @@ } ]; - $: tabs = isCloud - ? [ - ...permanentTabs, - { - href: `${path}/usage`, - event: 'usage', - title: 'Usage', - hasChildren: true - }, - { - href: `${path}/billing`, - event: 'billing', - title: 'Billing' - }, - ...permanentTabSettings - ] - : [...permanentTabs, ...permanentTabSettings]; + $: billingTabs = [ + { + href: `${path}/billing`, + event: 'billing', + title: 'Billing' + } + ]; + $: tabs = + isCloud && isBilling + ? [ + ...permanentTabs, + { + href: `${path}/usage`, + event: 'usage', + title: 'Usage', + hasChildren: true + }, + ...billingTabs, + ...permanentTabSettings + ] + : isCloud && !isBilling + ? [ + ...permanentTabs, + { + href: `${path}/usage`, + event: 'usage', + title: 'Usage', + hasChildren: true + }, + ...permanentTabSettings + ] + : [...permanentTabs, ...permanentTabSettings]; {#if $organization?.$id}