@api-client/ui 0.6.6 → 0.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/src/elements/setup/internals/OrganizationSelector.js +2 -2
- package/build/src/elements/setup/internals/OrganizationSelector.js.map +1 -1
- package/build/src/md/button/internals/base.d.ts +4 -1
- package/build/src/md/button/internals/base.d.ts.map +1 -1
- package/build/src/md/button/internals/base.js +4 -0
- package/build/src/md/button/internals/base.js.map +1 -1
- package/build/src/modeling/internals/DomainAutoFieldsDialog.d.ts.map +1 -1
- package/build/src/modeling/internals/DomainAutoFieldsDialog.js +11 -16
- package/build/src/modeling/internals/DomainAutoFieldsDialog.js.map +1 -1
- package/build/src/services/OrganizationService.d.ts +44 -6
- package/build/src/services/OrganizationService.d.ts.map +1 -1
- package/build/src/services/OrganizationService.js +79 -24
- package/build/src/services/OrganizationService.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/elements/setup/internals/OrganizationSelector.ts +2 -2
- package/src/md/button/internals/base.ts +5 -1
- package/src/modeling/internals/DomainAutoFieldsDialog.ts +11 -16
- package/src/services/OrganizationService.ts +84 -25
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OrganizationService.js","sourceRoot":"","sources":["../../../src/services/OrganizationService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAE5C,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAA;AAC5D,OAAO,QAAQ,MAAM,kBAAkB,CAAA;AAEvC,IAAK,KAIJ;AAJD,WAAK,KAAK;IACR,6CAAU,CAAA;IACV,yCAAQ,CAAA;IACR,uCAAO,CAAA;AACT,CAAC,EAJI,KAAK,KAAL,KAAK,QAIT;AAED,MAAM,CAAN,IAAY,eAiBX;AAjBD,WAAY,eAAe;IACzB;;OAEG;IACH,iDAAE,CAAA;IACF;;OAEG;IACH,mEAA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AAc,GAAG,GAAG,CAAC,GAAG,CAAA;IAC/B,CAAC","sourcesContent":["import { Service } from '../core/Service.js'\nimport type { IOrganization, UserOrganizationGrantType } from '@api-client/core/models/store/Organization.js'\nimport { CurrentOrganizationKey } from '../lib/StoreKeys.js'\nimport services from './api_service.js'\n\nenum State {\n NotChecked,\n Checking,\n Checked,\n}\n\nexport enum CompletionState {\n /**\n * The user has at least one organization and the current organization is selected.\n */\n OK,\n /**\n * The user has at leas two organizations and no valid selection.\n */\n NotSelected,\n /**\n * The user have no organizations. Setup is needed.\n */\n NoOrganizations,\n /**\n * Unable to determine the state due to an error.\n */\n Error,\n}\n\nconst OrgsCacheKey = 'user_organizations'\n\nexport default class OrganizationService extends Service {\n static override key = 'api-service/organization'\n\n state: State = State.NotChecked\n organizations: IOrganization[] = []\n organizationId: string | undefined\n /**\n * A pending organization ID that was set via the URL query parameter `oid`.\n * When it is set, it preselects the organization when loading list of organizations.\n * This will override the `organizationId` set in the local storage.\n */\n pendingOid: string | undefined | null\n\n /**\n * A hidden value for the `updateComplete` getter.\n */\n #updateComplete?: Promise<CompletionState>\n\n /**\n * The resolver to call when the update completes.\n */\n #updateResolver?: (state: CompletionState) => void\n\n protected override async onCreate(): Promise<void> {\n const url = new URL(window.location.href)\n this.pendingOid = url.searchParams.get('oid')\n }\n\n async getUserOrganization(id: string): Promise<IOrganization> {\n await this.#updateComplete\n const org = this.organizations.find((i) => i.key === id)\n if (!org) {\n throw new Error(`Organization with id ${id} not found`)\n }\n return org\n }\n\n async currentOrganization(): Promise<IOrganization | undefined> {\n if (this.state === State.Checking) {\n await this.#updateComplete\n }\n if (!this.organizationId) {\n return undefined\n }\n // TODO: we will store the orgs list in the local/session store\n // so we won't be pinging server with every page reload.\n return this.organizations.find((i) => i.key === this.organizationId)\n }\n\n protected async hasGrantType(grantType: UserOrganizationGrantType): Promise<boolean> {\n const org = await this.currentOrganization()\n if (!org) {\n return false\n }\n return org.grantType === grantType\n }\n\n async hasRoles(types: UserOrganizationGrantType[]): Promise<boolean> {\n const org = await this.currentOrganization()\n if (!org) {\n return false\n }\n return types.includes(org.grantType)\n }\n\n /**\n * Checks if the user is the owner of the current organization.\n * @returns `true` if the user is the owner of the current organization, `false` otherwise.\n */\n async isOwner(): Promise<boolean> {\n return this.hasGrantType('owner')\n }\n\n /**\n * Checks if the user is a manager of the current organization.\n * @returns `true` if the user is a manager of the current organization, `false` otherwise.\n */\n async isManager(): Promise<boolean> {\n return this.hasGrantType('manager')\n }\n\n /**\n * Checks if the user is an editor of the current organization.\n * @returns `true` if the user is a editor of the current organization, `false` otherwise.\n */\n async isEditor(): Promise<boolean> {\n return this.hasGrantType('editor')\n }\n\n /**\n * Forces to get the list of organizations from the server.\n *\n * Returns one of the following states:\n *\n * - `CompletionState.OK` - The user has at least one organization and the current organization is selected.\n * - `CompletionState.NotSelected` - The user has at leas two organizations and no valid selection.\n * - `CompletionState.NoOrganizations` - The user have no organizations. Setup is needed.\n * - `CompletionState.Error` - Unable to determine the state due to an error.\n *\n * @returns The state of the organization creation.\n */\n revalidate(): Promise<CompletionState> {\n if (this.state === State.Checking) {\n return this.#updateComplete as Promise<CompletionState>\n }\n if (this.organizationId) {\n return Promise.resolve(CompletionState.OK)\n }\n this.state = State.Checking\n this.#updateComplete = new Promise<CompletionState>((resolver) => {\n this.#updateResolver = resolver\n })\n this.#getState()\n return this.#updateComplete\n }\n\n /**\n * Reads the list of organizations either from the cache or from the server.\n */\n async #getState(): Promise<void> {\n const loaded = await this.#loadCache()\n if (!loaded) {\n try {\n await this.#callOrganizations()\n } catch {\n this.#resolve(CompletionState.Error)\n this.state = State.Checked\n return\n }\n }\n const result = await this.#readState()\n this.state = State.Checked\n this.#resolve(result)\n }\n\n async #loadCache(): Promise<boolean> {\n const cache = await services.get('cache')\n const orgs = await cache.get<IOrganization[]>(OrgsCacheKey)\n if (orgs) {\n this.organizations = orgs\n return true\n }\n return false\n }\n\n async #saveCache(orgs: IOrganization[]): Promise<void> {\n const cache = await services.get('cache')\n if (orgs.length === 0) {\n await cache.delete(OrgsCacheKey)\n } else {\n await cache.set<IOrganization[]>(OrgsCacheKey, orgs, cache.TTL.OneHour)\n }\n }\n\n async #callOrganizations(): Promise<void> {\n const sdk = await services.get('sdk')\n const orgs = await sdk.organizations.list()\n this.organizations = orgs.items || []\n await this.#saveCache(orgs.items)\n }\n\n #resolve(state: CompletionState): void {\n if (this.#updateResolver) {\n this.#updateResolver(state)\n this.#updateResolver = undefined\n }\n }\n\n async #readState(): Promise<CompletionState> {\n const config = await services.get('config')\n const selected = this.pendingOid ?? ((await config.local.get(CurrentOrganizationKey)) as string | undefined)\n this.pendingOid = undefined // Clear the pending OID after reading it\n const hasSelected = this.organizations.some((i) => i.key === selected)\n let result: CompletionState\n if (selected && hasSelected) {\n this.organizationId = selected\n result = CompletionState.OK\n await config.local.set(CurrentOrganizationKey, selected)\n } else if (this.organizations.length === 1) {\n const key = this.organizations[0].key\n await config.local.set(CurrentOrganizationKey, key)\n this.organizationId = key\n result = CompletionState.OK\n } else if (this.organizations.length) {\n result = CompletionState.NotSelected\n } else {\n result = CompletionState.NoOrganizations\n }\n return result\n }\n\n /**\n * When the user has no organizations, it set the organization as the only one\n * and persists the organization id as the selected organization.\n * @param org The organization to add.\n */\n async setupUserOrganization(org: IOrganization): Promise<void> {\n const hasOrg = this.organizations.some((i) => i.key === org.key)\n if (!hasOrg) {\n this.organizations.push(org)\n }\n const config = await services.get('config')\n await config.local.set(CurrentOrganizationKey, org.key)\n this.organizationId = org.key\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"OrganizationService.js","sourceRoot":"","sources":["../../../src/services/OrganizationService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAE5C,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAA;AAC5D,OAAO,QAAQ,MAAM,kBAAkB,CAAA;AAEvC,IAAK,KAIJ;AAJD,WAAK,KAAK;IACR,6CAAU,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AM,GAAG,eAAe,CAAC,WAAW,CAAA;QACtC,CAAC;aAAM,CAAC;YACN,MAAM,GAAG,eAAe,CAAC,eAAe,CAAA;QAC1C,CAAC;QACD,OAAO,MAAM,CAAA;IACf,CAAC","sourcesContent":["import { Service } from '../core/Service.js'\nimport type { IOrganization, UserOrganizationGrantType } from '@api-client/core/models/store/Organization.js'\nimport { CurrentOrganizationKey } from '../lib/StoreKeys.js'\nimport services from './api_service.js'\n\nenum State {\n NotChecked,\n Checking,\n Checked,\n}\n\nexport enum CompletionState {\n /**\n * The user has at least one organization and the current organization is selected.\n */\n OK,\n /**\n * The user has at leas two organizations and no valid selection.\n */\n NotSelected,\n /**\n * The user have no organizations. Setup is needed.\n */\n NoOrganizations,\n /**\n * Unable to determine the state due to an error.\n */\n Error,\n}\n\nconst OrgsCacheKey = 'user_organizations'\n\nexport default class OrganizationService extends Service {\n static override key = 'api-service/organization'\n\n state: State = State.NotChecked\n organizations: IOrganization[] = []\n organizationId: string | undefined\n /**\n * A pending organization ID that was set via the URL query parameter `oid`.\n * When it is set, it preselects the organization when loading list of organizations.\n * This will override the `organizationId` set in the local storage.\n */\n pendingOid: string | undefined | null\n\n /**\n * A hidden value for the `updateComplete` getter.\n */\n #updateComplete?: Promise<CompletionState>\n\n /**\n * The resolver to call when the update completes.\n */\n #updateResolver?: (state: CompletionState) => void\n\n protected override async onCreate(): Promise<void> {\n const url = new URL(window.location.href)\n this.pendingOid = url.searchParams.get('oid')\n this.revalidate()\n }\n\n /**\n * @deprecated Use #setOrganization instead\n */\n setupUserOrganization(org: IOrganization): Promise<void> {\n return this.setOrganization(org)\n }\n\n /**\n * Sets an org as the current user org.\n * @param org The organization to add.\n */\n async setOrganization(org: IOrganization): Promise<void> {\n const hasOrg = this.organizations.some((i) => i.key === org.key)\n if (!hasOrg) {\n this.organizations.push(org)\n }\n const config = await services.get('config')\n await config.local.set(CurrentOrganizationKey, org.key)\n this.organizationId = org.key\n }\n\n /**\n * Adds an organization to the list of organizations.\n * It also updated the cache. Used when creating a new org.\n * @param org The organization to add.\n */\n async addOrganization(org: IOrganization): Promise<void> {\n const hasOrg = this.organizations.some((i) => i.key === org.key)\n if (!hasOrg) {\n this.organizations.push(org)\n await this.#saveCache()\n }\n }\n\n /**\n * Removes an organization from the list of organizations.\n * It also updated the cache. Used when deleting an org.\n * @param org The organization to remove.\n */\n async removeOrganization(org: IOrganization): Promise<void> {\n const idx = this.organizations.findIndex((i) => i.key === org.key)\n if (idx !== -1) {\n this.organizations.splice(idx, 1)\n await this.#saveCache()\n if (this.organizationId === org.key) {\n this.organizationId = undefined\n const config = await services.get('config')\n await config.local.delete(CurrentOrganizationKey)\n }\n }\n }\n\n /**\n * @deprecated Use #readOrganization instead\n */\n getUserOrganization(id: string): Promise<IOrganization> {\n return this.readOrganization(id)\n }\n\n /**\n * Reads an organization from the list of organizations.\n * @param id The organization ID.\n * @returns The organization.\n */\n async readOrganization(id: string): Promise<IOrganization> {\n await this.#updateComplete\n const org = this.organizations.find((i) => i.key === id)\n if (!org) {\n throw new Error(`Organization with id ${id} not found`)\n }\n return org\n }\n\n /**\n * Gets the current user organization.\n * @returns The current organization or undefined if not set.\n */\n async currentOrganization(): Promise<IOrganization | undefined> {\n if (this.state === State.Checking) {\n await this.#updateComplete\n }\n if (!this.organizationId) {\n return undefined\n }\n return this.organizations.find((i) => i.key === this.organizationId)\n }\n\n /**\n * Checks if the user has a specific grant type in the current organization.\n * @param grantType The grant type to check.\n * @returns `true` if the user has the specified grant type, `false` otherwise.\n */\n protected async hasGrantType(grantType: UserOrganizationGrantType): Promise<boolean> {\n const org = await this.currentOrganization()\n if (!org) {\n return false\n }\n return org.grantType === grantType\n }\n\n /**\n * Checks if the user has any of the specified grant types in the current organization.\n * @param types The grant types to check.\n * @returns `true` if the user has any of the specified grant types, `false` otherwise.\n */\n async hasRoles(types: UserOrganizationGrantType[]): Promise<boolean> {\n const org = await this.currentOrganization()\n if (!org) {\n return false\n }\n return types.includes(org.grantType)\n }\n\n /**\n * Checks if the user is the owner of the current organization.\n * @returns `true` if the user is the owner of the current organization, `false` otherwise.\n */\n async isOwner(): Promise<boolean> {\n return this.hasGrantType('owner')\n }\n\n /**\n * Checks if the user is a manager of the current organization.\n * @returns `true` if the user is a manager of the current organization, `false` otherwise.\n */\n async isManager(): Promise<boolean> {\n return this.hasGrantType('manager')\n }\n\n /**\n * Checks if the user is an editor of the current organization.\n * @returns `true` if the user is a editor of the current organization, `false` otherwise.\n */\n async isEditor(): Promise<boolean> {\n return this.hasGrantType('editor')\n }\n\n /**\n * Forces to get the list of organizations from the server.\n *\n * Returns one of the following states:\n *\n * - `CompletionState.OK` - The user has at least one organization and the current organization is selected.\n * - `CompletionState.NotSelected` - The user has at leas two organizations and no valid selection.\n * - `CompletionState.NoOrganizations` - The user have no organizations. Setup is needed.\n * - `CompletionState.Error` - Unable to determine the state due to an error.\n *\n * @returns The state of the organization creation.\n */\n revalidate(): Promise<CompletionState> {\n if (this.state === State.Checking) {\n return this.#updateComplete as Promise<CompletionState>\n }\n this.state = State.Checking\n this.#updateComplete = new Promise<CompletionState>((resolver) => {\n this.#updateResolver = resolver\n })\n this.#getState()\n return this.#updateComplete\n }\n\n /**\n * Reads the list of organizations either from the cache or from the server.\n */\n async #getState(): Promise<void> {\n const loaded = await this.#loadCache()\n if (!loaded) {\n try {\n await this.#callOrganizations()\n } catch {\n this.#resolve(CompletionState.Error)\n this.state = State.Checked\n return\n }\n }\n const result = await this.#readState()\n this.state = State.Checked\n this.#resolve(result)\n }\n\n async #loadCache(): Promise<boolean> {\n const cache = await services.get('cache')\n const orgs = await cache.get<IOrganization[]>(OrgsCacheKey)\n if (orgs) {\n this.organizations = orgs\n return true\n }\n return false\n }\n\n async #saveCache(): Promise<void> {\n const cache = await services.get('cache')\n if (this.organizations.length === 0) {\n await cache.delete(OrgsCacheKey)\n } else {\n await cache.set<IOrganization[]>(OrgsCacheKey, this.organizations, cache.TTL.OneHour)\n }\n }\n\n async #callOrganizations(): Promise<void> {\n const sdk = await services.get('sdk')\n const orgs = await sdk.organizations.list()\n this.organizations = orgs.items || []\n await this.#saveCache()\n }\n\n #resolve(state: CompletionState): void {\n if (this.#updateResolver) {\n this.#updateResolver(state)\n this.#updateResolver = undefined\n }\n }\n\n async #readState(): Promise<CompletionState> {\n const config = await services.get('config')\n const selected = this.pendingOid ?? ((await config.local.get(CurrentOrganizationKey)) as string | undefined)\n this.pendingOid = undefined // Clear the pending OID after reading it\n const hasSelected = this.organizations.some((i) => i.key === selected)\n let result: CompletionState\n if (selected && hasSelected) {\n this.organizationId = selected\n result = CompletionState.OK\n await config.local.set(CurrentOrganizationKey, selected)\n } else if (this.organizations.length === 1) {\n const key = this.organizations[0].key\n await config.local.set(CurrentOrganizationKey, key)\n this.organizationId = key\n result = CompletionState.OK\n } else if (this.organizations.length) {\n result = CompletionState.NotSelected\n } else {\n result = CompletionState.NoOrganizations\n }\n return result\n }\n}\n"]}
|